每当我尝试拍摄时,为什么我的游戏会在PYGAME中崩溃

时间:2016-07-06 19:27:21

标签: python crash pygame

我正在制作一个2人格斗游戏,每当我尝试按“v”时,拍摄我的游戏的关键似乎崩溃而不起作用。我是pygame的新手,我正在学习。任何帮助表示赞赏。谢谢!

import pygame

pygame.init()

#Sets up 8 bit colours
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
lightblue = (180,235,255)
grassgreen =(20,200,50)

#Sets up pygame window
gameDisplay = pygame.display.set_mode((1000,600))
pygame.display.set_caption('Block Fighter')



#Variables
gameExit = False
x = 0
y = 0
x_change = 0
y_change = 0
isShooting = False

clock = pygame.time.Clock()

class Player:
    def __init__(self, x_change, y_change, x, y):
        self.x_change = 0
        self.y_change = 0
        self.x= 50
        self.y= 480

    def Left(self, x_change):
        self.x_change = -5
    def Right(self, x_change):
        self.x_change =5
    def Jump(self, y_change):
        if (self.y == 480):
            self.y_change = -70
            return
p1 = Player(x_change, y_change, x, y)

class Bullet:
    def __init__(self, x, y, Player, x_change):
        self.x = p1.x
        self.y = p1.y
        self.x_change = 0
    def Show(self, x_change, x , y, Player ):
        pygame.draw.rect(gameDisplay, black , [p1.x,p1.y,5,5])
        self.x_change = 10
b1 = Bullet(x, y, Player, x_change)

#Main Game Loop
while not gameExit:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            gameExit = True
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_a:
                p1.Left(x_change)
            if event.key == pygame.K_d:
                p1.Right(x_change)
            if event.key == pygame.K_w:
                p1.Jump(y_change)
            if event.key == pygame.K_v:
                b1.Show(x, y, x_change, Player)
                isShooting = True
        while isShooting:
            pygame.draw.rect(gameDisplay, black, [b1.x, b1.y , 5,5])
            if b1.x > 1000:
                pass
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_a or event.key == pygame.K_d:
                p1.x_change = 0

    if p1.x <= 0:
        p1.x = 0
    if p1.x >= 500:
        p1.x = 500

    if p1.y > 480:
        p1.y_change = 0
        p1.y = 480
    if p1.y < 480:
        p1.y_change = 5

    p1.x += p1.x_change
    p1.y += p1.y_change
    b1.x += b1.x_change
    gameDisplay.fill(lightblue)
    pygame.draw.rect(gameDisplay, grassgreen, [1000,600,-1000,-100])
    pygame.draw.rect(gameDisplay, red, [p1.x,p1.y,20,20])
    pygame.display.update()

    clock.tick(40)

pygame.quit()
quit()

2 个答案:

答案 0 :(得分:0)

看起来一旦isShooting设置为True,程序就会陷入while循环(而isShooting :)。看起来你需要在while循环中设置一个条件,如果满足某些条件,则将isShooting设置为False,例如,如果v键被释放。

答案 1 :(得分:0)

摆脱while循环。应删除while isShooting。玩家应该在每场比赛循环中最多射击一次 - 不仅如此,他们将无法看到他们射击的效果。另外,您创建了一个无限循环。

另一方面,欢迎来到Stack Overflow!如果您还没有,请查看导览here