From a5f021abb3a88f06c7749f5a92a72da3037be7a8 Mon Sep 17 00:00:00 2001 From: Treacky Date: Thu, 28 Nov 2024 15:56:47 +0100 Subject: [PATCH] =?UTF-8?q?Transf=C3=A9rer=20les=20fichiers=20vers=20''?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.py | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++ assets.pyxres | Bin 0 -> 255 bytes 2 files changed, 110 insertions(+) create mode 100644 app.py create mode 100644 assets.pyxres diff --git a/app.py b/app.py new file mode 100644 index 0000000..ce31b91 --- /dev/null +++ b/app.py @@ -0,0 +1,110 @@ +import pyxel +import random + +# Configuration de base du jeu +SCREEN_WIDTH = 128 +SCREEN_HEIGHT = 128 +GRAVITY = 0.7 +JUMP_STRENGTH = -4 +PIPE_WIDTH = 16 +PIPE_GAP = 60 +BIRD_WIDTH = 8 +BIRD_HEIGHT = 8 + +class FlappyBird: + def __init__(self): + pyxel.init(SCREEN_WIDTH, SCREEN_HEIGHT, title="Flappy Bird") + pyxel.load("assets.pyxres") # Assurez-vous d'avoir des images dans ce fichier + self.bird_y = SCREEN_HEIGHT // 2 + self.velocity = 0 + self.score = 0 + self.pipes = [] + self.game_over = False + pyxel.run(self.update, self.draw) + + def update(self): + if self.game_over: + return + + # Mécanisme de saut + if pyxel.btnp(pyxel.KEY_SPACE): + self.velocity = JUMP_STRENGTH + + # Appliquer la gravité + self.velocity += GRAVITY + self.bird_y += self.velocity + + # Limiter l'oiseau aux bords de l'écran + if self.bird_y > SCREEN_HEIGHT - BIRD_HEIGHT: + self.bird_y = SCREEN_HEIGHT - BIRD_HEIGHT + self.velocity = 0 + + if self.bird_y < 0: + self.bird_y = 0 + self.velocity = 0 + + # Déplacer les tuyaux + self.move_pipes() + + # Vérification des collisions + self.check_collisions() + + def move_pipes(self): + if len(self.pipes) == 0 or self.pipes[-1][0] < SCREEN_WIDTH - 60: + self.create_pipe() + + # Déplacer les tuyaux vers la gauche + for pipe in self.pipes: + pipe[0] -= 1 + + # Supprimer les tuyaux qui sont hors écran + self.pipes = [pipe for pipe in self.pipes if pipe[0] > -PIPE_WIDTH] + + # Vérifier si un tuyau a traversé l'écran + for pipe in self.pipes: + if pipe[0] == SCREEN_WIDTH // 2 and not pipe[2]: + self.score += 1 + pipe[2] = True + + def create_pipe(self): + gap_y = random.randint(30, SCREEN_HEIGHT - 30 - PIPE_GAP) + self.pipes.append([SCREEN_WIDTH, gap_y, False]) # (x, y, passed) + + def check_collisions(self): + # Collision avec le sol ou le plafond + if self.bird_y > SCREEN_HEIGHT - BIRD_HEIGHT or self.bird_y < 0: + self.game_over = True + + # Collision avec les tuyaux + for pipe in self.pipes: + # Vérifier la collision avec la partie haute des tuyaux + if pipe[0] < BIRD_WIDTH and pipe[0] + PIPE_WIDTH > 0: + if self.bird_y < pipe[1]: # Si l'oiseau touche le haut du tuyau + self.game_over = True + + # Vérifier la collision avec la partie basse des tuyaux + if pipe[0] < BIRD_WIDTH and pipe[0] + PIPE_WIDTH > 0: + if self.bird_y + BIRD_HEIGHT > pipe[1] + PIPE_GAP: # Si l'oiseau touche le bas du tuyau + self.game_over = True + + def draw(self): + pyxel.cls(0) + + if self.game_over: + pyxel.text(SCREEN_WIDTH // 2 - 24, SCREEN_HEIGHT // 2, "GAME OVER", pyxel.COLOR_RED) + pyxel.text(SCREEN_WIDTH // 2 - 24, SCREEN_HEIGHT // 2 + 10, f"Score: {self.score}", pyxel.COLOR_WHITE) + return + + # Dessiner l'oiseau + pyxel.rect(32, self.bird_y, BIRD_WIDTH, BIRD_HEIGHT, pyxel.COLOR_YELLOW) + + # Dessiner les tuyaux + for pipe in self.pipes: + pyxel.rect(pipe[0], 0, PIPE_WIDTH, pipe[1], pyxel.COLOR_GREEN) # Partie haute du tuyau + pyxel.rect(pipe[0], pipe[1] + PIPE_GAP, PIPE_WIDTH, SCREEN_HEIGHT - (pipe[1] + PIPE_GAP), pyxel.COLOR_GREEN) # Partie basse du tuyau + + # Afficher le score + pyxel.text(5, 5, f"Score: {self.score}", pyxel.COLOR_WHITE) + +if __name__ == "__main__": + FlappyBird() diff --git a/assets.pyxres b/assets.pyxres new file mode 100644 index 0000000000000000000000000000000000000000..8f780d728bf59352f83e11df05f16de9e83783c5 GIT binary patch literal 255 zcmWIWW@Zs#fB;1(4WamfR6%7$YEFDnYH@yPQF5w&08}vtP)d_8SP5Q||d zE=w&c&dkp6PX52Ob^@G7%M-YW!FHAFbn;}{m7?w2ZVrquEBfy)L Q4Wy0<2pfTP28hD|00eF?9{>OV literal 0 HcmV?d00001