pygame的Python矩形碰撞处理 [英] Python rectangle collision handling with pygame

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

问题描述

过去几天,我一直在对该主题进行广泛的研究,但似乎找不到确切的答案。

I've been doing extensive research on this topic for the past few days and I can't seem to find an answer for my exact problem.

所以,我有一个简单的游戏设置,其中我的玩家为0,0,宽度为10x10

So, I have a simple game set up where I have the player at 0, 0 with a width of 10x10

    player= pygame.Rect(0, 0, 10, 10)

除此之外,玩家的速度为x :0,y:10,这会让他跌倒(y是正的,因为屏幕的原点在左上角。)

and aside from that, the player has a velocity of x: 0, y: 10, which will make him fall (y is positive because the origin of the screen is at the top left.)

0,100,如下所示:

and I have a tile at 0, 100, as shown:

    dirt= pygame.Rect(0, 100, 10, 10)

所以,如何处理碰撞,我已经知道我可以使用Rect.colliderect进行检测(Rect)。

so, how can I handle collision, I already know I can detect it with Rect.colliderect(Rect).

我尝试了几种方法,但是遇到了一些问题:

I've tried a few ways, but encountered some problems:

我可以t当玩家击中某物体时将其速度降低为0,然后将其移回
直到他只是触摸物体,因为这仍然会导致问题走路的时间,当他走路时,我在x上施加+10的速度,但是不幸的是,游戏仍然处理着他摔倒和碰撞 并向侧面移动的情况,所以这只会将他移回开始的位置

I can't cut the player's velocity to 0 when he hits something and then move him back until he's just touching the object because that still causes the problem of walking, when he walks, I apply +10 velocity on x, but unfortunately, the game still processes that he is falling and colliding and moving sideways, so it just moves him back to where he started.

我是一个初学者,所以一个简单的答案将不胜感激,并且我喜欢不必使用 any 如果不需要,可以使用pygame以外的第三方模块。

I'm a beginner, so a simple answer would be appreciated, and I would like to not have to use any third party modules other that pygame if I didn't have to.

更新:

以下是我尝试过的一些粗略测试代码:

Here is some of the rough test code I have tried:

    def sim(obj, time, world):
        time= time / 1000
        obj.physProp['vel']= (obj.physProp['vel'][0] + (accel[0] * time), obj.physProp['vel'][1] + (accel[1] * time))
        if obj.physProp['vel'][1] > terminalY:
            obj.physProp['vel']= (obj.physProp['vel'][0], terminalY)
        obj.pos= (obj.pos[0] + (obj.physProp['vel'][0] * time) + ((accel[0] / 2) * (time ** 2)), obj.pos[1] + (obj.physProp['vel'][1] * time) + ((accel[1] / 2) * (time ** 2)))

        for ID in world:
            if obj.getRect().colliderect(world[ID].getRect()) == True:
                pass

        return (obj.pos, obj.physProp['vel'])


推荐答案

Pygame API邀请您以面向对象的方式编写所有游戏主题-这样您的堕落角色将拥有所有的方法和属性,以正确地应对场景中的事物-例如击中某些事物。

The Pygame API invites you to write all your game subjects in an Object oriented way - so that your falling character will have all the "methods" and "attributes" to properly respond to things on the scenario - like hitting something.

因此,如果您的角色被定义为简单的事物,例如:

So, if your character is defined for something as simple as:

class Char(object):
    # these start as class attributes, 
    # but whenever they are assigned to with a "self.var = bla" in
    # a method, an instance attribute starts existing
    x, y = 0,0
    vx, vy = 0,0

    def update(self):
        self.x += self.vx
        self.y += self.vy

而您的外部代码在检测到冲突时可以执行以下操作:

And your external code, upon detecting a collision, could do just this:

def mainloop():
   while True:
       ...
       obj.update()
       if obj.getRect().colliderect(world[ID].getRect()): # don't do "== True" in `if's - it is just silly
             # take character back to the precious position
             obj.x -= obj.vx
             obj.y -= obj.vy
             # zero out velocities to make it stop:
             obj.vx = obj.vy = 0

依此类推-您很快就会将游戏中的事物视为对象,因为它们在编程中使用使代码自然流动-一旦您了解了它的工作方式,请查看Pygame的sprite模块-w hich允许您自动执行大量检查和更新,而不必为每个检查显式编写 c循环

And so on - you will soon perceive thinking of your game "things" as "objects" as they are used in programing make the code flows quite naturally - as soon as you get the way this works, look at Pygame's sprite module - which allows you to automate a lot of checks, and updates without having to explicitly write for loops for each check

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

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