Martin Trueman
Martin Trueman

Reputation: 45

Pygame collision trouble

I've been making progress with this little programme thanks to help received here. Now I get the following error message: "AttributeError: 'Sprite' object has no attribute 'draw'" which occurs after a collision event, from the line: "orchids.draw(screen)". The programme works fine until the collision event. I'm new to pygame and new to Object Oriented Programming. Any ideas on how to get the programme running?

#Import and Init
import pygame
import random
from pygame.locals import *
pygame.init()

class Orchid(pygame.sprite.Sprite):
    def __init__(self, pos):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load('orchid1.gif')
        self.rect = self.image.get_rect()
        self.rect.topleft = pos
        self.x_inc = 4
        self.y_inc = 4

    def update(self):
        self.rect.x += self.x_inc
        self.rect.y += self.y_inc
        if self.rect.y > 700 or self.rect.y < 0:
            self.y_inc = self.y_inc * -1                   
        if self.rect.x > 1240 or self.rect.x < 0:
            self.x_inc = self.x_inc * -1

#Set Up the Window
width = 1240
height = 700
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Pollination!")

background = pygame.Surface(screen.get_size())
background = pygame.image.load("Forrest.jpg")
background = background.convert()

#Load and Convert the wasp
wasp = pygame.sprite.Sprite()
wasp.image = pygame.image.load("wasp1.gif")
wasp.rect = wasp.image.get_rect()
wasp_group = pygame.sprite.GroupSingle(wasp)

orchids = pygame.sprite.OrderedUpdates()

for i in range(7):
    tmp_x = random.randrange(0, 1240)
    tmp_y = random.randrange(0, 700)
    orchids.add(Orchid([tmp_x, tmp_y])) 

buzz = pygame.mixer.Sound("buzz.wav")
#Clock and Loop Variables
framerate = pygame.time.Clock()
GameGo = True

wasp_move = 5
#The Main Loop
while GameGo:

    #Tick the Clock
    framerate.tick(60)

    #Keyborad Keypress Events, Movement

    if pygame.key.get_pressed()[K_UP]:
        wasp.rect.top -= wasp_move
        if wasp.rect.top < -30:
            wasp.rect.top = -30
    if pygame.key.get_pressed()[K_DOWN]:
        wasp.rect.top += wasp_move
        if wasp.rect.top > 625:
            wasp.rect.top = 625
    if pygame.key.get_pressed()[K_LEFT]:
        wasp.rect.right -= wasp_move
        if wasp.rect.right < 110:
            wasp.rect.right = 110
    if pygame.key.get_pressed()[K_RIGHT]:
        wasp.rect.right += wasp_move
        if wasp.rect.right > 1255:
            wasp.rect.right = 1255  

    for orchids in pygame.sprite.groupcollide(wasp_group, orchids, False, True):
        buzz.play()
##  #Blit our Images
    screen.blit(background, (0, 0))
    wasp_group.draw(screen)
    orchids.draw(screen)
    orchids.update()
    pygame.display.update()

  #Handle a Close Event
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            GameGo = False

pygame.quit()

Upvotes: 1

Views: 114

Answers (1)

Eli Rose
Eli Rose

Reputation: 6988

Your loop

    for orchids in pygame.sprite.groupcollide(wasp_group, orchids, False, True):
        buzz.play()

causes the name orchids to be assigned to each orchid returned from groupcollide(), one by one. In Python, for loops don't introduce a new scope, so what's happening is that you're overwriting the name orchids each iteration of this loop.

Then, at orchids.draw, you are trying to find that method in the individual orchid (the Sprite object) when, as Malik Brahimi points out, it's a method of sprite groups. Hence 'Sprite' object has no attribute 'draw'.

You can fix this by changing the loop variable from orchids to orchid. Then nothing gets overwritten.

Upvotes: 1

Related Questions