为什么我的碰撞测试总是返回' true'为什么图像的矩形位置总是错误的(0,0)?
我的 collide_rect 函数无法正常工作。当它不应该返回时,它总是返回 True。我尝试在互联网上查找,但没有任何效果对我有用。我认为碰撞矩形不知何故没有使用两个精灵的实际坐标。谁能帮忙解决这个问题吗?
import pygame
import pygame.sprite
import sys
gameDisplay = pygame.display.set_mode((800,600))
pygame.display.set_caption("test_collision")
clock = pygame.time.Clock()
crashed = False
class Ball(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("ball.png")
self.rect = self.image.get_rect()
self.x = 280
self.y = 475
self.col = False
def update(self):
gameDisplay.blit(self.image, (self.x,self.y))
self.rect = self.image.get_rect()
def test_collisions(self,sprite):
self.col = pygame.sprite.collide_rect(self,sprite)
class Obstacle(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.x = 1000
self.y = 483
self.image = pygame.image.load("obstacle.png")
self.time = pygame.time.get_ticks()
self.rect = self.image.get_rect()
def change_x(self):
self.time = pygame.time.get_ticks()
self.x = -(self.time/5) + 800
def update(self):
self.rect = self.image.get_rect()
gameDisplay.blit(self.image,(self.x,self.y))
obstacle = Obstacle()
ball = Ball()
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
gameDisplay.fill((255,255,255))
ball.update()
obstacle.change_x()
obstacle.update()
ball.test_collisions(obstacle)
if ball.col:
print("colided")
pygame.display.flip()
clock.tick(1000)
pygame.quit()
sys.exit()
PS这是我的第一篇文章:)
My collide_rect function isn't working properly. It always returns True, when it's not suppose to. I have tried looking on the internet but nothing is working for me. I think the collide rect somehow did not use the actual coordinates for the two sprites. Can anyone help with this?
import pygame
import pygame.sprite
import sys
gameDisplay = pygame.display.set_mode((800,600))
pygame.display.set_caption("test_collision")
clock = pygame.time.Clock()
crashed = False
class Ball(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("ball.png")
self.rect = self.image.get_rect()
self.x = 280
self.y = 475
self.col = False
def update(self):
gameDisplay.blit(self.image, (self.x,self.y))
self.rect = self.image.get_rect()
def test_collisions(self,sprite):
self.col = pygame.sprite.collide_rect(self,sprite)
class Obstacle(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.x = 1000
self.y = 483
self.image = pygame.image.load("obstacle.png")
self.time = pygame.time.get_ticks()
self.rect = self.image.get_rect()
def change_x(self):
self.time = pygame.time.get_ticks()
self.x = -(self.time/5) + 800
def update(self):
self.rect = self.image.get_rect()
gameDisplay.blit(self.image,(self.x,self.y))
obstacle = Obstacle()
ball = Ball()
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
gameDisplay.fill((255,255,255))
ball.update()
obstacle.change_x()
obstacle.update()
ball.test_collisions(obstacle)
if ball.col:
print("colided")
pygame.display.flip()
clock.tick(1000)
pygame.quit()
sys.exit()
P.S This is my first post :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

pygame.Surface.get_rect.get_rect()
返回一个与 Surface 对象大小相同的矩形,但它返回的矩形始终从 (0, 0) 开始,因为 Surface 对象具有不位置。
函数放置在显示器上的某个位置。Surface 通过 blit
您必须通过关键字参数来设置矩形的位置,例如:
或对虚拟属性的赋值(请参阅
pygame.Rect
),eg:完全没有必要添加一些额外的属性
self.x
和self .y
。请改用矩形的位置。例如:进一步注意,如果您使用
Ball.update()
方法,则可以分别删除Obstacle.update()
方法(您可以删除它们) a href="https://www.pygame.org/docs/ref/sprite.html#pygame.sprite.Group" rel="nofollow noreferrer">pygame.sprite.Group
并调用.draw()
,它使用所包含精灵的.image
和.rect
属性来绘制它们。例如:pygame.Surface.get_rect.get_rect()
returns a rectangle with the size of the Surface object, but it returns a rectangle that always starts at (0, 0) since a Surface object has no position.The Surface is placed at a position on the display with the
blit
function.You've to set the location of the rectangle, either by a keyword argument, e.g:
or an assignment to a virtual attribute (see
pygame.Rect
), e.g:It is absolutely unnecessary to add some extra attributes
self.x
andself.y
. Use the location of the rectangle instead. e.g:Further note, that you can get rid of the methods
Ball.update()
respectivelyObstacle.update()
(you can delete them), if you use apygame.sprite.Group
and call.draw()
, which uses the.image
and.rect
properties of the contained sprites, to draw them. e.g.: