Pygame 始终检测到两个矩形对象之间的碰撞
Pygame is always sensing collision between two rect objects
一旦我 运行 代码,Pygame returns Bird 和 Pipe 正在碰撞,即使它们显然没有碰撞。
我已经尝试使用 colliderect
函数并为我在屏幕上渲染的每张图片添加了 get_rect
代码:
import pygame
from random import randint
pygame.init()
screen = pygame.display.set_mode((500, 750))
gravity = 0
class Pipe(pygame.sprite.Sprite):
def __init__(self, x_change, x, y):
pygame.sprite.Sprite.__init__(self)
self.x_change = x_change
self.x = x
self.y = y
self.pic1 = pygame.image.load('file/path.png')
self.pic1 = pygame.transform.scale(self.pic1, (28*2, 600*2)).convert_alpha()
self.pic2 = pygame.image.load('file/path.png')
self.pic2 = pygame.transform.scale(self.pic2, (28*2, 600*2)).convert_alpha()
self.rect1 = self.pic1.get_rect()
self.rect2 = self.pic2.get_rect()
def collision(self, sprite):
return self.rect1.colliderect(sprite) or self.rect2.colliderect(sprite)
class Bird(pygame.sprite.Sprite):
def __init__(self, bird_width, bird_height, y_change, x, y):
pygame.sprite.Sprite.__init__(self)
self.pic = pygame.image.load('file/path.png').convert_alpha()
self.pic = pygame.transform.scale(self.pic, (bird_width, bird_height))
self.rect = self.pic.get_rect()
self.bird_width = bird_width
self.bird_height = bird_height
self.y_change = y_change
self.x = x
self.y = y
self.velocity = 0
self.term_velocity = 15
bird = Bird(40, 30, 5, 0, 0)
pipe = Pipe(3, 500, randint(30, 650))
while True:
if pipe.collision(bird.pic.get_rect()):
print('collision')
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gravity += 0.01
pygame.display.flip()
pygame.time.Clock().tick(60)
编辑:
按照下面 Hoog 的建议后,两个矩形的位置都是 0、0。我将如何更改 get_rect()
的位置?
我实际上回答了我自己的问题。我说 get_rect()
的地方, rect
的位置是 (0, 0)。由于上面的评论,我明白了这一点。我需要说:
self.rect = self.pic.get_rect(center=(self.x, self.y))
现在,我已经开始工作了。
一旦我 运行 代码,Pygame returns Bird 和 Pipe 正在碰撞,即使它们显然没有碰撞。
我已经尝试使用 colliderect
函数并为我在屏幕上渲染的每张图片添加了 get_rect
代码:
import pygame
from random import randint
pygame.init()
screen = pygame.display.set_mode((500, 750))
gravity = 0
class Pipe(pygame.sprite.Sprite):
def __init__(self, x_change, x, y):
pygame.sprite.Sprite.__init__(self)
self.x_change = x_change
self.x = x
self.y = y
self.pic1 = pygame.image.load('file/path.png')
self.pic1 = pygame.transform.scale(self.pic1, (28*2, 600*2)).convert_alpha()
self.pic2 = pygame.image.load('file/path.png')
self.pic2 = pygame.transform.scale(self.pic2, (28*2, 600*2)).convert_alpha()
self.rect1 = self.pic1.get_rect()
self.rect2 = self.pic2.get_rect()
def collision(self, sprite):
return self.rect1.colliderect(sprite) or self.rect2.colliderect(sprite)
class Bird(pygame.sprite.Sprite):
def __init__(self, bird_width, bird_height, y_change, x, y):
pygame.sprite.Sprite.__init__(self)
self.pic = pygame.image.load('file/path.png').convert_alpha()
self.pic = pygame.transform.scale(self.pic, (bird_width, bird_height))
self.rect = self.pic.get_rect()
self.bird_width = bird_width
self.bird_height = bird_height
self.y_change = y_change
self.x = x
self.y = y
self.velocity = 0
self.term_velocity = 15
bird = Bird(40, 30, 5, 0, 0)
pipe = Pipe(3, 500, randint(30, 650))
while True:
if pipe.collision(bird.pic.get_rect()):
print('collision')
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
gravity += 0.01
pygame.display.flip()
pygame.time.Clock().tick(60)
编辑:
按照下面 Hoog 的建议后,两个矩形的位置都是 0、0。我将如何更改 get_rect()
的位置?
我实际上回答了我自己的问题。我说 get_rect()
的地方, rect
的位置是 (0, 0)。由于上面的评论,我明白了这一点。我需要说:
self.rect = self.pic.get_rect(center=(self.x, self.y))
现在,我已经开始工作了。