Herramientas de usuario

Herramientas del sitio


informatica:programacion:python:pyxel:juego_tipo_flappy_bird

Flappy Bird clon

Juego tipo Flappy Bird usando Pyxel.

El código fue generado por DeepSeek (una inteligencia artificial generativa)

import pyxel
import random
 
class FlappyBird:
    def __init__(self):
        pyxel.init(128, 256, title="Pyxel Flappy", fps=60)
        self.reset_game()
        self.setup_resources()
        pyxel.run(self.update, self.draw)
 
    def reset_game(self):
        # Jugador
        self.bird_x = 32
        self.bird_y = 128
        self.bird_velocity = 0
        self.gravity = 0.4
        self.jump_force = -6
 
        # Obstáculos
        self.pipes = []
        self.pipe_gap = 72
        self.pipe_spacing = 80
        self.pipe_speed = 2
        self.next_pipe = 128
 
        # Estado del juego
        self.score = 0
        self.game_over = False
        self.flap_animation = 0
 
        # Configurar sonidos
        pyxel.sound(0).set(notes="A3", tones="T", volumes="5", effects="N", speed=20)  # Salto
        pyxel.sound(1).set(notes="C2", tones="T", volumes="5", effects="N", speed=20)  # Colisión
 
    def setup_resources(self):
        # Cargar sprites simples (8x8)
        pyxel.image(0).set(0, 0, [
            "00011000",
            "00111100",
            "11111111",
            "11100111",
            "11111111",
            "00111100",
            "00011000",
            "00000000"
        ])
 
    def update(self):
        if self.game_over:
            if pyxel.btnp(pyxel.KEY_R):
                self.reset_game()
            return
 
        # Física del pájaro
        self.bird_velocity += self.gravity
        self.bird_y += self.bird_velocity
        self.flap_animation = (self.flap_animation + 1) % 10
 
        # Salto
        if pyxel.btnp(pyxel.KEY_SPACE) or pyxel.btnp(pyxel.KEY_UP):
            self.bird_velocity = self.jump_force
            pyxel.play(0, 0)
 
        # Generar tuberías
        if self.next_pipe - self.bird_x < 128:
            self.create_pipe()
            self.next_pipe += self.pipe_spacing
 
        # Mover tuberías
        for pipe in self.pipes:
            pipe["x"] -= self.pipe_speed
 
        # Eliminar tuberías fuera de pantalla
        self.pipes = [pipe for pipe in self.pipes if pipe["x"] > -32]
 
        # Detectar colisiones
        self.check_collisions()
 
        # Actualizar puntuación
        for pipe in self.pipes:
            if pipe["x"] + 32 < self.bird_x and not pipe["counted"]:
                self.score += 1
                pipe["counted"] = True
 
    def create_pipe(self):
        gap_y = random.randint(64, 256 - self.pipe_gap - 64)
        self.pipes.append({
            "x": 128,
            "top": gap_y - self.pipe_gap,
            "bottom": gap_y,
            "counted": False
        })
 
    def check_collisions(self):
        # Colisión con el suelo/techo
        if self.bird_y < 8 or self.bird_y > 256 - 8:
            self.game_over = True
            pyxel.play(0, 1)
 
        # Colisión con tuberías
        bird_rect = (self.bird_x - 4, self.bird_y - 4, 8, 8)
        for pipe in self.pipes:
            # Tubería superior
            if self.check_collision(bird_rect, (pipe["x"], 0, 32, pipe["top"])):
                self.game_over = True
                pyxel.play(0, 1)
            # Tubería inferior
            if self.check_collision(bird_rect, (pipe["x"], pipe["bottom"], 32, 256 - pipe["bottom"])):
                self.game_over = True
                pyxel.play(0, 1)
 
    def check_collision(self, rect1, rect2):
        return not (rect1[0] + rect1[2] < rect2[0] or
                    rect1[0] > rect2[0] + rect2[2] or
                    rect1[1] + rect1[3] < rect2[1] or
                    rect1[1] > rect2[1] + rect2[3])
 
    def draw(self):
        pyxel.cls(12)  # Fondo celeste
 
        # Dibujar suelo
        pyxel.rect(0, 240, 128, 16, 3)
        pyxel.rect(0, 240, 128, 4, 11)
 
        # Dibujar tuberías
        for pipe in self.pipes:
            # Tubería superior
            pyxel.rect(pipe["x"], 0, 32, pipe["top"], 10)
            pyxel.rect(pipe["x"] - 2, pipe["top"] - 16, 36, 16, 10)
            # Tubería inferior
            pyxel.rect(pipe["x"], pipe["bottom"], 32, 256 - pipe["bottom"], 10)
            pyxel.rect(pipe["x"] - 2, pipe["bottom"], 36, 16, 10)
 
        # Dibujar pájaro (con animación de aleteo)
        frame = 0 if self.flap_animation < 5 else 1
        pyxel.blt(
            self.bird_x - 8,
            self.bird_y - 8,
            0,
            frame * 8,
            0,
            8,
            8,
            0
        )
 
        # Dibujar UI
        pyxel.text(5, 5, f"SCORE: {self.score}", 7)
 
        if self.game_over:
            pyxel.text(40, 100, "GAME OVER", 8)
            pyxel.text(32, 120, "Presiona R para reiniciar", 7)
 
FlappyBird()
informatica/programacion/python/pyxel/juego_tipo_flappy_bird.txt · Última modificación: por tempwin