I've been trying to make a game but I can't get the animation to work. When I launch the game it loads all of the images on top of itself and it doesn't animate. Here's my code:
import pygame
import os
pygame.init()
width = 800
height = 600
ship_width = 56
ship_height = 64
disp = pygame.display.set_mode((width,height))
pygame.display.set_caption("space_game")
clock = pygame.time.Clock()
background = pygame.image.load(os.path.join("Backgrounds", "Space.png"))
img_names = ["sprite_00.png", "sprite_01.png", "sprite_02.png", "sprite_03.png", "sprite_04.png", "sprite_05.png", "sprite_06.png", "sprite_07.png", "sprite_08.png", "sprite_09.png"] #i load all the images here
all_imgs = {}
for img in img_names:
all_imgs[img] = pygame.image.load(img)
def gameLoop():
x = (width * 0.45)
y = (height * 0.8)
x_ch = 0
y_ch = 0
x_bg = 0
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
if event.type == pygame.KEYDOWN:
if event.key == ord("a"):
x_ch = -5
elif event.key == ord("d"):
x_ch = 5
elif event.key == ord("w"):
y_ch = -5
elif event.key == ord("s"):
y_ch = 5
if event.type == pygame.KEYUP:
if event.key == ord("a") or event.key == ord("d"):
x_ch = 0
if event.key == ord("w") or event.key == ord("s"):
y_ch = 0
x += x_ch
y += y_ch
if x > width - ship_width or x < 0:
x_ch = 0
if y > height - ship_height or y < 0:
y_ch = 0
x_loop = x_bg % background.get_rect().height
disp.blit(background, (0, x_loop - background.get_rect().height))
if x_loop < height:
disp.blit(background, (0, x_loop))
x_bg += 5
for img in img_names:
disp.blit(all_imgs[img], (x, y)) #but this part doesnt work it blits
#all the images on top of eachother
pygame.display.update()
clock.tick(60)
gameLoop()
pygame.quit()
quit()
For some reason it doesnt animate it just loads all the images on top of eachother please help me. thanks.