I am making a simple platformer game using pygame 2.1.2 and python 3.10. Everything was going well until I ran into a puzzle, which was simply switching between 2 images.
I've been trying to get it working for about 2 days and haven't gotten anything but the same thing, with only one showing at a time.
Here is my full code:
# import library/libraries
import pygame as pg
pg.init ()
# creates the window
display = pg.display.set_mode ((960, 720))
# window details
pg.display.set_caption ("Pygame Window")
# player
player_img_idle = pg.image.load ("player_idle.png")
player_img_left1 = pg.image.load ("player_left1.png")
player_img_right1 = pg.image.load ("player_right1.png")
player_img_left2 = pg.image.load ("player_left2.png")
player_img_right2 = pg.image.load ("player_right2.png")
player_img = player_img_idle
player_x = 10
player_y = 10
player_x_change = 0
player_y_change = 0.5
# definitions
def player (x, y):
display.blit (player_img, (x, y))
# keeps the window open
execute = True
while execute:
# sets window color (red, green, blue)
display.fill ((255, 255, 255))
# events
for event in pg.event.get ():
# checks if the X button has been pressed
if event.type == pg.QUIT:
execute = False
# checks if a key has been pressed
if event.type == pg.KEYDOWN:
if event.key == pg.K_w and player_y == 675:
player_y_change = -2
if event.key == pg.K_a:
player_x_change = -0.5
if event.key == pg.K_d:
player_x_change = 0.5
# checks if a key has been released
if event.type == pg.KEYUP:
if event.key == pg.K_a:
player_x_change = 0
player_img = player_img_idle
if event.key == pg.K_d:
player_x_change = 0
player_img = player_img_idle
# moves actors
player_x = player_x + player_x_change
player_y = player_y + player_y_change
player_y_change = player_y_change + 0.0075
# border
if player_x > 915:
player_x = 915
if player_x < 0:
player_x = 0
if player_y > 675:
player_y = 675
# loads actors
player (player_x, player_y)
# updates game window
pg.display.update ()
I am not certain where or if my code is supposed to be inside the branch of the events but there aren't any places where I think It would've worked.
Also, if you need the dimensions for the player, it is 45x45.