为什么这个游戏不开始,它在循环 [英] why won't this game start, it's looping

查看:63
本文介绍了为什么这个游戏不开始,它在循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经检查了这段代码的每一寸,我无法找出导致它进入循环的原因.一旦我按下键开始它播放开始噪音然后它进入结束屏幕并说按键再次播放我按下它然后它开始游戏并播放游戏的声音和一个滴答声并说游戏结束.

i have gone over every inch of this code i can not find out what is causing it to go into a loop. once i press key to start it plays the start noise and then it goes to the end screen and says press key to play again and i press it and it goes to start the game and plays the soung and one tick of the game and says game over.

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()

推荐答案

if playerHasHitShoot(playerRect, baddieRect):
    score = 0 
break

那个 break 将在每一帧被击中,并立即进入最后的游戏结束"部分.我猜你只是想在 playerHasHitShoot 返回 true 时发生这种情况?如果是这样:

That break will be hit every frame, and immediately go to the 'Game Over' section at the end. I'm guessing you only intended this to happen if playerHasHitShoot returned true? If so:

if playerHasHitShoot(playerRect, baddieRect):
    score = 0 
    break

不太清楚以下位(目前从未运行)的用途,但您应该检查并确保所有条件路径都正确:

It's less clear what the following bit (that's currently never running) is meant to do, but you should check and make sure all your conditional paths are correct:

if baddieHasHitShoot(baddieRect, shoot):
    if score > topScore:
        score = topscore
    break

这篇关于为什么这个游戏不开始,它在循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆