如何让敌人跟随pygame中的玩家? [英] How to make an enemy follow the player in pygame?

查看:293
本文介绍了如何让敌人跟随pygame中的玩家?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我参与了游戏.它运行良好,但我想在游戏中添加敌人.到目前为止,我在pygame精灵中添加了敌人的图像.但是,如何使敌人跟随玩家呢?我尝试这样做,但这只是使敌人的形象直接指向玩家:

I made part of a game. It runs well but I would like to add enemies in my game. So far I add the image of the enemies in pygame sprites. But how do I make the enemies follow the player? I tried do this but it just made the image of the enemy direct to the player:

def moveEnemy(self):
    enemies.rect.x = player.rect.x
    enemies.rect.y = player.rect.y
    all_sprites_list.add(enemies)
    enemies_list.add(enemies)

我认为这会使敌人的形象跟随玩家.相反,它只是重叠了玩家的图像.我读了很多pygame sprite例子,但是这些例子说用'enemies.rect.x = -5'或类似的东西替换'enemies.rect.x = player.rect.x'.我也尝试过此操作,但是它只是将图像向上移动,而不是跟随播放器.我必须公式化吗?如果是这样,我不知道该怎么办.我该如何使敌人移动,又如何使其跟随玩家?有人可以帮我解决这个问题吗?任何帮助,将不胜感激. Windows 7,Python 2.6,Pygame 2.6,精灵.

I thought this would make the image of the enemy follow the player. Instead it just overlaped the player's image. I read though many pygame sprites examples but the examples saids to replace the 'enemies.rect.x = player.rect.x' with 'enemies.rect.x = -5' or something around that. I also tried this but it just move the image up instead of following the player. Do I have to formulate an equation? If so I do not know how to. How do I make the enemy move but as well make it follow the player? Can someone help me solve this problem? Any help would be appreciated. Windows 7, Python 2.6, Pygame 2.6, sprites.

推荐答案

您需要通过更改敌人的位置来缩短敌人与玩家之间的距离.这可以通过找到它们位置之间的差异,然后使用该向量来计算归一化来完成. (单位长度)方向向量.这样,通过将敌人的速度乘以方向矢量来改变敌人的位置.

You need to reduce the distance between the enemy and the player by changing the enemy's position. This can be done by finding the difference between their positions and then using that vector to compute a normalized (unit length) direction vector. With that, change the enemy's position by adding its speed times the direction vector to it.

一种实现方法是在Enemy类中添加如下所示的方法.可以使用内置的math模块或pygame.math模块进行数学运算.后者还支持2D Vector2类,因此它可能是实际使用的更好的类.

One way of doing this would be by adding a method like the following to your Enemy class. The mathmatical operations can be done using the built-in math module or the pygame.math module. The latter also has support for a 2D Vector2 class, so it would probably be the better one to actually use.

import math
import pygame
from pygame.locals import *

class Enemy(object):
    ...
    def move_towards_player(self, player):
        # Find direction vector (dx, dy) between enemy and player.
        dx, dy = player.rect.x - self.rect.x, player.rect.y - self.rect.y
        dist = math.hypot(dx, dy)
        dx, dy = dx / dist, dy / dist  # Normalize.
        # Move along this normalized vector towards the player at current speed.
        self.rect.x += dx * self.speed
        self.rect.y += dy * self.speed

    # Same thing using only pygame utilities
    def move_towards_player2(self, player):
        # Find direction vector (dx, dy) between enemy and player.
        dirvect = pygame.math.Vector2(player.rect.x - self.rect.x,
                                      player.rect.y - self.rect.y)
        dirvect.normalize()
        # Move along this normalized vector towards the player at current speed.
        dirvect.scale_to_length(self.speed)
        self.rect.move_ip(dirvect)

您将需要添加检查以确定敌方物体是否会过冲,并在移动该距离的过程中击中玩家,并做出相应的反应.提示:只要要移动的量(速度矢量的长度,即对象的速度)大于或等于它们之间的距离,就会发生碰撞.

You will need to add checks to determine whether the enemy object will overshoot and thereby hit the player along the way if it moves this distance, and react accordingly. Hint: A collision will occur whenever the amount to be moved—the length of the speed vector, i.e. the object's speed—is greater or equal to the distance between them.

这篇关于如何让敌人跟随pygame中的玩家?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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