pygame里面物体闪烁运动_Pygame从A点到B点的子弹运动

我正在尝试使用名为“ Pygame”(v1.9.2)的python库制作游戏,并且已经为玩家制作了角色.这个字符应该“射击”“子弹”或“咒语”到mouse_pos的地步,而我需要帮助的是,“子弹”具有分配给1 self.speed = 1的恒定速度,如果我尝试增加当子弹到达mouse_pos时,它的速度将导致闪烁效果,因为self.pos会高于或低于mouse_pos.

我如何才能使此子弹快速前进&像子弹一样平滑,并且仍然到达设置mouse_pos的确切点?

self.speed = 1的示例

相关代码在update()函数内部

Sprites.py(法术/子弹类)

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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值