Python get_rect() problem

Started by
1 comment, last by K1NNY 12 years, 6 months ago
Basicaly this bit of code is for a game im making where your just a zombie eating brains that appear on the map. Simple enough right? well when i use the code im written it keeps saying that the class i use for the brain has no attribute get_rect().

I would appreciate any help to this problem. Heres the code:

import pygame
import random

pygame.init()

#Get those colors
red = (255, 0, 0)
black = ( 0, 0, 0)
white = (255,255,255)

#load player image
brain = pygame.image.load("Brain.BMP")
background = pygame.image.load("background.BMP")
zombie = pygame.image.load("zombie.BMP")
killer = pygame.image.load("killer.BMP")

#load player class, because he is a sprite
class Player(pygame.sprite.Sprite):
change_x=0
change_y=0

def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)

self.image = zombie

self.rect = self.image.get_rect()
self.rect.topleft = [x,y]

def changespeed(self, x, y):
self.change_x+=x
self.change_y+=y

def update(self):
self.rect.top += self.change_y
self.rect.left += self.change_x


#Load the Brains class
class Brain(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)

self.image = brain
self.rect = self.image.get_rect()

brain_list = pygame.sprite.RenderPlain()
all_things_list = pygame.sprite.RenderPlain()

for i in range(50):
brain = Brain()

brain.rect.x = random.randrange(800)
brain.rect.y = random.randrange(600)

brain_list.add(brain)
all_things_list.add(brain)

player = Player(0, 0)
all_things_list.add(player)

#Make the display
screen = pygame.display.set_mode((800, 600))

pygame.display.set_caption("My Game")

score = 0

done = False

clock = pygame.time.Clock()

#--------Main Game loop!--------
while done is False:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True

if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.changespeed(-3, 0)
player.image = pygame.transform.rotate(zombie, 90)
if event.key == pygame.K_RIGHT:
player.changespeed(3,0)
player.image = pygame.transform.rotate(zombie, -90)
if event.key == pygame.K_UP:
player.changespeed(0,-3)
player.image = pygame.transform.rotate(zombie, 0)
if event.key == pygame.K_DOWN:
player.changespeed(0,3)
player.image = pygame.transform.rotate(zombie, 180)

if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
player.changespeed(3,0)
if event.key == pygame.K_RIGHT:
player.changespeed(-3,0)
if event.key == pygame.K_UP:
player.changespeed(0,3)
if event.key == pygame.K_DOWN:
player.changespeed(0,-3)

brains_hit_list = pygame.sprite.spritecollide(player, brain_list, True)

if len(brains_hit_list) > 0:
score +=len(blocks_hit_list)
print( score )

screen.fill(white)
screen.blit(background,(0,0))

all_things_list.draw((screen))

player.update()


clock.tick(50)

pygame.display.flip()


pygame.quit()

Advertisement
Its because you are accidentally reusing the identifier brain.

First you create a surface called brain:

brain = pygame.image.load("Brain.png")

Then you are creating an instance of the Brain class also called brain:

brain = Brain()




This will rebind the identifier brain, which is probably not what you want.

I would suggest you use different names for the surfaces, like


surface_brain = pygame.image.load("Brain.png")
surface_background = pygame.image.load("background.jpg")
surface_zombie = pygame.image.load("zombie.jpg")
surface_killer = pygame.image.load("killer.png")
Thank you. Obviously a rookie mistake now that i think of it. I gave u a +1

This topic is closed to new replies.

Advertisement