Herramientas de usuario

Herramientas del sitio


informatica:programacion:python:pyxel:juego_tipo_celeste

¡Esta es una revisión vieja del documento!


Celeste clon

Juego tipo Celeste usando Pyxel.

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

import pyxel
 
class Player:
    def __init__(self):
        self.reset_position()
        self.vx = 0
        self.vy = 0
        self.jump_power = -4.5
        self.gravity = 0.3
        self.dash_speed = 5
        self.dash_cooldown = 0
        self.dash_duration = 0
        self.on_ground = False
 
    def reset_position(self):
        self.x = 40
        self.y = 40
 
    def update(self, platforms):
        if self.dash_duration <= 0:
            if pyxel.btn(pyxel.KEY_LEFT):
                self.vx = max(-3, self.vx - 0.5)
            elif pyxel.btn(pyxel.KEY_RIGHT):
                self.vx = min(3, self.vx + 0.5)
            else:
                self.vx *= 0.8
 
        if pyxel.btnp(pyxel.KEY_Z) and self.on_ground:
            self.vy = self.jump_power
            self.on_ground = False
 
        self.dash_cooldown = max(0, self.dash_cooldown - 1)
        if pyxel.btnp(pyxel.KEY_X) and self.dash_cooldown == 0:
            self.dash_cooldown = 60
            self.dash_duration = 10
            self.vx = (pyxel.btn(pyxel.KEY_RIGHT) - pyxel.btn(pyxel.KEY_LEFT)) * self.dash_speed
            self.vy = (pyxel.btn(pyxel.KEY_DOWN) - pyxel.btn(pyxel.KEY_UP)) * self.dash_speed * 0.7
 
        self.dash_duration = max(0, self.dash_duration - 1)
 
        if not self.on_ground and self.dash_duration == 0:
            self.vy += self.gravity
 
        self.x = max(0, min(self.x, 160 - 8))
 
        self.x += self.vx
        self.check_collisions(platforms, "horizontal")
 
        self.y += self.vy
        self.check_collisions(platforms, "vertical")
 
    def check_collisions(self, platforms, axis):
        self.on_ground = False
        for (px, py, w, h) in platforms:
            if (self.x + 8 > px and self.x < px + w and
                self.y + 8 > py and self.y < py + h):
 
                if axis == "horizontal":
                    if self.vx > 0:
                        self.x = px - 8
                    elif self.vx < 0:
                        self.x = px + w
                    self.vx = 0
                else:
                    if self.vy > 0:
                        self.y = py - 8
                        self.on_ground = True
                    elif self.vy < 0:
                        self.y = py + h
                    self.vy = 0
 
    def draw(self):
        pyxel.rect(self.x, self.y, 8, 8, 8)
 
class App:
    def __init__(self):
        pyxel.init(160, 120, title="Celeste-like", fps=30)
        self.player = Player()
        self.levels = [
            [(0, 112, 160, 8), (40, 80, 40, 8), (100, 60, 30, 8)],
            [(0, 112, 160, 8), (20, 90, 30, 8), (80, 70, 40, 8), (120, 50, 20, 8)],
            [(0, 112, 160, 8), (10, 100, 20, 8), (60, 80, 30, 8), (110, 60, 40, 8)]
        ]
        self.current_level = 0
        self.current_platforms = self.levels[self.current_level]
        pyxel.run(self.update, self.draw)
 
    def update(self):
        self.player.update(self.current_platforms)
 
        # Avanzar de nivel (solo si no es el último)
        if self.player.x >= 160 - 8 and self.current_level < len(self.levels) - 1:
            self.current_level += 1
            self.current_platforms = self.levels[self.current_level]
            self.player.reset_position()
            self.player.vx = 0
            self.player.vy = 0
 
        # Retroceder de nivel (solo si no es el primero)
        elif self.player.x <= 0 and self.current_level > 0:
            self.current_level -= 1
            self.current_platforms = self.levels[self.current_level]
            self.player.reset_position()
            self.player.x = 160 - 8  # Aparece en el lado derecho
            self.player.vx = 0
            self.player.vy = 0
 
    def draw(self):
        pyxel.cls(0)
 
        # Plataformas
        for plat in self.current_platforms:
            pyxel.rect(*plat, 3)
 
        # Jugador
        self.player.draw()
 
        # UI
        nivel_texto = f"Nivel: {self.current_level + 1}/{len(self.levels)}"
        pyxel.text(5, 5, nivel_texto, 7)
 
        if self.player.dash_cooldown > 0:
            pyxel.rect(10, 15, 20, 4, 1)
            pyxel.rect(10, 15, (20 * (60 - self.player.dash_cooldown) // 60), 4, 11)
 
App()
informatica/programacion/python/pyxel/juego_tipo_celeste.1742224447.txt.gz · Última modificación: por tempwin