Python Forum
Boomerang implementing logic
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Boomerang implementing logic
#11
What about this?
def update(self):
    direction = +1
    if self.game.player.weapon == 'boomerang':
        now = pg.time.get_ticks()
        if now - self.last >= self.cooldown:
            direction = -1
    self.pos += self.vel * self.game.dt * direction
    self.rect.center = self.pos
Reply
#12
(Nov-01-2019, 03:18 PM)nilamo Wrote: What about this?
def update(self):
    direction = +1
    if self.game.player.weapon == 'boomerang':
        now = pg.time.get_ticks()
        if now - self.last >= self.cooldown:
            direction = -1
    self.pos += self.vel * self.game.dt * direction
    self.rect.center = self.pos

Sorry but what is direction supposed to be?
Reply
#13
(Nov-02-2019, 04:22 PM)Piethon Wrote: Sorry but what is direction supposed to be?
If direction is "+1", then the boomerang's pos will be multiplied by a positive number, positive movement, if it has been a certain amount of ticks past when the boomerang was first thrown, then direction is negative and the pos will become negative when everything is being multiplied.
Reply
#14
I now have got this and it isn't working:

def update(self):
        if self.game.player.weapon == 'boomerang':
            self.direction = +1
            now = pg.time.get_ticks()
            if self.direction == +1:
                self.pos += self.vel * self.game.dt
            if self.direction == -1:
                self.pos -= self.vel * self.game.dt
            if now - self.last >= self.cooldown:
                self.direction = -1
                #self.pos -= self.vel * self.game.dt
            self.pos += self.vel * self.game.dt
            self.rect.center = self.pos
Didn't I understood that self.direction thing or what is the problem with the code?
Reply
#15
You don't need the first two if statements and their code. The pos is changed at the very end.
Reply
#16
(Nov-03-2019, 09:32 PM)SheeppOSU Wrote: You don't need the first two if statements and their code.

But if I don't have these, pyjama doesn't know what self.direction is.

class Boomerang(pg.sprite.Sprite):
    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
        # spread = uniform(-BLOWPIPE_SPREAD, BLOWPIPE_SPREAD)
        self.vel = dir * WEAPONS[game.player.weapon]['speed']
        self.spawn_time = pg.time.get_ticks()
        self.rot = 360
        self.last = pg.time.get_ticks()
        self.cooldown = 300
        self.direction = +1

    def update(self):
        if self.game.player.weapon == 'boomerang':
            now = pg.time.get_ticks()
            if now - self.last >= self.cooldown:
                self.direction = -1
                #self.pos -= self.vel * self.game.dt
            self.pos += self.vel * self.game.dt
            self.rect.center = self.pos
That is what it looks like now
Reply
#17
Are there any problems in using the boomerang. Also, there's a question that's been itching at me. What is pos? Whenever I make a game, I make it representing an x and y value. But the way pos is being interacted with makes it look like it's not made that sort of way. Could you give me an example of what it would look like printed?
Reply
#18
(Nov-05-2019, 08:58 PM)SheeppOSU Wrote: Are there any problems in using the boomerang. Also, there's a question that's been itching at me. What is pos? Whenever I make a game, I make it representing an x and y value. But the way pos is being interacted with makes it look like it's not made that sort of way. Could you give me an example of what it would look like printed?

I'm not sure if you mean this:

WEAPONS['boomerang'] = {'img': 'boomerang.png',
                       'speed': 340,
                       'lifetime': 550,
                       'rate': 900,
                       'kickback': 0,
                       'spread': 3,
                       'damage': 8,
                       'size': 'boomerang',
                       'count': 1}
This is in the player class, that the player is able to shoot weapons:

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))
The position is a vector. vec is defined as this: vec = pg.math.Vector2

class Boomerang(pg.sprite.Sprite):
    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
        # spread = uniform(-BLOWPIPE_SPREAD, BLOWPIPE_SPREAD)
        self.vel = dir * WEAPONS[game.player.weapon]['speed']
        self.spawn_time = pg.time.get_ticks()
        self.rot = 360
        self.last = pg.time.get_ticks()
        self.cooldown = 300
        self.direction = +1

    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()
        if self.game.player.weapon == 'boomerang':
            now = pg.time.get_ticks()
            if now - self.last >= self.cooldown:
                self.direction = -1
                #self.pos -= self.vel * self.game.dt
            self.pos += self.vel * self.game.dt
            self.rect.center = self.pos
And yes, I can use the boomerang. It flies forward but is not coming back to the player.
Reply
#19
I don't know much about how vectors work, but from the looks of it, I think I know what's wrong. Change line 30 in the code above to self.pos += self.vel * self.game.dt * self.direction
Reply
#20
(Nov-06-2019, 11:22 PM)SheeppOSU Wrote: I don't know much about how vectors work, but from the looks of it, I think I know what's wrong. Change line 30 in the code above to self.pos += self.vel * self.game.dt * self.direction

I tried it out, but it didn't work. The boomerang is just flying forward and then stopping there and then it disappears.
Hm...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  A question about implementing the state engine code marienbad 4 3,265 Oct-20-2018, 02:08 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020