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

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.




1 Answer
1



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.


draw


screen


self



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


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



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)