如何使对象自行移动以避免与其他对象发生碰撞

时间:2019-07-06 05:24:38

标签: python-3.x pygame python-3.6

我正在制作关于躲避来自不同方向的物体的游戏。我要实现的目标是使该对象(由计算机控制的“玩家”类)能够像由用户控制的玩家那样躲避。

我尝试使用pygame提供的基本冲突处理(当然,请阅读一些有关其工作原理以及如何解决该问题的文档)。我所做的基本上是检查对象的右矩形位置是否与“玩家的”左矩形重叠,反之亦然,因此它会朝相反的方向移动以避免碰撞。

TITLE = "Dodging-IA Attempt";
WIDTH = 320; HEIGHT = 400;
FPS = 60;

BLACK = (0,0,0); WHITE = (255,255,255);
RED = (255,0,0); GREEN = (0,255,0); BLUE = (0,0,255);

import pygame as pg;
import random, math;

class Game():
    def __init__(self):
        self.screen = pg.display.set_mode((WIDTH,HEIGHT));
        self.clock = pg.time.Clock(); self.dt = FPS/1000;
        self.running = True;
        self.all_sprites = pg.sprite.Group();
        self.bullets = pg.sprite.Group();
        self.other_sprites = pg.sprite.Group();

        self.player = Player(); self.all_sprites.add(self.player);
        self.bullet_spawn = BulletSpawn(); self.other_sprites.add(self.bullet_spawn);


class Player(pg.sprite.Sprite):
    def __init__(self):
        pg.sprite.Sprite.__init__(self);
        self.image = pg.Surface((16,32)); self.image.fill(GREEN);
        self.rect = self.image.get_rect();
        self.rect.centerx = WIDTH//2;
        self.rect.y = HEIGHT*3//4;
        self.vx = self.vy = 300;

    def update(self,dt):
        pass;

class Bullet(pg.sprite.Sprite):
    def __init__(self,x):
        pg.sprite.Sprite.__init__(self);
        self.image = pg.Surface((16,16)); self.image.fill(RED);
        self.rect = self.image.get_rect();
        self.rect.centerx = x;
        self.rect.bottom = 0;
        self.vy = 70;

    def update(self,dt):
        self.rect.y += self.vy * dt;
        if self.rect.top > HEIGHT:
            self.kill();

class BulletSpawn(pg.sprite.Sprite):
    def __init__(self):
        pg.sprite.Sprite.__init__(self);
        self.image = pg.Surface((1,1)); self.rect = self.image.get_rect();
        self.new_bullet = pg.time.get_ticks();

    def update(self):
        now = pg.time.get_ticks();
        if now - self.new_bullet > 500:
            self.new_bullet = now;
            for i in range(5):
                bullet = Bullet(random.randint(8,WIDTH-8));
                game.bullets.add(bullet);
                game.all_sprites.add(bullet);

pg.init();
pg.display.set_caption(TITLE);

game = Game();

while game.running:

    game.dt = game.clock.tick(FPS)/1000;

    for event in pg.event.get():
        if event.type == pg.QUIT:
            game.running = False;

    hits = pg.sprite.spritecollide(game.player,game.bullets,False)
    for hit in hits:
        if hit.rect.right > game.player.rect.left:  #dodge moving to left
            game.player.rect.x += game.player.vx*game.dt
            print("DODGED TO LEFT")
        if hit.rect.left < game.player.rect.right:  #dodge moving to right
            game.player.rect.x -= game.player.vx*game.dt
            print("DODGED TO RIGHT")


    game.all_sprites.update(game.dt);
    game.other_sprites.update();

    game.screen.fill(BLUE);
    game.all_sprites.draw(game.screen)
    game.other_sprites.draw(game.screen)
    pg.display.flip();

pg.quit();

结果,“玩家”总是向一个方向移动,无论它是否在其路径中发生碰撞,而不是实际上躲避掉落的物体。我该怎么办?也许我必须重新制定碰撞的条件? 非常感谢您的宝贵时间。

2 个答案:

答案 0 :(得分:2)

无论在冲突时刻玩家和子弹的位置如何,bullet.rect.right始终大于player.rect.left,而bullet.rect.left始终小于player.rect.right。如果在纸上绘图,您将在视觉上看到它。在这里最好检查移动侧推动直肠中心。 这段代码:

        if hit.rect.right > game.player.rect.left:  #dodge moving to left
            game.player.rect.x += game.player.vx*game.dt
            print("DODGED TO LEFT")
        if hit.rect.left < game.player.rect.right:  #dodge moving to right
            game.player.rect.x -= game.player.vx*game.dt
            print("DODGED TO RIGHT")

您可以对此进行更改:

        if hit.rect.center > game.player.rect.center:
            game.player.rect.x -= game.player.vx*game.dt
        elif hit.rect.center < game.player.rect.center:
            game.player.rect.x += game.player.vx*game.dt

您可以对抽搐的传递进行一些优化,但是我认为错误的性质很明显。

答案 1 :(得分:1)

检查方块边缘是否在玩家范围内是一种方法。如果您将支票包括在内,即使只有边缘对齐,播放器也应闪避。

if hit.rect.right >= game.player.rect.left and hit.rect.left <= game.player.rect.left:
    game.player.rect.x += game.player.vx*game.dt
    print("DODGED TO LEFT")
elif hit.rect.left <= game.player.rect.right and hit.rect.right >= game.player.rect.right:
    game.player.rect.x -= game.player.vx*game.dt
    print("DODGED TO RIGHT")