我正在尝试按照本教程:https://www.youtube.com/watch?v=UdsNBIzsmlI
我已经复制了代码(我认为完全正确)但是当我在终端中运行它时,任何关键事件都会转到终端。 pygame窗口中没有任何事情发生,当我关闭它时,所有关键事件都试图在终端中执行。我错过了什么吗?
FWIW,当我添加print(keys)
时,当我点击某个键时,列表中出现的值不是pygame.K_LEFT
(或任何键)为该键指定的值。这是解释器为该键显示的值([(C
或其他)。
我的东西:
这是我的代码:
import pygame
pygame.init()
win = pygame.display.set_mode((500,500))
pygame.display.set_caption("Learning pyGame")
x = 50
y = 425
width = 40
height = 60
vel = 5
isJump = False
jumpCount = 10
clock = pygame.time.Clock()
run = True
while run:
clock.tick(60)
ev = pygame.event.poll()
if ev.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and x > vel:
x -= vel
if keys[pygame.K_RIGHT] and x < 500 - width - vel:
x += vel
if not(isJump):
if keys[pygame.K_UP] and y > vel:
y -= vel
if keys[pygame.K_DOWN] and y < 500 - height - vel:
y += vel
else:
if jumpCount >= -10:
neg = 1
if jumpCount < 0:
neg = -1
y -= (jumpCount ** 2) * 0.5 * neg
jumpCount -= 1
else:
isJump = False
jumpCount = 10
win.fill((0, 0, 0))
pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))
pygame.display.update()
pygame.quit()