Pygame碰撞麻烦

时间:2015-03-29 20:39:10

标签: python pygame

由于这里收到的帮助,我在这个小程序上取得了进步。现在我收到以下错误消息:“AttributeError:'Sprite'对象没有属性'draw'”,它发生在碰撞事件之后,来自以下行:“orchids.draw(screen)”。该程序正常工作,直到碰撞事件。我是pygame的新手,也是面向对象编程的新手。关于如何让程序运行的任何想法?

#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()

1 个答案:

答案 0 :(得分:1)

你的循环

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

导致名称orchids被逐一分配给从groupcollide()返回的每个兰花。在Python中,for循环不会引入新范围,因此发生的事情是您在此循环的每次迭代中覆盖名称orchids

然后,在orchids.draw,你试图在单个兰花(Sprite对象)中找到该方法,正如Malik Brahimi所指出的那样,它是精灵群的一种方法。因此'Sprite' object has no attribute 'draw'

您可以通过将循环变量从orchids更改为orchid来解决此问题。然后什么都不会被覆盖。