Reputation: 197
My Pygame code returns an error stating:
apple.rect=pygame.rect(apple_x,apple_y,32,37)
TypeError: 'module' object is not callable
My complete code is as follow:
import sys, pygame, random
from menu_lib import *
from credit import credit
class Player(pygame.sprite.Sprite):
def __init__(self, *groups):
super(Player, self).__init__(*groups)
self.image = pygame.image.load('catcher_left.png')
self.rect = pygame.rect.Rect((320,240), self.image.get_size())
self.rect.bottom = 452
self.rect.left = 320
def update(self):
key = pygame.key.get_pressed()
if key[pygame.K_LEFT]:
self.rect.x -= 10
self.image = pygame.image.load('catcher_left.png')
pygame.display.flip()
if key[pygame.K_RIGHT]:
self.rect.x += 10
self.image = pygame.image.load('catcher_right.png')
pygame.display.flip()
if self.rect.left < 0:
self.rect.left = 0
elif self.rect.right > 640:
self.rect.right = 640
if self.rect.colliderect(apple.rect):
print "Collided!"
class Game(object):
def main(self, screen):
clock = pygame.time.Clock()
background = pygame.image.load('background.jpg')
background = pygame.transform.scale(background, (640, 480))
apple = pygame.image.load('apple.png')
apple_x=0
apple_y=0
apple_count=0
sprites = pygame.sprite.Group()
self.player = Player(sprites)
while 1:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
screen.fill((0,0,0))
pygame.display.flip()
Mainmenu()
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
screen.fill((0,0,0))
pygame.display.flip()
Mainmenu()
if apple_y==0:
apple_y+=10
apple_x=random.randint(10, 630)
apple_count+=1
elif apple_y!=0 and apple_y<480:
apple_y+=10
elif apple_y>=480:
apple_y=0
apple_x=0
apple.rect=pygame.rect(32,37,apple_x,apple_y)
sprites.update()
screen.fill((200, 200, 200))
screen.blit(background, (0, 0))
sprites.draw(screen)
score_txt='Score: '+str(apple_count)
label = myfont.render(score_txt, 1, (255,0,0))
screen.blit(label, (500, 10))
screen.blit(apple, (apple_x,apple_y))
pygame.display.flip()
def Mainmenu():
screen = pygame.display.set_mode((640, 480))
menu = cMenu(50, 50, 20, 5, 'vertical', 300, screen,
[('Start Game', 1, None),
('Credits', 2, None),
('Exit', 3, None)])
menu.set_center(True, True)
menu.set_alignment('center', 'center')
state = 0
prev_state = 1
rect_list = []
pygame.event.set_blocked(pygame.MOUSEMOTION)
while 1:
key = pygame.key.get_pressed()
if key[pygame.K_ESCAPE]:
pygame.quit()
sys.exit()
if prev_state != state:
pygame.event.post(pygame.event.Event(EVENT_CHANGE_STATE, key = 0))
prev_state = state
e = pygame.event.wait()
if e.type == pygame.KEYDOWN or e.type == EVENT_CHANGE_STATE:
if state == 0:
rect_list, state = menu.update(e, state)
elif state == 1:
screen = pygame.display.set_mode((640, 480))
Game().main(screen)
elif state == 2:
credits()
else:
print 'Exit!'
pygame.quit()
sys.exit()
if e.type == pygame.QUIT:
pygame.quit()
sys.exit()
pygame.display.update(rect_list)
def credits():
clock = pygame.time.Clock()
screen=pygame.display.set_mode((640,480))
screen.fill((0, 0, 0))
text = "Credits \n _ _ _ _ _ _ _ _ _ _ _ _ \n\n\n Designer \n Creator \n\n Manikiran P"
color = 0xa0a0a000
credit(text,myfont,color)
Mainmenu()
while 1:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
screen.fill((0,0,0))
pygame.display.flip()
Mainmenu()
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
screen.fill((0,0,0))
pygame.display.flip()
Mainmenu()
pygame.display.flip()
if __name__ == '__main__':
pygame.init()
screen=pygame.display.set_mode((640,480))
pygame.display.set_caption("Evenure - (c) 2014")
myfont = pygame.font.Font("captureit.ttf", 20)
apple_x=0
apple_y=0
apple = pygame.image.load('apple.png')
Mainmenu()
I am trying to create a rectangle of apple, for purpose of collision but ended up get the above error. If i remove the rectangle and the collision statements, It works super fine. Hoping to get the correction as soon as possible. Thank You.
Upvotes: 0
Views: 8600
Reputation: 1121744
You want pygame.Rect()
(uppercase R); pygame.rect
is the module defining the Rect
type, but it is also available in the top-level module.
In one location you are using it correctly:
self.rect = pygame.rect.Rect((320,240), self.image.get_size())
Update the problematic line to:
apple.rect = pygame.Rect(32, 37, apple_x, apple_y)
or
apple.rect = pygame.rect.Rect(32, 37, apple_x, apple_y)
However, apple
is a a Surface
object, which does not have a rect
attribute, so this will throw an exception.
Perhaps you wanted to clip the image? Use Surface.set_clip()
for that.
If you expected a Surface
object to act like a sprite, then you'll first need to create an actual sprite object. If you don't yet know how to create a sprite, you'll need to read up on sprites first:
Upvotes: 2