Sign in to follow this  
K1NNY

Python get_rect() problem

Recommended Posts

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:

[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()
[/code]

Share this post


Link to post
Share on other sites
Its because you are accidentally reusing the identifier brain.

First you create a surface called brain:

[i]brain = pygame.image.load("Brain.png") [/i]

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

[i]brain = Brain()[/i]
[i]
[/i]
[i]
[/i]
This will rebind the identifier brain, which is probably not what you want.

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


[i]surface_brain = pygame.image.load("Brain.png")[/i]
[i]surface_background = pygame.image.load("background.jpg")[/i]
[i]surface_zombie = pygame.image.load("zombie.jpg")[/i]
[i]surface_killer = pygame.image.load("killer.png")[/i]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this