Whenever I run the game, it says TypeError: draw_window() missing 2 required positional arguments: 'red' and 'yellow'

Sorry that this is a very basic question, but I’ve tried looking through the code, and it won’t work. Here’s my code:

import pygame
import os

WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Simple Game')

WHITE = (255, 255, 255)

FPS = 60
SPACESHIP_WIDTH, SPACESHIP_HEIGHT = 55, 40

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

YELLOW_SPACESHIP = pygame.transform.rotate(pygame.transform.scale(
    YELLOW_SPACESHIP_IMAGE, (SPACESHIP_WIDTH, SPACESHIP_HEIGHT)), 90 )

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

RED_SPACESHIP = pygame.transform.rotate(pygame.transform.scale(
    RED_SPACESHIP_IMAGE, (SPACESHIP_WIDTH, SPACESHIP_HEIGHT)), -90)

def draw_window(red, yellow):
    WIN.fill(WHITE)
    WIN.blit(YELLOW_SPACESHIP, (yellow.x, yellow.y))
    WIN.blit(RED_SPACESHIP, (red.x, red.y))
    pygame.display.update()


def main():
    red = pygame.Rect(100, 300, SPACESHIP_WIDTH, SPACESHIP_HEIGHT)
    yellow = pygame.Rect(700, 300, 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

        red.x += 1
        draw_window()

    pygame.quit()


if __name__ == "__main__":
    main()

whenever I run it, I get the error message TypeError: draw_window() missing 2 required positional arguments: 'red' and 'yellow'Anyone know why? I’ve looked over the code multiple times, but I can’t figure it out

>Solution :

When you call that function in your main() you need to pass in two arguments to it:

    while run:
        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False

        red.x += 1
        draw_window() <--- Here

    pygame.quit()


pass in two values like draw_window(red, yellow)

It isn’t sufficient to have variables named around it, you have to specify them in place when you call the function.

Leave a Reply