I need help with Pygame

I need help with Pygame

Postby CeroZeta » Thu Sep 08, 2016 12:05 pm

Hi, first of all! Sorry about my English! I don´t speak very well (but I will learn a lot writing in this forum :P)
I am learning to program in python and pygame and I have an error. I don´t know why :( if someone can help me i will be really happy xD

"program"
Code: Select all
import pygame,sys
from pygame.locals import *
#global  variables
width = 900
high = 480

class Rocket(pygame.sprite.Sprite):
   """Class for Rocket"""

   def __init__(self):
      pygame.sprite.Sprite.__init__(self)
      self.ImageRocket = pygame.image.load('Images/rocket.jpg')

      self.rect = slef.ImageRocket.get_rect()
      self.rect.centerx = width/2
      self.rect.centery = high-30

      self.shootingList = []
      self.Health = True

   def shoot(self):
      pass

   def draw(self,surface):
      surface.blit(self.ImageRocket, self.rect)

def SpaceInvader():
   pygame.init()
   screen = pygame.display.set_mode((width,high))
   pygame.display.set_caption("Space Invader")

   player = Rocket

   while True:
      for evento in pygame.event.get():
         if evento.type == QUIT:
            pygame.quit()
            sys.exit()

      player.draw(screen)
      pygame.display.update()

SpaceInvader()


"console"

C:\Pygame\Space_Invaders>Project.py
Traceback (most recent call last):
File "C:\Pygame\Space_Invaders\Project.py", line 43, in <module>
SpaceInvader()
File "C:\Pygame\Space_Invaders\Project.py", line 40, in SpaceInvader
player.draw(screen)
TypeError: unbound method draw() must be called with Rocket instance as first argument (got Surface instance instead)

C:\Pygame\Space_Invaders>
CeroZeta
 
Posts: 1
Joined: Thu Sep 08, 2016 11:45 am

Re: I need help with Pygame

Postby metulburr » Thu Sep 08, 2016 12:29 pm

Code: Select all
   player = Rocket

You are not creating an object, but just transferring the class to another variable. You need to add parenthesis after Rocket

Code: Select all
   player = Rocket()
we will be moving to python-forum.io on October 1 2016
more details here
User avatar
metulburr
 
Posts: 2244
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to Game Development

Who is online

Users browsing this forum: Google [Bot] and 4 guests