TINI-8/core/renderer.py

51 lines
1.8 KiB
Python
Raw Normal View History

# engine/renderer.py
import pygame
class Renderer:
def __init__(self, surface):
self.surface = surface
# Define a simple 8-bit palette (e.g., NES palette)
self.palette = [
(84, 84, 84), # Color 0
(0, 30, 116), # Color 1
(8, 16, 144), # Color 2
(48, 0, 136), # Color 3
(68, 0, 100), # Color 4
(92, 0, 48), # Color 5
(84, 4, 0), # Color 6
(60, 24, 0), # Color 7
(32, 42, 0), # Color 8
(8, 58, 0), # Color 9
(0, 64, 0), # Color 10
(0, 60, 0), # Color 11
(0, 50, 60), # Color 12
(0, 0, 0), # Color 13
(0, 0, 0), # Color 14
(0, 0, 0), # Color 15
]
def clear(self, color=0):
if 0 <= color < len(self.palette):
self.surface.fill(self.palette[color])
else:
self.surface.fill(self.palette[0]) # Default to background color
def draw_pixel(self, x, y, color_index):
if 0 <= color_index < len(self.palette):
if 0 <= x < self.surface.get_width() and 0 <= y < self.surface.get_height():
self.surface.set_at((int(x), int(y)), self.palette[color_index])
def draw_sprite(self, x, y, sprite, color_index=1):
"""
Draws a sprite at position (x, y).
:param x: X-coordinate
:param y: Y-coordinate
:param sprite: 2D list representing the sprite
:param color_index: Color index to use for the sprite
"""
for row_idx, row in enumerate(sprite):
for col_idx, pixel in enumerate(row):
if pixel:
self.draw_pixel(x + col_idx, y + row_idx, color_index)