pygame与对象和矩形碰撞检测 [英] Pygame collision detect with object and rect

查看:192
本文介绍了pygame与对象和矩形碰撞检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是的,我想问这个程序的另一个问题:D

Yep, I'm asking another question about this program :D

无论如何,我目前正在创建一个在屏幕上创建两行并在它们之间留有空隙的程序可以滚动。从这里开始,我显然需要查看两个对象是否碰撞。因为我只有一个精灵和一个矩形,所以我认为为它们创建两个类是有点毫无意义的,并且过于刻板。但是,我只能找到与我显然不需要的课程相关的教程。因此,我的问题确实是:
是否可以测试标准图片和Pygame rect 之间的碰撞?如果不是,我该如何转换图像,矩形或同时转换两个精灵。 (最好不使用类。)

Anyway, I currently a program that creates two lines on the screen with a gap in between them that can scroll. From here, I obviously need to see if the two objects are colliding. Since I only have one sprite and one rectangle I thought it was slightly pointless and overkill to make two classes for them. However, I can only find tutorials relating to classes which I obviously don't need. So, my question really is: Is it possible to test for collision between a standard image and a Pygame rect? If it is not, how can I convert either the image, rectangle or both the sprites to do this. (All preferably without using classes.)

注意:图像和矩形的创建方式如下(如果有所不同)

Note: the image and rectangle are created in the following ways (if it makes a difference)

bird = pygame.image.load("bird.png").convert_alpha()
pipeTop = pygame.draw.rect(screen, (0,200,30), Rect((scrollx,0),(30,height)))
pipeBottom = pygame.draw.rect(screen, (0,200,30), Rect((scrollx,900),(30,-bheight)))


推荐答案

图像本身不会有一个位置。您无法测试rect与世界上未放置的物体之间的碰撞。我建议创建一个Bird类以及一个Pipe类,它们都将成为pygame.Sprite的子类。

An image by itself does not have a position. You cannot test collision between a rect and something that is not placed in the world. I would recommend to create a class Bird along with a class Pipe that will both subclass pygame.Sprite.

Pygame已经内置了碰撞检测功能。

Pygame already has collision detection built in.

一个简短示例

bird = Bird()
pipes = pygame.Group()
pipes.add(pipeTop)
pipes.add(pipeBottom)

while True:    
    if pygame.sprite.spritecollide(bird,pipes):
        print "Game Over"

编辑:

不要害怕上课,您迟早都要使用它们。
如果您真的不想使用精灵,可以使用鸟形体和管道,并调用 collide_rect 来检查它们是否重叠。

Don't be afraid of classes, you will have to use them anyways sooner or later. If you really don't want to use sprites, you can use the birds rect and the pipe and call collide_rect to check if they overlap.

EDIT2:

从pygame文档修改的示例Bird类

an example Bird class modified from pygame docs

class Bird(pygame.sprite.Sprite):
    def __init__(self):
       pygame.sprite.Sprite.__init__(self)

       self.image = pygame.image.load("bird.png").convert_alpha()

       # Fetch the rectangle object that has the dimensions of the image
       # Update the position of this object by setting the values of rect.x and rect.y
       self.rect = self.image.get_rect()

然后可以添加诸如move之类的方法,该方法将通过重力将鸟移下。

You could then add methods such as move, which will move the bird down with the force of gravity.

Pipe ,但是您可以创建一个空的Surface并为其填充颜色,而不是加载图像。

The same would apply for the Pipe but instead of loading an image, you can create an empty Surface, and fill it with a color.

image = pygame.Surface(width,height)
image.fill((0,200,30)

这篇关于pygame与对象和矩形碰撞检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆