I am trying to make a game with a python
library called 'Pygame
' (v1.9.2) and I have made a character for the player. This character is supposed to "shoot" "bullets" or "spells" to the point of mouse_pos
and what I need help with is that the "bullet" has a constant speed assigned to 1 self.speed = 1
, if I try to increase the speed it will cause a flickering effect when the bullet reaches mouse_pos because self.pos will either be higher or lower than mouse_pos.
How can I make this bullet go quickly & smooth like a bullet and still get to the exact point where mouse_pos is set?
Example with self.speed = 1
Example with self.speed = 2
http://4.1m.yt/d_TmNNq.gif
related code is inside update() function
Sprites.py (Spell/Bullet class)
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()