informatica:programacion:python:pyxel:juego_tipo_tetris
Tetris clon
Juego tipo Tetris usando Pyxel.
El código fue generado por DeepSeek (una inteligencia artificial generativa)
import pyxel import random # Configuración del tablero BOARD_WIDTH = 10 BOARD_HEIGHT = 20 BLOCK_SIZE = 8 # Colores de las piezas (paleta Pyxel) COLORS = [ 0, # Vacío (negro) 8, # Rojo (I) 2, # Azul (J) 11, # Naranja (L) 3, # Verde (S) 5, # Morado (T) 4, # Amarillo (O) 9 # Cyan (Z) ] # Formas de las piezas (tetrominos) SHAPES = [ [[1, 1, 1, 1]], # I [[1, 0, 0], [1, 1, 1]], # J [[0, 0, 1], [1, 1, 1]], # L [[1, 1], [1, 1]], # O [[0, 1, 1], [1, 1, 0]], # S [[0, 1, 0], [1, 1, 1]], # T [[1, 1, 0], [0, 1, 1]] # Z ] class Tetris: def __init__(self): pyxel.init(160, 256, title="Pyxel Tetris", fps=60) self.reset() pyxel.run(self.update, self.draw) def reset(self): self.board = [[0] * BOARD_WIDTH for _ in range(BOARD_HEIGHT)] self.score = 0 self.current_piece = None self.next_piece = random.randint(0, 6) self.pos_x = 0 self.pos_y = 0 self.game_over = False self.spawn_piece() # Configurar sonidos pyxel.sound(0).set(notes="C3", tones="T", volumes="5", effects="N", speed=10) # Rotar pyxel.sound(1).set(notes="E4", tones="T", volumes="5", effects="N", speed=10) # Línea def spawn_piece(self): self.current_piece = self.next_piece self.next_piece = random.randint(0, 6) self.pos_x = BOARD_WIDTH // 2 - len(SHAPES[self.current_piece][0]) // 2 self.pos_y = 0 if self.check_collision(self.pos_x, self.pos_y, SHAPES[self.current_piece]): self.game_over = True def check_collision(self, x, y, shape): for row in range(len(shape)): for col in range(len(shape[row])): if shape[row][col]: board_x = x + col board_y = y + row if (board_x < 0 or board_x >= BOARD_WIDTH or board_y >= BOARD_HEIGHT or (board_y >= 0 and self.board[board_y][board_x])): return True return False def rotate_piece(self): new_shape = list(zip(*reversed(SHAPES[self.current_piece]))) if not self.check_collision(self.pos_x, self.pos_y, new_shape): SHAPES[self.current_piece] = new_shape pyxel.play(0, 0) def update(self): if self.game_over: if pyxel.btnp(pyxel.KEY_R): self.reset() return # Movimiento horizontal if pyxel.btnp(pyxel.KEY_LEFT, 5, 2): if not self.check_collision(self.pos_x - 1, self.pos_y, SHAPES[self.current_piece]): self.pos_x -= 1 if pyxel.btnp(pyxel.KEY_RIGHT, 5, 2): if not self.check_collision(self.pos_x + 1, self.pos_y, SHAPES[self.current_piece]): self.pos_x += 1 # Rotación if pyxel.btnp(pyxel.KEY_UP): self.rotate_piece() # Caída rápida if pyxel.btn(pyxel.KEY_DOWN): self.drop_interval = 5 else: self.drop_interval = 30 # Caída automática if pyxel.frame_count % self.drop_interval == 0: if not self.check_collision(self.pos_x, self.pos_y + 1, SHAPES[self.current_piece]): self.pos_y += 1 else: self.lock_piece() def lock_piece(self): for row in range(len(SHAPES[self.current_piece])): for col in range(len(SHAPES[self.current_piece][row])): if SHAPES[self.current_piece][row][col]: self.board[self.pos_y + row][self.pos_x + col] = self.current_piece + 1 lines_cleared = 0 for row in range(BOARD_HEIGHT): if 0 not in self.board[row]: del self.board[row] self.board.insert(0, [0] * BOARD_WIDTH) lines_cleared += 1 if lines_cleared > 0: self.score += (lines_cleared ** 2) * 100 pyxel.play(0, 1) self.spawn_piece() def drop_piece(self): while not self.check_collision(self.pos_x, self.pos_y + 1, SHAPES[self.current_piece]): self.pos_y += 1 self.lock_piece() def draw(self): pyxel.cls(0) # Dibujar tablero for y in range(BOARD_HEIGHT): for x in range(BOARD_WIDTH): if self.board[y][x]: pyxel.rect(x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE-1, BLOCK_SIZE-1, COLORS[self.board[y][x]]) # Dibujar pieza actual if not self.game_over: for row in range(len(SHAPES[self.current_piece])): for col in range(len(SHAPES[self.current_piece][row])): if SHAPES[self.current_piece][row][col]: x = (self.pos_x + col) * BLOCK_SIZE y = (self.pos_y + row) * BLOCK_SIZE pyxel.rect(x, y, BLOCK_SIZE-1, BLOCK_SIZE-1, COLORS[self.current_piece + 1]) # Dibujar siguiente pieza pyxel.text(120, 10, "SIGUIENTE:", 7) next_shape = SHAPES[self.next_piece] for row in range(len(next_shape)): for col in range(len(next_shape[row])): if next_shape[row][col]: x = 120 + col * BLOCK_SIZE y = 30 + row * BLOCK_SIZE pyxel.rect(x, y, BLOCK_SIZE-1, BLOCK_SIZE-1, COLORS[self.next_piece + 1]) # Dibujar puntuación pyxel.text(120, 100, f"PUNTOS: {self.score}", 7) # Game Over if self.game_over: pyxel.text(50, 120, "GAME OVER", pyxel.frame_count % 16) pyxel.text(40, 140, "Presiona R para reiniciar", 7) Tetris()
informatica/programacion/python/pyxel/juego_tipo_tetris.txt · Última modificación: por tempwin
