我正在尝试编写游戏代码并进行碰撞,但是我总是得到同样的错误

时间:2015-11-22 15:59:56

标签: python pygame collision rect

# Basic Pygame Structure



import pygame                              # Imports pygame and other libraries
import os,sys
import random

# Define Classes (sprites) here



img_path = os.path.join("punch.png")
img_rock = os.path.join("rocks_rotated.png")

class gen_object(object):

    def __init__(self, x = random.randint(1,580), y = random.randint(1,480)):
        self.image = pygame.Surface([160,160])
        self.image.set_colorkey(black)
        self.image.blit(pygame.image.load(img_rock),(0,0))
        self.image_b = self.image.get_rect()
        self.x = x
        self.y = y
        dist = 10
        self.dist = dist

    def rock(self):
        dist = 10
        self.x -=dist
    def draw(self,surface):

        surface.blit(self.image,(self.x,self.y))
    def collision(self, sprite1, sprite2):
        col = pygame.sprite.collide_rect(sprite1, sprite2)
        if col == True:
            sys.exit()

class character(object):



    def __init__(self, x = 275, y = 250):


        self.image = pygame.Surface([160,160])
        self.image.set_colorkey(black)
        self.image.blit(pygame.image.load(img_path),(0,0))
        self.image_b = self.image.get_rect()
        self.x = x

        self.y = y



    def movement(self):

        key = pygame.key.get_pressed()

        dist = 10

        if key[pygame.K_w]:

            self.y -= dist

        elif key[pygame.K_s]:

            self.y += dist

        if key[pygame.K_a]:

            self.x -= dist

        elif key[pygame.K_d]:

            self.x += dist


        if self.x > 580:

            self.x = 580

        if self.y < 0:

            self.y = 0

        elif self.y > 480:

            self.y = 480
        if self.x <0:
            self.x = 0
        elif self.x == 580 and self.y > 220 and self.y < 240:
           self.x = 5
        if self.y == 0 and self.x >260 and self.x < 285:
            self.y = 475
        elif self.x == 0 and self.y > 220 and self.y < 240:
            self.x = 575
        if self.y == 480 and self.x >260 and self.x < 285:
            self.y = 5

    # top of left and right doors are y = 220, bottom of left and right doors are y = 240
    # right side of top and bottom doors are x = 285, left of top and bottom doors are x = 260
    def draw(self,surface):

        surface.blit(self.image,(self.x,self.y))





    pygame.init()                               # Pygame is initialised (starts running)

    players = pygame.sprite.Group()

    screen = pygame.display.set_mode([700,600]) # Set the width and height of    the screen [width,height]

    pygame.display.set_caption("My Game")       # Name your window

    background_image = pygame.image.load("untilted.png")

    done = False                                # Loop until the user clicks the close button.

    clock = pygame.time.Clock()                 # Used to manage how fast the    screen updates

    black    = (   0,   0,   0)                 # Define some colors using rgb values.  These can be

    white    = ( 255, 255, 255)                 # used throughout the game    instead of using rgb values.



    bird = character()
    rock = gen_object()

# -------- Main Program Loop -----------

    while done == False:



    for event in pygame.event.get():        # Check for an event (mouse click, key press)

        if event.type == pygame.QUIT:       # If user clicked close window

            done = True                     # Flag that we are done so we exit this loop

    # Update sprites here
        if rock.x < 0:
            y = random.randint(0,590)
            rock = gen_object(700,y)
    rock.collision(bird.image_b,rock.image_b)
    bird.movement()

    screen.blit(background_image,[0,0])

    bird.draw(screen)


    rock.rock()

    rock.draw(screen)

    pygame.display.update()                   # Go ahead and update the screen with what we've drawn.

    clock.tick(20)                        # Limit to 20 frames per second

pygame.quit()                               # Close the window and quit.

然后我收到了这个错误:

Traceback (most recent call last):
File "/Users/arranmcfadyen/Desktop/CourseWork/coursework.py", line 154, in <module>
rock.collision(bird.image_b,rock.image_b)
File "/Users/arranmcfadyen/Desktop/CourseWork/coursework.py", line 37, in collision
col = pygame.sprite.collide_rect(sprite1, sprite2)
File   "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site- packages/pygame/sprite.py", line 1300, in collide_rect
return left.rect.colliderect(right.rect)
AttributeError: 'pygame.Rect' object has no attribute 'rect'

1 个答案:

答案 0 :(得分:0)

pygame.sprite.collide_rect()需要两个Sprite个对象或其他具有self.rect

的对象

更改

self.image_b = self.image.get_rect()

进入

self.rect = self.image.get_rect()

然后使用

 rock.collision(bird, rock) # without .image_b or .rect
顺便说一句:为什么要使用

self.image = pygame.Surface([160,160])
self.image.set_colorkey(black)
self.image.blit(pygame.image.load(img_rock),(0,0))

取代

self.image = pygame.image.load(img_rock)
self.image.set_colorkey(black)