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

Question:

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

Asked By: DerpySmiley

||

Answers:

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.

Answered By: brunson

I think it’s cuz you call draw_window() without any arguments in the fourth to last line. The draw_window(red, yellow) function can’t run without the data you want it to use (red and yellow).

Answered By: Cdp
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.