So i recently started trying to make a game. Im having trouble moving the player that is animated. I have it flip through a sprite sheet for the animation i just dont know how to have that animation move around the screen. So I just need to know how i can move an animating image. In the game I have a running animation and a idle animation i want it to switch to the running animation when i hold a arrow key and do the running animation while moving across the screen. Then go back to the idle animation while not moving.
import pygame
from sys import exit
import time
pygame.init()
pygame.display.set_caption('Rocket_Jump')
width = 1750
height = 1250
screen = pygame.display.set_mode((width,height))
#FPS
clock = pygame.time.Clock()
#Black Gunner
bg_idle_sheet = pygame.image.load(r'Rocket_Jump\TeamGunner_By_SecretHideout_060519\CHARACTER_SPRITES\Black\Gunner_ Black_Idle.png').convert_alpha(); bg_idle_frames = 5
bg_run_sheet = pygame.image.load(r'Rocket_Jump\TeamGunner_By_SecretHideout_060519\CHARACTER_SPRITES\Black\Gunner_Black_run.png').convert_alpha(); bg_run_frames = 6
bg_crouch_sheet = pygame.image.load(r'Rocket_Jump\TeamGunner_By_SecretHideout_060519\CHARACTER_SPRITES\Black\Gunner_Black_crouch.png').convert_alpha(); bg_crouch_frames = 3
bg_death_sheet = pygame.image.load(r'Rocket_Jump\TeamGunner_By_SecretHideout_060519\CHARACTER_SPRITES\Black\Gunner_Black_death.png').convert_alpha(); bg_death_frames = 8
bg_jump_sheet = pygame.image.load(r'Rocket_Jump\TeamGunner_By_SecretHideout_060519\CHARACTER_SPRITES\Black\Gunner_Black_jump.png').convert_alpha(); bg_jump_frames = 2
#Background
BG = (178, 190, 181)
#start position
start_x = 100
start_y = 100
#Gets images from a sprite sheet
def get_image(sheet, frame, width, height, scale, color):
image = pygame.Surface((width, height)).convert_alpha()
image.blit(sheet, (0, 0), (frame * width, 0, width, height))
image = pygame.transform.scale(image, (width * scale, width * scale))
image.set_colorkey(color)
return image
#Animation
class Gunner(pygame.sprite.Sprite):
def __init__(self, sheet, frames, pos_x, pos_y, type):
pygame.sprite.Sprite.__init__(self)
self.sprites = [get_image(sheet, i, 48, 48, 4, 'Black') for i in range(frames)]
self.current_sprite = 0
self.image = self.sprites[self.current_sprite]
self.rect = self.image.get_rect()
self.x = pos_x
self.y = pos_y
self.rect.center = [self.x, self.y]
self.type = type
def update(self):
self.current_sprite += 1
if self.current_sprite >= len(self.sprites):
self.current_sprite = 0
self.image = self.sprites[self.current_sprite]
def draw(self, screen):
screen.blit(self.image, self.rect)
moving_sprites = pygame.sprite.Group()
run = True
while run:
#What keys are pressed
keys = pygame.key.get_pressed()
player_idle = Gunner(bg_idle_sheet, bg_idle_frames, start_x, start_y, 1)
player_run = Gunner(bg_run_sheet, bg_run_frames, 100, 100, 2)
moving_sprites.add(player_idle)
moving_sprites.add(player_run)
#Screen color
screen.fill(BG)
#Quit Program
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if keys[pygame.K_RIGHT]:
for sprite in moving_sprites:
if sprite.type == 1:
start_x += 1
sprite.draw(screen)
elif sprite.type == 2:
sprite.draw(screen)
moving_sprites.update()
pygame.display.flip()
pygame.display.update()
clock.tick(12)
