python class 玩家移动问题
python class problem with player movement
我在学习python
我为玩家移动创建了代码,当它不在 class 中时它可以工作,但我需要它在 class 中。
当我按 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()
根据@jasonharper 的建议,您只需在开始时创建一次播放器,然后在 class.
中创建左右变量
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()
首先,要检查您的大部分代码有什么问题,您应该 post 以便我们可以尝试对其进行测试和调试。
其次,类 在编程中确实很有用,但您不应该在代码中做任何没有意义的事情。 类 是为了帮助你制作很多对象,同时也保持你代码的稳定性。
所以你所有的代码都应该有意义。例如,在您的代码中,您让播放器更新显示。 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。
我在学习python 我为玩家移动创建了代码,当它不在 class 中时它可以工作,但我需要它在 class 中。 当我按 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()
根据@jasonharper 的建议,您只需在开始时创建一次播放器,然后在 class.
中创建左右变量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()
首先,要检查您的大部分代码有什么问题,您应该 post 以便我们可以尝试对其进行测试和调试。 其次,类 在编程中确实很有用,但您不应该在代码中做任何没有意义的事情。 类 是为了帮助你制作很多对象,同时也保持你代码的稳定性。
所以你所有的代码都应该有意义。例如,在您的代码中,您让播放器更新显示。 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。