Polybrow
Polybrow

Reputation: 55

Pygame - can't draw image to screen: TypeError: draw() missing 1 required positional argument: 'surface'

I want to draw a box to my screen, however when I call the function to do so it says I'm missing the argument for "surface"

As seen in the code below, the function is in a class. The function contains two parameters: "self" and "surface", and I'm passing the variable "screen" in place of "surface" so I can draw the box:

import pygame
import time
pygame.init()
(width, height) = (600, 400)
bg_colour = (100, 20, 156)

class infoBox(object):
    def __init__(self, surface):
        self.box = pygame.draw.rect(surface, (255,255,255),(0,400,400,100),2)

    def draw(self, surface):
        surface.blit(self.box)

screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("battle EduGame")

clock = pygame.time.Clock()

pygame.display.flip()

gameRun = True


while gameRun:
    event = pygame.event.poll()
    if event.type == pygame.QUIT: #if the "x" is pressed
       pygame.quit() #quit game
       gameRun = False #break the loop.
       quit()

    screen.fill(bg_colour)

    infoBox.draw(screen)


    pygame.display.update()


    clock.tick(60)

I've done things exactly like this in previous codes however it's choosing not to work here.

Upvotes: 0

Views: 299

Answers (1)

Prune
Prune

Reputation: 77837

Note your calling technique:

class infoBox(object):

    def draw(self, surface):
        surface.blit(self.box)
...

infoBox.draw(screen)

draw is an instance method, but you've invoked it as a class method. Therefore, you have only one argument, screen. You haven't provided the required self instance ... which will be needed immediately.

You need to create the object and use that to invoke the routine, something like

game_box = InfoBox(screen)
...
game_box.draw(screen)

Upvotes: 1

Related Questions