如何在Pygame中测试碰撞?

2024-09-28 21:27:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在努力研究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)'''





Tags: therectselfifdeffunctionscreenpygame
2条回答

问题是您没有更新播放器矩形的位置,而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

这样玩家的矩形坐标就会被更新

在这些情况下,始终打印出不起作用的内容,即player.rect和barrier.rect。您将看到您没有更新player.rect,它总是

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

因为它不与barrier.rect重叠,后者总是

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

未检测到碰撞

相关问题 更多 >