Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python/PyGames sinple sprite animation: I have a spaceship sprite, very simple.

ID: 3755902 • Letter: P

Question

Python/PyGames sinple sprite animation:

I have a spaceship sprite, very simple. Just need to have it animate propullsion. No matter which direction it is going. I only have 4 sprites but this is good enough for what I need. Here is my code:

import pygame
pygame.init()

win = pygame.display.set_mode((800, 600))

pygame.display.set_caption("Space Force")

char = [pygame.image.load('spaceshit1.png'), pygame.image.load('spaceshit2.png'), pygame.image.load('spaceshit3.png'), pygame.image.load('spaceshit4.png')]
bg = pygame.image.load('bg.png')
clock = pygame.time.Clock()
x = 50
y = 50
width = 48
height = 48
vel = 25
screen_width = 800
screen_height = 600
count = 0

def redrawGameWindow():
global count
win.blit(bg, (0, 0))
  
if count + 1 >=4:
count = 0
win.blit(char[count], (x, y))
count += 1
  
pygame.display.update()
  
run = True
while run:
clock.tick(12)
  
for event in pygame.event.get(): #Checks for any events. mouseclicks button press etc
if event.type == pygame.QUIT:
run = False
  
keys = pygame.key.get_pressed()
  
if keys[pygame.K_LEFT] and x > vel:
x -= vel

if keys[pygame.K_RIGHT] and x < screen_width - width - vel:
x += vel

if keys[pygame.K_UP] and y > vel:
y -= vel

if keys[pygame.K_DOWN] and y < screen_height - height - vel:
y += vel

  
redrawGameWindow()
  
pygame.quit()

Here are my sprites

Background is just generic black bg

Explanation / Answer

import pygame
pygame.init()

win = pygame.display.set_mode((800, 600))

pygame.display.set_caption("Space Force")

char = [pygame.image.load('spaceshit1.png'), pygame.image.load('spaceshit2.png'), pygame.image.load('spaceshit3.png'), pygame.image.load('spaceshit4.png')]
bg = pygame.image.load('bg.png')
clock = pygame.time.Clock()
x = 50
y = 50
width = 48
height = 48
vel = 25
screen_width = 800
screen_height = 600
count = 0

def redrawGameWindow():
global count
win.blit(bg, (0, 0))

if count + 1 >=4:
count = 0
win.blit(char[count], (x, y))
count += 1

pygame.display.update()

run = True
while run:
clock.tick(12)

for event in pygame.event.get(): #Checks for any events. mouseclicks button press etc
if event.type == pygame.QUIT:
run = False

keys = pygame.key.get_pressed()

if keys[pygame.K_LEFT] and x > vel:
x -= vel

if keys[pygame.K_RIGHT] and x < screen_width - width - vel:
x += vel

if keys[pygame.K_UP] and y > vel:
y -= vel

if keys[pygame.K_DOWN] and y < screen_height - height - vel:
y += vel


redrawGameWindow()

pygame.quit()