Partage
  • Partager sur Facebook
  • Partager sur Twitter

saut réaliste avec Pygame

    29 juin 2020 à 19:13:11

    Bonjour, j'essaye de créer un jeu en 2d qui se joue en 1v1 mais j'ai des difficultés avec le saut. 

    1) Déjà je n'arrive pas à augmenter l'amplitude de mon saut.

    2) J'aimerais que mon saut soit constant : c'est à dire que mon joueur à la fin de son saut atterrisse au même endroit de départ.

    3) Enfin je veux qu'il soit possible de se déplacer tout en effectuant un saut.

    Voici ma class Player:

    import pygame
    from projectile import Lance, Laser
    
    
    class Player(pygame.sprite.Sprite):
    
        def __init__(self, game):
            super().__init__()
            self.game = game
            self.health = 100
            self.max_health = 100
            self.attack = 10
            self.velocity = 6
            self.all_projectile = pygame.sprite.Group()
            self.mass = 0.5
            self.is_jumping = False
    
    
        def damage(self, amount):
            if self.health > 0:
                self.health -= amount
    
    
    
    class Warrior(Player):
    
        def __init__(self, game):
            super().__init__(game)
            self.all_projectile = pygame.sprite.Group()
            self.image = pygame.image.load('game/warrior.png')
            self.image = pygame.transform.scale(self.image, (100, 100))
            self.rect = self.image.get_rect()
            self.rect.x = 15
            self.rect.y = 355
            self.isJump = False
            self.jumpCount = 10
    
        def move_right(self):
            if not pygame.sprite.collide_rect(self, self.game.shooter):
                self.rect.x += self.velocity
    
        def move_left(self):
            self.rect.x -= self.velocity
    
        def launch_lance(self, game):
            self.all_projectile.add(Lance(self, game))
    
        def update_health_bar(self, surface):
    
            pygame.draw.rect(surface, (60, 63, 60), [self.rect.x-10, self.rect.y - 10, self.max_health, 5])
            pygame.draw.rect(surface,(11, 210, 46),  [self.rect.x-10, self.rect.y-10, self.health, 5])
    
        def jump(self):
            if self.rect.y <= 362 :
                self.is_jumping = True
                print(self.is_jumping)
    
        def update_jump(self):
            if self.is_jumping:
                self.force = (0.5 * self.mass * (self.velocity * self.velocity))
    
                self.rect.y -= self.force
                self.velocity -= 0.5
    
                if self.velocity <= 0:
                    self.mass -= 0.5
    
                if self.velocity == -4:
                    self.is_jumping = False
    
                    self.velocity = 6
                    self.mass = 0.5
    
            pygame.time.delay(10)
    
    
    class Shooter(Player):
    
        def __init__(self, game):
            super().__init__(game)
            self.all_projectile = pygame.sprite.Group()
            self.image = pygame.image.load('game/shooter.png')
            self.image = pygame.transform.scale(self.image, (100, 100))
            self.rect = self.image.get_rect()
            self.rect.x = 485
            self.rect.y = 362
    
        def launch_laser(self, game):
            self.all_projectile.add(Laser(self, game))
    
        def update_health_bar(self, surface):
    
            pygame.draw.rect(surface, (60, 63, 60), [self.rect.x+15, self.rect.y - 17, self.max_health, 5])
            pygame.draw.rect(surface,(11, 210, 46),  [self.rect.x+15, self.rect.y-17, self.health, 5])
    
    
    
        def move_right(self):
            self.rect.x += self.velocity
    
        def move_left(self):
            if not pygame.sprite.collide_rect(self, self.game.warrior):
                self.rect.x -= self.velocity
    
    
        def jump(self):
            if self.rect.y <= 362 :
                self.is_jumping = True
                print(self.is_jumping)
    
        def update_jump(self):
            if self.is_jumping:
                self.force = (0.5 * self.mass*(self.velocity*self.velocity))
    
                self.rect.y -= self.force
                self.velocity -= 0.5
    
                if self.velocity <= 0:
                    self.mass -= 0.5
    
                if self.velocity == -4:
                    self.is_jumping = False
    
                    self.velocity = 6
                    self.mass = 0.5
    
            pygame.time.delay(10)

    Une partie du code principal:

    while running:
    
        screen.blit(background,(0,0))
    
        screen.blit(game.warrior.image, game.warrior.rect)
        screen.blit(game.shooter.image, game.shooter.rect)
    
        game.warrior.update_health_bar(screen)
        game.shooter.update_health_bar(screen)
        game.shooter.update_jump()
    
        for projectile in game.warrior.all_projectile:
            projectile.move()
    
        for projectile in game.shooter.all_projectile:
            projectile.move()
    
        game.warrior.all_projectile.draw(screen)
        game.shooter.all_projectile.draw(screen)
    
        if game.pressed.get(pygame.K_RIGHT) and game.shooter.rect.x + game.shooter.rect.width < 585:
            game.shooter.move_right()
        elif game.pressed.get(pygame.K_LEFT) and game.shooter.rect.x > 0:
            game.shooter.move_left()
    
        if game.pressed.get(pygame.K_UP) :
            game.shooter.jump()
            print(game.shooter.rect.y)
    
        if game.pressed.get(pygame.K_e) :
            game.warrior.jump()
    
        if game.pressed.get(pygame.K_f) and game.warrior.rect.x + game.warrior.rect.width < screen.get_width():
            game.warrior.move_right()
        elif game.pressed.get(pygame.K_s) and game.warrior.rect.x > 0:
            game.warrior.move_left()

    Merci d'avance pour vos réponses !:)


    -
    Edité par AbcAbc6 29 juin 2020 à 19:22:32

    • Partager sur Facebook
    • Partager sur Twitter
      29 juin 2020 à 19:23:41

      Bonjour, j'ai modifié le titre de votre sujet, en effet les titres avec 'problème' ne sont pas tolérés.

      • Partager sur Facebook
      • Partager sur Twitter

      saut réaliste avec Pygame

      × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
      × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
      • Editeur
      • Markdown