【发布时间】:2017-10-12 13:56:34
【问题描述】:
我正在尝试使用名为“Pygame”(v1.9.2)的python 库制作游戏,并且我已经为玩家制作了一个角色。这个角色应该“射击”“子弹”或“咒语”到mouse_pos,我需要帮助的是“子弹”有一个恒定的速度分配给1self.speed = 1,如果我尝试增加当子弹到达mouse_pos时它会导致闪烁效果的速度,因为self.pos会高于或低于mouse_pos。
我怎样才能使这个子弹像子弹一样快速平稳地移动,并且仍然到达设置 mouse_pos 的确切点?
self.speed = 1 的示例
self.speed = 2 的示例
http://4.1m.yt/d_TmNNq.gif
相关代码在 update() 函数中
Sprites.py(Spell/Bullet 类)
class Spell(pygame.sprite.Sprite):
def __init__(self,game,x,y,spelltype,mouse_pos):
pygame.sprite.Sprite.__init__(self)
self.game = game
self.width = TILESIZE
self.height = TILESIZE
self.type = spelltype
self.effects = [
'effect_'+self.type+'_00',
'effect_'+self.type+'_01',
'effect_'+self.type+'_02'
]
self.image = pygame.image.load(path.join(IMG_DIR,"attack/attack_"+self.type+".png")).convert_alpha()
self.image = pygame.transform.scale(self.image,(self.width,self.height))
self.rect = self.image.get_rect()
self.rect.x = x+self.width
self.rect.y = y+self.height
self.speed = 1
self.mouse_pos = mouse_pos
self.idx = 0
def update(self):
if not self.rect.collidepoint(self.mouse_pos):
if self.mouse_pos[0] < self.rect.x:
self.rect.x -= self.speed
elif self.mouse_pos[0] > self.rect.x:
self.rect.x += self.speed
if self.mouse_pos[1] < self.rect.y:
self.rect.y -= self.speed
elif self.mouse_pos[1] > self.rect.y:
self.rect.y += self.speed
else:
self.image = pygame.image.load(path.join(IMG_DIR,"effects/"+self.effects[self.idx]+".png"))
self.idx += 1
if self.idx >= len(self.effects):
self.kill()
【问题讨论】:
-
要向特定方向射击子弹,您应该使用三角函数或向量。这是一个向您展示它如何与向量一起使用的答案:stackoverflow.com/a/42281315/6220679
-
如果你的子弹飞得太快以至于它穿过了目标,你可能想测试子弹当前点和最后一点之间的线是否与你的目标相交。
-
每次更新时我应该检查距离是奇数还是偶数?
-
@Lanting 好的,这有帮助。发布答案,以便我接受。
标签: python python-2.7 pygame equations