1

一段时间以来,我一直在努力解决这个问题。我正在尝试使用 PyGame 制作游戏,但我遇到了碰撞部分并且已经卡住了一段时间并检查了一些线程。

这是我拥有的代码(删除了中间的其他方法和条件语句,但留下了相关部分)。我对这个错误有点困惑,因为我在两个类 init 中都做了一个 self.imageRect = self.image.get_rect() ,但是我有这个错误。当程序尝试在狗类中执行碰撞检测部分时,错误具体为:
AttributeError: 'Pebble'对象没有属性'rect'”。我做错了什么?

import random
import pygame, sys

pygame.init()
clock = pygame.time.Clock() # fps clock

screenSize = WIDTH, HEIGHT = [800, 600]
screen = pygame.display.set_mode(screenSize)

background = pygame.Surface(screen.get_size())
bgColorRGB = [153, 204, 255]
background.fill(bgColorRGB)


pebbleGroup = pygame.sprite.Group()
pebbleSingle = pygame.sprite.GroupSingle() 
dogSingle = pygame.sprite.GroupSingle()


#----------------------------------------------------------------------
class Dog(pygame.sprite.Sprite):
    def __init__(self, path, speed):
        pygame.sprite.Sprite.__init__(self) #call Sprite initializer
        self.image = pygame.image.load(path) # load sprite from path/file loc.
        self.imageRect = self.image.get_rect() # get bounds of image
        self.imageWidth = self.image.get_width()
        self.imageHeight = self.image.get_height()
        self.speed = speed

    # sets location of the image, gets the start location of object
    # sets the start location as the image's left and top location
    def setLocation(self, location):
        self.imageRect.left, self.imageRect.top = location


    def checkCollision(self, pebble, dogGroup):
        if pygame.sprite.spritecollide(pebble, dogGroup, False):
                print "collided"

#---------------------------------------------------------------------
class Pebble(pygame.sprite.Sprite):
    def __init__(self, path, speed, location):
        pygame.sprite.Sprite.__init__(self) 
        self.image = pygame.image.load(path) 
        self.imageRect = self.image.get_rect()
        self.imageWidth = self.image.get_width()
        self.imageHeight = self.image.get_height()
        self.imageRect.left, self.imageRect.top = location
        self.speed = speed # initialize speed
        self.isDragged = False


#----------------------------------------------------------------------
def startGame():

    pebblePaths = ['images/pebble/pebble1.jpg', 'images/pebble/pebble2.jpg']
    for i in range(3):
        pebblePath = pebblePaths[random.randrange(0, len(pebblePaths))]
        pebbleSpeed = [random.randrange(1, 7), 0]
        pebbleLocation = [0, random.randrange(20, HEIGHT - 75)]
        pebble = Pebble(pebblePath, pebbleSpeed, pebbleLocation)
        pebbleGroup.add(pebble)

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

        #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


        for pebble in pebbleGroup:
            dog.checkCollision(pebble, dogSingle)

        pygame.display.flip()
        clock.tick(30) # wait a little before starting again
startGame()
4

1 回答 1

0

这是期待的Sprite.rect,所以从

self.imageRect = self.image.get_rect()
#to
self.rect = self.image.get_rect()

笔记

self.imageWidth = self.image.get_width()
self.imageHeight = self.image.get_height()
self.imageRect.left, self.imageRect.top = location

这些不是必需的,因为 rect 有很多属性,比如self.rect.widthor self.rect.topleft = location。另一个有用的是centerx.

完整列表位于:pygame Rect docs

于 2013-10-20T10:30:11.197 回答