-1

So im making a small game where you have to jump from platform to platform and i wanted to make a power up that gives you an extra jump. However when I pick up my power up it doesn't give an extra jump. I tried a bunch of things. I messed with the variables a lot and went over my code 100 times and I can't find the problem. Keep in mind im still very inexperienced and this basically my first project. Everything else is working fine I just don't understand why im not getting the extra jump.

class Player(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.surf = pygame.Surface((40, 60))
        self.surf.fill((128, 255, 40))
        self.rect = self.surf.get_rect()

        self.pos = vec((200, 420))
        self.vel = vec(0, 0)
        self.acc = vec(0, 0)
        self.score = 0
        djenabled = False  #this is the variable im using for my double jump#
.
.
.
.
#from my understanding it should be that if the variable is true and im in the air i should be able to jump again#

    def djump(self):
        hits = pygame.sprite.spritecollide(self, platforms, False)
        if hits and djenabled == True:
            self.djumping = True
            self.vel.y = -15
.
.
.
.
#this is the code for the power up and when i pick it up my djenabled variable should be set to True#

class Djump_power(pygame.sprite.Sprite):
    def __init__(self, pos):
        super().__init__()

        self.image = pygame.image.load('pfeil.png')
        self.rect = self.image.get_rect()

        self.rect.topleft = pos

    def update(self):
        if self.rect.colliderect(P.rect):
            djenabled = True
            self.kill()

I even tried using something like this. Basically the same thing but with self.dj I also tried combining the 2 of those and nothing seems to have worked. I really don't see the problem here if anyone could help me I would appreciate it.

class Player(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.surf = pygame.Surface((40, 60))
        self.surf.fill((128, 255, 40))
        self.rect = self.surf.get_rect()

        self.pos = vec((200, 420))
        self.vel = vec(0, 0)
        self.acc = vec(0, 0)
        self.score = 0
        self.dj = 1 
.
.
.
.

    def djump(self):
        hits = pygame.sprite.spritecollide(self, platforms, False)
        if hits and self.dj == 0:
            self.djumping = True
            self.vel.y = -15
.
.
.
.

class Djump_power(pygame.sprite.Sprite):
    def __init__(self, pos):
        super().__init__()

        self.image = pygame.image.load('pfeil.png')
        self.rect = self.image.get_rect()

        self.rect.topleft = pos

    def update(self):
        if self.rect.colliderect(P.rect):
            self.dj = 0
            self.kill()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Bruc
  • 1
  • 1
  • `hits = pygame.sprite.spritecollide(self, platforms, False)` returns a list of collisions, if there are none (which I expect it to be if you are mid air) then it returns an empty list that evaluates as `False`. You then get to `if hits and djenabled == True:` which if you are midair, becomes `if False and djenabled == True:`, which just evaluates to `False` – Shorn Feb 23 '23 at 07:38

1 Answers1

0

When you get back your hits, you'll want to check that it is empty (saying that you're not hitting anything). So instead of hits and djenabled == True, you'll want to, more explicitly check your hits.

Check if hits is None rather than "if hits"

I also noticed that the boolean starts off as False. If nothing flips it to True, the double jump will not be possible. It also feels strange that it flips to True when you take the double jump. Maybe you have that conditional backwards too? Maybe you need something that reads more like: If hits is None and not doublejump

Shaun Ramsey
  • 562
  • 4
  • 14
  • I set it to false in the start because i tough of it in a way that it its false its not allowed to do a double jump. When i pick up the power up it should set it to True meaning it is then allowed to double jump but for some reason the code still doesn't work. I tried doing If hits is None and djallowed == True but that still didnt work. I don't know if its because of the hits or is it because my djallowed is not being set to True after i pick the power-up up. – Bruc Mar 16 '23 at 07:57