Forráskód Böngészése

Déplacements gauche-droite

Hvrnbi 2 hete
szülő
commit
70b642ebed
7 módosított fájl, 57 hozzáadás és 5 törlés
  1. BIN
      images/player-ld.png
  2. BIN
      images/player-lu.png
  3. BIN
      images/player-rd.png
  4. BIN
      images/player-ru.png
  5. 7 1
      main.py
  6. 46 3
      player.py
  7. 4 1
      variables.py

BIN
images/player-ld.png


BIN
images/player-lu.png


BIN
images/player-rd.png


BIN
images/player-ru.png


+ 7 - 1
main.py

@@ -39,6 +39,12 @@ def main():
 
                 elif event.key == pygame.K_s:
                     player.move("down")
+
+                elif event.key == pygame.K_q:
+                    player.move("left")
+
+                elif event.key == pygame.K_d:
+                    player.move("right")
             
             # Fermeture du jeu
             if event.type == QUIT:
@@ -48,8 +54,8 @@ def main():
         # À chaque tick on fait ce qu'il y a là
         display.fill(BACKGROUND_COLOR)
         world.draw()
-        player.draw()
         nest.draw(world.pos_list[0], 0)
+        player.draw()
 
         pygame.display.update()
         clock.tick(FPS)

+ 46 - 3
player.py

@@ -10,12 +10,13 @@ class Player:
         self.display = display
         self.x: int = x
         self.y: int = y
-        self.picture: str = "player"
+        self.picture =  PLAYER_RU
         self.world: World = world
+        self.next_move = "up"
 
     def draw(self):
         """Affiche le joueur"""
-        image = pygame.transform.scale(PLAYER, (self.world.tile_width, self.world.tile_height))
+        image = pygame.transform.scale(self.picture, (self.world.tile_width, self.world.tile_height))
         self.display.blit(image, (self.x - self.world.tile_width // 2, self.y - self.world.tile_height // 2))  #la valeur ici est arbitraire mais marche. elle est la pour centrer l'affichage du joueur sur une tuile, a changer dans le futur
     
     
@@ -24,8 +25,50 @@ class Player:
         if direction == "up":
             if self.y - self.world.gap - self.world.tile_height >= self.world.top_border:   # Il faut que ça soit supérieur parce que les coos descendent 
                 self.y -= self.world.gap + self.world.tile_height
+                self.next_move = "up"
+                if self.picture == PLAYER_LD:
+                    self.picture = PLAYER_LU
+                elif self.picture == PLAYER_RD:
+                    self.picture = PLAYER_RU
+
         elif direction == "down":
             if self.y + self.world.gap + self.world.tile_height <= self.world.bottom_border:    # Inversement ici
                 self.y += self.world.gap + self.world.tile_height
+                self.next_move = "down"
+                if self.picture == PLAYER_LU:
+                    self.picture = PLAYER_LD
+                elif self.picture == PLAYER_RU:
+                    self.picture = PLAYER_RD
+
+        elif direction == "left":
+            if self.x - self.world.gap - 3 * self.world.tile_width // 4 >= self.world.left_border:
+                if self.next_move == "up":
+                    if self.y - self.world.gap // 2 - self.world.tile_height // 2 >= self.world.top_border:
+                        self.x -= self.world.gap + 3 * self.world.tile_width // 4
+                        self.y -= self.world.gap // 2 + self.world.tile_height // 2
+                        self.next_move = "down"
+                        self.picture = PLAYER_LD
+
+                elif self.next_move == "down":
+                    if self.y + self.world.gap // 2 + self.world.tile_height // 2 <= self.world.bottom_border:
+                        self.x -= self.world.gap + 3 * self.world.tile_width // 4
+                        self.y += self.world.gap // 2 + self.world.tile_height // 2
+                        self.next_move = "up"
+                        self.picture = PLAYER_LU
+
+        elif direction == "right":
+            if self.x + self.world.gap + 3 * self.world.tile_width // 4 <= self.world.right_border:
+                if self.next_move == "up":
+                    if self.y - self.world.gap // 2 - self.world.tile_height // 2 >= self.world.top_border:
+                        self.x += self.world.gap + 3 * self.world.tile_width // 4
+                        self.y -= self.world.gap // 2 + self.world.tile_height // 2
+                        self.next_move = "down"
+                        self.picture = PLAYER_RD
+
+                elif self.next_move == "down":
+                    if self.y + self.world.gap // 2 + self.world.tile_height // 2 <= self.world.bottom_border:
+                        self.x += self.world.gap + 3 * self.world.tile_width // 4
+                        self.y += self.world.gap // 2 + self.world.tile_height // 2
+                        self.next_move = "up"
+                        self.picture = PLAYER_RU
 
-    

+ 4 - 1
variables.py

@@ -20,7 +20,10 @@ GAP = 8
 HEXA = image.load(path.join("images/hexa.png"))
 SEED = image.load(path.join("images/seed.png"))
 
-PLAYER = image.load(path.join("images/player.png"))
+PLAYER_RU = image.load(path.join("images/player-ru.png"))
+PLAYER_RD = image.load(path.join("images/player-rd.png"))
+PLAYER_LU = image.load(path.join("images/player-lu.png"))
+PLAYER_LD = image.load(path.join("images/player-ld.png"))
 
 NEST = image.load(path.join("images/nest.png"))