如何在pygame中检查冲突



正如您在代码中看到的,有一个垃圾桶和一个我可以移动的正方形。我的目标是创造一场比赛,这样他们就必须把广场扔进垃圾桶。但我不知道怎么做。

代码:

import pygame
from pygame.locals import *
pygame.init
red = (255,0,0)
blue = (0,0,255)
clock = pygame.time.Clock()
x = 100
y = 100
screen = pygame.display.set_mode((500,500))
pygame.display.set_caption("recycling game!")
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
exit()
keyPressed = pygame.key.get_pressed()
screen.fill((0,0,0))
if keyPressed[pygame.K_UP]:
y-=3;
keyPressed = pygame.key.get_pressed()
if keyPressed[pygame.K_DOWN]:
y+=3;
keyPressed = pygame.key.get_pressed()
if keyPressed[pygame.K_LEFT]:
x-=3;
keyPressed = pygame.key.get_pressed()
if keyPressed[pygame.K_RIGHT]:
x+=3;
rect = pygame.draw.rect(screen,red,(x,y,50,50))
imagevariable = pygame.image.load("recycle.png")
imagevariable = pygame.transform.scale(imagevariable,(100,100))
screen.blit(imagevariable,(380,380))
pygame.display.update()
clock.tick(60) 

此外,我想在pygame屏幕的顶部制作一个计时器,所以请帮助处理碰撞和计时器!

我会回答,让您开始一些简单的碰撞。

PyGame中的碰撞主要是用pygame.Rect矩形来完成的。该代码定义了一个围绕图像边缘的矩形,当一个矩形与另一个矩形重叠时,意味着它们已经碰撞。

pygame Rect只是左上角的简单xy坐标,以及widthheight。您已经有了一个红色方块,要将其制作成pygame Rect,我们只需将x,y50,50:组合起来

# Create the moveable item 
x = 100
y = 100
rubbish_rect = pygame.Rect( x, y, 50, 50 )

回收钻头的图像还需要几个步骤。这是因为我们希望矩形的大小与图像的大小相同。您的代码加载图像,对其进行缩放,然后在380,380处绘制。pygame Surfaces的一个有用特性是,它们有一个.get_rect()成员函数,该函数将自动为您提供一个与图像大小相同的矩形。然后,我们可以将矩形移动到380,380,并将其用于图像绘制位置:

# Create the recycling-bin object
recycling_image = pygame.image.load("recycle.png")                                # Load the image 
recycling_image = pygame.transform.smoothscale( recycling_image, ( 100, 100 ) )   # Scale to size
recycling_rect  = recycling_image.get_rect()                                      # Make a Rect for it
recycling_rect.topleft = ( 380, 380 )  

因此,现在有一个Rect既用于垃圾项目,也用于回收箱。我们可以使用Rect成员函数Rect.colliderect( other_rect ),如果矩形Rectother_rect重叠,它将返回True

这使我们能够非常简单地检查进入回收的物品:

if ( rubbish_rect.colliderect( recycling_rect ) ):
print( "*rubbish has been recycled*" )

显然,即使垃圾撞到垃圾桶的侧面,这也是真的,所以你真的想要一个"垃圾桶";反弹";当项目没有在顶部输入时。

我希望这个答案能让你从物体碰撞开始。

参考代码:

import pygame
from pygame.locals import *
pygame.init()                   # <<-- Added '()'
# Create the recycling-bin object
recycling_image = pygame.image.load("recycle.png")                                # Load the image 
recycling_image = pygame.transform.smoothscale( recycling_image, ( 100, 100 ) )   # Scale to size
recycling_rect  = recycling_image.get_rect()                                      # Make a Rect for it
recycling_rect.topleft = ( 380, 380 )                                             # Position the Rect
# Create the moveable item 
x = 100
y = 100
rubbish_rect = pygame.Rect( x, y, 50, 50 )
red = (255,0,0)
blue = (0,0,255)
clock = pygame.time.Clock()
screen = pygame.display.set_mode((500,500))
pygame.display.set_caption("recycling game!")
while True:
# handle user input
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
exit()
keyPressed = pygame.key.get_pressed()
if keyPressed[pygame.K_UP]:
rubbish_rect.y -= 3;
keyPressed = pygame.key.get_pressed()
if keyPressed[pygame.K_DOWN]:
rubbish_rect.y += 3;
keyPressed = pygame.key.get_pressed()
if keyPressed[pygame.K_LEFT]:
rubbish_rect.x -= 3;
keyPressed = pygame.key.get_pressed()
if keyPressed[pygame.K_RIGHT]:
rubbish_rect.x += 3;
# Draw the screen
screen.fill((0,0,0))
pygame.draw.rect( screen, red, rubbish_rect )
#imagevariable = pygame.image.load("recycle.png")
#imagevariable = pygame.transform.scale(imagevariable,(100,100))
#screen.blit(imagevariable,(380,380))
screen.blit( recycling_image, recycling_rect )
# Did the rubbish enter the bin?
if ( rubbish_rect.colliderect( recycling_rect ) ):
print( "*rubbish has been recycled*" )
# move the rubbich back to the start
rubbish_rect.topleft = ( x, y )  # starting x,y
pygame.display.update()
clock.tick(60)

您写道:

screen.blit(imagevariable,(380,380))

让我们改变那些神奇的数字:

trash_x, trash_y = 380, 380
screen.blit(imagevariable, (trash_x, trash_y))
inc = 3  # the increment that player moves on each keypress

现在我们可以很好地评估玩家是否接近垃圾:

is_near = ((trash_x - inc < x < trash_x + inc)
and (trash_y - inc < y < trash_y + inc))

有了is_near布尔值,您可以决定接下来应该显示什么。

最新更新