gpt4 book ai didi

python - 为什么每次尝试射击时游戏都会在PYGAME中崩溃

转载 作者:行者123 更新时间:2023-12-03 17:48:34 26 4
gpt4 key购买 nike

我正在制作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()

最佳答案

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

关于python - 为什么每次尝试射击时游戏都会在PYGAME中崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38232232/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com