我正在尝试使用名为“ 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()