[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[pygame] Immovable Car, and TypeError



You guys helped me out with the invisible car, and now I have that settled, but now, in typical newbie fashion, I've messed up something else. Now, my car doesn't move at all, and I'm getting all types of TypeErrors, such as this:

 actor.erase(screen, background)
TypeError: unbound method erase() must be called with instance as first argument

I'm getting this with all kinds of code. Here's the full code, and I'll tell ya when we get to what I believe to be the trouble area.

#!C:\Python21

import whrandom, os.path, sys
import pygame, pygame.image
from pygame.locals import *

#image loader
def load_image(file, transparent):
   "loads an image, prepares it for play"
   file = os.path.join('data', file)
   try:
       surface = pygame.image.load(file)
   except pygame.error:
       raise SystemExit, 'Could not load image "%s" %s'%(file, pygame.get_error())
   if transparent:
       corner = surface.get_at((0, 0))
       surface.set_colorkey(corner, RLEACCEL)
   return surface.convert()

#Load Background
def load_back(file):
   "loads background, prepares it for play"
   file=os.path.join('data',file)
   try:
       surface = pygame.image.load(file)
   except pygame.error:
       raise SystemExit, 'Could not load image "%s" %s'%(file,pygame.get_error())
   return surface.convert()

#constants
SCREENRECT = Rect(0, 0, 350, 450)
PLAYER_SPEED = 6
FRAMES_PER_SEC = 50

#some globals for friendly access
dirtyrects = [] # list of update_rects
next_tick = 0   # used for timing
class Img: pass # container for images
class Back: pass #container for background

def wait_frame():
   "wait for the correct fps time to expire"
   global next_tick
   this_tick = pygame.time.get_ticks()
   if this_tick < next_tick:
       pygame.time.delay(next_tick - this_tick)
   next_tick = this_tick + (1000/FRAMES_PER_SEC)

class Actor:
   "An enhanced sort of sprite class"
   def __init__(self, image):
       self.image = image
       self.rect = image.get_rect()
       
   def update(self):
       "update the sprite state for this frame"
       pass
   
   def draw(self, screen):
       "draws the sprite into the screen"
       r = screen.blit(self.image, self.rect)
       dirtyrects.append(r)
       
   def erase(self, screen, background):
       "gets the sprite off of the screen"
       r = screen.blit(background, self.rect, self.rect)
       dirtyrects.append(r)

class Road(Actor):
   "The proving grounds"
   def __init__(self):
       Actor.__init__(self, Back.road)
       self.rect.centerx = SCREENRECT.centerx
       
class Player(Actor):
   "Cheer for our hero"
   def __init__(self):
       Actor.__init__(self, Img.car)
       self.alive = 1
       self.reloading = 0
       self.rect.centerx = SCREENRECT.centerx
       self.rect.bottom = SCREENRECT.bottom - 5

   def move(self, direction):
       self.rect = self.rect.move(direction*PLAYER_SPEED, 0).clamp(road)

def main():

   global dirtyrects

   #initiate pygame/display settings
   pygame.init()
   screen = pygame.display.set_mode(SCREENRECT.size, 0)
   pygame.display.set_caption('Cop Chase')
   pygame.mouse.set_visible(0)

   # Load the Resources
   Img.cop = load_image('cop.bmp', 1)
   Img.car = load_image('car.bmp', 1)
   Back.road = load_back('road.bmp')

   #create background
   background = "pygame.Surface(screen.get_size())"
   background.fill((25,150,50))

   #flip display/show background
   screen.blit(background,(0,0))
   screen.blit(Back.road, (0,0))
   screen.blit(Img.car, (0,0))
   pygame.display.flip()

   #initialize characters
   player = Player()

   while 1:

       wait_frame()
       
       # Gather Events
       pygame.event.pump()
       keystate = pygame.key.get_pressed()
       if keystate[K_ESCAPE] or pygame.event.peek(QUIT):
           break

         ######!!!???All kinds of type areas in this section of code???!!!######

       #Clear screen and update actors
       for actor in [Player]:
           actor.erase(screen, background)
           actor.update()

       # Move the player
       direction = keystate[K_RIGHT] - keystate[K_LEFT]
       Player.move(direction)

         ######!!!???End what I believe to be THE SECTION???!!!######

       # Draw everybody
       for actor in [Player]:
           actor.draw(screen)

       pygame.display.update(dirtyrects)
       dirtyrects = []

if __name__ == '__main__':
   main()

Ok, so basically, I need some hints on getting my car moving at all again, and fixing all these nasty TypeErrors. Any help would be appreciated, since I'm learning my first programming language, at this time, and it's quite frustrating fixing one thing, and then breaking another thing:)

c ya round,

Josh