如何解决游戏结束在 pygame 中无法正常工作
我现在面临三个问题,我想分别得到答案,请合作....
import pygame,sys
from random import randint
pygame.init()
screen_size = (350,674)
screen = pygame.display.set_mode(screen_size)
pygame.display.set_caption("First Game")
x_poss = randint(0,350)
y_poss = randint(0,675)
width = 20
height = 20
vel = 8
bases = []
spines = []
red = (255,0)
image_1 = pygame.image.load('bird2.png').convert_alpha()
image_2 = pygame.image.load('base.png').convert_alpha()
image_3 = pygame.image.load('base2.png').convert_alpha()
image_4 = pygame.image.load('enemy.png').convert_alpha()
j = 10
isJump = False
jumpCount = j
run = True
base = pygame.draw.rect(screen,(255,255,255),(randint(0,330),randint(0,675),width,height))
def jump():
global jumpCount,y_poss
for base in bases:
if bird.colliderect(base):
isJump = True
if jumpCount >= -j:
y_poss -= (jumpCount * abs(jumpCount)) * 0.5
jumpCount -= 1
else:
jumpCount = j
isJump = False
def draw_rect(color,x,y,height):
pygame.draw.rect(screen,color,(x,height))
def game_over(x,y):
game_over_font = pygame.font.Font('Omnibus.ttf',40)
game_over_text = game_over_font.render('GAME OVER',True,(0,0))
screen.blit(game_over_text,y))
num_bases = 5
num_spines = 5
is_game_over = False
grav = 1
velo = 2
while len(bases) < 2*num_bases:
rect = pygame.Rect(randint(0,height)
if rect.collidelist(bases) < 0:
bases.append(rect)
while len(spines) < num_spines:
rect = pygame.Rect(randint(0,10,10)
if rect.collidelist(spines) < 0:
spines.append(rect)
while run:
pygame.time.delay(40)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
key = pygame.key.get_pressed()
if key[pygame.K_LEFT] and x_poss > vel - width:
x_poss -= vel
if key[pygame.K_q]:
run = False
sys.exit()
if key[pygame.K_RIGHT] and x_poss < 350 - vel - width:
x_poss += vel
if not(isJump):
if key[pygame.K_UP] and y_poss > vel:
y_poss -= vel
if key[pygame.K_DOWN] and y_poss < 670 - height - vel:
y_poss += vel
if key[pygame.K_SPACE]:
isJump = True
else:
if jumpCount >= -10:
y_poss -= (jumpCount * abs(jumpCount)) * 0.5
jumpCount -= 1
else:
jumpCount = 10
isJump = False
bird = pygame.draw.rect(screen,(x_poss,y_poss,height))
spine = pygame.draw.rect(screen,0),(300,440,10))
screen.fill((255,255))
draw_rect((255,x_poss,height)
screen.blit(image_1,bird)
for spine in spines:
screen.blit(image_4,spine)
spine.x += velo
if spine.x >= 355:
spine.x = -15
if spine.colliderect(bird):
is_game_over = True
break
for base_1 in bases[:num_bases]:
screen.blit(image_2,base_1)
for base_2 in bases[num_bases:]:
screen.blit(image_3,base_2)
for i,base_1 in enumerate(bases[:num_bases]):
if base.collidelist(bases[i+1:]) >= 0:
bases.remove(base_1)
bases.append(pygame.draw.rect(screen,height)))
screen.blit(image_2,base_1)
for i,base_2 in enumerate(bases[num_bases:]):
if base.collidelist(bases[i+1:]) >= 0:
bases.remove(base_2)
bases.append(pygame.draw.rect(screen,height)))
screen.blit(image_3,base_2)
for base_1 in bases[:num_bases]:
base_1.y += grav
base_1.x += 0#-1
if base_1.x == -50:
base_1.x = 360
if base_1.y == 675:
base_1.x = randint(0,360)
base_1.y = randint(-10,0)
for base_2 in bases[num_bases:]:
base_2.y += grav
base_2.x += 0#1
if base_2.x == 360:
base_2.x = -50
if base_2.y == 675:
base_2.x = randint(0,360)
base_2.y = randint(-10,0)
"""for i,base in enumerate(bases):
if base.y - base[i+1].y < 20:
base.y + (bases[i+1].y - base.y)
for i,spine in enumerate(spines):
i += 1
if spines[i].y - spines[0].y < 20:
spine.y + (spines[i].y - spine.y)"""
if is_game_over:
grav = 0
velo = 0
game_over(45,336)
jump()
pygame.display.update()
1.第一个问题,游戏结束功能运行不正常。它的目的是当鸟类与脊椎发生碰撞时它应该显示“游戏结束”并且每件事都必须停止......但是当我运行代码时,如果鸟类碰撞它会显示游戏结束直到当脊椎向前移动时鸟类碰撞它又消失了……
if spine.colliderect(bird):
is_game_over = True
break
我尝试了一些我出来的东西。现在每当鸟与刺相撞时,底座和刺都会停止,但我想从屏幕上删除我的鸟图像...
if is_game_over:
grav = 0
velo = 0
game_over(45,336)
接下来的两个问题很快就会出现.....请站好并帮助我.....
解决方法
我认为问题在于您只是告诉它让游戏结束然后继续其他所有事情,您并没有阻止其余代码的运行。添加另一个变量如何,is_game_over
然后将您的 if 代码替换为:
if spine.colliderect(bird):
is_game_over = True
break
然后,在 for
循环外调用 game_over:
if is_game_over:
game_over(45,336)
continue # or break,depending on what you're trying to do with game_over
现在这样,它应该跳过其余的代码,这样您的游戏就会以 game_over
停止。希望这对你有用。
另外,为什么要在 game_over
循环中定义 while
函数?这意味着它会在每个循环中重新定义。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。