1

私はしばらくこれに頭を悩ませてきました。私は PyGame でゲームを作成しようとしていますが、衝突セグメントに到達し、しばらくスタックしていくつかのスレッドをチェックしました。

これは私が持っているコードです (他のメソッドとその間の条件ステートメントを削除しましたが、関連する部分は残しました)。両方のクラスの初期化で self.imageRect = self.image.get_rect() を実行しているため、エラーに少し混乱していますが、このエラーがあります。プログラムが犬クラスの衝突検出部分を実行しようとすると、エラーは具体的には
AttributeError: 'Pebble' object has no attribute '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

self.rect.widthrect にはやなどの多くのプロパティがあるため、これらは必要ありませんself.rect.topleft = location。もう 1 つの便利な機能はcenterx.

完全なリスト: pygame Rect docs

于 2013-10-20T10:30:11.197 に答える