我知道你必须拥有除玩家移动之外的一切,但我无法弄清楚如何将移动背景集成到我的代码中。目前我有一个移动屏幕的字符。并希望屏幕移动角色。很多人都在使用Vector 2来实现他们的速度,我也很困惑。 这是播放器对象的当前代码:
class Player(object):
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = 7
self.left = False
self.right = False
self.up = False
self.down = False
self.walkCount = 0
self.standing = True
def draw(self, gameDisplay):
if self.walkCount + 1 >= 27:
self.walkCount = 0
if not self.standing:
if self.left:
gameDisplay.blit(walkLeft[self.walkCount // 3], (self.x, self.y))
self.walkCount += 1
elif self.right:
gameDisplay.blit(walkRight[self.walkCount // 3], (self.x, self.y))
self.walkCount += 1
else:
if self.right:
gameDisplay.blit(walkRight[0], (self.x, self.y))
else:
gameDisplay.blit(walkLeft[0], (self.x, self.y))
def redrawgamewindow():
gameDisplay.blit(bg, (0, 0))
char.draw(gameDisplay)
goblin.draw(gameDisplay)
for fireball in fireballs:
fireball.draw(gameDisplay)
pygame.display.update()
def redrawgamewindow():
gameDisplay.blit(bg, (0, 0))
char.draw(gameDisplay)
goblin.draw(gameDisplay)
for fireball in fireballs:
fireball.draw(gameDisplay)
pygame.display.update()
重绘功能:
def redrawgamewindow():
gameDisplay.blit(bg, (0, 0))
char.draw(gameDisplay)
goblin.draw(gameDisplay)
for fireball in fireballs:
fireball.draw(gameDisplay)
pygame.display.update()
主循环
char = Player(250, 410, 64, 64)
run = True
fireballs = []
while run:
clock.tick(27)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
for fireball in fireballs:
if 500 > fireball.x > 0:
fireball.x += fireball.vel
else:
fireballs.pop(fireballs.index(fireball))
keys = pygame.key.get_pressed()
buttons = pygame.mouse.get_pressed()
if keys[pygame.K_SPACE]:
if char.left:
facing = -1
else:
facing = 1
if len(fireballs) < 5:
fireballs.append(projectile(round(char.x + char.width // 2), round(char.y + char.height // 2), facing))
if keys[pygame.K_a] and char.x > char.vel:
char.x -= char.vel
char.left = True
char.right = False
char.standing = False
elif keys[pygame.K_d] and char.x < display_width - char.width - char.vel:
char.x += char.vel
char.right = True
char.left = False
char.standing = False
else:
char.standing = True
char.walkCount = 0
if keys[pygame.K_w] and char.y > char.vel:
char.y -= char.vel
char.up = True
char.down = False
elif keys[pygame.K_s] and char.y < display_height - char.height - char.vel:
char.y += char.vel
char.down = True
char.up = False
else:
char.down = False
char.up = False
redrawgamewindow()
pygame.quit()
quit()