我正在使用Visual Studio 2017 IDE。 我在输出中显示字符,但无法控制移动。请检查我是否犯了任何错误。 我正在使用Pygame,它是用于角色移动的键盘输入。
import pygame, sys
from pygame import *
characterSrc = 'character.png'
pygame.init()
window = display.set_mode((1000, 600))
display.set_caption('Character Movement')
speed = 3
posX = 0
posY = 0
white = (255, 255, 255)
window.fill(white)
character = pygame.image.load("character.png")
pressedkeys = pygame.key.get_pressed()
if pressedkeys[K_LEFT]:
posX -= speed
elif pressedkeys[K_RIGHT]:
posX += speed
if pressedkeys[K_UP]:
posY -= speed
elif pressedkeys[K_DOWN]:
posY += speed
if posX > 1000:
posX = 1000
elif posX < 0:
posX = 0
if posY > 600:
posY = 600
elif posY < 0:
posY = 0
window.blit(character, (posX, posY))
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
exit()
答案 0 :(得分:1)
非常感谢@musikreck我进行了您建议的更改,现在可以使用了。这是上面代码的修改版本:
import pygame, sys
from pygame import *
characterSrc = 'character.png'
pygame.init()
window = display.set_mode((1000, 600))
display.set_caption('Flappy Bird')
speed = 3
posX = 0
posY = 0
white = (255, 255, 255)
character = pygame.image.load("character.png")
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
exit()
window.fill(white)
pressedkeys = pygame.key.get_pressed()
pygame.key.set_repeat()
if pressedkeys[K_LEFT]:
posX -= speed
elif pressedkeys[K_RIGHT]:
posX += speed
if pressedkeys[K_UP]:
posY -= speed
elif pressedkeys[K_DOWN]:
posY += speed
if posX > 1000:
posX = 1000
elif posX < 0:
posX = 0
if posY > 600:
posY = 600
elif posY < 0:
posY = 0
window.blit(character, (posX, posY))
pygame.display.update()