I'm new to coding so bear with me. I have two sprites : one on the left and one on the right. They are supposed to move with keys WASD (left side) and the arrows (right side). They both move properly, except that the right sprite isn't moving left with the left key.
Here's my code! I'd appreciate any help I can get.
import pygame
import os
pygame.init()
#window
WIDTH , HEIGHT= 900,500
WIN = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("Bugaboo")
#color
PEACH = (100,250,125)
BLACK=(0,0,0)
BORDER = pygame.Rect(WIDTH/2-5,0,2,HEIGHT)
#speed (fps)
FPS = 60
VEL = 5
HOBBIT_WIDTH, HOBBIT_HEIGHT = 150,100
EYE_WIDTH, EYE_HEIGHT = 100,100
#IMAGE FILE
FRODO = pygame.image.load(os.path.join('Assets','frodo.png'))
FRODO = pygame.transform.scale(FRODO,(HOBBIT_HEIGHT,HOBBIT_WIDTH))
SAURON = pygame.image.load(os.path.join('Assets','sauron.png'))
SAURON = pygame.transform.scale(SAURON,(EYE_HEIGHT,EYE_WIDTH))
#WINDOW
def draw_window(hobbit,eye):
WIN.fill(PEACH)
pygame.draw.rect(WIN,BLACK,BORDER)
#yellow.x, yellow.y
WIN.blit(FRODO,(hobbit.x,hobbit.y))
#red.x, red.y
WIN.blit(SAURON,(eye.x,eye.y))
pygame.display.update()
def hobbit_movement(keys_pressed,hobbit):
if keys_pressed[pygame.K_a] and hobbit.x + VEL > 0 : # left
hobbit.x -= VEL
if keys_pressed[pygame.K_d] and hobbit.x - VEL + hobbit.width < BORDER.x : # right
hobbit.x += VEL
if keys_pressed[pygame.K_w] and hobbit.y + VEL > 0: # up
hobbit.y -= VEL
if keys_pressed[pygame.K_s] and hobbit.y - VEL + hobbit.height < HEIGHT - 15: # down
hobbit.y += VEL
def eye_movement(keys_pressed,eye):
if keys_pressed[pygame.K_LEFT] and eye.x + VEL > 0 : # left
eye.x -= VEL
if keys_pressed[pygame.K_RIGHT] and eye.x - VEL + eye.width < BORDER.x : # right
eye.x += VEL
if keys_pressed[pygame.K_UP] and eye.y + VEL > 0: # up
eye.y -= VEL
if keys_pressed[pygame.K_DOWN] and eye.y - VEL + eye.height < HEIGHT - 15: # down
eye.y += VEL
#MAIN
def main():
hobbit = pygame.Rect(50,100,HOBBIT_WIDTH,HOBBIT_HEIGHT)
eye = pygame.Rect(600,100,EYE_WIDTH,EYE_HEIGHT)
clock = pygame.time.Clock()
run=True
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run=False
keys_pressed = pygame.key.get_pressed()
#"hobbit movement"
hobbit_movement(keys_pressed, hobbit)
#eye movement
eye_movement(keys_pressed,eye)
draw_window(hobbit,eye)
pygame.quit()
#IDK WHAT THIS IS
if __name__ == "__main__":
main()