python - 如何在pygame中让子弹瞄准玩家
问题描述
到目前为止,我游戏中的敌人只会直接向下射击。我希望能够瞄准玩家。这是我的敌人班的射击方法。
class Enemy(sprite.Sprite):
def shoot(self):
# Origin position, direction, speed
# Direction of (0,1) means straight down. 0 pixels along the x axis, and +1 pixel along the y axis
# Speed of (10,10) means that every frame, the object will move 10 px along the x and y axis.
self.bullets.shoot(self.rect.center, (0,1), (10,10))
self.bullets 是我的 BulletPool 类的一个实例。
class BulletPool(sprite.Group):
def shoot(self, pos, direction, speed):
# Selects a bullet from the pool
default = self.add_bullet() if self.unlimited else None
bullet = next(self.get_inactive_bullets().__iter__(), default)
if bullet is None:
return False
# Sets up bullet movement
bullet.rect.center = pos
bullet.start_moving(direction)
bullet.set_speed(speed)
# Adds bullet to a Group of active bullets
self.active_bullets.add(bullet)
这是我的子弹课。
class Bullet(sprite.Sprite):
def update(self):
self.move()
def set_speed(self, speed):
self.speed = Vector2(speed)
def start_moving(self, direction):
self.direction = Vector2(direction)
self.is_moving = True
def stop_moving(self):
self.is_moving = False
def move(self):
if self.is_moving:
x_pos = int(self.direction.x*self.speed.x)
y_pos = int(self.direction.y*self.speed.y)
self.rect = self.rect.move(x_pos,y_pos)
使用它,我只能使精灵向上 (0,-1)、向下 (0,1)、向左 (-1,0) 或向右 (1,0),以及组合 x 和轴来制作45 度角,(即 (1,1) 向下和向右)。我不知道如何倾斜某些东西以使其朝着除这些以外的特定方向移动。我应该改变移动对象的方式吗?我使用相同的方法来移动我的播放器,当它只是通过箭头键进行控制时,它可以完美地工作。
解决方案
推荐阅读
- rest - 将 HTTP 重定向到 HTTPS 会丢失数据?
- exception-handling - 与java连接时出现SQLException错误
- java - 从可为空的列表创建 Java 8 流
- url-rewriting - 无法清除子文件夹中的重写规则
- php - 如何使用 PDO 设置 GROUP_CONCAT_MAX_LEN?
- python - 使用 PCA 减少维度:AttributeError:“numpy.ndarray”对象没有属性“items”
- sql - Hive Rank 查询的问题
- haskell - 不在范围内:尝试创建单元测试时键入构造函数或类“Test.Framework.TestSuite”
- java - JGraphx如何从图形中导出SVG文件
- java - 如何在hibernate中执行sql函数?