Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pygame无类碰撞_Python_Python 2.7_Pygame - Fatal编程技术网

Python Pygame无类碰撞

Python Pygame无类碰撞,python,python-2.7,pygame,Python,Python 2.7,Pygame,我是一个相对较新的python开发人员,我偶然发现了一个问题。我无法让pygame.sprite.collide_rect()函数工作,而且我在web上找不到任何解决方案。这是我的密码: import sys import pygame pygame.init() gameDisplay = pygame.display.set_mode((600,600)) pygame.display.set_caption('Doge Adventures') gameexit = False

我是一个相对较新的python开发人员,我偶然发现了一个问题。我无法让pygame.sprite.collide_rect()函数工作,而且我在web上找不到任何解决方案。这是我的密码:

import sys
import pygame

pygame.init()

gameDisplay = pygame.display.set_mode((600,600))

pygame.display.set_caption('Doge Adventures')

gameexit = False

white = (255,255,255)
black = (0,0,0)

move_x = 300
move_y = 300

def checkCollision(sprite1, sprite2):
    col = pygame.sprite.collide_rect(sprite1,sprite2)
    if col == True:
        sys.exit()

while not gameexit:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            gameexit = True
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
            move_x -= 15 
        elif event.key == pygame.K_RIGHT:
            move_x += 15
        elif event.key == pygame.K_UP:
            move_y -= 15
        elif event.key == pygame.K_DOWN:
            move_y += 15

gameDisplay.fill(white)

b1 = pygame.draw.rect(gameDisplay, black, [move_x, move_y, 40, 40])
b2 = pygame.draw.rect(gameDisplay, black, [450 , 450, 50, 50])

b1,b2

checkCollision(b1,b2)

pygame.QUIT()
quit()
我得到的错误是:

Traceback (most recent call last):
  File "C:\Users\DEREK\My Documents\LiClipse Workspace\Doge           Adventures\Game.py", line 45, in <module>
    checkCollision(b1,b2)
  File "C:\Users\DEREK\My Documents\LiClipse Workspace\Doge     Adventures\Game.py", line 19, in checkCollision
    col = pygame.sprite.collide_rect(sprite1,sprite2)
  File "C:\Python26\lib\site-packages\pygame\sprite.py", line 1147, in     collide_rect
    return left.rect.colliderect(right.rect)
 AttributeError: 'pygame.Rect' object has no attribute 'rect'
回溯(最近一次呼叫最后一次):
文件“C:\Users\DEREK\My Documents\LiClipse Workspace\Doge Adventures\Game.py”,第45行,在
检查碰撞(b1、b2)
文件“C:\Users\DEREK\My Documents\LiClipse Workspace\Doge Adventures\Game.py”,第19行,在checkCollision中
col=pygame.sprite.collide_rect(sprite1,sprite2)
文件“C:\Python26\lib\site packages\pygame\sprite.py”,第1147行,在collide\u rect中
返回left.rect.colliderect(right.rect)
AttributeError:“pygame.Rect”对象没有属性“Rect”

顺便说一句,我使用的是Python2.7,pygame.sprite.Collide Rect方法用于检查sprite之间的冲突,而不是您正在使用的矩形。精灵是
pygame.sprite.sprite
类的实例。您试图检测的是矩形之间的碰撞,它们有自己的碰撞检测方法:
b1.collizerect(b2)
。矩形不是精灵,精灵也不是矩形。

请注意,我对python类/对象的了解有限,因此如果可以,请不要在解决方案中包含类。谢谢:)