roguepy/game.py

122 lines
3.9 KiB
Python
Raw Normal View History

2023-03-01 19:47:24 +00:00
from terminal import Terminal
2023-03-13 18:46:29 +00:00
from entity import Creature, Player, Enemy
2023-03-01 19:47:24 +00:00
from actions import *
from floor import Floor, TileType
2023-03-13 18:46:29 +00:00
import sys
2023-03-01 19:47:24 +00:00
TEXTURES = {
TileType.WALL: '#',
TileType.AIR: '.',
Player: '@'
}
class Game:
def __init__(self):
self.term = Terminal()
self.player = Player()
self.floor = Floor(20, 10)
self.should_exit = False
2023-03-02 16:51:17 +00:00
self.ticks = 0
self.schedule = {}
2023-03-13 18:46:29 +00:00
self.redirect_io('prints.log', 'errors.log')
def __del__(self):
self.restore_io()
def restore_io(self) -> None:
sys.stdout = self.stdout_original
sys.stderr = self.stderr_original
self.stdout_file.close()
self.stderr_file.close()
def redirect_io(self, stdout_file, stderr_file) -> None:
self.stdout_original = sys.stdout
self.stderr_original = sys.stderr
self.stdout_file = open(stdout_file, 'w')
self.stderr_file = open(stderr_file, 'w')
sys.stdout = self.stdout_file
sys.stderr = self.stderr_file
2023-03-01 19:47:24 +00:00
def run(self):
"""
Runs the game
"""
2023-03-13 18:46:29 +00:00
self.instance_creature(3, 3, self.player)
2023-03-02 17:08:02 +00:00
self.render()
2023-03-01 19:47:24 +00:00
while not self.should_exit:
2023-03-13 18:46:29 +00:00
self.step()
if self.action_was_performed:
2023-03-02 17:08:02 +00:00
self.render()
2023-03-02 16:51:17 +00:00
2023-03-13 18:46:29 +00:00
def instance_creature(self, x, y, creature: Creature):
2023-03-02 17:00:42 +00:00
"""
2023-03-13 18:46:29 +00:00
Instances a creature in space and time
2023-03-02 17:00:42 +00:00
"""
2023-03-13 18:46:29 +00:00
self.floor.entities.add_creature(x, y, creature)
self.reschedule(creature)
2023-03-02 16:51:17 +00:00
2023-03-13 18:46:29 +00:00
def reschedule(self, creature: Creature):
2023-03-02 16:51:17 +00:00
"""
2023-03-13 18:46:29 +00:00
Calculates the ticks in which the creature will perform its next action
2023-03-02 16:51:17 +00:00
"""
2023-03-13 18:46:29 +00:00
time = (ACTION_TIMING[creature.action.__class__] // creature.speed) + self.ticks
2023-03-02 16:51:17 +00:00
if self.schedule.get(time):
2023-03-13 18:46:29 +00:00
self.schedule[time].append(creature)
2023-03-02 16:51:17 +00:00
else:
2023-03-13 18:46:29 +00:00
self.schedule[time] = [creature]
2023-03-01 19:47:24 +00:00
def render(self):
"""
Renders the game
"""
for x in range(self.floor.width):
for y in range(self.floor.height):
2023-03-02 16:51:17 +00:00
if (x, y) in self.floor.entities.pos_creatures:
2023-03-01 19:47:24 +00:00
self.term.put_char(x, y, TEXTURES[Player])
2023-03-02 16:51:17 +00:00
elif (x, y) in self.floor.entities.items:
2023-03-01 19:47:24 +00:00
self.term.put_char(x, y, 'i')
else:
self.term.put_char(x, y, TEXTURES[self.floor.get_tile(x, y)])
def step(self):
2023-03-02 17:08:02 +00:00
"""
2023-03-13 18:46:29 +00:00
Perfoms a step in the game. Sets the `action_was_performed` attribute accordingly
2023-03-02 17:08:02 +00:00
"""
2023-03-13 18:46:29 +00:00
self.action_was_performed = False
creatures = self.schedule.get(self.ticks)
if creatures:
for creature in creatures:
if isinstance(creature, Player):
self.input_event()
elif isinstance(creature, Enemy):
creature.calculate_action()
self.reschedule(creature)
self.perform(creature)
2023-03-02 16:51:17 +00:00
self.schedule.pop(self.ticks)
2023-03-13 18:46:29 +00:00
self.action_was_performed = True
self.ticks += 1
2023-03-02 17:08:02 +00:00
2023-03-13 18:46:29 +00:00
def perform(self, creature: Creature):
2023-03-01 19:47:24 +00:00
"""
2023-03-02 16:51:17 +00:00
Performs an action for a creature
2023-03-01 19:47:24 +00:00
"""
2023-03-13 18:46:29 +00:00
if isinstance(creature.action, Move):
pos = self.floor.entities.pop_creature_ref(creature)
2023-03-02 16:51:17 +00:00
if pos:
2023-03-13 18:46:29 +00:00
self.floor.entities.add_creature(pos[0] + creature.action.x, pos[1] + creature.action.y, creature)
2023-03-01 19:47:24 +00:00
2023-03-13 18:46:29 +00:00
def input_event(self):
2023-03-01 19:47:24 +00:00
"""
2023-03-13 18:46:29 +00:00
Waits for a game event to happen, and behaves accordingly
2023-03-01 19:47:24 +00:00
"""
keycode = self.term.get_key()
if keycode == "q":
self.should_exit = True
elif keycode == "KEY_RIGHT":
self.player.action = Move(1, 0)
elif keycode == "KEY_LEFT":
self.player.action = Move(-1, 0)
elif keycode == "KEY_DOWN":
self.player.action = Move(0, 1)
elif keycode == "KEY_UP":
self.player.action = Move(0, -1)