【问题标题】:How do I test for a collision in Pygame?如何在 Pygame 中测试碰撞?
【发布时间】:2020-03-31 13:03:01
【问题描述】:

我正在努力弄清楚碰撞在 pygame 中是如何工作的。我知道这与 pygame.rect.colliderect 有关,而且我可能非常接近,但我会感谢比我了解更多的人看看! :-)

这是我编写的一个简单程序,用于将一个小的绿色方块引导到一个较大的红色方块上,我正在尝试在两者相遇时实现碰撞检测,但在这个阶段一切都有效碰撞除外。

提前致谢!!

pygame.init()

import random #import random

size = (700,500) # set up the screen
screen = pygame.display.set_mode((size))

BLACK = (0,0,0) #define colours
WHITE = (255,255,255)
GREEN = (0,255,0)
RED =   (255, 0, 0)

class player(): #assign a player class
    def __init__(self):
        self.xpos = 450
        self.ypos = 250
        self.rect = pygame.Rect(self.xpos,self.ypos,5,5)
        self.xvel = 0
        self.yvel = 0
        self.colour = GREEN

    def update(self): #define a function to update the payer
        #self.xpos +=self.xvel  Ignore this bit. I implemented velocity, but it quickly flew off the screen
        #self.ypos +=self.yvel
        if player.rect.colliderect(obstacle.rect):    #<--------- this is the bit I think might be wrong?
            print("collision!")

    def draw(self): #define a function to draw the player
        pygame.draw.rect(screen, self.colour,[self.xpos,self.ypos,5,5])

class obstacle(): #define an obstacle class
    def __init__ (self):
        self.xpos = random.uniform(0,700)
        self.ypos = random.uniform(0,500)
        self.rect = pygame.Rect(self.xpos,self.ypos,20,20)
        self.colour = RED

    def draw(self): #define a function to draw the obstacle
        pygame.draw.rect(screen, self.colour,[self.xpos,self.ypos, 20,20])

player = player() #run an instance of the player class
obstacle = obstacle() #run an instance of the obstacle class
clock = pygame.time.Clock()


while True: #game loop
    for event in pygame.event.get(): #quit 
        if event.type == pygame.QUIT:
           pygame.display.quit()

#-----Game logic           
    keys = pygame.key.get_pressed() #check for key presses and do whatever
    if keys[pygame.K_LEFT]:
        player.xpos -= 1
    if keys[pygame.K_RIGHT]:
        player.xpos += 1
    if keys[pygame.K_UP]:
        player.ypos -= 1
    if keys[pygame.K_DOWN]:
        player.ypos += 1


    player.update() #Update the player - obstacle shouldn't need updating

#-----Drawing code

    screen.fill(BLACK) #draw screen black
    obstacle.draw() #draw the obstacle from the function
    player.draw() #draw the player from the function
    pygame.display.flip() #update

    clock.tick(60)'''




【问题讨论】:

  • colliderect 可以帮助您检测 碰撞(因为它返回一个布尔值),而且您毕竟是在询问“碰撞检测”。您是否还需要以任何方式解决冲突?还是您只需要知道 2 个矩形何时重叠?

标签: python pygame collision-detection


【解决方案1】:

问题是您没有更新玩家矩形的位置,colliderect 在检测到碰撞时会查看该位置。您在更改 xposypox 时正在绘制矩形,但矩形 rect.xrect.y 的坐标并未相应更新。我换了台词

    def draw(self): #define a function to draw the player
        pygame.draw.rect(screen, self.colour,[self.xpos,self.ypos,5,5])

    def draw(self): #define a function to draw the player
        pygame.draw.rect(screen, self.colour,[self.rect.x,self.rect.y,5,5])

    if keys[pygame.K_LEFT]:
        player.xpos -= 1
    if keys[pygame.K_RIGHT]:
        player.xpos += 1
    if keys[pygame.K_UP]:
        player.ypos -= 1
    if keys[pygame.K_DOWN]:
        player.ypos += 1

    if keys[pygame.K_LEFT]:
        player.rect.x -= 1
    if keys[pygame.K_RIGHT]:
        player.rect.x += 1
    if keys[pygame.K_UP]:
        player.rect.y -= 1
    if keys[pygame.K_DOWN]:
        player.rect.y += 1

以便更新玩家的矩形坐标。

【讨论】:

  • 太棒了!完全符合我的目标。所以更新 rect.x 会改变矩形的位置,而在我只是改变矩形的绘制位置之前??
  • @CodingGuy75 我不知何故错过了这条评论,但是在你只改变绘制矩形的位置之前是的。
【解决方案2】:

在这些情况下,总是打印出不工作的内容,即 player.rect 和 barrier.rect。你会看到你没有更新 player.rect,它总是

<rect(150, 150, 5, 5)>

由于它不与总是有的障碍物矩形重叠

<rect(34, 204, 20, 20)>

没有检测到碰撞。

【讨论】:

  • 有道理!感谢您提供故障排除工具。
猜你喜欢
  • 1970-01-01
  • 2015-06-20
  • 2019-10-10
相关资源
最近更新 更多