initial commit, kind of working.
This commit is contained in:
@@ -0,0 +1 @@
|
||||
"""Diamond Hollow II Pygame preservation port."""
|
||||
@@ -0,0 +1,5 @@
|
||||
from .main import main
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,35 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
import pygame
|
||||
|
||||
|
||||
class AssetManager:
|
||||
def __init__(self, image_dir: Path, sound_dir: Path) -> None:
|
||||
self.image_dir = image_dir
|
||||
self.sound_dir = sound_dir
|
||||
self._images: dict[str, pygame.Surface] = {}
|
||||
self._sounds: dict[str, pygame.mixer.Sound] = {}
|
||||
|
||||
def image(self, name: str) -> pygame.Surface:
|
||||
if name not in self._images:
|
||||
path = self.image_dir / name
|
||||
self._images[name] = pygame.image.load(path).convert_alpha()
|
||||
return self._images[name]
|
||||
|
||||
def frames(self, name: str, frame_width: int, frame_height: int) -> list[pygame.Surface]:
|
||||
sheet = self.image(name)
|
||||
frames: list[pygame.Surface] = []
|
||||
for y in range(0, sheet.get_height(), frame_height):
|
||||
for x in range(0, sheet.get_width(), frame_width):
|
||||
if x + frame_width <= sheet.get_width() and y + frame_height <= sheet.get_height():
|
||||
frame = pygame.Surface((frame_width, frame_height), pygame.SRCALPHA)
|
||||
frame.blit(sheet, (0, 0), (x, y, frame_width, frame_height))
|
||||
frames.append(frame)
|
||||
return frames
|
||||
|
||||
def sound(self, name: str) -> pygame.mixer.Sound:
|
||||
if name not in self._sounds:
|
||||
self._sounds[name] = pygame.mixer.Sound(self.sound_dir / name)
|
||||
return self._sounds[name]
|
||||
@@ -0,0 +1,26 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import pygame
|
||||
|
||||
from . import config
|
||||
|
||||
|
||||
class Camera:
|
||||
def __init__(self, world_width: int, world_height: int) -> None:
|
||||
self.world_width = world_width
|
||||
self.world_height = world_height
|
||||
self.x = 0.0
|
||||
self.y = 0.0
|
||||
|
||||
def follow(self, rect: pygame.Rect) -> None:
|
||||
self.x = rect.centerx - config.WIDTH / 2
|
||||
self.y = rect.centery - config.HEIGHT / 2
|
||||
self.x = max(0.0, min(self.x, max(0, self.world_width - config.WIDTH)))
|
||||
self.y = max(0.0, min(self.y, max(0, self.world_height - config.HEIGHT)))
|
||||
|
||||
def apply(self, rect: pygame.Rect) -> pygame.Rect:
|
||||
return rect.move(-round(self.x), -round(self.y))
|
||||
|
||||
def world_mouse(self) -> tuple[int, int]:
|
||||
mx, my = pygame.mouse.get_pos()
|
||||
return mx // config.SCALE + round(self.x), my // config.SCALE + round(self.y)
|
||||
@@ -0,0 +1,19 @@
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
BASE_DIR = Path(__file__).resolve().parents[2]
|
||||
ASSETS_DIR = BASE_DIR / "assets"
|
||||
IMAGE_DIR = ASSETS_DIR / "images"
|
||||
SOUND_DIR = ASSETS_DIR / "sounds"
|
||||
FONT_DIR = ASSETS_DIR / "fonts"
|
||||
|
||||
WIDTH = 360
|
||||
HEIGHT = 240
|
||||
SCALE = 2
|
||||
WINDOW_SIZE = (WIDTH * SCALE, HEIGHT * SCALE)
|
||||
FPS = 60
|
||||
BLOCK_SIZE = 12
|
||||
BLOCK_WIDTH = WIDTH // BLOCK_SIZE
|
||||
BLOCK_HEIGHT = HEIGHT // BLOCK_SIZE
|
||||
VERSION = "1.21"
|
||||
RELEASE = "Pegas Games"
|
||||
@@ -0,0 +1 @@
|
||||
"""Gameplay entities."""
|
||||
@@ -0,0 +1,219 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
|
||||
import pygame
|
||||
|
||||
from .. import config
|
||||
from ..assets import AssetManager
|
||||
from ..camera import Camera
|
||||
from ..input import Input
|
||||
from ..tilemap import ChapterTileMap
|
||||
|
||||
|
||||
LEFT = -1
|
||||
RIGHT = 1
|
||||
HITBOX_OFFSET_X = 5
|
||||
HITBOX_OFFSET_Y = 10
|
||||
HITBOX_WIDTH = 6
|
||||
HITBOX_HEIGHT = 12
|
||||
COYOTE_TIME = 0.08
|
||||
JUMP_BUFFER_TIME = 0.1
|
||||
WALK_FRAME_TIME = 0.18
|
||||
STAND_FRAME_TIME = 0.5
|
||||
|
||||
|
||||
class Player:
|
||||
def __init__(self, assets: AssetManager, x: float, y: float) -> None:
|
||||
self.frames = assets.frames("player.png", 16, 24)
|
||||
self.gun_right = assets.frames("player_gun_right.png", 16, 24)
|
||||
self.gun_up = assets.frames("player_gun_up.png", 16, 24)
|
||||
self.gun_down = assets.frames("player_gun_down.png", 16, 24)
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.vx = 0.0
|
||||
self.vy = 0.0
|
||||
self.facing = RIGHT
|
||||
self.on_ground = False
|
||||
self.touching_up = False
|
||||
self.anim = "stand"
|
||||
self.anim_time = 0.0
|
||||
self.anim_index = 0
|
||||
self.frame = 6
|
||||
self.aim_radians = 0.0
|
||||
self.jump_hold = 0.0
|
||||
self.jumping = False
|
||||
self.invulnerable = 0.0
|
||||
self.coyote_timer = 0.0
|
||||
self.jump_buffer = 0.0
|
||||
self.has_gun = False
|
||||
|
||||
@property
|
||||
def sprite_rect(self) -> pygame.Rect:
|
||||
return pygame.Rect(round(self.x), round(self.y), 16, 24)
|
||||
|
||||
@property
|
||||
def hitbox(self) -> pygame.Rect:
|
||||
return pygame.Rect(round(self.x) + HITBOX_OFFSET_X, round(self.y) + HITBOX_OFFSET_Y, HITBOX_WIDTH, HITBOX_HEIGHT)
|
||||
|
||||
def update(self, dt: float, input_: Input, tilemap: ChapterTileMap, camera: Camera) -> None:
|
||||
mouse_x, mouse_y = camera.world_mouse()
|
||||
self.invulnerable = max(0.0, self.invulnerable - dt)
|
||||
midpoint = self.hitbox.center
|
||||
self.aim_radians = math.atan2(mouse_y - midpoint[1], mouse_x - midpoint[0])
|
||||
self.facing = RIGHT if mouse_x >= self.x else LEFT
|
||||
|
||||
moving_left = input_.pressed(pygame.K_a) or input_.pressed(pygame.K_LEFT)
|
||||
moving_right = input_.pressed(pygame.K_d) or input_.pressed(pygame.K_RIGHT)
|
||||
if moving_right and not moving_left:
|
||||
self.vx = 80
|
||||
elif moving_left and not moving_right:
|
||||
self.vx = -80
|
||||
else:
|
||||
drag = 400 * dt
|
||||
if self.vx > 0:
|
||||
self.vx = max(0, self.vx - drag)
|
||||
elif self.vx < 0:
|
||||
self.vx = min(0, self.vx + drag)
|
||||
|
||||
jump_pressed = (
|
||||
input_.just_pressed(pygame.K_w)
|
||||
or input_.just_pressed(pygame.K_SPACE)
|
||||
or input_.just_pressed(pygame.K_z)
|
||||
or input_.just_pressed(pygame.K_UP)
|
||||
)
|
||||
jump_held = (
|
||||
input_.pressed(pygame.K_w)
|
||||
or input_.pressed(pygame.K_SPACE)
|
||||
or input_.pressed(pygame.K_z)
|
||||
or input_.pressed(pygame.K_UP)
|
||||
)
|
||||
if self.on_ground:
|
||||
self.coyote_timer = COYOTE_TIME
|
||||
else:
|
||||
self.coyote_timer = max(0.0, self.coyote_timer - dt)
|
||||
|
||||
if jump_pressed:
|
||||
self.jump_buffer = JUMP_BUFFER_TIME
|
||||
else:
|
||||
self.jump_buffer = max(0.0, self.jump_buffer - dt)
|
||||
|
||||
if self.jump_buffer > 0 and (self.on_ground or self.coyote_timer > 0):
|
||||
self._start_jump()
|
||||
elif self.jumping and jump_held and self.jump_hold > 0 and not self.touching_up:
|
||||
self.jump_hold -= dt
|
||||
self.vy = -200
|
||||
else:
|
||||
self.jumping = False
|
||||
|
||||
self.vy = min(400, self.vy + 700 * dt)
|
||||
self._move_x(dt, tilemap)
|
||||
self._move_y(dt, tilemap)
|
||||
if self.on_ground:
|
||||
self.coyote_timer = COYOTE_TIME
|
||||
self.x = max(-HITBOX_OFFSET_X, min(self.x, config.WIDTH - HITBOX_OFFSET_X - HITBOX_WIDTH))
|
||||
self._update_animation(dt)
|
||||
|
||||
def _start_jump(self) -> None:
|
||||
self.jumping = True
|
||||
self.jump_hold = 0.2
|
||||
self.jump_buffer = 0.0
|
||||
self.coyote_timer = 0.0
|
||||
self.vy = -200
|
||||
self.on_ground = False
|
||||
|
||||
def _move_x(self, dt: float, tilemap: ChapterTileMap) -> None:
|
||||
self.x += self.vx * dt
|
||||
rect = self.hitbox
|
||||
for solid in tilemap.solid_rects_near(rect):
|
||||
if rect.colliderect(solid):
|
||||
if self.vx > 0:
|
||||
rect.right = solid.left
|
||||
elif self.vx < 0:
|
||||
rect.left = solid.right
|
||||
self.x = rect.x - HITBOX_OFFSET_X
|
||||
self.vx = 0
|
||||
|
||||
def _move_y(self, dt: float, tilemap: ChapterTileMap) -> None:
|
||||
self.y += self.vy * dt
|
||||
rect = self.hitbox
|
||||
self.on_ground = False
|
||||
self.touching_up = False
|
||||
for solid in tilemap.solid_rects_near(rect):
|
||||
if rect.colliderect(solid):
|
||||
if self.vy > 0:
|
||||
rect.bottom = solid.top
|
||||
self.on_ground = True
|
||||
elif self.vy < 0:
|
||||
rect.top = solid.bottom
|
||||
self.touching_up = True
|
||||
self.y = rect.y - HITBOX_OFFSET_Y
|
||||
self.vy = 0
|
||||
|
||||
def _update_animation(self, dt: float) -> None:
|
||||
if abs(self.vy) > 1 and not self.on_ground:
|
||||
self._set_animation("jump", dt)
|
||||
self.frame = 5
|
||||
elif abs(self.vx) > 1:
|
||||
self._set_animation("walk", dt)
|
||||
if self.anim_time >= WALK_FRAME_TIME:
|
||||
self.anim_time -= WALK_FRAME_TIME
|
||||
self.anim_index = (self.anim_index + 1) % 5
|
||||
self.frame = self.anim_index
|
||||
else:
|
||||
self._set_animation("stand", dt)
|
||||
if self.anim_time >= STAND_FRAME_TIME:
|
||||
self.anim_time -= STAND_FRAME_TIME
|
||||
self.anim_index = (self.anim_index + 1) % 2
|
||||
self.frame = 6 + self.anim_index
|
||||
|
||||
def _set_animation(self, name: str, dt: float) -> None:
|
||||
if self.anim != name:
|
||||
self.anim = name
|
||||
self.anim_time = 0.0
|
||||
self.anim_index = 0
|
||||
else:
|
||||
self.anim_time += dt
|
||||
|
||||
def draw(self, surface: pygame.Surface, camera: Camera) -> None:
|
||||
frame = self.frames[self.frame]
|
||||
gun_frames = self._gun_frames()
|
||||
gun_frame = gun_frames[self.frame] if self.frame < len(gun_frames) else gun_frames[0]
|
||||
dest = camera.apply(self.sprite_rect)
|
||||
if self.facing == LEFT:
|
||||
frame = pygame.transform.flip(frame, True, False)
|
||||
gun_frame = pygame.transform.flip(gun_frame, True, False)
|
||||
if self.invulnerable > 0:
|
||||
frame = frame.copy()
|
||||
gun_frame = gun_frame.copy()
|
||||
frame.set_alpha(165)
|
||||
gun_frame.set_alpha(165)
|
||||
surface.blit(frame, dest)
|
||||
if self.has_gun:
|
||||
surface.blit(gun_frame, dest)
|
||||
|
||||
def _gun_frames(self) -> list[pygame.Surface]:
|
||||
if -2.74 < self.aim_radians <= -0.4 or self.aim_radians > 3.9:
|
||||
return self.gun_up
|
||||
if 0.4 <= self.aim_radians < 2.74:
|
||||
return self.gun_down
|
||||
return self.gun_right
|
||||
|
||||
def hurt(self, knockback_from_x: float) -> bool:
|
||||
if self.invulnerable > 0:
|
||||
return False
|
||||
self.invulnerable = 1.0
|
||||
self.vx = -160 if knockback_from_x > self.x else 160
|
||||
self.vy = -80
|
||||
return True
|
||||
|
||||
def reset_to(self, x: float, y: float) -> None:
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.vx = 0.0
|
||||
self.vy = 0.0
|
||||
self.invulnerable = 0.0
|
||||
self.on_ground = False
|
||||
self.jumping = False
|
||||
self.coyote_timer = 0.0
|
||||
self.jump_buffer = 0.0
|
||||
@@ -0,0 +1,48 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import pygame
|
||||
|
||||
from . import config
|
||||
|
||||
|
||||
class Input:
|
||||
def __init__(self) -> None:
|
||||
self.keys = pygame.key.get_pressed()
|
||||
self.mouse_buttons = pygame.mouse.get_pressed(3)
|
||||
self.mouse_pos = (0, 0)
|
||||
self.held_keys: set[int] = set()
|
||||
self.just_pressed_keys: set[int] = set()
|
||||
self.just_pressed_mouse: set[int] = set()
|
||||
|
||||
def begin_frame(self) -> None:
|
||||
self.just_pressed_keys.clear()
|
||||
self.just_pressed_mouse.clear()
|
||||
|
||||
def handle_event(self, event: pygame.Event) -> None:
|
||||
if event.type == pygame.KEYDOWN:
|
||||
self.held_keys.add(event.key)
|
||||
self.just_pressed_keys.add(event.key)
|
||||
elif event.type == pygame.KEYUP:
|
||||
self.held_keys.discard(event.key)
|
||||
elif event.type == pygame.MOUSEBUTTONDOWN:
|
||||
self.just_pressed_mouse.add(event.button)
|
||||
|
||||
def refresh(self) -> None:
|
||||
self.keys = pygame.key.get_pressed()
|
||||
self.mouse_buttons = pygame.mouse.get_pressed(3)
|
||||
mx, my = pygame.mouse.get_pos()
|
||||
self.mouse_pos = (mx // config.SCALE, my // config.SCALE)
|
||||
|
||||
def pressed(self, key: int) -> bool:
|
||||
if key in self.held_keys:
|
||||
return True
|
||||
try:
|
||||
return bool(self.keys[key])
|
||||
except IndexError:
|
||||
return False
|
||||
|
||||
def just_pressed(self, key: int) -> bool:
|
||||
return key in self.just_pressed_keys
|
||||
|
||||
def mouse_just_pressed(self, button: int = 1) -> bool:
|
||||
return button in self.just_pressed_mouse
|
||||
@@ -0,0 +1,59 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import pygame
|
||||
|
||||
from . import config
|
||||
from .assets import AssetManager
|
||||
from .input import Input
|
||||
from .state import StateMachine
|
||||
from .states.main_menu import MainMenu
|
||||
|
||||
|
||||
class Game:
|
||||
def __init__(self) -> None:
|
||||
pygame.init()
|
||||
try:
|
||||
pygame.mixer.init()
|
||||
except pygame.error:
|
||||
# Audio is optional during early development and in headless checks.
|
||||
pass
|
||||
|
||||
pygame.display.set_caption("Diamond Hollow II")
|
||||
self.window = pygame.display.set_mode(config.WINDOW_SIZE)
|
||||
self.canvas = pygame.Surface((config.WIDTH, config.HEIGHT)).convert()
|
||||
self.clock = pygame.time.Clock()
|
||||
self.input = Input()
|
||||
self.assets = AssetManager(config.IMAGE_DIR, config.SOUND_DIR)
|
||||
self.states = StateMachine()
|
||||
self.states.switch(MainMenu(self.assets, self.states))
|
||||
pygame.mouse.set_visible(False)
|
||||
|
||||
def run(self) -> None:
|
||||
running = True
|
||||
while running:
|
||||
dt = self.clock.tick(config.FPS) / 1000.0
|
||||
self.input.begin_frame()
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
running = False
|
||||
elif event.type == pygame.KEYDOWN and event.key == pygame.K_F4 and event.mod & pygame.KMOD_ALT:
|
||||
running = False
|
||||
self.input.handle_event(event)
|
||||
|
||||
self.input.refresh()
|
||||
if self.input.just_pressed(pygame.K_ESCAPE):
|
||||
running = False
|
||||
|
||||
self.states.update(dt, self.input)
|
||||
self.canvas.fill((51, 51, 51))
|
||||
self.states.draw(self.canvas)
|
||||
|
||||
scaled = pygame.transform.scale(self.canvas, config.WINDOW_SIZE)
|
||||
self.window.blit(scaled, (0, 0))
|
||||
pygame.display.flip()
|
||||
|
||||
pygame.quit()
|
||||
|
||||
|
||||
def main() -> None:
|
||||
Game().run()
|
||||
@@ -0,0 +1 @@
|
||||
"""Gameplay objects."""
|
||||
@@ -0,0 +1,40 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
|
||||
import pygame
|
||||
|
||||
from .. import config
|
||||
from ..assets import AssetManager
|
||||
from ..camera import Camera
|
||||
from ..tilemap import ChapterTileMap
|
||||
|
||||
|
||||
class Bullet:
|
||||
def __init__(self, assets: AssetManager, x: float, y: float, radians: float) -> None:
|
||||
self.image = assets.image("bullet_prime.png")
|
||||
self.x = x + 500 * math.cos(radians) * 0.005
|
||||
self.y = y + 500 * math.sin(radians) * 0.005
|
||||
self.vx = 500 * math.cos(radians)
|
||||
self.vy = 500 * math.sin(radians)
|
||||
self.alive = True
|
||||
self.damage = 20
|
||||
self.impact: tuple[float, float] | None = None
|
||||
|
||||
@property
|
||||
def rect(self) -> pygame.Rect:
|
||||
return pygame.Rect(round(self.x) + 1, round(self.y) + 1, 2, 2)
|
||||
|
||||
def update(self, dt: float, tilemap: ChapterTileMap, camera: Camera) -> None:
|
||||
self.x += self.vx * dt
|
||||
self.y += self.vy * dt
|
||||
if tilemap.solid_at_pixel(self.rect.centerx, self.rect.centery):
|
||||
tilemap.break_tile_at_pixel(self.rect.centerx, self.rect.centery)
|
||||
self.impact = self.rect.center
|
||||
self.alive = False
|
||||
screen = camera.apply(self.rect)
|
||||
if screen.right < 0 or screen.left > config.WIDTH or screen.bottom < -60 or screen.top > config.HEIGHT + 60:
|
||||
self.alive = False
|
||||
|
||||
def draw(self, surface: pygame.Surface, camera: Camera) -> None:
|
||||
surface.blit(self.image, camera.apply(self.image.get_rect(topleft=(round(self.x), round(self.y)))))
|
||||
@@ -0,0 +1,185 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import random
|
||||
|
||||
import pygame
|
||||
|
||||
from .. import config
|
||||
from ..assets import AssetManager
|
||||
from ..camera import Camera
|
||||
from ..tilemap import ChapterTileMap
|
||||
|
||||
|
||||
class Enemy:
|
||||
def __init__(
|
||||
self,
|
||||
frames: list[pygame.Surface],
|
||||
col: int,
|
||||
row: int,
|
||||
hp: int = 50,
|
||||
static: bool = False,
|
||||
animation: list[int] | None = None,
|
||||
can_shoot: bool = False,
|
||||
behavior: str = "walker",
|
||||
) -> None:
|
||||
self.frames = frames
|
||||
self.animation = animation or list(range(len(frames)))
|
||||
self.x = float(col * config.BLOCK_SIZE)
|
||||
self.y = float(row * config.BLOCK_SIZE)
|
||||
self.vx = 0.0 if static else random.choice((-50.0, 50.0))
|
||||
self.vy = 0.0
|
||||
self.hp = hp
|
||||
self.max_hp = hp
|
||||
self.alive = True
|
||||
self.static = static
|
||||
self.time = random.random()
|
||||
self.show_health = 0.0
|
||||
self.shoot_timer = random.uniform(1.0, 2.5) if static else 0.0
|
||||
self.can_shoot = can_shoot
|
||||
self.behavior = behavior
|
||||
self.on_ground = False
|
||||
self.hanging = behavior == "spider"
|
||||
self.home_y = self.y
|
||||
self.spider_state = "hanging" if behavior == "spider" else ""
|
||||
first_frame = frames[self.animation[0]]
|
||||
self.width = min(10, first_frame.get_width())
|
||||
self.height = min(12, first_frame.get_height())
|
||||
self.offset_x = max(0, (first_frame.get_width() - self.width) // 2)
|
||||
self.offset_y = max(0, first_frame.get_height() - self.height)
|
||||
|
||||
@property
|
||||
def rect(self) -> pygame.Rect:
|
||||
return pygame.Rect(round(self.x) + self.offset_x, round(self.y) + self.offset_y, self.width, self.height)
|
||||
|
||||
def update(self, dt: float, tilemap: ChapterTileMap, player_rect: pygame.Rect | None = None) -> None:
|
||||
self.time += dt
|
||||
self.show_health = max(0.0, self.show_health - dt)
|
||||
self.shoot_timer = max(0.0, self.shoot_timer - dt)
|
||||
if self.behavior == "spider":
|
||||
self._update_spider(dt, player_rect)
|
||||
return
|
||||
if self.static:
|
||||
return
|
||||
self.vy = min(400, self.vy + 400 * dt)
|
||||
self.x += self.vx * dt
|
||||
rect = self.rect
|
||||
hit_wall = False
|
||||
for solid in tilemap.solid_rects_near(rect):
|
||||
if rect.colliderect(solid):
|
||||
hit_wall = True
|
||||
if self.vx > 0:
|
||||
rect.right = solid.left
|
||||
else:
|
||||
rect.left = solid.right
|
||||
self.x = rect.x - self.offset_x
|
||||
if hit_wall:
|
||||
self.vx *= -1
|
||||
edge_col = int((self.rect.centerx + (6 if self.vx > 0 else -6)) // config.BLOCK_SIZE)
|
||||
foot_row = int((self.rect.bottom + 2) // config.BLOCK_SIZE)
|
||||
if self.behavior == "bouncer" and self.on_ground and not tilemap.solid_at(edge_col, foot_row):
|
||||
self.vy = -250
|
||||
self.on_ground = False
|
||||
elif self.behavior != "bouncer" and not tilemap.solid_at(edge_col, foot_row):
|
||||
self.vx *= -1
|
||||
self.y += self.vy * dt
|
||||
rect = self.rect
|
||||
self.on_ground = False
|
||||
for solid in tilemap.solid_rects_near(rect):
|
||||
if rect.colliderect(solid):
|
||||
if self.vy > 0:
|
||||
rect.bottom = solid.top
|
||||
self.on_ground = True
|
||||
elif self.vy < 0:
|
||||
rect.top = solid.bottom
|
||||
self.y = rect.y - self.offset_y
|
||||
self.vy = 0
|
||||
|
||||
def _update_spider(self, dt: float, player_rect: pygame.Rect | None) -> None:
|
||||
if self.spider_state == "hanging":
|
||||
self.vy = 0
|
||||
if player_rect is not None and abs(player_rect.centerx - self.rect.centerx) < 18 and player_rect.centery > self.rect.centery:
|
||||
self.spider_state = "dropping"
|
||||
self.hanging = False
|
||||
self.vy = 140
|
||||
return
|
||||
if self.spider_state == "dropping":
|
||||
self.y += self.vy * dt
|
||||
if self.y >= self.home_y + 72:
|
||||
self.spider_state = "rising"
|
||||
self.vy = -45
|
||||
return
|
||||
if self.spider_state == "rising":
|
||||
self.y += self.vy * dt
|
||||
if self.y <= self.home_y:
|
||||
self.y = self.home_y
|
||||
self.vy = 0
|
||||
self.hanging = True
|
||||
self.spider_state = "hanging"
|
||||
|
||||
def damage(self, amount: int) -> None:
|
||||
self.hp -= amount
|
||||
self.show_health = 2.0
|
||||
if self.hp <= 0:
|
||||
self.alive = False
|
||||
|
||||
def draw(self, surface: pygame.Surface, camera: Camera) -> None:
|
||||
frame_index = self.animation[int(self.time * 5) % len(self.animation)]
|
||||
frame = self.frames[frame_index]
|
||||
if self.vx < 0:
|
||||
frame = pygame.transform.flip(frame, True, False)
|
||||
dest = camera.apply(frame.get_rect(topleft=(round(self.x), round(self.y))))
|
||||
surface.blit(frame, dest)
|
||||
if self.show_health > 0 and self.max_hp > 0 and self.hp < self.max_hp:
|
||||
width = max(10, frame.get_width())
|
||||
x = dest.centerx - width // 2
|
||||
y = dest.top - 5
|
||||
pygame.draw.rect(surface, (28, 18, 18), (x, y, width, 3))
|
||||
fill = max(0, round(width * self.hp / self.max_hp))
|
||||
pygame.draw.rect(surface, (210, 50, 45), (x, y, fill, 3))
|
||||
|
||||
|
||||
def make_enemy(assets: AssetManager, col: int, row: int, marker: int) -> Enemy | None:
|
||||
mapping = {
|
||||
16711680: ("enemy_slime.png", 14, 11, 50, False, "slime", False, "walker"),
|
||||
9699328: ("enemy_bouncing_slime.png", 14, 11, 50, False, "slime", False, "bouncer"),
|
||||
7766374: ("enemy_plant.png", 14, 11, 45, True, "first", True, "plant"),
|
||||
5069370: ("enemy_plant.png", 14, 11, 45, True, "first", True, "plant"),
|
||||
1427036: ("enemy_ceiling_plant.png", 19, 14, 45, True, "first", True, "plant"),
|
||||
12221184: ("enemy_spider.png", 13, 10, 50, True, "all", False, "spider"),
|
||||
12500670: ("enemy_spike.png", 12, 12, 9999, True, "first", False, "static"),
|
||||
7929600: ("enemy_bird.png", 11, 18, 50, False, "all", False, "walker"),
|
||||
13369599: ("enemy_slime_king.png", 57, 14, 300, False, "first", False, "walker"),
|
||||
}
|
||||
if marker not in mapping:
|
||||
return None
|
||||
image, width, height, hp, static, animation_type, default_can_shoot, behavior = mapping[marker]
|
||||
frames = assets.frames(image, width, height)
|
||||
if animation_type == "slime":
|
||||
variant = random.randrange(max(1, len(frames) // 2))
|
||||
animation = [variant * 2, variant * 2 + 1]
|
||||
elif animation_type == "first":
|
||||
animation = [0]
|
||||
else:
|
||||
animation = None
|
||||
return Enemy(frames, col, row, hp, static, animation, default_can_shoot, behavior)
|
||||
|
||||
|
||||
class Seed:
|
||||
def __init__(self, image: pygame.Surface, x: float, y: float, vx: float) -> None:
|
||||
self.image = image
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.vx = vx
|
||||
self.alive = True
|
||||
|
||||
@property
|
||||
def rect(self) -> pygame.Rect:
|
||||
return self.image.get_rect(topleft=(round(self.x), round(self.y)))
|
||||
|
||||
def update(self, dt: float, tilemap: ChapterTileMap) -> None:
|
||||
self.x += self.vx * dt
|
||||
if any(self.rect.colliderect(rect) for rect in tilemap.solid_rects_near(self.rect)):
|
||||
self.alive = False
|
||||
|
||||
def draw(self, surface: pygame.Surface, camera: Camera) -> None:
|
||||
surface.blit(self.image, camera.apply(self.rect))
|
||||
@@ -0,0 +1,131 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
|
||||
import pygame
|
||||
|
||||
from .. import config
|
||||
from ..assets import AssetManager
|
||||
from ..camera import Camera
|
||||
|
||||
|
||||
class Pickup:
|
||||
def __init__(
|
||||
self,
|
||||
image: pygame.Surface,
|
||||
x: int,
|
||||
y: int,
|
||||
value: int = 1,
|
||||
frames: list[pygame.Surface] | None = None,
|
||||
fps: int = 8,
|
||||
kind: str = "diamond",
|
||||
) -> None:
|
||||
self.image = image
|
||||
self.frames = frames or [image]
|
||||
self.x = float(x)
|
||||
self.y = float(y)
|
||||
self.base_y = float(y)
|
||||
self.vx = 0.0
|
||||
self.vy = 0.0
|
||||
self.value = value
|
||||
self.alive = True
|
||||
self.time = 0.0
|
||||
self.fps = fps
|
||||
self.kind = kind
|
||||
|
||||
@property
|
||||
def rect(self) -> pygame.Rect:
|
||||
return self.frames[0].get_rect(topleft=(round(self.x), round(self.y)))
|
||||
|
||||
def update(self, dt: float, player_rect: pygame.Rect | None = None) -> None:
|
||||
self.time += dt
|
||||
if self.kind in ("diamond", "red_diamond") and player_rect is not None:
|
||||
dx = player_rect.centerx - self.rect.centerx
|
||||
dy = player_rect.centery - self.rect.centery
|
||||
distance = math.hypot(dx, dy)
|
||||
if 0 < distance < 55:
|
||||
speed = 240 if self.kind == "diamond" else 180
|
||||
self.vx = speed * dx / distance
|
||||
self.vy = speed * dy / distance
|
||||
elif self.vx or self.vy:
|
||||
self.vx *= max(0.0, 1 - 8 * dt)
|
||||
self.vy *= max(0.0, 1 - 8 * dt)
|
||||
self.x += self.vx * dt
|
||||
self.y += self.vy * dt
|
||||
elif self.kind != "checkpoint":
|
||||
self.y = self.base_y + math.sin(self.time * 3.0) * 2
|
||||
|
||||
def draw(self, surface: pygame.Surface, camera: Camera) -> None:
|
||||
frame = self.frames[int(self.time * self.fps) % len(self.frames)]
|
||||
surface.blit(frame, camera.apply(frame.get_rect(topleft=(round(self.x), round(self.y)))))
|
||||
|
||||
|
||||
class Checkpoint(Pickup):
|
||||
def __init__(self, assets: AssetManager, col: int, row: int) -> None:
|
||||
self.inactive = assets.image("checkpoint.png")
|
||||
self.active = assets.image("checkpoint_red.png")
|
||||
self.activated = False
|
||||
super().__init__(self.inactive, col * config.BLOCK_SIZE, row * config.BLOCK_SIZE - 5, kind="checkpoint")
|
||||
|
||||
def activate(self) -> None:
|
||||
self.activated = True
|
||||
self.image = self.active
|
||||
self.frames = [self.active]
|
||||
|
||||
|
||||
def diamond_value_from_level(level: int) -> int:
|
||||
if level < 9:
|
||||
return 1
|
||||
if level < 20:
|
||||
return 2
|
||||
if level < 31:
|
||||
return 3
|
||||
if level < 42:
|
||||
return 4
|
||||
if level < 53:
|
||||
return 5
|
||||
if level < 61:
|
||||
return 6
|
||||
return 10
|
||||
|
||||
|
||||
def diamond_frames_for_value(frames: list[pygame.Surface], value: int) -> list[pygame.Surface]:
|
||||
color_index = {1: 0, 2: 1, 3: 2, 4: 3, 5: 4, 6: 5, 10: 6}.get(value, 0)
|
||||
start = color_index * 6
|
||||
color_frames = frames[start : start + 6]
|
||||
if len(color_frames) < 6:
|
||||
return frames[:1]
|
||||
return color_frames + color_frames[4:0:-1]
|
||||
|
||||
|
||||
def make_pickup(assets: AssetManager, col: int, row: int, marker: int, level: int) -> Pickup | Checkpoint | None:
|
||||
x = col * config.BLOCK_SIZE
|
||||
y = row * config.BLOCK_SIZE
|
||||
if marker == 16763904:
|
||||
frames = assets.frames("diamond.png", 10, 10)
|
||||
value = diamond_value_from_level(level)
|
||||
diamond_frames = diamond_frames_for_value(frames, value)
|
||||
return Pickup(diamond_frames[0], x + 1, y + 1, value, diamond_frames, kind="diamond")
|
||||
if marker == 8171970:
|
||||
return Checkpoint(assets, col, row)
|
||||
if marker == 16711932:
|
||||
frames = assets.frames("red_diamond.png", 20, 20)
|
||||
return Pickup(frames[0], x - 1, y - 1, 50, frames, kind="red_diamond")
|
||||
if 2949120 <= marker <= 2949142:
|
||||
frames = assets.frames("heart_crystal.png", 17, 17)
|
||||
return Pickup(frames[0], x - 1, y - 5, 0, frames, kind="heart_crystal")
|
||||
if marker in (16747367, 6473198, 6746111):
|
||||
image = {
|
||||
16747367: "powerup_fire.png",
|
||||
6473198: "powerup_magnet.png",
|
||||
6746111: "powerup_shield.png",
|
||||
}[marker]
|
||||
frames = assets.frames(image, 13, 13)
|
||||
kind = {16747367: "fire", 6473198: "magnet", 6746111: "shield"}[marker]
|
||||
return Pickup(frames[0], x - 1, y - 5, 0, frames, kind=kind)
|
||||
if marker in (255, 65791, 590079):
|
||||
frames = assets.frames("powerup_equipment.png", 14, 14)
|
||||
equipment_frame = {255: 0, 65791: 1, 590079: 5}[marker]
|
||||
kind = {255: "autofirer", 65791: "lasersight", 590079: "prime"}[marker]
|
||||
return Pickup(frames[equipment_frame], x - 1, y - 5, 0, [frames[equipment_frame]], fps=0, kind=kind)
|
||||
return None
|
||||
@@ -0,0 +1,60 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import random
|
||||
|
||||
import pygame
|
||||
|
||||
from .camera import Camera
|
||||
|
||||
|
||||
class Particle:
|
||||
def __init__(self, x: float, y: float, color: tuple[int, int, int], vx: float, vy: float, life: float) -> None:
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.color = color
|
||||
self.vx = vx
|
||||
self.vy = vy
|
||||
self.life = life
|
||||
self.max_life = life
|
||||
|
||||
def update(self, dt: float) -> None:
|
||||
self.life -= dt
|
||||
self.x += self.vx * dt
|
||||
self.y += self.vy * dt
|
||||
self.vy += 120 * dt
|
||||
|
||||
def draw(self, surface: pygame.Surface, camera: Camera) -> None:
|
||||
if self.life <= 0:
|
||||
return
|
||||
alpha = max(0, min(255, int(255 * self.life / self.max_life)))
|
||||
rect = camera.apply(pygame.Rect(round(self.x), round(self.y), 2, 2))
|
||||
image = pygame.Surface((2, 2), pygame.SRCALPHA)
|
||||
image.fill((*self.color, alpha))
|
||||
surface.blit(image, rect)
|
||||
|
||||
|
||||
class ParticleSystem:
|
||||
def __init__(self) -> None:
|
||||
self.particles: list[Particle] = []
|
||||
|
||||
def emit(self, x: float, y: float, color: tuple[int, int, int], count: int = 6, speed: float = 70, life: float = 0.45) -> None:
|
||||
for _ in range(count):
|
||||
self.particles.append(
|
||||
Particle(
|
||||
x,
|
||||
y,
|
||||
color,
|
||||
random.uniform(-speed, speed),
|
||||
random.uniform(-speed, speed * 0.2),
|
||||
random.uniform(life * 0.7, life * 1.2),
|
||||
)
|
||||
)
|
||||
|
||||
def update(self, dt: float) -> None:
|
||||
for particle in self.particles:
|
||||
particle.update(dt)
|
||||
self.particles = [particle for particle in self.particles if particle.life > 0]
|
||||
|
||||
def draw(self, surface: pygame.Surface, camera: Camera) -> None:
|
||||
for particle in self.particles:
|
||||
particle.draw(surface, camera)
|
||||
@@ -0,0 +1,60 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Callable
|
||||
|
||||
import pygame
|
||||
|
||||
from .input import Input
|
||||
|
||||
|
||||
class Sprite:
|
||||
def __init__(self, image: pygame.Surface, x: int = 0, y: int = 0) -> None:
|
||||
self.image = image
|
||||
self.x = float(x)
|
||||
self.y = float(y)
|
||||
self.alpha = 255
|
||||
|
||||
@property
|
||||
def rect(self) -> pygame.Rect:
|
||||
return self.image.get_rect(topleft=(round(self.x), round(self.y)))
|
||||
|
||||
def draw(self, surface: pygame.Surface) -> None:
|
||||
if self.alpha >= 255:
|
||||
surface.blit(self.image, self.rect)
|
||||
return
|
||||
image = self.image.copy()
|
||||
image.set_alpha(max(0, min(255, round(self.alpha))))
|
||||
surface.blit(image, self.rect)
|
||||
|
||||
|
||||
class Button:
|
||||
def __init__(
|
||||
self,
|
||||
frames: list[pygame.Surface],
|
||||
x: int,
|
||||
y: int,
|
||||
callback: Callable[[], None] | None = None,
|
||||
enabled: bool = True,
|
||||
) -> None:
|
||||
if not frames:
|
||||
raise ValueError("button requires at least one frame")
|
||||
self.frames = frames
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.callback = callback
|
||||
self.enabled = enabled
|
||||
self.hovered = False
|
||||
|
||||
@property
|
||||
def rect(self) -> pygame.Rect:
|
||||
return self.frames[0].get_rect(topleft=(self.x, self.y))
|
||||
|
||||
def update(self, input_: Input) -> None:
|
||||
self.hovered = self.rect.collidepoint(input_.mouse_pos)
|
||||
if self.enabled and self.hovered and input_.mouse_just_pressed():
|
||||
if self.callback is not None:
|
||||
self.callback()
|
||||
|
||||
def draw(self, surface: pygame.Surface) -> None:
|
||||
frame_index = 1 if self.hovered and len(self.frames) > 1 else 0
|
||||
surface.blit(self.frames[frame_index], self.rect)
|
||||
@@ -0,0 +1,31 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Protocol
|
||||
|
||||
import pygame
|
||||
|
||||
from .input import Input
|
||||
|
||||
|
||||
class GameState(Protocol):
|
||||
def update(self, dt: float, input_: Input) -> None:
|
||||
...
|
||||
|
||||
def draw(self, surface: pygame.Surface) -> None:
|
||||
...
|
||||
|
||||
|
||||
class StateMachine:
|
||||
def __init__(self) -> None:
|
||||
self.state: GameState | None = None
|
||||
|
||||
def switch(self, state: GameState) -> None:
|
||||
self.state = state
|
||||
|
||||
def update(self, dt: float, input_: Input) -> None:
|
||||
if self.state is not None:
|
||||
self.state.update(dt, input_)
|
||||
|
||||
def draw(self, surface: pygame.Surface) -> None:
|
||||
if self.state is not None:
|
||||
self.state.draw(surface)
|
||||
@@ -0,0 +1 @@
|
||||
"""Game states."""
|
||||
@@ -0,0 +1,173 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import random
|
||||
|
||||
import pygame
|
||||
|
||||
from .. import config
|
||||
from ..assets import AssetManager
|
||||
from ..input import Input
|
||||
from ..sprites import Button, Sprite
|
||||
from ..state import StateMachine
|
||||
from .story_mode import StoryMode
|
||||
|
||||
|
||||
class BackgroundLayer:
|
||||
def __init__(self, image: pygame.Surface, speed: float) -> None:
|
||||
self.image = image
|
||||
self.y = 0.0
|
||||
self.speed = speed
|
||||
|
||||
def update(self, dt: float) -> None:
|
||||
self.y += dt * 20 * self.speed
|
||||
screen_y = round(self.y)
|
||||
if screen_y < -420:
|
||||
self.y = 0
|
||||
elif screen_y > 0:
|
||||
self.y = -420 + screen_y
|
||||
|
||||
def draw(self, surface: pygame.Surface) -> None:
|
||||
y = round(self.y)
|
||||
surface.blit(self.image, (0, y))
|
||||
surface.blit(self.image, (0, y + 420))
|
||||
|
||||
|
||||
class MainMenu:
|
||||
def __init__(self, assets: AssetManager, states: StateMachine) -> None:
|
||||
self.assets = assets
|
||||
self.states = states
|
||||
self.rng = random.Random()
|
||||
self.font = pygame.font.Font(None, 10)
|
||||
self.title = Sprite(assets.image("main_menu_title.png"), 4, 4)
|
||||
self.title_glow = Sprite(assets.image("main_menu_title_glow.png"), 4, 4)
|
||||
self.title_glow.alpha = 255
|
||||
self.title_fade_in = False
|
||||
self.fader_alpha = 255.0
|
||||
self.layers = [
|
||||
BackgroundLayer(assets.image("main_menu_background_3.png"), 0.4),
|
||||
BackgroundLayer(assets.image("main_menu_background_2.png"), 0.6),
|
||||
BackgroundLayer(assets.image("main_menu_background_1.png"), 0.8),
|
||||
]
|
||||
self.buttons: list[Button] = []
|
||||
self.tooltip: tuple[str, str] | None = None
|
||||
self.cursor = assets.image("mouse_cursor.png")
|
||||
self._build_main_screen()
|
||||
|
||||
def _button(
|
||||
self,
|
||||
image_name: str,
|
||||
x: int,
|
||||
y: int,
|
||||
width: int,
|
||||
height: int,
|
||||
callback=None,
|
||||
enabled: bool = True,
|
||||
) -> Button:
|
||||
button = Button(self.assets.frames(image_name, width, height), x, y, callback, enabled)
|
||||
self.buttons.append(button)
|
||||
return button
|
||||
|
||||
def _build_main_screen(self) -> None:
|
||||
self.story = self._button("button_story.png", 43, 103, 87, 26, self._start_story)
|
||||
self.escape = self._button("button_escape_locked.png", 134, 103, 87, 26, enabled=False)
|
||||
self.boss = self._button("button_boss_locked.png", 43, 136, 87, 26, enabled=False)
|
||||
self.minigame = self._button("button_minigame.png", 134, 136, 87, 26, enabled=False)
|
||||
self.help = self._button("button_help.png", 43, 169, 87, 26, enabled=False)
|
||||
self.credits = self._button("button_credits.png", 134, 169, 87, 26, enabled=False)
|
||||
self.pegas = self._button("button_pegas.png", 230, 100, 106, 46, enabled=False)
|
||||
self.more_games = self._button("button_more_games.png", 240, 145, 87, 26, enabled=False)
|
||||
self.host = self._button("button_host.png", 240, 170, 87, 26, enabled=False)
|
||||
self.options = self._button("button_options.png", 160, 213, 34, 29, enabled=False)
|
||||
|
||||
def _start_story(self) -> None:
|
||||
self.states.switch(StoryMode(self.assets, chapter=1))
|
||||
|
||||
def update(self, dt: float, input_: Input) -> None:
|
||||
if self.fader_alpha > 0:
|
||||
self.fader_alpha = max(0.0, self.fader_alpha - dt * 765)
|
||||
return
|
||||
|
||||
for layer in self.layers:
|
||||
layer.update(dt)
|
||||
|
||||
if self.title_fade_in:
|
||||
self.title_glow.alpha += dt * 255
|
||||
if self.title_glow.alpha >= 255:
|
||||
self.title_glow.alpha = 255
|
||||
self.title_fade_in = False
|
||||
else:
|
||||
self.title_glow.alpha -= dt * 255
|
||||
if self.title_glow.alpha <= 0:
|
||||
self.title_glow.alpha = 0
|
||||
self.title_fade_in = True
|
||||
|
||||
self.tooltip = None
|
||||
for button in self.buttons:
|
||||
button.update(input_)
|
||||
|
||||
if self.boss.hovered:
|
||||
self.tooltip = ("Boss Rush Locked", "Complete chapter 7 of story mode to unlock Boss Rush")
|
||||
elif self.escape.hovered:
|
||||
self.tooltip = ("Escape Mode Locked", "Complete chapter 2 of story mode to unlock Escape Mode")
|
||||
elif self.minigame.hovered or self.help.hovered or self.credits.hovered or self.options.hovered:
|
||||
self.tooltip = ("Coming Soon", "This screen will be ported after the main menu and chapter 1.")
|
||||
elif self.pegas.hovered or self.more_games.hovered or self.host.hovered:
|
||||
self.tooltip = ("External Link Stubbed", "Portal and advertising links are intentionally not ported.")
|
||||
|
||||
def draw(self, surface: pygame.Surface) -> None:
|
||||
for layer in self.layers:
|
||||
layer.draw(surface)
|
||||
|
||||
self.title_glow.draw(surface)
|
||||
self.title.draw(surface)
|
||||
|
||||
for button in self.buttons:
|
||||
button.draw(surface)
|
||||
|
||||
self._draw_bottom_bar(surface)
|
||||
if self.tooltip is not None:
|
||||
self._draw_tooltip(surface, *self.tooltip)
|
||||
|
||||
mx, my = pygame.mouse.get_pos()
|
||||
surface.blit(self.cursor, (mx // config.SCALE, my // config.SCALE))
|
||||
|
||||
if self.fader_alpha > 0:
|
||||
fader = pygame.Surface((config.WIDTH, config.HEIGHT), pygame.SRCALPHA)
|
||||
fader.fill((0, 0, 0, round(self.fader_alpha)))
|
||||
surface.blit(fader, (0, 0))
|
||||
|
||||
def _draw_bottom_bar(self, surface: pygame.Surface) -> None:
|
||||
bar = pygame.Surface((config.WIDTH, 9), pygame.SRCALPHA)
|
||||
bar.fill((0, 0, 0, 153))
|
||||
surface.blit(bar, (0, 228))
|
||||
left = self.font.render("CREATED BY ARKEUS", False, (210, 210, 210))
|
||||
right_text = f"VERSION {config.VERSION} - {config.RELEASE.upper()}"
|
||||
right = self.font.render(right_text, False, (210, 210, 210))
|
||||
surface.blit(left, (4, 230))
|
||||
surface.blit(right, (config.WIDTH - right.get_width() - 4, 230))
|
||||
|
||||
def _draw_tooltip(self, surface: pygame.Surface, title: str, body: str) -> None:
|
||||
mx, my = pygame.mouse.get_pos()
|
||||
x = min(mx // config.SCALE + 8, 214)
|
||||
y = min(my // config.SCALE + 8, 196)
|
||||
box = pygame.Surface((138, 36), pygame.SRCALPHA)
|
||||
box.fill((12, 10, 18, 220))
|
||||
pygame.draw.rect(box, (174, 146, 78), box.get_rect(), 1)
|
||||
surface.blit(box, (x, y))
|
||||
title_image = self.font.render(title.upper(), False, (255, 236, 142))
|
||||
surface.blit(title_image, (x + 4, y + 4))
|
||||
words = body.split()
|
||||
line = ""
|
||||
lines: list[str] = []
|
||||
for word in words:
|
||||
candidate = f"{line} {word}".strip()
|
||||
if len(candidate) > 28:
|
||||
lines.append(line)
|
||||
line = word
|
||||
else:
|
||||
line = candidate
|
||||
if line:
|
||||
lines.append(line)
|
||||
for index, text in enumerate(lines[:2]):
|
||||
image = self.font.render(text, False, (230, 230, 230))
|
||||
surface.blit(image, (x + 4, y + 15 + index * 9))
|
||||
@@ -0,0 +1,261 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import pygame
|
||||
|
||||
from .. import config
|
||||
from ..assets import AssetManager
|
||||
from ..camera import Camera
|
||||
from ..entities.player import Player
|
||||
from ..input import Input
|
||||
from ..objects.bullet import Bullet
|
||||
from ..objects.enemies import Enemy, Seed, make_enemy
|
||||
from ..objects.pickups import Checkpoint, Pickup, make_pickup
|
||||
from ..particles import ParticleSystem
|
||||
from ..tilemap import ChapterTileMap
|
||||
|
||||
|
||||
class StaticObject:
|
||||
def __init__(self, image: pygame.Surface, x: int, y: int) -> None:
|
||||
self.image = image
|
||||
self.x = x
|
||||
self.y = y
|
||||
|
||||
@property
|
||||
def rect(self) -> pygame.Rect:
|
||||
return self.image.get_rect(topleft=(self.x, self.y))
|
||||
|
||||
def draw(self, surface: pygame.Surface, camera: Camera) -> None:
|
||||
surface.blit(self.image, camera.apply(self.rect))
|
||||
|
||||
|
||||
class ParallaxLayer:
|
||||
def __init__(self, image: pygame.Surface, factor: float) -> None:
|
||||
self.image = image
|
||||
self.factor = factor
|
||||
|
||||
def draw(self, surface: pygame.Surface, camera: Camera) -> None:
|
||||
y = -round(camera.y * self.factor) % 420 - 420
|
||||
while y < config.HEIGHT:
|
||||
surface.blit(self.image, (0, y))
|
||||
y += 420
|
||||
|
||||
|
||||
class StoryMode:
|
||||
def __init__(self, assets: AssetManager, chapter: int = 1) -> None:
|
||||
if chapter != 1:
|
||||
raise ValueError("only Chapter 1 is implemented")
|
||||
self.assets = assets
|
||||
self.tilemap = ChapterTileMap(assets)
|
||||
self.camera = Camera(self.tilemap.pixel_width, self.tilemap.pixel_height)
|
||||
self.start_pos = (128.0, float(self.tilemap.pixel_height - 36))
|
||||
self.checkpoint_pos = self.start_pos
|
||||
self.player = Player(assets, *self.start_pos)
|
||||
self.camera.follow(self.player.hitbox)
|
||||
self.layers = [
|
||||
ParallaxLayer(assets.image("ch1_bg3.png"), 0.4),
|
||||
ParallaxLayer(assets.image("ch1_bg2.png"), 0.6),
|
||||
ParallaxLayer(assets.image("ch1_bg1.png"), 0.8),
|
||||
]
|
||||
self.cursor = assets.image("mouse_cursor.png")
|
||||
self.bullets: list[Bullet] = []
|
||||
self.pickups: list[Pickup] = []
|
||||
self.enemies: list[Enemy] = []
|
||||
self.enemy_projectiles: list[Seed] = []
|
||||
self.static_objects: list[StaticObject] = []
|
||||
self.diamonds = 0
|
||||
self.red_diamonds = 0
|
||||
self.hp = 3
|
||||
self.max_hp = 3
|
||||
self.shoot_timer = 0.0
|
||||
self.font = pygame.font.Font(None, 14)
|
||||
self.large_font = pygame.font.Font(None, 24)
|
||||
self.complete = False
|
||||
self.message = "Find SAGA and escape the cave."
|
||||
self.message_timer = 4.0
|
||||
self.seed_image = assets.image("enemy_seed.png")
|
||||
self.heart_full = assets.image("heart_full.png")
|
||||
self.heart_empty = assets.image("heart_empty.png")
|
||||
self.diamond_icon = assets.image("ui_diamonds.png")
|
||||
self.particles = ParticleSystem()
|
||||
self._spawn_markers()
|
||||
|
||||
def _spawn_markers(self) -> None:
|
||||
for col, row, marker in self.tilemap.object_markers:
|
||||
level = int((1 - (row * config.BLOCK_SIZE) / self.tilemap.pixel_height) * 10 + 1)
|
||||
pickup = make_pickup(self.assets, col, row, marker, level)
|
||||
if pickup is not None:
|
||||
self.pickups.append(pickup)
|
||||
continue
|
||||
enemy = make_enemy(self.assets, col, row, marker)
|
||||
if enemy is not None:
|
||||
self.enemies.append(enemy)
|
||||
continue
|
||||
if marker == 8750469:
|
||||
self.static_objects.append(StaticObject(self.assets.image("boss_door.png"), col * config.BLOCK_SIZE, row * config.BLOCK_SIZE - 12))
|
||||
|
||||
def update(self, dt: float, input_: Input) -> None:
|
||||
if self.complete:
|
||||
return
|
||||
self.shoot_timer = max(0.0, self.shoot_timer - dt)
|
||||
self.message_timer = max(0.0, self.message_timer - dt)
|
||||
self.player.update(dt, input_, self.tilemap, self.camera)
|
||||
if self.player.has_gun and input_.mouse_buttons[0] and self.shoot_timer <= 0:
|
||||
cx, cy = self.player.hitbox.center
|
||||
self.bullets.append(Bullet(self.assets, cx - 2, cy - 2, self.player.aim_radians))
|
||||
self.shoot_timer = 0.3
|
||||
|
||||
for bullet in self.bullets:
|
||||
bullet.update(dt, self.tilemap, self.camera)
|
||||
if bullet.impact is not None:
|
||||
self.particles.emit(*bullet.impact, (245, 238, 160), count=4, speed=45, life=0.25)
|
||||
bullet.impact = None
|
||||
for enemy in self.enemies:
|
||||
enemy.update(dt, self.tilemap, self.player.hitbox)
|
||||
if enemy.can_shoot and enemy.shoot_timer <= 0 and abs(enemy.rect.centery - self.player.hitbox.centery) < 120:
|
||||
direction = -1 if self.player.hitbox.centerx < enemy.rect.centerx else 1
|
||||
self.enemy_projectiles.append(Seed(self.seed_image, enemy.rect.centerx, enemy.rect.centery - 2, 100 * direction))
|
||||
enemy.shoot_timer = 2.5
|
||||
for projectile in self.enemy_projectiles:
|
||||
projectile.update(dt, self.tilemap)
|
||||
for pickup in self.pickups:
|
||||
pickup.update(dt, self.player.hitbox)
|
||||
self.particles.update(dt)
|
||||
|
||||
self._handle_overlaps()
|
||||
self.bullets = [bullet for bullet in self.bullets if bullet.alive]
|
||||
self.enemy_projectiles = [projectile for projectile in self.enemy_projectiles if projectile.alive]
|
||||
self.enemies = [enemy for enemy in self.enemies if enemy.alive]
|
||||
self.pickups = [pickup for pickup in self.pickups if pickup.alive or isinstance(pickup, Checkpoint)]
|
||||
if self.player.y > self.tilemap.pixel_height + 60 or self.hp <= 0:
|
||||
self._restart()
|
||||
if self.player.y < 36 or (self.player.y < 260 and not any(enemy.max_hp >= 300 for enemy in self.enemies)):
|
||||
self.complete = True
|
||||
self.camera.follow(self.player.hitbox)
|
||||
|
||||
def _handle_overlaps(self) -> None:
|
||||
for bullet in self.bullets:
|
||||
if not bullet.alive:
|
||||
continue
|
||||
for enemy in self.enemies:
|
||||
if enemy.alive and bullet.rect.colliderect(enemy.rect):
|
||||
was_alive = enemy.alive
|
||||
enemy.damage(bullet.damage)
|
||||
self.particles.emit(bullet.rect.centerx, bullet.rect.centery, (220, 60, 55), count=4, speed=55, life=0.3)
|
||||
if was_alive and not enemy.alive:
|
||||
self.particles.emit(enemy.rect.centerx, enemy.rect.centery, (210, 40, 40), count=12, speed=90, life=0.5)
|
||||
bullet.alive = False
|
||||
break
|
||||
|
||||
player_rect = self.player.hitbox
|
||||
for pickup in self.pickups:
|
||||
if not pickup.alive and not isinstance(pickup, Checkpoint):
|
||||
continue
|
||||
if isinstance(pickup, Checkpoint):
|
||||
if self._close_enough(player_rect, pickup.rect, 26) and not pickup.activated:
|
||||
pickup.activate()
|
||||
self.checkpoint_pos = (pickup.x + 4, pickup.y + 12)
|
||||
self.hp = self.max_hp
|
||||
self.particles.emit(pickup.rect.centerx, pickup.rect.centery, (255, 40, 40), count=16, speed=95, life=0.6)
|
||||
self._show_message("Checkpoint activated.")
|
||||
elif self._close_enough(player_rect, pickup.rect, 26) and pickup.activated:
|
||||
self.hp = self.max_hp
|
||||
continue
|
||||
if player_rect.colliderect(pickup.rect):
|
||||
self._activate_pickup(pickup)
|
||||
self.particles.emit(pickup.rect.centerx, pickup.rect.centery, (255, 235, 80), count=6, speed=60, life=0.35)
|
||||
pickup.alive = False
|
||||
|
||||
for enemy in self.enemies:
|
||||
if enemy.alive and player_rect.colliderect(enemy.rect):
|
||||
if self.player.hurt(enemy.x):
|
||||
self.hp -= 1
|
||||
|
||||
for projectile in self.enemy_projectiles:
|
||||
if projectile.alive and player_rect.colliderect(projectile.rect):
|
||||
projectile.alive = False
|
||||
self.particles.emit(projectile.rect.centerx, projectile.rect.centery, (120, 220, 90), count=4, speed=50, life=0.25)
|
||||
if self.player.hurt(projectile.x):
|
||||
self.hp -= 1
|
||||
|
||||
def _close_enough(self, a: pygame.Rect, b: pygame.Rect, distance: int) -> bool:
|
||||
dx = a.centerx - b.centerx
|
||||
dy = a.centery - b.centery
|
||||
return dx * dx + dy * dy <= distance * distance
|
||||
|
||||
def _activate_pickup(self, pickup: Pickup) -> None:
|
||||
if pickup.kind == "diamond":
|
||||
self.diamonds += pickup.value
|
||||
elif pickup.kind == "red_diamond":
|
||||
self.red_diamonds += 1
|
||||
self.diamonds += pickup.value
|
||||
self._show_message("Red diamond obtained.")
|
||||
elif pickup.kind == "heart_crystal":
|
||||
self.max_hp += 1
|
||||
self.hp = self.max_hp
|
||||
self._show_message("Heart crystal obtained.")
|
||||
elif pickup.kind == "prime":
|
||||
self.player.has_gun = True
|
||||
self._show_message("SAGA rescued. Mouse button shoots.", 5.0)
|
||||
elif pickup.kind in ("fire", "magnet", "shield"):
|
||||
self._show_message(f"{pickup.kind.title()} powerup obtained.")
|
||||
elif pickup.kind in ("autofirer", "lasersight"):
|
||||
self._show_message(f"{pickup.kind.title()} chip obtained.")
|
||||
|
||||
def _show_message(self, text: str, duration: float = 3.0) -> None:
|
||||
self.message = text
|
||||
self.message_timer = duration
|
||||
|
||||
def _restart(self) -> None:
|
||||
self.hp = self.max_hp
|
||||
self.player.reset_to(*self.checkpoint_pos)
|
||||
self.camera.follow(self.player.hitbox)
|
||||
|
||||
def draw(self, surface: pygame.Surface) -> None:
|
||||
for layer in self.layers:
|
||||
layer.draw(surface, self.camera)
|
||||
self.tilemap.draw(surface, self.camera)
|
||||
for pickup in self.pickups:
|
||||
if pickup.alive or isinstance(pickup, Checkpoint):
|
||||
pickup.draw(surface, self.camera)
|
||||
for obj in self.static_objects:
|
||||
obj.draw(surface, self.camera)
|
||||
for enemy in self.enemies:
|
||||
enemy.draw(surface, self.camera)
|
||||
for projectile in self.enemy_projectiles:
|
||||
projectile.draw(surface, self.camera)
|
||||
for bullet in self.bullets:
|
||||
bullet.draw(surface, self.camera)
|
||||
self.particles.draw(surface, self.camera)
|
||||
self.player.draw(surface, self.camera)
|
||||
self._draw_ui(surface)
|
||||
if self.complete:
|
||||
self._draw_complete(surface)
|
||||
mx, my = pygame.mouse.get_pos()
|
||||
surface.blit(self.cursor, (mx // config.SCALE, my // config.SCALE))
|
||||
|
||||
def _draw_ui(self, surface: pygame.Surface) -> None:
|
||||
for index in range(min(self.max_hp, 8)):
|
||||
heart = self.heart_full if index < self.hp else self.heart_empty
|
||||
surface.blit(heart, (6 + index * 10, 5))
|
||||
surface.blit(self.diamond_icon, (6, 20))
|
||||
diamonds = self.font.render(str(self.diamonds), False, (255, 240, 120))
|
||||
surface.blit(diamonds, (23, 21))
|
||||
if self.red_diamonds:
|
||||
red = self.font.render(f"RED {self.red_diamonds}", False, (255, 90, 120))
|
||||
surface.blit(red, (70, 21))
|
||||
if not self.player.has_gun:
|
||||
hint = self.font.render("Find SAGA", False, (255, 235, 128))
|
||||
surface.blit(hint, (config.WIDTH - hint.get_width() - 6, 6))
|
||||
if self.message_timer > 0:
|
||||
box = pygame.Surface((config.WIDTH - 24, 24), pygame.SRCALPHA)
|
||||
box.fill((0, 0, 0, 155))
|
||||
surface.blit(box, (12, config.HEIGHT - 34))
|
||||
text = self.font.render(self.message, False, (240, 230, 205))
|
||||
surface.blit(text, (18, config.HEIGHT - 27))
|
||||
|
||||
def _draw_complete(self, surface: pygame.Surface) -> None:
|
||||
overlay = pygame.Surface((config.WIDTH, config.HEIGHT), pygame.SRCALPHA)
|
||||
overlay.fill((0, 0, 0, 150))
|
||||
surface.blit(overlay, (0, 0))
|
||||
image = self.large_font.render("CHAPTER 1 COMPLETE", False, (255, 235, 128))
|
||||
surface.blit(image, ((config.WIDTH - image.get_width()) // 2, 103))
|
||||
@@ -0,0 +1,28 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import pygame
|
||||
|
||||
from .. import config
|
||||
from ..input import Input
|
||||
|
||||
|
||||
class StoryPlaceholder:
|
||||
def __init__(self) -> None:
|
||||
self.font = pygame.font.Font(None, 16)
|
||||
|
||||
def update(self, dt: float, input_: Input) -> None:
|
||||
_ = dt, input_
|
||||
|
||||
def draw(self, surface: pygame.Surface) -> None:
|
||||
surface.fill((18, 16, 28))
|
||||
lines = [
|
||||
"Chapter 1 port starts here next.",
|
||||
"Menu path is wired; gameplay is not implemented yet.",
|
||||
"Press ESC to quit.",
|
||||
]
|
||||
y = 96
|
||||
for line in lines:
|
||||
image = self.font.render(line, False, (235, 235, 235))
|
||||
x = (config.WIDTH - image.get_width()) // 2
|
||||
surface.blit(image, (x, y))
|
||||
y += 16
|
||||
@@ -0,0 +1,253 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import random
|
||||
|
||||
import pygame
|
||||
|
||||
from . import config
|
||||
from .assets import AssetManager
|
||||
from .camera import Camera
|
||||
|
||||
|
||||
GRASS = 5228053
|
||||
GRASS2 = 2652672
|
||||
DIRT = 10052885
|
||||
WATER = 32511
|
||||
LAVA = 14045513
|
||||
WOODEN_CRATE = 7028992
|
||||
STEEL_CRATE = 1579032
|
||||
RED_DIAMOND = 16711932
|
||||
WHITE = 16777215
|
||||
TILES = (GRASS, GRASS2, DIRT, WATER, LAVA)
|
||||
OBJECT_TILES = (WOODEN_CRATE, STEEL_CRATE)
|
||||
|
||||
|
||||
class ChapterTileMap:
|
||||
def __init__(self, assets: AssetManager) -> None:
|
||||
self.rng = random.Random(1)
|
||||
self.object_counter = 2
|
||||
self.level_pixels = assets.image("ch1_map.png")
|
||||
self.overlay_pixels = assets.image("ch1_overlay.png")
|
||||
self.tile_frames = assets.frames("ch1_tiles.png", config.BLOCK_SIZE, config.BLOCK_SIZE)
|
||||
self.width = self.level_pixels.get_width()
|
||||
self.height = self.level_pixels.get_height()
|
||||
self.pixel_width = self.width * config.BLOCK_SIZE
|
||||
self.pixel_height = self.height * config.BLOCK_SIZE
|
||||
self.tiles: list[list[int]] = []
|
||||
self.object_markers: list[tuple[int, int, int]] = []
|
||||
self._decode()
|
||||
|
||||
def _decode(self) -> None:
|
||||
for row in range(self.height):
|
||||
tile_row: list[int] = []
|
||||
for col in range(self.width):
|
||||
color = self.get_layer_tile(col, row, include_objects=True)
|
||||
tile_id = self.color_to_tile_id(color, col, row)
|
||||
tile_row.append(tile_id)
|
||||
marker = self.get_pixel(self.level_pixels, col, row)
|
||||
if marker not in TILES and marker not in OBJECT_TILES and marker != WHITE:
|
||||
self.object_markers.append((col, row, marker))
|
||||
self.tiles.append(tile_row)
|
||||
|
||||
def get_pixel(self, surface: pygame.Surface, col: int, row: int) -> int:
|
||||
if col < 0 or row < 0 or col >= surface.get_width() or row >= surface.get_height():
|
||||
return 0
|
||||
color = surface.get_at((col, row))
|
||||
return color.r << 16 | color.g << 8 | color.b
|
||||
|
||||
def get_layer_tile(self, col: int, row: int, include_objects: bool = False) -> int:
|
||||
base = self.get_pixel(self.level_pixels, col, row)
|
||||
overlay = self.get_pixel(self.overlay_pixels, col, row)
|
||||
for color in TILES:
|
||||
if base == color or overlay == color:
|
||||
return color
|
||||
if include_objects:
|
||||
for color in OBJECT_TILES:
|
||||
if base == color or overlay == color:
|
||||
return color
|
||||
return 0
|
||||
|
||||
def is_tile_color(self, color: int) -> bool:
|
||||
return color in (GRASS, DIRT)
|
||||
|
||||
def rand(self, start: int, end: int) -> int:
|
||||
return self.rng.randint(start, end)
|
||||
|
||||
def color_to_tile_id(self, color: int, col: int, row: int, overlay_pass: bool = False) -> int:
|
||||
if not overlay_pass and self.is_tile_color(self.get_pixel(self.overlay_pixels, col, row)):
|
||||
return 0
|
||||
|
||||
up = self.get_layer_tile(col, row - 1)
|
||||
right = self.get_layer_tile(col + 1, row)
|
||||
down = self.get_layer_tile(col, row + 1)
|
||||
left = self.get_layer_tile(col - 1, row)
|
||||
down_right = self.get_layer_tile(col + 1, row + 1)
|
||||
down_left = self.get_layer_tile(col - 1, row + 1)
|
||||
up_right = self.get_layer_tile(col + 1, row - 1)
|
||||
up_left = self.get_layer_tile(col - 1, row - 1)
|
||||
|
||||
has_left = left != 0 or col == 0
|
||||
has_right = right != 0 or col == config.BLOCK_WIDTH - 1
|
||||
has_up = up != 0
|
||||
has_down = down != 0 or row == self.height - 1
|
||||
has_down_right = down_right != 0 or row == self.height - 1 or col == config.BLOCK_WIDTH - 1
|
||||
has_down_left = down_left != 0 or row == self.height - 1 or col == 0
|
||||
|
||||
water_left = left == WATER
|
||||
water_right = right == WATER
|
||||
water_up_left = up_left == WATER
|
||||
water_up_right = up_right == WATER
|
||||
water_down_left = down_left == WATER
|
||||
water_down_right = down_right == WATER
|
||||
water_down = down == WATER
|
||||
|
||||
lava_left = left == LAVA
|
||||
lava_right = right == LAVA
|
||||
lava_up_left = up_left == LAVA
|
||||
lava_up_right = up_right == LAVA
|
||||
lava_down_left = down_left == LAVA
|
||||
lava_down_right = down_right == LAVA
|
||||
lava_down = down == LAVA
|
||||
|
||||
liquid_left = water_left or lava_left
|
||||
liquid_right = water_right or lava_right
|
||||
liquid_up_left = water_up_left or lava_up_left
|
||||
liquid_up_right = water_up_right or lava_up_right
|
||||
liquid_down_left = water_down_left or lava_down_left
|
||||
liquid_down_right = water_down_right or lava_down_right
|
||||
liquid_down = water_down or lava_down
|
||||
|
||||
solid_left = has_left and not liquid_left
|
||||
solid_right = has_right and not liquid_right
|
||||
solid_down = has_down and not liquid_down
|
||||
solid_down_left = has_down_left and not liquid_down_left
|
||||
solid_down_right = has_down_right and not liquid_down_right
|
||||
|
||||
tile_id = 0
|
||||
if color == DIRT:
|
||||
if solid_left and solid_right and solid_down and solid_down_left and solid_down_right:
|
||||
if left == GRASS:
|
||||
tile_id = 132
|
||||
elif right == GRASS:
|
||||
tile_id = 133
|
||||
elif left == GRASS2:
|
||||
tile_id = 134
|
||||
elif right == GRASS2:
|
||||
tile_id = 135
|
||||
else:
|
||||
tile_id = self.rand(136, 159)
|
||||
elif solid_left and solid_right and solid_down and solid_down_left and liquid_down_right:
|
||||
tile_id = 101 if water_down_right else 103 if lava_down_right else 0
|
||||
elif solid_left and solid_right and solid_down and solid_down_right and liquid_down_left:
|
||||
tile_id = 100 if water_down_left else 102 if lava_down_left else 0
|
||||
elif solid_left and solid_right and solid_down and has_down_left and not has_down_right:
|
||||
tile_id = 117 if water_right else 119 if lava_right else 86
|
||||
elif solid_left and solid_right and solid_down and not has_down_left and has_down_right:
|
||||
tile_id = 116 if water_left else 118 if lava_left else 85
|
||||
elif solid_left and solid_down:
|
||||
if water_right:
|
||||
tile_id = 117 if water_up_right else 129
|
||||
elif lava_right:
|
||||
tile_id = 119 if lava_up_right else 131
|
||||
else:
|
||||
tile_id = 87
|
||||
elif solid_right and solid_down:
|
||||
if water_left:
|
||||
tile_id = 116 if water_up_left else 128
|
||||
elif lava_left:
|
||||
tile_id = 118 if lava_up_left else 130
|
||||
else:
|
||||
tile_id = 84
|
||||
elif solid_left and solid_right:
|
||||
tile_id = self.rand(21, 22) if water_down else self.rand(25, 26) if lava_down else self.rand(5, 6)
|
||||
elif solid_left:
|
||||
tile_id = 23 if water_down else 27 if lava_down else 7
|
||||
elif solid_right:
|
||||
tile_id = 20 if water_down else 24 if lava_down else 4
|
||||
elif color == GRASS:
|
||||
if has_left and has_right and not liquid_left and not liquid_right:
|
||||
tile_id = self.rand(81, 82)
|
||||
elif has_left and not liquid_left:
|
||||
tile_id = 109 if water_up_right else 111 if lava_up_right else 113 if water_right else 115 if lava_right else 83
|
||||
elif water_up_left:
|
||||
tile_id = 108
|
||||
elif lava_up_left:
|
||||
tile_id = 110
|
||||
elif water_left:
|
||||
tile_id = 112
|
||||
elif lava_left:
|
||||
tile_id = 114
|
||||
else:
|
||||
tile_id = 80
|
||||
elif color == GRASS2:
|
||||
if has_left and has_right and not liquid_left and not liquid_right:
|
||||
tile_id = self.rand(97, 98)
|
||||
elif has_left and not liquid_left:
|
||||
tile_id = 121 if water_up_right else 123 if lava_up_right else 125 if water_right else 127 if lava_right else 99
|
||||
elif water_up_left:
|
||||
tile_id = 120
|
||||
elif lava_up_left:
|
||||
tile_id = 122
|
||||
elif water_left:
|
||||
tile_id = 124
|
||||
elif lava_left:
|
||||
tile_id = 126
|
||||
else:
|
||||
tile_id = 96
|
||||
elif color == WATER:
|
||||
tile_id = 8 if self.get_pixel(self.overlay_pixels, col, row - 1) == WHITE and self.get_pixel(self.level_pixels, col, row - 1) != DIRT else 12
|
||||
elif color == LAVA:
|
||||
tile_id = 9 if self.get_pixel(self.overlay_pixels, col, row - 1) == WHITE and self.get_pixel(self.level_pixels, col, row - 1) != DIRT else 13
|
||||
elif color == WOODEN_CRATE:
|
||||
tile_id = 52
|
||||
elif color == STEEL_CRATE:
|
||||
tile_id = 53
|
||||
|
||||
if self.get_pixel(self.level_pixels, col, row) == WHITE and tile_id == 0 and not overlay_pass and down in (DIRT, GRASS):
|
||||
self.object_counter -= 1
|
||||
if self.object_counter <= 0 and down_left != 0 and down_right != 0:
|
||||
tile_id = self.rand(68, 75)
|
||||
self.object_counter = self.rand(5, 11)
|
||||
return tile_id
|
||||
|
||||
def solid_at(self, col: int, row: int) -> bool:
|
||||
if col < 0 or row < 0 or col >= self.width or row >= self.height:
|
||||
return True
|
||||
color = self.get_layer_tile(col, row, include_objects=True)
|
||||
return color in (GRASS, GRASS2, DIRT, WOODEN_CRATE, STEEL_CRATE)
|
||||
|
||||
def solid_at_pixel(self, x: float, y: float) -> bool:
|
||||
return self.solid_at(int(x) // config.BLOCK_SIZE, int(y) // config.BLOCK_SIZE)
|
||||
|
||||
def break_tile_at_pixel(self, x: float, y: float) -> None:
|
||||
col = int(x) // config.BLOCK_SIZE
|
||||
row = int(y) // config.BLOCK_SIZE
|
||||
if 0 <= col < self.width and 0 <= row < self.height and self.get_layer_tile(col, row, include_objects=True) == WOODEN_CRATE:
|
||||
self.tiles[row][col] = 0
|
||||
self.level_pixels.set_at((col, row), (255, 255, 255, 255))
|
||||
|
||||
def solid_rects_near(self, rect: pygame.Rect) -> list[pygame.Rect]:
|
||||
left = max(0, rect.left // config.BLOCK_SIZE - 1)
|
||||
right = min(self.width - 1, rect.right // config.BLOCK_SIZE + 1)
|
||||
top = max(0, rect.top // config.BLOCK_SIZE - 1)
|
||||
bottom = min(self.height - 1, rect.bottom // config.BLOCK_SIZE + 1)
|
||||
rects: list[pygame.Rect] = []
|
||||
for row in range(top, bottom + 1):
|
||||
for col in range(left, right + 1):
|
||||
if self.solid_at(col, row):
|
||||
rects.append(pygame.Rect(col * config.BLOCK_SIZE, row * config.BLOCK_SIZE, config.BLOCK_SIZE, config.BLOCK_SIZE))
|
||||
return rects
|
||||
|
||||
def draw(self, surface: pygame.Surface, camera: Camera) -> None:
|
||||
start_col = max(0, int(camera.x) // config.BLOCK_SIZE)
|
||||
end_col = min(self.width, (int(camera.x) + config.WIDTH) // config.BLOCK_SIZE + 2)
|
||||
start_row = max(0, int(camera.y) // config.BLOCK_SIZE)
|
||||
end_row = min(self.height, (int(camera.y) + config.HEIGHT) // config.BLOCK_SIZE + 2)
|
||||
for row in range(start_row, end_row):
|
||||
for col in range(start_col, end_col):
|
||||
tile_id = self.tiles[row][col]
|
||||
if 0 < tile_id < len(self.tile_frames):
|
||||
surface.blit(
|
||||
self.tile_frames[tile_id],
|
||||
(col * config.BLOCK_SIZE - round(camera.x), row * config.BLOCK_SIZE - round(camera.y)),
|
||||
)
|
||||
Reference in New Issue
Block a user