Test some functions in the game

This commit is contained in:
Yohann D'ANELLO 2020-11-06 19:12:56 +01:00
parent dc0d478d1e
commit c97a9a2e6c
3 changed files with 102 additions and 4 deletions

View File

@ -32,13 +32,13 @@ class Game:
self.settings.load_settings() self.settings.load_settings()
self.settings.write_settings() self.settings.write_settings()
def new_game(self) -> None: def new_game(self, init_pad: bool = True) -> None:
# TODO generate a new map procedurally # TODO generate a new map procedurally
self.m = Map.load("example_map.txt") self.m = Map.load("example_map.txt")
self.player = Player() self.player = Player()
self.player.move(1, 6) self.player.move(1, 6)
self.m.add_entity(self.player) self.m.add_entity(self.player)
self.d = MapDisplay(self.m, self.player) self.d = MapDisplay(self.m, self.player, init_pad)
@staticmethod @staticmethod
def load_game(filename: str) -> None: def load_game(filename: str) -> None:

View File

@ -6,10 +6,11 @@ from dungeonbattle.interfaces import Map
class MapDisplay: class MapDisplay:
def __init__(self, m: Map, player: Player): def __init__(self, m: Map, player: Player, init_pad: bool = True):
self.map = m self.map = m
self.pad = curses.newpad(m.height, m.width + 1)
self.player = player self.player = player
if init_pad:
self.pad = curses.newpad(m.height, m.width + 1)
def update_pad(self) -> None: def update_pad(self) -> None:
self.pad.addstr(0, 0, self.map.draw_string()) self.pad.addstr(0, 0, self.map.draw_string())

View File

@ -0,0 +1,97 @@
import unittest
from dungeonbattle.game import Game, KeyValues, GameMode
from dungeonbattle.menus import MainMenuValues
class TestGame(unittest.TestCase):
def setUp(self) -> None:
"""
Setup game.
"""
self.game = Game()
self.game.new_game(False)
def test_load_game(self) -> None:
self.assertRaises(NotImplementedError, Game.load_game, "game.save")
def test_key_translation(self) -> None:
"""
Test key bindings.
"""
self.assertEqual(self.game.translate_key(
self.game.settings.KEY_UP_PRIMARY), KeyValues.UP)
self.assertEqual(self.game.translate_key(
self.game.settings.KEY_UP_SECONDARY), KeyValues.UP)
self.assertEqual(self.game.translate_key(
self.game.settings.KEY_DOWN_PRIMARY), KeyValues.DOWN)
self.assertEqual(self.game.translate_key(
self.game.settings.KEY_DOWN_SECONDARY), KeyValues.DOWN)
self.assertEqual(self.game.translate_key(
self.game.settings.KEY_LEFT_PRIMARY), KeyValues.LEFT)
self.assertEqual(self.game.translate_key(
self.game.settings.KEY_LEFT_SECONDARY), KeyValues.LEFT)
self.assertEqual(self.game.translate_key(
self.game.settings.KEY_RIGHT_PRIMARY), KeyValues.RIGHT)
self.assertEqual(self.game.translate_key(
self.game.settings.KEY_RIGHT_SECONDARY), KeyValues.RIGHT)
self.assertEqual(self.game.translate_key(
self.game.settings.KEY_ENTER), KeyValues.ENTER)
def test_key_press(self) -> None:
"""
Press a key and see what is done.
"""
self.assertEqual(self.game.state, GameMode.MAINMENU)
self.assertEqual(self.game.main_menu.validate(),
MainMenuValues.START)
self.game.handle_key_pressed(KeyValues.UP)
self.assertEqual(self.game.main_menu.validate(),
MainMenuValues.START)
self.game.handle_key_pressed(KeyValues.DOWN)
self.assertEqual(self.game.main_menu.validate(),
MainMenuValues.SETTINGS)
self.game.handle_key_pressed(KeyValues.ENTER)
self.assertEqual(self.game.state, GameMode.SETTINGS)
self.game.state = GameMode.MAINMENU
self.game.handle_key_pressed(KeyValues.DOWN)
self.assertEqual(self.game.main_menu.validate(),
MainMenuValues.EXIT)
self.assertRaises(SystemExit, self.game.handle_key_pressed,
KeyValues.ENTER)
self.game.handle_key_pressed(KeyValues.UP)
self.assertEqual(self.game.main_menu.validate(),
MainMenuValues.SETTINGS)
self.game.handle_key_pressed(KeyValues.UP)
self.assertEqual(self.game.main_menu.validate(),
MainMenuValues.START)
self.game.handle_key_pressed(KeyValues.ENTER)
self.assertEqual(self.game.state, GameMode.PLAY)
y, x = self.game.player.y, self.game.player.x
self.game.handle_key_pressed(KeyValues.DOWN)
new_y, new_x = self.game.player.y, self.game.player.x
self.assertEqual(new_y, y + 1)
self.assertEqual(new_x, x)
y, x = new_y, new_x
self.game.handle_key_pressed(KeyValues.RIGHT)
new_y, new_x = self.game.player.y, self.game.player.x
self.assertEqual(new_y, y)
self.assertEqual(new_x, x + 1)
y, x = self.game.player.y, self.game.player.x
self.game.handle_key_pressed(KeyValues.UP)
new_y, new_x = self.game.player.y, self.game.player.x
self.assertEqual(new_y, y - 1)
self.assertEqual(new_x, x)
y, x = self.game.player.y, self.game.player.x
self.game.handle_key_pressed(KeyValues.LEFT)
new_y, new_x = self.game.player.y, self.game.player.x
self.assertEqual(new_y, y)
self.assertEqual(new_x, x - 1)