Herramientas de usuario

Herramientas del sitio


informatica:programacion:python:pyxel:juego_tipo_space_invaders

Space Invaders clon

Juego tipo Space Invaders usando Pyxel.

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

import pyxel
import random
 
class SpaceInvaders:
    def __init__(self):
        # Configuración inicial
        pyxel.init(224, 256, title="Pyxel Space Invaders", fps=60)
 
        # Jugador
        self.player_x = 112
        self.player_speed = 3
        self.lives = 3
 
        # Disparos
        self.bullets = []
        self.bullet_speed = -5
 
        # Enemigos
        self.enemies = []
        self.enemy_speed = 1
        self.enemy_direction = 1
        self.enemy_cooldown = 0
        self.enemy_bullets = []
 
        # Inicializar enemigos (5 filas x 11 columnas)
        for y in range(5):
            for x in range(11):
                self.enemies.append((x * 16 + 24, y * 16 + 30))
 
        # Configurar sonidos directamente
#        self.setup_sounds()
 
        pyxel.run(self.update, self.draw)
 
#    def setup_sounds(self):
#        # Sonido 0: Disparo del jugador (tono agudo)
#        pyxel.sound(0).set(
#            notes="C5C5",
#            tones="TT",
#            volumes="55",
#            effects="NN",
#            speed=20
#        )
#        
#        # Sonido 1: Explosión enemiga (tono grave)
#        pyxel.sound(1).set(
#            notes="C2C2",
#            tones="TT",
#            volumes="55",
#            effects="NN",
#            speed=10
#        )
 
    def update(self):
        if self.lives <= 0:
            return
 
        # Movimiento jugador
        if pyxel.btn(pyxel.KEY_LEFT) and self.player_x > 0:
            self.player_x -= self.player_speed
        if pyxel.btn(pyxel.KEY_RIGHT) and self.player_x < 184:
            self.player_x += self.player_speed
 
        # Disparar
        if pyxel.btnp(pyxel.KEY_SPACE) and len(self.bullets) < 2:
            self.bullets.append((self.player_x + 8, 240))
            pyxel.play(0, 0)
 
        # Actualizar balas jugador
        for i in range(len(self.bullets)-1, -1, -1):
            x, y = self.bullets[i]
            y += self.bullet_speed
            if y < 0:
                del self.bullets[i]
            else:
                self.bullets[i] = (x, y)
 
                # Colisión bala-enemigo
                for j in range(len(self.enemies)-1, -1, -1):
                    ex, ey = self.enemies[j]
                    if (ex < x < ex + 16) and (ey < y < ey + 16):
                        del self.enemies[j]
                        del self.bullets[i]
                        pyxel.play(0, 1)
                        break
 
        # Movimiento enemigos
        self.enemy_cooldown += 1
        if self.enemy_cooldown >= 30:
            self.enemy_cooldown = 0
            self.enemy_direction *= -1
            for i in range(len(self.enemies)):
                x, y = self.enemies[i]
                self.enemies[i] = (x + self.enemy_speed * self.enemy_direction, y + 2)
 
            # Disparo enemigo aleatorio
            if self.enemies:
                enemy = random.choice(self.enemies)
                self.enemy_bullets.append((enemy[0] + 8, enemy[1] + 16))
 
        # Actualizar balas enemigas
        for i in range(len(self.enemy_bullets)-1, -1, -1):
            x, y = self.enemy_bullets[i]
            y += 2
            if y > 256:
                del self.enemy_bullets[i]
            else:
                self.enemy_bullets[i] = (x, y)
 
                # Colisión bala-jugador
                if (self.player_x < x < self.player_x + 32) and (240 < y < 256):
                    self.lives -= 1
                    del self.enemy_bullets[i]
                    pyxel.play(0, 0)
 
        # Game Over si enemigos llegan abajo
        for x, y in self.enemies:
            if y > 200:
                self.lives = 0
 
    def draw(self):
        pyxel.cls(0)
 
        # Dibujar jugador (usando formas geométricas)
        pyxel.rect(self.player_x, 240, 32, 8, 11)  # Cuerpo principal
        pyxel.tri(
            self.player_x + 12, 232,
            self.player_x + 8, 240,
            self.player_x + 16, 240,
            11  # Cañón superior
        )
 
        # Dibujar enemigos (naves alienígenas básicas)
        for x, y in self.enemies:
            pyxel.rect(x, y, 12, 8, 8)  # Cuerpo
            pyxel.rect(x - 2, y + 8, 16, 4, 8)  # Alas
 
        # Balas
        for x, y in self.bullets:
            pyxel.rect(x, y, 2, 6, 10)  # Balas jugador (verde)
        for x, y in self.enemy_bullets:
            pyxel.rect(x, y, 2, 6, 8)  # Balas enemigas (rojo)
 
        # UI
        pyxel.text(5, 5, f"VIDAS: {'♥' * self.lives}", 7)
        pyxel.rect(0, 248, 224, 8, 3)  # Borde inferior
        if self.lives <= 0:
            pyxel.text(80, 120, "GAME OVER", pyxel.frame_count % 16)
 
SpaceInvaders()
informatica/programacion/python/pyxel/juego_tipo_space_invaders.txt · Última modificación: por tempwin