任何人都可以看到我做错了吗?我要块跳,但不能正常工作。这是我的第一个python项目,请尝试解释我做错了什么,所以我再也不做了,谢谢。
import pygame
pygame.init()
bg = pygame.image.load('bg.jpg')
class Player():
def __init__(self, x, y, height, width):
self.x = x
self.y = y
self.height = height
self.width = width
self.Jumpcount = 10
self.Isjump = False
def draw(self, window):
pygame.draw.rect(window, (255, 0, 0), (self.x, self.y, self.width, self.height))
pygame.draw.rect(window, (0, 255, 0), (self.x, self.y, self.width / 2, self.height / 2))
pygame.draw.rect(window, (0, 255, 0), (self.x + self.width / 2, self.y + self.height / 2, self.width / 2, self.height / 2))
pygame.draw.rect(window, (139, 26, 26), (0, 400 + self.height, 500, 500 - self.height))
def Jump(self, Jumping):
if Jumping:
self.Isjump = True
if self.Isjump:
if self.Jumpcount >= -10:
neg = 1
if self.Jumpcount < 0:
neg = -1
self. y = self.Jumpcount**2 * 0.25 * neg
self.Jumpcount -= 1
else:
self.Isjump = False
self.Jumpcount = 10
win = pygame.display.set_mode((500, 500))
pygame.display.set_caption('Test')
Jumping = False
run = True
man = Player(70, 400, 40, 40)
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
print('123')
Jumping = True
man.Jump(Jumping)
win.blit(bg, (0, 0))
man.draw(win)
pygame.display.update()
pygame.quit()
没有错误消息,但是当我按空格键时它什么也没做。
答案 0 :(得分:0)
删除第二个pygame.event.get()
循环。第一个循环使用所有事件,但仅检查QUIT
事件,而对其他事件不执行任何操作。第二个循环可能为空。在第一个循环中进行所有事件检查。
您只希望每个主循环迭代只有一个事件循环。
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
print('123')
Jumping = True
man.Jump(Jumping)
答案 1 :(得分:0)
我认为您的问题在于两个for循环event in pygame.event.get()
要解决此问题,您只想将两个if语句放在一个for循环下,如下所示:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
print('123')
Jumping = True
man.Jump(Jumping)
应该可以解决您的问题。