-3

我正在学习python,我为玩家运动创建了代码,它不在课堂上时工作,但我在课堂上需要它。当我按 W 或 S 时,玩家只移动一次 vel = 5,然后它回到原来的坐标。如何解决?

right = False
left = False

class player:    
    def __init__(self, x, y, vel = 5, walkCount = 0):
        self.x = x
        self.y = y
        self.vel = vel
        self.walkCount = walkCount
    
    def update(self):
        if self.walkCount + 1 >= 40:
            self.walkCount = 0
        if right:
            screen.blit(charRun[walkCount//5],(self.x, self.y))
            self.walkCount += 1        
        elif left:
            screen.blit(charBack[walkCount//5],(self.x, self.y))
            self.walkCount += 1
        else:
            screen.blit(char,(self.x, self.y))
            pygame.display.update()

    def move(self):
        keys = pygame.key.get_pressed()
        if keys[pygame.K_w]:
            self.x += self.vel
            right = True
            left = False
        elif keys[pygame.K_s]:
            self.x -= self.vel
            left = True
            right = False
        else:
            right = False
            left = False
            self.walkCount = 0

def redrawGameWindow():
    screen.blit(bg, (0, 0))
    screen.blit(car, (800,500))

run = True
while run:
    clock.tick(60)    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    redrawGameWindow()
    b = player(500,500)
    b.move()
    b.update()
4

2 回答 2

0

正如@jasonharper 所建议的那样,您只需要在开始时创建一次播放器,并且在类中创建更多的左右变量。

class player:    
    def __init__(self, x, y, vel = 5, walkCount = 0):
        self.x = x
        self.y = y
        self.vel = vel
        self.walkCount = walkCount
        self.right = False
        self.left = False
    
    def update(self):
        if self.walkCount + 1 >= 40:
            self.walkCount = 0
        if right:
            screen.blit(charRun[walkCount//5],(self.x, self.y))
            self.walkCount += 1        
        elif left:
            screen.blit(charBack[walkCount//5],(self.x, self.y))
            self.walkCount += 1
        else:
            screen.blit(char,(self.x, self.y))
            pygame.display.update()

    def move(self):
        keys = pygame.key.get_pressed()
        if keys[pygame.K_w]:
            self.x += self.vel
            self.right = True
            self.left = False
        elif keys[pygame.K_s]:
            self.x -= self.vel
            self.left = True
            self.right = False
        else:
            self.right = False
            self.left = False
            self.walkCount = 0

def redrawGameWindow():
    screen.blit(bg, (0, 0))
    screen.blit(car, (800,500))

run = True
b = player(500,500)
while run:
    clock.tick(60)    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    redrawGameWindow()
    b.move()
    b.update()
于 2020-06-27T18:12:26.533 回答
0

首先,要检查有什么问题,您应该发布大部分代码,以便我们可以尝试对其进行测试和调试。其次,编程中的类非常有用,但你不应该做任何在你的代码中没有意义的事情。类可以帮助您制作大量对象,同时也可以保持代码的稳定性。

所以你所有的代码都应该有意义。例如,在您的代码中,您让播放器更新显示。display.update 方法在您的 while 循环中会更好,因为播放器不应该在您更新屏幕时进行控制。想象一下,稍后您添加内容,但您的代码不起作用,因为显示没有更新......

无论如何,让我们回到代码。您可以在此处看到您在 while 循环中创建了对象。因此,每次执行循环时,都会使用默认值创建一个新对象:

b = player(500,500)

因此,该函数执行的每个 while 循环:

def __init__(self, x, y, vel = 5, walkCount = 0):
    self.x = x
    self.y = y
    self.vel = vel
    self.walkCount = walkCount

所以 x 和 y 保持在 500。

于 2020-06-27T18:28:20.497 回答