Building a simple PyGame in which I need the player sprite to move and change image based on arrow key presses. I added the facing function in the Sprite1 class and call it in the if statements for key presses in the game loop hoping it would change image every time a key is pressed but it doesn't seem to want to update the sprite's image after key presses. Is the problem with my facing function? Or with something else?
import pygame
pygame.init()
#game Window
screen_width = 800
screen_height = 400
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Warrior Quest')
#game variables
main_menu = True
#background image
background_img = pygame.image.load('background.png')
#button images
start_img = pygame.image.load('startbutton.PNG')
cancel_img = pygame.image.load('cancelbutton.PNG')
title_img = pygame.image.load('warriorquestTile.PNG')
#background function
def draw_bg():
screen.blit(background_img, (0,0))
class Sprite1(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.faceUp = True
self.faceDown = False
self.faceLeft = False
self.faceRight = False
self.image = pygame.image.load('player.png').convert_alpha()
self.rect = self.image.get_rect()
def draw(self):
screen.blit(self.image, self.rect)
def facing(self):
if self.faceUp == True:
self.image = pygame.image.load('player.png').convert_alpha()
self.rect.w = self.image.get_rect().w
self.rect.h = self.image.get_rect().h
elif self.faceDown == True:
self.image = pygame.image.load('playerDown.png').convert_alpha()
self.rect = self.image.get_rect()
elif self.faceLeft == True:
self.image = pygame.image.load('playerLeft.png').convert_alpha()
self.rect = self.image.get_rect()
elif self.faceRight == True:
self.image = pygame.image.load('playerRight.png').convert_alpha()
self.rect = self.image.get_rect()
#setup player
player = Sprite1()
player.rect.x = 400
player.rect.y = 380
class Button():
def __init__(self,x,y,image):
self.image = image
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.clicked = False
def draw(self):
action = False
#get mouse position
pos = pygame.mouse.get_pos()
if self.rect.collidepoint(pos):
if pygame.mouse.get_pressed()[0] == 1 and self.clicked == False:
action = True
self.clicked = True
if pygame.mouse.get_pressed()[0] == 0:
self.clicked = False
#draw Button
screen.blit(self.image, self.rect)
return action
#create buttons
start_button = Button(screen_width // 2 -350, screen_height // 2, start_img)
cancel_button = Button(screen_width // 2 + 150, screen_height // 2, cancel_img)
title_button = Button(300,400,title_img)
#game loop running
running = True
while running:
draw_bg()
if main_menu == True:
if start_button.draw():
main_menu = False
if cancel_button.draw():
running = False
else:
player.draw()
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and player.rect.x>5:
player.faceLeft = True
player.facing()
player.rect.x -= 5
if keys[pygame.K_RIGHT] and player.rect.x<790:
player.faceRight = True
player.facing()
player.rect.x += 5
if keys[pygame.K_UP] and player.rect.y>10:
player.faceUp = True
player.facing()
player.rect.y -= 5
if keys[pygame.K_DOWN]and player.rect.y<395:
player.faceDown = True
player.facing()
player.rect.y += 5
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.update()
pygame.quit()