Treacky
2 months ago
2 changed files with 110 additions and 0 deletions
@ -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() |
Binary file not shown.
Loading…
Reference in new issue