因此,我已经开始为学校制作一款RPG游戏。我不知道如何使角色从左向左移动到站立动画,从右向右移动到站立动画。
这是我的运行代码:
def get_keys(self):
self.acc = vec(0, 0)
keys = pg.key.get_pressed()
if keys[pg.K_LEFT] or keys[pg.K_a]:
self.acc.x = -PLAYER_SPEED
if keys[pg.K_RIGHT] or keys[pg.K_d]:
self.acc.x = PLAYER_SPEED
if keys[pg.K_UP] or keys[pg.K_w]:
self.acc.y = -PLAYER_SPEED
if keys[pg.K_DOWN] or keys[pg.K_s]:
self.acc.y = PLAYER_SPEED
if keys[pg.K_SPACE]:
now = pg.time.get_ticks()
if now - self.last_shot > SWING_RATE:
self.last_shot = now
dir = vec(1, 0)
pos = self.pos + SWORD_OFFSET
if self.vel.x < 0:
dir = vec(-1, 0)
self.game.swing_img = pg.transform.flip(self.game.swing_img, True, False)
pos = self.pos + SWORD_OFFSET2
Swing(self.game, pos, dir)
self.acc += self.vel * PLAYER_FRICTION
self.vel += self.acc
if abs(self.vel.x) < 0.1:
self.vel.x = 0
if abs(self.vel.y) < 0.1:
self.vel.y = 0
self.pos += self.vel + 0.5 * self.acc
以下是Sprite的所有代码:
def load_images(self):
self.standing_frames_r = [self.game.spritesheet.get_image(0, 110, 46,58),
self.game.spritesheet.get_image(53, 110, 46,58),
self.game.spritesheet.get_image(106, 110, 50,58),
self.game.spritesheet.get_image(163, 110, 50,58)]
for frame in self.standing_frames_r:
frame.set_colorkey(WHITE)
self.standing_frames_l= []
for frame in self.standing_frames_r:
frame.set_colorkey(WHITE)
self.standing_frames_l.append(pg.transform.flip(frame, True, False))
self.run_frames_r = [self.game.spritesheet.get_image(0, 0, 40, 58),
self.game.spritesheet.get_image(47, 0, 40, 58),
self.game.spritesheet.get_image(94, 0, 40, 58),
self.game.spritesheet.get_image(141, 0, 46, 58),
self.game.spritesheet.get_image(194, 0, 40, 58),
self.game.spritesheet.get_image(241, 0, 40, 58)]
for frame in self.run_frames_r:
frame.set_colorkey(WHITE)
self.run_frames_l = []
for frame in self.run_frames_r:
frame.set_colorkey(WHITE)
self.run_frames_l.append(pg.transform.flip(frame, True, False))
self.swing_r = [self.game.swingsheet.get_image(24, 15, 5, 3),
self.game.swingsheet.get_image(83, 14, 17, 6),
self.game.swingsheet.get_image(147, 14, 28, 7),
self.game.swingsheet.get_image(213, 15, 33, 15),
self.game.swingsheet.get_image(282, 15, 31, 36),
self.game.swingsheet.get_image(353, 15, 24, 48),
self.game.swingsheet.get_image(410, 16, 31, 48),
self.game.swingsheet.get_image(472, 23, 33, 41),
self.game.swingsheet.get_image(534, 27, 35, 37),
self.game.swingsheet.get_image(596, 35, 37, 29),
self.game.swingsheet.get_image(660, 48, 34, 15)]
for frame in self.swing:
frame.set_colorkey(WHITE)
self.swing_l = []
for frame in self.swing_r:
frame.set_colorkey(WHITE)
self.swing_l.append(pg.transform.flip(frame, True, False))
以及动画:
def animate(self):
now = pg.time.get_ticks()
if self.vel.x != 0 or self.vel.y != 0:
self.running = True
else:
self.running = False
#show walk animation
if self.running:
if now - self.last_update > 200:
self.last_update = now
self.current_frame = (self.current_frame + 1) %
len(self.run_frames_l)
rect = self.rect.center
if self.vel.x > 0:
self.image = self.run_frames_r[self.current_frame]
else:
self.image = self.run_frames_l[self.current_frame]
self.rect = self.image.get_rect()
self.rect.center = rect
#show idle animation
if not self.running:
if now - self.last_update > 500:
self.last_update = now
self.current_frame = (self.current_frame + 1) %
len(self.standing_frames_r)
rect = self.rect.center
self.image = self.standing_frames_r[self.current_frame]
#self.image = self.standing_frames_l[self.current_frame]
self.rect = self.image.get_rect()
self.rect.center = rect
使用pygame.key.get_pressed()
可以做到这一点吗?例如,如果我释放standing_frames_l animation
来开始。
如果需要,我可以删除所有文件和所有代码。