我目前正在制作游戏,我正在添加播放器动画并尝试使用pyganim制作动画。我有想要用于左空闲,右空闲,左跑,右跑的图像。这是我的播放器的代码:
import pygame, time
import pygame.mixer
from bullet import Bullet
from constants import *
# Player
class Player(pygame.sprite.Sprite):
def __init__(self, x, y, gravity):
pygame.mixer.init()
# Player dimensions and position
# Player image and animation
self.images = []
self.images.append(pygame.image.load('data/images/Sprites/player.png'))
self.images.append(pygame.image.load('data/images/Sprites/player2.png'))
self.maxImage = len(self.images)
self.currentImage = 0
self.jumpSound = pygame.mixer.Sound('data/sounds/jump.ogg')
self.shootSound = pygame.mixer.Sound('data/sounds/laser.ogg')
self.power = POWER_NONE
#~ self.rect = pygame.Rect(x, y, 80, 80)
self.rect = self.images[0].get_rect()
self.rect.x = x
self.rect.y = y
self.timeTarget = 10
self.timeNum = 0
self.velX = 0
self.velY = 0
self.friction = 1
self.health = 200
self.lives = 3
self.score = 0
self.second = 1000
self.minute = 60000
self.start_alivetime = pygame.time.get_ticks()
self.start_alivetime2 = pygame.time.get_ticks()
self.direction = DIR_RIGHT
# Jump and gravity
self.jumping = False
self.on_ground = False
self.origJumpVel = 17
self.jumpVel = self.origJumpVel
self.gravity = 0.5
# Jump inputs
def do_jump(self):
if self.jumping and not self.on_ground:
self.velY = -self.jumpVel
self.jumpVel -= self.gravity
if self.on_ground:
self.jumping = False
self.jumpVel = self.origJumpVel
self.velY = 0
self.on_ground = True
def handle_events(self, event):
key = pygame.key.get_pressed()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
self.jumpSound.play(0)
if not self.jumping:
self.jumping = True
self.on_ground = False
if event.key == pygame.K_s:
self.shootSound.play(0)
if key[pygame.K_a]:
self.velX = -7
if key[pygame.K_d]:
self.velX = +7
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
self.shootSound.play(0)
elif event.type == pygame.KEYUP:
if event.key in (pygame.K_a, pygame.K_d):
self.velX = 0
elif event.type == pygame.KEYUP:
if event.key in (pygame.K_LEFT, pygame.K_RIGHT):
bullet.velX = 0
# PLayer updates
def update(self):
#self.timeNum += 1
# Animations
#if self.direction == DIR_LEFT:
#if self.timeNum == self.timeTarget:
#self.currentImage += 1
#if self.currentImage >= self.maxImage:
#self.currentImage = 0
#self.timeNum = 0
# Timer
if self.health > 0:
self.alivetime = (pygame.time.get_ticks() - self.start_alivetime) / self.second
if self.health <= 0:
self.alivetime = pygame.time.get_ticks() + 0
self.start_alivetime = self.alivetime
# Screen wrap
if self.rect.right > 1280:
self.rect.left = 0
elif self.rect.left < 0:
self.rect.right = 1280
if self.velX < 0 and self.direction != DIR_RIGHT: # Moving right
self.direction = DIR_RIGHT
self.images[self.currentImage] = pygame.transform.flip(self.images[self.currentImage], True, False)
elif self.velX > 0 and self.direction != DIR_LEFT: # Moving left
self.direction = DIR_LEFT
self.images[self.currentImage] = pygame.transform.flip(self.images[self.currentImage], True, False)
# Player rendering
def render(self, surface):
surface.blit(self.images[self.currentImage], self.rect)
以下是我的播放器动画的两个图像(向左跑):