怎样用python写一个可以重复猜字谜(不是猜数字)的游戏?

怎样用python写一个可以重复猜字谜(不是猜数字)的游戏?,第1张

默认情况下,dict迭代的是key。如果要迭代value,可以用for value in d.values(),如果要同时迭代key和value,可以用for k, v in d.items()。

idea +python3.8+pygame

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="python" cid="n5" mdtype="fences" style="box-sizing: border-boxoverflow: visiblefont-family: var(--monospace)font-size: 0.9emdisplay: blockbreak-inside: avoidtext-align: leftwhite-space: normalbackground-image: inheritbackground-position: inheritbackground-size: inheritbackground-repeat: inheritbackground-attachment: inheritbackground-origin: inheritbackground-clip: inheritbackground-color: rgb(248, 248, 248)position: relative !importantborder: 1px solid rgb(231, 234, 237)border-radius: 3pxpadding: 8px 4px 6pxmargin-bottom: 15pxmargin-top: 15pxwidth: inheritcolor: rgb(51, 51, 51)font-style: normalfont-variant-ligatures: normalfont-variant-caps: normalfont-weight: 400letter-spacing: normalorphans: 2text-indent: 0pxtext-transform: nonewidows: 2word-spacing: 0px-webkit-text-stroke-width: 0pxtext-decoration-style: initialtext-decoration-color: initial">import random

rang1 = int(input("请设置本局游戏的最小值:"))

rang2 = int(input("请设置本局游戏的最大值:"))

num = random.randint(rang1,rang2)

guess = "guess"

print("数字猜谜游戏!")

i = 0

while guess != num:

i += 1

guess = int(input("请输入你猜的数字:"))

if guess == num:

print("恭喜,你猜对了!")

elif guess <num:

print("你猜的数小了...")

else:

print("你猜的数大了...")

print("你总共猜了%d" %i + "次",end = '')

print(",快和你朋友较量一下...")</pre>

成果图

[图片上传失败...(image-6ef72d-1619168958721)]

<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="python" cid="n10" mdtype="fences" style="box-sizing: border-boxoverflow: visiblefont-family: var(--monospace)font-size: 0.9emdisplay: blockbreak-inside: avoidtext-align: leftwhite-space: normalbackground-image: inheritbackground-position: inheritbackground-size: inheritbackground-repeat: inheritbackground-attachment: inheritbackground-origin: inheritbackground-clip: inheritbackground-color: rgb(248, 248, 248)position: relative !importantborder: 1px solid rgb(231, 234, 237)border-radius: 3pxpadding: 8px 4px 6pxmargin-bottom: 15pxmargin-top: 15pxwidth: inheritcolor: rgb(51, 51, 51)font-style: normalfont-variant-ligatures: normalfont-variant-caps: normalfont-weight: 400letter-spacing: normalorphans: 2text-indent: 0pxtext-transform: nonewidows: 2word-spacing: 0px-webkit-text-stroke-width: 0pxtext-decoration-style: initialtext-decoration-color: initial">import pygame

import random

from pygame.locals import *

WIDTH = 1200

HEIGHT = 600

class Peas:

def init (self):

self.image = pygame.image.load("./res/peas.gif")

self.image_rect = self.image.get_rect()

self.image_rect.top = 285

self.image_rect.left = 30

self.is_move_up = False

self.is_move_down = False

self.is_shout = False

def display(self):

"""显示豌豆的方法"""

screen.blit(self.image, self.image_rect)

def move_up(self):

"""豌豆向上移动"""

if self.image_rect.top >30:

self.image_rect.move_ip(0, -10)

else:

for z in Zombie.zombie_list:

if self.image_rect.colliderect(z.image_rect):

pygame.quit()

exit()

def move_down(self):

"""豌豆向下移动"""

if self.image_rect.bottom <HEIGHT - 10:

self.image_rect.move_ip(0, 10)

else:

for z in Zombie.zombie_list:

if self.image_rect.colliderect(z.image_rect):

pygame.quit()

exit()

def shout_bullet(self):

"""发射炮d方法"""

bullet = Bullet(self)

Bullet.bullet_list.append(bullet)

class Bullet:

bullet_list = []

interval = 0

def init (self, peas):

self.image = pygame.image.load("./res/bullet.gif")

self.image_rect = self.image.get_rect()

self.image_rect.top = peas.image_rect.top

self.image_rect.left = peas.image_rect.right

def display(self):

"""显示炮d的方法"""

screen.blit(self.image, self.image_rect)

def move(self):

"""移动炮d"""

self.image_rect.move_ip(8, 0)

if self.image_rect.right >WIDTH - 20:

Bullet.bullet_list.remove(self)

else:

for z in Zombie.zombie_list[:]:

if self.image_rect.colliderect(z.image_rect):

Zombie.zombie_list.remove(z)

Bullet.bullet_list.remove(self)

break

class Zombie:

zombie_list = []

interval = 0

def init (self):

self.image = pygame.image.load("./res/zombie.gif")

self.image = pygame.transform.scale(self.image, (70, 70))

self.image_rect = self.image.get_rect()

self.image_rect.top = random.randint(10, HEIGHT-70)

self.image_rect.left = WIDTH

def display(self):

"""显示炮d的方法"""

screen.blit(self.image, self.image_rect)

def move(self):

"""移动僵尸"""

self.image_rect.move_ip(-2, 0)

if self.image_rect.left <0:

Zombie.zombie_list.remove(self)

else:

if self.image_rect.colliderect(peas.image_rect):

pygame.quit()

exit()

for b in Bullet.bullet_list[:]:

if self.image_rect.colliderect(b.image_rect):

Bullet.bullet_list.remove(b)

Zombie.zombie_list.remove(self)

break

def key_control():

for event in pygame.event.get():

if event.type == QUIT:

pygame.quit()

exit()

if event.type == KEYDOWN:

if event.key == K_UP:

peas.is_move_up = True

peas.is_move_down = False

elif event.key == K_DOWN:

peas.is_move_up = False

peas.is_move_down = True

elif event.key == K_SPACE:

peas.is_shout = True

if event.type == KEYUP:

if event.key == K_UP:

peas.is_move_up = False

elif event.key == K_DOWN:

peas.is_move_down = False

elif event.key == K_SPACE:

peas.is_shout = False

if name == ' main ':

pygame.init()

screen = pygame.display.set_mode((WIDTH, HEIGHT))

background_image = pygame.image.load("./res/background.png")

scale_background_image = pygame.transform.scale(background_image, (WIDTH, HEIGHT))

scale_background_image_rect = scale_background_image.get_rect()

clock = pygame.time.Clock()

peas = Peas()

while True:

screen.fill((0,0,0))

screen.blit(scale_background_image, scale_background_image_rect)

peas.display()

key_control()

if peas.is_move_up:

peas.move_up()

if peas.is_move_down:

peas.move_down()

Bullet.interval += 1

if peas.is_shout and Bullet.interval >= 15:

Bullet.interval = 0

peas.shout_bullet()

Zombie.interval += 1

if Zombie.interval >= 15:

Zombie.interval = 0

Zombie.zombie_list.append(Zombie())

for bullet in Bullet.bullet_list:

bullet.display()

bullet.move()

for zombie in Zombie.zombie_list:

zombie.display()

zombie.move()

pygame.display.update()

clock.tick(60)</pre>

成果[图片上传失败...(image-983fba-1619168958718)]


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11840288.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存