0

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()
Buddy Bob
  • 5,829
  • 1
  • 13
  • 44
  • This is what it does: [What does if __name__ == “__main__”: do?](https://stackoverflow.com/questions/419163/what-does-if-name-main-do?rq=1). – Have a nice day May 23 '21 at 03:52
  • It's supposed to be `for events in pygame.event.get()` `if events.type == pygame.QUIT:` – Buddy Bob May 23 '21 at 03:57

1 Answers1

0

There is an issue when you try to move to left or right with the right sprite:

The left boundary of the right sprite is the border in the middle of the screen instead of 0

if keys_pressed[pygame.K_LEFT] and eye.x + VEL > 0 : # left

if keys_pressed[pygame.K_LEFT] and eye.x + VEL > BORDER.x:

The right boundary of the right sprite is the width of the window instead of the border in the middle of the screen

if keys_pressed[pygame.K_RIGHT] and eye.x - VEL + eye.width < BORDER.x : # right

if keys_pressed[pygame.K_RIGHT] and eye.x - VEL + eye.width < WIDTH :  # right

You can simplify the code with clamp_ip():

def hobbit_movement(keys_pressed,hobbit):
    hobbit.x += (keys_pressed[pygame.K_d] - keys_pressed[pygame.K_a]) * VEL
    hobbit.y += (keys_pressed[pygame.K_s] - keys_pressed[pygame.K_w]) * VEL

    boundaries = pygame.Rect(0, 0, BORDER.x, HEIGHT)
    hobbit.clamp_ip(boundaries)

def eye_movement(keys_pressed,eye):
    eye.x += (keys_pressed[pygame.K_RIGHT] - keys_pressed[pygame.K_LEFT]) * VEL
    eye.y += (keys_pressed[pygame.K_DOWN] - keys_pressed[pygame.K_UP]) * VEL

    boundaries = pygame.Rect(BORDER.x, 0, WIDTH-BORDER.x, HEIGHT)
    eye.clamp_ip(boundaries)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174