【问题标题】:How to check for collisions in pygame [duplicate]如何检查pygame中的碰撞[重复]
【发布时间】:2020-07-25 22:54:21
【问题描述】:

正如您在代码中看到的,有一个垃圾桶和一个可以移动的方块。我的目标是制作一个游戏,这样他们就必须让广场去垃圾桶。但我不知道怎么做。

代码:

    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 屏幕的顶部制作一个计时器,所以请帮忙处理碰撞和计时器!

【问题讨论】:

  • 您好,欢迎来到 Stack Overflow :-)。一个小提示:有时问太多问题会使回答任何一个问题变得更加困难。问一个问题可以使帖子保持重点。我推荐editing你的问题集中在碰撞上。一旦你得到这个工作,你可以为计时器打开另一个问题。
  • 好的,非常感谢。会做(:。你也可以回答我关于如何制作计时器的问题。因为我真的需要帮助

标签: python pygame collision-detection


【解决方案1】:

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

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

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 )  

所以现在垃圾和回收箱都有一个矩形。我们可以使用矩形成员functionRect.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)

【讨论】:

  • 非常感谢我一直在 YouTube 上观看视频!!!你帮了很多忙。好的,非常感谢你救了我的命。我做了一些改动,这样当球碰到垃圾桶时,我就退出了 pygame。就是这样
【解决方案2】:

你写道:

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

让我们改变那些Magic Numbers

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 布尔值,您可以决定接下来应该显示什么。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-20
    • 2019-10-10
    相关资源
    最近更新 更多