我已经超过了这段代码的每一寸,我无法找出导致它进入循环的原因。一旦我按下键开始它就会播放起始噪音,然后它会进入结束屏幕并按下按键再次播放然后我按下它然后开始游戏并播放游戏的一个标记并说游戏结束了。
import pygame, random, sys
from pygame.locals import *
WINDOWWIDTH = 600
WINDOWHEIGHT = 600
TEXTCOLOR = (255, 255, 255)
BGC = (0, 0, 0)
FPS = 80
PLAYERMOVERATE = 5
countBy = 10
def terminate():
pygame.quit()
sys.exit()
def waitForPlayerToPressKey():
while True:
for event in pygame.event.get():
if event.type == QUIT:
terminate()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
terminate()
return
def playerHasHitShoot(playerRect, shoot):
if playerRect.colliderect(shoot):
return True
return False
def baddieHasHitShoot(baddieRect, shoot):
if baddieRect.colliderect(shoot):
return True
return False
def drawText(text, font, surface, x, y):
textobj = font.render(text, 1, TEXTCOLOR)
textrect = textobj.get_rect()
textrect.topleft = (x, y)
surface.blit(textobj, textrect)
def fire(shoot):
shootRect.topleft = ((playerRect / 2), 101)
shootRect.move_ip(5, 1)
pygame.init()
pygame.font.init()
font = pygame.font.SysFont(None,48)
mainClock = pygame.time.Clock()
screen = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
pygame.display.set_caption('Mothers Day')
pygame.mouse.set_visible(True)
pygame.display.set_icon(pygame.image.load('gameicon.gif'))
gameStartSound = pygame.mixer.Sound('gamestart.wav')
gameOverSound = pygame.mixer.Sound('gamestart.wav')
pygame.mixer.music.load('background.wav')
playerImage = pygame.image.load('starship.bmp')
playerRect = playerImage.get_rect()
baddieImage = pygame.image.load('enemy.bmp')
baddieRect = baddieImage.get_rect()
shootImage = pygame.image.load('shoot.bmp')
shootRect = shootImage.get_rect()
drawText('Star Trek', font, screen, (WINDOWWIDTH / 2.5), (WINDOWHEIGHT / 3))
drawText('Press a key to start.', font, screen, (WINDOWWIDTH / 3.75) - 30, (WINDOWHEIGHT / 3) + 50)
pygame.display.update()
waitForPlayerToPressKey()
topScore = 10000
while True:
score = 10000
playerRect.topleft = (0, 100)
baddieRect.topright = (600, 100)
moveUp = moveDown = False
pygame.mixer.music.play(-1, 0.0)
while True:
score -= countBy
for event in pygame.event.get():
if event.type == QUIT:
terminate()
if event.type == KEYDOWN:
if event.key == K_UP:
moveDown = False
moveUp = True
if event.key == ord(' '):
fire
if event.key == K_DOWN:
moveUp = False
moveDown = True
if event.type == KEYUP:
if event.key == ord(' '):
fire
if event.key == K_ESCAPE:
terminate()
if event.key == K_UP or event.key == ord('w'):
moveUp = False
if event.key == K_DOWN or event.key == ord('s'):
moveDown = False
if moveUp and playerRect.top > 0:
playerRect.move_ip(0, -1 * PLAYERMOVERATE)
if moveDown and playerRect.bottom < WINDOWHEIGHT:
playerRect.move_ip(0, PLAYERMOVERATE)
drawText('Score: %s' % (score), font, screen, 10, 0)
drawText('Top Score: %s' % (topScore), font, screen, 10, 40)
screen.blit(playerImage, playerRect)
screen.blit(shootImage, shootRect)
screen.blit(baddieImage, baddieRect)
pygame.display.update()
if playerHasHitShoot(playerRect, baddieRect):
score = 0
break
if baddieHasHitShoot(baddieRect, shoot):
if score > topScore:
score = topscore
break
mainClock.tick(FPS)
sb = pygame.image.load('sb.png')
screen.blit(sb, (600, 600))
screen.fill(BGC)
pygame.mixer.music.stop()
gameOverSound.play()
drawText('GAME OVER', font, screen, (WINDOWWIDTH / 3), (WINDOWHEIGHT / 3))
drawText('Press a key to play again.', font, screen, (WINDOWWIDTH / 3) - 80, (WINDOWHEIGHT / 3) + 50)
pygame.display.update()
waitForPlayerToPressKey()
gameOverSound.stop()
答案 0 :(得分:1)
if playerHasHitShoot(playerRect, baddieRect):
score = 0
break
每帧都会点击break
,并立即转到“游戏结束”部分。我猜你只是在playerHasHitShoot
返回true时才打算这样做?如果是这样的话:
if playerHasHitShoot(playerRect, baddieRect):
score = 0
break
不太清楚下一位(当前从未运行过)是什么意思,但你应该检查并确保所有条件路径都是正确的:
if baddieHasHitShoot(baddieRect, shoot):
if score > topScore:
score = topscore
break