'ive been running my code and getting an attribute error. the error was 'pygame.key.ScancodeWrapper' object has no attribute 'x'

the code is meant to run a two-player spaceship game

import pygame 
import os


# important window variables

WIDTH, HEIGHT = 900, 500


SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))

pygame.display.set_caption('GAME')


# COLOURS

WHITE = (255, 255, 255)

BLACK = (0, 0, 0)




# other important variables:

BORDER = pygame.Rect(WIDTH/2 - 19, 0, 10, (HEIGHT))

FPS = 120

VEL = 5


SPACESHIP_WIDTH, SPACESHIP_HEIGHT = 55, 40





# all image uploads

SPACE = pygame.image.load(os.path.join('Assets', 'space.png'))

RED_SPACESHIP = pygame.image.load(os.path.join('Assets', 'spaceship_red.png'))

YELLOW_SPACESHIP = pygame.image.load(os.path.join('Assets', 'spaceship_yellow.png'))




# fitted images

RED_SPACESHIP1 = pygame.transform.scale(RED_SPACESHIP, (SPACESHIP_WIDTH, SPACESHIP_HEIGHT))

YELLOW_SPACESHP2 = pygame.transform.scale(YELLOW_SPACESHIP, (SPACESHIP_WIDTH, SPACESHIP_HEIGHT))

SPACE1 = pygame.transform.scale(SPACE, (WIDTH, HEIGHT))




# FINAL IMAGES

RED_SPACESHIP_FINAL = pygame.transform.rotate(RED_SPACESHIP1, 90)
YELLOW_SPACESHIP_FINAL = pygame.transform.rotate(YELLOW_SPACESHP2, -90)





# display function (what shows up on the screen)

def display(RED, YELLOW):

    SCREEN.fill(WHITE)

    SCREEN.blit(SPACE, (0, 0))

    pygame.draw.rect(SCREEN, BLACK, BORDER)

    SCREEN.blit(RED_SPACESHIP_FINAL, (RED.x, RED.y))

    SCREEN.blit(YELLOW_SPACESHIP_FINAL, (YELLOW.x, YELLOW.y))
    
    pygame.display.update()





# YELLOW SPACESHIP MOVEMENT FUNCTION
def yellow_movement(keys_pressed, YELLOW):
    
     keys_pressed = pygame.key.get_pressed()

     if keys_pressed[pygame.K_UP]: # UP (RIGHT SIDE)
            YELLOW.y -= VEL

     if keys_pressed[pygame.K_DOWN]: # DOWN (RIGHT SIDE)
            YELLOW.y += VEL

     if keys_pressed[pygame.K_RIGHT]:  # RIGHT (RIGHT SIDE)
            YELLOW.x += VEL

     if keys_pressed[pygame.K_LEFT]:   # LEFT (RIGHT SIDE)
            YELLOW.x -= VEL





# RED SPACESHIP MOVEMENT FUNCTION
def red_movement(keys_pressed, RED):

        keys_pressed = pygame.key.get_pressed()

        if  keys_pressed[pygame.K_w]: # UP (left side)
            RED.y -= VEL

        if keys_pressed[pygame.K_s]: # DOWN (left side)
            RED.y += VEL

        if keys_pressed[pygame.K_d]: # RIGHT (left side)
            RED.x += VEL

        if keys_pressed[pygame.K_a]: # LEFT (left side)
            RED.x -= VEL
        




# main function/loop function
def main():

    RED = pygame.Rect(100, 230, SPACESHIP_WIDTH, SPACESHIP_HEIGHT)

    YELLOW = pygame.Rect(700, 230, SPACESHIP_WIDTH, SPACESHIP_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()
       
        yellow_movement(YELLOW, keys_pressed)

        red_movement(RED, keys_pressed)
             
        display(RED, YELLOW)

    pygame.quit()



if __name__ == '__main__':
    main()


Solution 1:[1]

The functions red_movement and yellow_movement are defined such that they take keys_pressed as the first argument and the spaceship as the second argument. But you pass first the spaceship and then keys_pressed as an argument into those functions. You should therefore swap the arguments in the function definition or when they are passed into the function.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 The_spider