Oct-28-2019, 07:09 PM
Hello everybody.
I am making a game with pygame and till know there are different weapons such as a shurikan and a blowpipe.
But now I wanted to implement a boomerang, and I don't know what to do, that it comes back to the player, after a certain amount of time.
So in a settings file, I am defining my weapons (I'm posting the code of the shurikan, because I haven't implemented a boomerang yet. I first want to know how to make it coming back to the player):
Then we have the shoot section:
Hope it is not so complicated for you to understand.
Thanks for helping me.
Piethon
I am making a game with pygame and till know there are different weapons such as a shurikan and a blowpipe.
But now I wanted to implement a boomerang, and I don't know what to do, that it comes back to the player, after a certain amount of time.
So in a settings file, I am defining my weapons (I'm posting the code of the shurikan, because I haven't implemented a boomerang yet. I first want to know how to make it coming back to the player):
WEAPONS['shurikan'] = {'speed': 575,
'lifetime': 800,
'rate': 400,
'kickback': 0,
'spread': 3,
'damage': 8,
'size': 'shurikan',
'count': 1} Then we have the shoot section:
class Player(pg.sprite.Sprite):
def __init__(self, game, x, y):
# I've left out the things that are not important for this question
self.weapon = 'shurikan'
self.shurikan = True
self.shoot_ammo = True
# Then there is key press stuff and now the shoot section
def shoot(self):
if self.shoot_ammo == True:
now = pg.time.get_ticks()
if now - self.last_shot > WEAPONS[self.weapon]['rate']:
self.last_shot = now
dir = vec(1, 0).rotate(-self.rot)
pos = self.pos + BARREL_OFFSET.rotate(-self.rot)
self.vel = vec(-WEAPONS[self.weapon]['rate'], 0).rotate(-self.rot)
for i in range(WEAPONS[self.weapon]['count']):
spread = uniform(-WEAPONS[self.weapon]['spread'], WEAPONS[self.weapon]['spread'])
Blowpipe(self.game, pos, dir.rotate(spread))Then there is also a weapon class.class Blowpipe(pg.sprite.Sprite): # I haven't renamed the class yet but it is for all kind of weapons
def __init__(self, game, pos, dir):
self.groups = game.all_sprites, game.blowpipes
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.image = game.blowpipe_images[WEAPONS[game.player.weapon]['size']]
self.rect = self.image.get_rect()
self.pos = vec(pos)
self.rect.center = pos
self.vel = dir * WEAPONS[game.player.weapon]['speed']
self.spawn_time = pg.time.get_ticks()
self.rot = 360
def update(self):
self.pos += self.vel * self.game.dt
self.rect.center = self.pos
if pg.sprite.spritecollideany(self, self.game.walls):
self.kill()
if pg.time.get_ticks() - self.spawn_time > WEAPONS[self.game.player.weapon]['lifetime']:
self.kill()So I just posted some code here, because I want to know, how to make the boomerang coming back to the player and how to implement that in my code. Hope it is not so complicated for you to understand.
Thanks for helping me.
Piethon
