Camil
Camil

Reputation: 15

My draw isn't working?

I am trying to make a basic game and I was wondering why I kept getting an error when trying to draw my sprite? Any help would be gratefully appreciated.

My Code:

import pygame
import sys
import time
from time import sleep
import random

class sprite:
    def __init__(self):
        self.image = pygame.image.load(image1)
        self.x = 50
        self.y = 50
        self.image.fill(255, 255, 0)

    def background(self, surface):
        self.image2 = pygame.image.load(white.png)
        screen.blit(self.image2, (0,0))

    def draw(self, surface):
        screen.blit(self.image, (self.x, self.y))

screen = pygame.display.set_mode((1250, 500))
screen.fill((255,255,255))

sprite=sprite

while True:
    pygame.init()
    pygame.event.get()
    pygame.event.pump
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
           pygame.quit()

    sprite.draw(sprite)

My error:

Traceback (most recent call last):
  File "C:/Users/camil/Desktop/Polar Bear/PB.py", line 43, in <module>
    sprite.draw(sprite)
TypeError: draw() missing 1 required positional argument: 'surface'

Upvotes: 1

Views: 1075

Answers (1)

rlms
rlms

Reputation: 11060

Your first problem is that you need to actually instantiate the sprite class. The line sprite=sprite should be something like my_sprite = sprite(), and then my_sprite.draw(sprite) instead of sprite.draw(sprite).

However, you have other problems in your code as well.

In the draw function, the surface argument is unnecessary. You should just do this instead:

def draw(self):
    screen.blit(self.image, (self.x, self.y))

and then my_sprite.draw() rather than my_sprite.draw(sprite).

Alternatively, you could do this:

def draw(self, surface):
    surface.blit(self.image, (self.x, self.y))

and my_sprite.draw(screen), which would also make sense.

Upvotes: 1

Related Questions