我找不到我所做的更改,因此我的move功能不再起作用,它之前运行得非常完美。我没有任何错误,它根本不会移动。 对不起,如果代码确实草率。 大多数缩进错误可能是因为我格式化错误(这是我第一次使用stackoverflow)。
import pygame
pygame.init()
work = True
b_y = 425
key = pygame.key.get_pressed()
newWindow = pygame.display.set_mode((500, 500))
pygame.display.set_caption("NewFile")
class player:
def __init__(self, vel, x):
self.x = x
self.vel = vel
def playerdraw(self):
pygame.draw.rect(newWindow, (255, 255, 255), (p1.x, 425,
40,20))
def move(self):
if key[pygame.K_LEFT] and p1.x>5:
p1.x -= self.vel
elif key[pygame.K_RIGHT] and p1.x < 455:
p1.x += self.vel
p1 = player(2, 250)
b_x = p1.x
while work:
for event in pygame.event.get():
if event.type == pygame.QUIT:
work= False
newWindow.fill((0, 0, 0))
p1.move()
p1.playerdraw()
pygame.display.update()
pygame.quit()
我需要玩家移动
答案 0 :(得分:1)
您必须在主游戏循环中连续设置key
:
while work:
# [...]
key = pygame.key.get_pressed()
请注意,您已经在应用程序的开头初始化了key
,但是您错过了对其进行更新的操作。在类key
的方法move()
中评估存储在全局名称空间列表player
中的键的状态。
pygame.key.get_pressed()
返回表示每个键状态的布尔值列表。评估(key)事件时,将更新键的内部状态。之后,pygame.key.get_pressed()
将返回新值和实际值。
答案 1 :(得分:1)
您需要在每个循环上阅读get_pressed键功能
app = Application().start("setups/wampserver3.1.7_x64.exe")
ctrl = app["Select Setup Language"]
ctrl.Ok.click()
完整代码
def move(self):
key = pygame.key.get_pressed() # You need to read the get_pressed key function on each loop
if key[pygame.K_LEFT] and p1.x>5:
p1.x -= self.vel
elif key[pygame.K_RIGHT] and p1.x < 455:
p1.x += self.vel