====== Plataformas clon ======
Juego tipo plataformas usando [[informatica:programacion:python:pyxel|Pyxel]].
El código fue generado por DeepSeek (una inteligencia artificial generativa)
import pyxel
class Platformer:
def __init__(self):
pyxel.init(160, 160, title="Llave del Tesoro", fps=60)
self.reset_game()
pyxel.run(self.update, self.draw)
def reset_game(self):
self.player_x = 80
self.player_y = 140
self.player_dy = 0
self.has_key = False
self.current_level = 0
self.levels = [
{ # Nivel 1
"platforms": [(0, 150, 160, 10), (40, 120, 40, 10), (100, 90, 40, 10)],
"obstacles": [(60, 140, 10, 10), (120, 110, 10, 10)],
"key": (140, 80),
"door": (10, 100)
},
{ # Nivel 2
"platforms": [(0, 150, 160, 10), (20, 120, 40, 10), (100, 90, 40, 10), (60, 60, 40, 10)],
"obstacles": [(90, 140, 10, 10), (140, 110, 10, 10), (30, 80, 10, 10)],
"key": (10, 140),
"door": (140, 100)
},
{ # Nivel 3
"platforms": [(0, 150, 160, 10), (40, 120, 20, 10), (100, 100, 20, 10), (60, 70, 20, 10)],
"obstacles": [(60, 140, 10, 10), (140, 120, 10, 10), (20, 90, 10, 10)],
"key": (80, 30),
"door": (75, 120)
}
]
self.load_level()
def load_level(self):
level = self.levels[self.current_level]
self.platforms = level["platforms"]
self.obstacles = level["obstacles"]
self.key = level["key"]
self.door = level["door"]
self.has_key = False
def check_platform_collision(self, x, y):
for (px, py, pw, ph) in self.platforms:
if (x + 8 > px and x < px + pw and
y + 8 > py and y < py + ph):
return True
return False
def check_obstacle_collision(self):
for (ox, oy, ow, oh) in self.obstacles:
if (self.player_x + 8 > ox and self.player_x < ox + ow and
self.player_y + 8 > oy and self.player_y < oy + oh):
return True
return False
def update(self):
if self.check_obstacle_collision():
self.load_level()
self.player_x = 80
self.player_y = 140
return
# Movimiento horizontal
if pyxel.btn(pyxel.KEY_LEFT):
self.player_x = max(0, self.player_x - 2)
if pyxel.btn(pyxel.KEY_RIGHT):
self.player_x = min(152, self.player_x + 2)
# Salto
if pyxel.btnp(pyxel.KEY_SPACE) and self.check_platform_collision(self.player_x, self.player_y + 1):
self.player_dy = -4
# Gravedad
self.player_dy = min(self.player_dy + 0.2, 3)
new_y = self.player_y + self.player_dy
# Colisión vertical
if not self.check_platform_collision(self.player_x, new_y):
self.player_y = new_y
else:
self.player_dy = 0
# Recolectar llave
if (abs(self.player_x - self.key[0]) < 8 and
abs(self.player_y - self.key[1]) < 8):
self.has_key = True
# Cambiar de nivel
if self.has_key and (abs(self.player_x - self.door[0]) < 8 and
abs(self.player_y - self.door[1]) < 8):
if self.current_level < len(self.levels) - 1:
self.current_level += 1
self.load_level()
self.player_x = 80
self.player_y = 140
else:
pyxel.text(60, 80, "¡HAS GANADO!", pyxel.frame_count % 16)
def draw(self):
pyxel.cls(0)
# Dibujar plataformas
for (x, y, w, h) in self.platforms:
pyxel.rect(x, y, w, h, 3)
# Dibujar obstáculos
for (x, y, w, h) in self.obstacles:
pyxel.rect(x, y, w, h, 8)
# Dibujar llave
if not self.has_key:
pyxel.rect(self.key[0], self.key[1], 8, 8, 9)
# Dibujar puerta
if self.has_key:
door_color = 7
else:
door_color = 4
pyxel.rect(self.door[0], self.door[1], 8, 8, door_color)
# Dibujar jugador
pyxel.rect(self.player_x, self.player_y, 8, 8, 11)
# UI
pyxel.text(5, 5, f"Nivel: {self.current_level + 1}", 7)
pyxel.text(105, 5, f"Llave: {'SI' if self.has_key else 'NO'}", 9)
# pyxel.text(5, 25, "Controles:", 7)
# pyxel.text(5, 35, "Flechas: Movimiento", 7)
# pyxel.text(5, 45, "Espacio: Saltar", 7)
Platformer()