我想创建一个GameOver屏幕,其中包含"再次播放"使用KEYDOWN
事件。退出选项有效但我的游戏循环不会更新。当我输入gameLoop()
时,字体不会变为蓝色。
我的代码:
def gameLoop():
gameExit=False
gameOver=False
lead_x = 300
lead_y = 300
lead_x_change = 0
lead_y_change = 0
randAppleX = random.randrange(0, display_width-block_size)
randAppleY = random.randrange(0, display_height-block_size)
gameDisplay = pygame.display.set_mode((display_width,display_height))
while not gameExit:
while gameOver == True:
gameDisplay.fill(white)
message_to_screen("Game Over, Press C to Play Again or Q to Quit",red)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q: #This works fine!
gameExit = True
gameOver = False
if event.key == pygame.K_c:
gameLoop() #This won't work
if lead_x > 789 or lead_x < 1 or lead_y > 589 or lead_y < 1:
lead_y_change = 0
lead_x_change = 0
print "Game Over"
gameOver = True
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
lead_x_change = -block_size
lead_y_change = 0
if event.key == pygame.K_RIGHT:
lead_x_change = block_size
lead_y_change = 0
if event.key == pygame.K_UP:
lead_y_change = -block_size
lead_x_change = 0
if event.key == pygame.K_DOWN:
lead_y_change = block_size
lead_x_change = 0
lead_x += lead_x_change
lead_y += lead_y_change
gameDisplay.fill(white)
pygame.draw.rect(gameDisplay, red, [randAppleX,randAppleY,block_size,block_size])
pygame.draw.rect(gameDisplay, black, [lead_x,lead_y,block_size,block_size])
pygame.display.update()
clock.tick(FPS)
message_to_screen("You Lose Turd", red)
pygame.display.update()
pygame.quit()
quit()
gameLoop()