From 9c8a352ecd3f46188400807fd070536e38aa2c37 Mon Sep 17 00:00:00 2001 From: Charles Peyrat Date: Fri, 23 Oct 2020 17:46:44 +0200 Subject: [PATCH 01/16] Removed unwanted code from mapdisplay.MapDisplay; the player should be drawn as a map entity, not on its own --- dungeonbattle/mapdisplay.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/dungeonbattle/mapdisplay.py b/dungeonbattle/mapdisplay.py index 2e3b8cc..52d63c1 100644 --- a/dungeonbattle/mapdisplay.py +++ b/dungeonbattle/mapdisplay.py @@ -4,16 +4,14 @@ from dungeonbattle.interfaces import Map class MapDisplay: - def __init__(self, m: Map, player): + def __init__(self, m: Map): self.map = m self.pad = curses.newpad(m.height, m.width+1) - self.player = player def update_pad(self): self.pad.addstr(0, 0, self.map.draw_string()) for e in self.map.entities: self.pad.addch(e.y, e.x, e.img) - self.pad.addstr(self.player.getPosY(), self.player.getPosX(), '🐿') def display(self, y, x): deltay, deltax = (curses.LINES // 2) + 1, (curses.COLS //2) + 1 From f3bc3101f06e578755a37e6268a3a9433aa97b1c Mon Sep 17 00:00:00 2001 From: Charles Peyrat Date: Fri, 23 Oct 2020 18:01:39 +0200 Subject: [PATCH 02/16] Code refactoring in game.Game --- dungeonbattle/game.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/dungeonbattle/game.py b/dungeonbattle/game.py index 9efd63f..dd2e6ef 100644 --- a/dungeonbattle/game.py +++ b/dungeonbattle/game.py @@ -8,25 +8,29 @@ class Game: def init(self) -> None: Game.INSTANCE = self - with TermManager() as term_manager: - self._start_game(term_manager.screen) + self.key_handler = player_move - def _start_game(self, screen): - # TODO Generate map, or make the possibility to load another one + def new_game(self): + # TODO generate a new map procedurally m = Map.load("example_map.txt") - player = Player() - self.player = player - player.y = 1 - player.x = 6 - d = MapDisplay(m, player) + self.player = Player() + self.player.y = 1 + self.player.x = 6 + + @classmethod + def load_game(filename): + # TODO loading map from a file + raise NotImplementedError() + + def run(self, screen): while True: screen.clear() screen.refresh() d.display(player.getPosY(), player.getPosX()) key = screen.getkey() - self.handle_key_pressed(key) + self.key_handler(key) - def handle_key_pressed(self, key): + def player_move(self, key): # TODO load keys from settings if key == 'z' or key == 'KEY_UP': self.player.move_up() From bbbcb1b10e10fc01e5a409e151ddc6279514565c Mon Sep 17 00:00:00 2001 From: Nicolas Margulies Date: Fri, 6 Nov 2020 14:23:12 +0100 Subject: [PATCH 03/16] =?UTF-8?q?Corrig=C3=A9=20la=20merde=20de=20Charles?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dungeonbattle/game.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/dungeonbattle/game.py b/dungeonbattle/game.py index dd2e6ef..ba61fe6 100644 --- a/dungeonbattle/game.py +++ b/dungeonbattle/game.py @@ -8,11 +8,12 @@ class Game: def init(self) -> None: Game.INSTANCE = self - self.key_handler = player_move + self.key_handler = self.player_move def new_game(self): # TODO generate a new map procedurally - m = Map.load("example_map.txt") + self.m = Map.load("example_map.txt") + self.d = MapDisplay(self.m) self.player = Player() self.player.y = 1 self.player.x = 6 @@ -26,7 +27,7 @@ class Game: while True: screen.clear() screen.refresh() - d.display(player.getPosY(), player.getPosX()) + self.d.display(self.player.getPosY(), self.player.getPosX()) key = screen.getkey() self.key_handler(key) From dadd1fd146a5812ea47a28ecc2bf893db1ca7d97 Mon Sep 17 00:00:00 2001 From: eichhornchen Date: Fri, 6 Nov 2020 14:32:05 +0100 Subject: [PATCH 04/16] added texture pack --- dungeonbattle/texturepack.py | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 dungeonbattle/texturepack.py diff --git a/dungeonbattle/texturepack.py b/dungeonbattle/texturepack.py new file mode 100644 index 0000000..679f72e --- /dev/null +++ b/dungeonbattle/texturepack.py @@ -0,0 +1,8 @@ +#This is the base ascii texturepack + +textures = { + "EMPTY" : ' ' + "WALL" : '#' + "FLOOR" : '.' + "PLAYER" : '@' + } From bfce9487c57b3b6f7ba166f367c5c4b030412a02 Mon Sep 17 00:00:00 2001 From: eichhornchen Date: Fri, 6 Nov 2020 14:41:05 +0100 Subject: [PATCH 05/16] repaired the texture pack --- dungeonbattle/texturepack.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/dungeonbattle/texturepack.py b/dungeonbattle/texturepack.py index 679f72e..3a4eb99 100644 --- a/dungeonbattle/texturepack.py +++ b/dungeonbattle/texturepack.py @@ -1,8 +1,8 @@ #This is the base ascii texturepack -textures = { - "EMPTY" : ' ' - "WALL" : '#' - "FLOOR" : '.' +ascii = { + "EMPTY" : ' ', + "WALL" : '#', + "FLOOR" : '.', "PLAYER" : '@' } From 60ca00b8fa1b013e4b58df7f8b1298b910c411dc Mon Sep 17 00:00:00 2001 From: Nicolas Margulies Date: Fri, 6 Nov 2020 16:20:07 +0100 Subject: [PATCH 06/16] Generic menu implementation --- dungeonbattle/menus.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 dungeonbattle/menus.py diff --git a/dungeonbattle/menus.py b/dungeonbattle/menus.py new file mode 100644 index 0000000..e188864 --- /dev/null +++ b/dungeonbattle/menus.py @@ -0,0 +1,14 @@ +class Menu: + + def __init__(self, values:list): + self.values = values + self.position = 0 + + def go_up(self): + self.position = max(0, self.position -1) + + def go_down(self): + self.position = min(len(self.values)-1, self.position +1) + + def validate(self): + return self.values[self.position] \ No newline at end of file From 901406351ee2f48d0a6d6f27dabab5e66ffe16e4 Mon Sep 17 00:00:00 2001 From: Nicolas Margulies Date: Fri, 6 Nov 2020 16:40:43 +0100 Subject: [PATCH 07/16] Better menus --- dungeonbattle/menus.py | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/dungeonbattle/menus.py b/dungeonbattle/menus.py index e188864..f2da237 100644 --- a/dungeonbattle/menus.py +++ b/dungeonbattle/menus.py @@ -1,7 +1,11 @@ +from enum import Enum, auto + + class Menu: - def __init__(self, values:list): - self.values = values + values:list + + def __init__(self): self.position = 0 def go_up(self): @@ -11,4 +15,19 @@ class Menu: self.position = min(len(self.values)-1, self.position +1) def validate(self): - return self.values[self.position] \ No newline at end of file + return self.values[self.position] + +class MainMenuValues(Enum): + START = auto() + SETTINGS = auto() + EXIT = auto() + +class MainMenu(Menu): + + values = [ e for e in MainMenuValues ] + +class ArbitraryMenu: + + def __init__(self, values:list): + super().__init__(self) + self.values = values \ No newline at end of file From 6e5cd9084b1a4d9db970a1dbbdf20930c40a870d Mon Sep 17 00:00:00 2001 From: Nicolas Margulies Date: Fri, 6 Nov 2020 17:02:59 +0100 Subject: [PATCH 08/16] Finished cleaning conflicts --- dungeonbattle/game.py | 4 ++-- dungeonbattle/mapdisplay.py | 7 +------ main.py | 7 ++++++- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/dungeonbattle/game.py b/dungeonbattle/game.py index e974e41..77daf35 100644 --- a/dungeonbattle/game.py +++ b/dungeonbattle/game.py @@ -18,10 +18,10 @@ class Game: def new_game(self): # TODO generate a new map procedurally self.m = Map.load("example_map.txt") - self.d = MapDisplay(self.m) self.player = Player() self.player.y = 1 self.player.x = 6 + self.d = MapDisplay(self.m, self.player) @classmethod def load_game(filename): @@ -32,7 +32,7 @@ class Game: while True: screen.clear() screen.refresh() - self.d.display(self.player.getPosY(), self.player.getPosX()) + self.d.display(self.player.y, self.player.x) key = screen.getkey() self.handle_key_pressed(key) diff --git a/dungeonbattle/mapdisplay.py b/dungeonbattle/mapdisplay.py index fb977a0..d61b7e6 100644 --- a/dungeonbattle/mapdisplay.py +++ b/dungeonbattle/mapdisplay.py @@ -5,23 +5,18 @@ from typing import Any from dungeonbattle.interfaces import Map -<<<<<<< HEAD - def __init__(self, m: Map): - self.map = m - self.pad = curses.newpad(m.height, m.width+1) -======= class MapDisplay: def __init__(self, m: Map, player: Any): # TODO Type the player field with the good type self.map = m self.pad = curses.newpad(m.height, m.width + 1) self.player = player ->>>>>>> master def update_pad(self) -> None: self.pad.addstr(0, 0, self.map.draw_string()) for e in self.map.entities: self.pad.addch(e.y, e.x, e.img) + self.pad.addstr(self.player.y, self.player.x, '🐿️') def display(self, y: int, x: int) -> None: deltay, deltax = (curses.LINES // 2) + 1, (curses.COLS // 2) + 1 diff --git a/main.py b/main.py index a3c6f79..56cc77c 100755 --- a/main.py +++ b/main.py @@ -1,5 +1,10 @@ #!/usr/bin/env python from dungeonbattle.game import Game +from dungeonbattle.term_manager import TermManager if __name__ == "__main__": - Game().init() + with TermManager() as term_manager: + game = Game() + game.init() + game.new_game() + game.run(term_manager.screen) From e3a1bf96c2dc23b10bc5d3fa5e4735e082926f8f Mon Sep 17 00:00:00 2001 From: Nicolas Margulies Date: Fri, 6 Nov 2020 17:24:20 +0100 Subject: [PATCH 09/16] Used settings for keys --- dungeonbattle/game.py | 41 ++++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/dungeonbattle/game.py b/dungeonbattle/game.py index 77daf35..c22e0b1 100644 --- a/dungeonbattle/game.py +++ b/dungeonbattle/game.py @@ -4,6 +4,22 @@ from .interfaces import Map from .mapdisplay import MapDisplay from .settings import Settings from .term_manager import TermManager +from enum import Enum, auto + + +class GameMode(Enum): + MAINMENU = auto() + PLAY = auto() + SETTINGS = auto() + INVENTORY = auto() + + +class KeyValues(Enum): + UP = auto() + DOWN = auto() + LEFT = auto() + RIGHT = auto() + ENTER = auto() class Game: @@ -11,6 +27,7 @@ class Game: def init(self) -> None: Game.INSTANCE = self + self.state = GameMode.MAINMENU self.settings = Settings() self.settings.load_settings() self.settings.write_settings() @@ -34,17 +51,27 @@ class Game: screen.refresh() self.d.display(self.player.y, self.player.x) key = screen.getkey() - self.handle_key_pressed(key) + self.handle_key_pressed(self.translate_key(key)) - def handle_key_pressed(self, key: str) -> None: - # TODO load keys from settings - if key == 'z' or key == 'KEY_UP': + def translate_key(self, key:str) -> KeyValues: + if key in (self.settings.KEY_DOWN_SECONDARY, self.settings.KEY_DOWN_PRIMARY): + return KeyValues.DOWN + elif key in (self.settings.KEY_LEFT_PRIMARY, self.settings.KEY_LEFT_SECONDARY): + return KeyValues.LEFT + elif key in (self.settings.KEY_RIGHT_PRIMARY, self.settings.KEY_RIGHT_SECONDARY): + return KeyValues.RIGHT + elif key in (self.settings.KEY_UP_PRIMARY, self.settings.KEY_UP_SECONDARY): + return KeyValues.UP + + def handle_key_pressed(self, key: KeyValues) -> None: + + if key == KeyValues.UP: self.player.move_up() - if key == 's' or key == 'KEY_DOWN': + if key == KeyValues.DOWN: self.player.move_down() - if key == 'q' or key == 'KEY_LEFT': + if key == KeyValues.LEFT: self.player.move_left() - if key == 'd' or key == 'KEY_RIGHT': + if key == KeyValues.RIGHT: self.player.move_right() From 43001f6ede2c86f7b5e38aac1fe0a11bcebe815e Mon Sep 17 00:00:00 2001 From: Nicolas Margulies Date: Fri, 6 Nov 2020 18:06:28 +0100 Subject: [PATCH 10/16] Basic menu handling --- dungeonbattle/game.py | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/dungeonbattle/game.py b/dungeonbattle/game.py index c22e0b1..1541898 100644 --- a/dungeonbattle/game.py +++ b/dungeonbattle/game.py @@ -1,11 +1,12 @@ from typing import Any +import sys from .interfaces import Map from .mapdisplay import MapDisplay from .settings import Settings from .term_manager import TermManager from enum import Enum, auto - +from . import menus class GameMode(Enum): MAINMENU = auto() @@ -28,6 +29,7 @@ class Game: def init(self) -> None: Game.INSTANCE = self self.state = GameMode.MAINMENU + self.main_menu = menus.MainMenu() self.settings = Settings() self.settings.load_settings() self.settings.write_settings() @@ -40,7 +42,7 @@ class Game: self.player.x = 6 self.d = MapDisplay(self.m, self.player) - @classmethod + @staticmethod def load_game(filename): # TODO loading map from a file raise NotImplementedError() @@ -64,15 +66,28 @@ class Game: return KeyValues.UP def handle_key_pressed(self, key: KeyValues) -> None: - - if key == KeyValues.UP: - self.player.move_up() - if key == KeyValues.DOWN: - self.player.move_down() - if key == KeyValues.LEFT: - self.player.move_left() - if key == KeyValues.RIGHT: - self.player.move_right() + if self.state == GameMode.PLAY: + if key == KeyValues.UP: + self.player.move_up() + if key == KeyValues.DOWN: + self.player.move_down() + if key == KeyValues.LEFT: + self.player.move_left() + if key == KeyValues.RIGHT: + self.player.move_right() + if self.state == GameMode.MAINMENU: + if key == KeyValues.DOWN: + self.main_menu.go_down() + if key == KeyValues.UP: + self.main_menu.go_up() + if key == KeyValues.ENTER: + option = self.main_menu.validate() + if option == menus.MainMenuValues.START: + self.state = GameMode.PLAY + elif option == menus.MainMenuValues.SETTINGS: + self.state = GameMode.SETTINGS + elif option == menus.MainMenuValues.EXIT: + sys.exit(0) class Player: From 8d9b5166b73a06c083156d323447e557af1057a2 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Fri, 6 Nov 2020 18:11:59 +0100 Subject: [PATCH 11/16] Linting --- dungeonbattle/game.py | 24 ++++++++++++++---------- dungeonbattle/menus.py | 30 +++++++++++++++--------------- dungeonbattle/texturepack.py | 12 ++++++------ 3 files changed, 35 insertions(+), 31 deletions(-) diff --git a/dungeonbattle/game.py b/dungeonbattle/game.py index 1541898..1466dfd 100644 --- a/dungeonbattle/game.py +++ b/dungeonbattle/game.py @@ -1,13 +1,13 @@ +import sys from typing import Any -import sys from .interfaces import Map from .mapdisplay import MapDisplay from .settings import Settings -from .term_manager import TermManager from enum import Enum, auto from . import menus + class GameMode(Enum): MAINMENU = auto() PLAY = auto() @@ -34,7 +34,7 @@ class Game: self.settings.load_settings() self.settings.write_settings() - def new_game(self): + def new_game(self) -> None: # TODO generate a new map procedurally self.m = Map.load("example_map.txt") self.player = Player() @@ -43,11 +43,11 @@ class Game: self.d = MapDisplay(self.m, self.player) @staticmethod - def load_game(filename): + def load_game(filename: str) -> None: # TODO loading map from a file raise NotImplementedError() - def run(self, screen): + def run(self, screen: Any) -> None: while True: screen.clear() screen.refresh() @@ -55,14 +55,18 @@ class Game: key = screen.getkey() self.handle_key_pressed(self.translate_key(key)) - def translate_key(self, key:str) -> KeyValues: - if key in (self.settings.KEY_DOWN_SECONDARY, self.settings.KEY_DOWN_PRIMARY): + def translate_key(self, key: str) -> KeyValues: + if key in (self.settings.KEY_DOWN_SECONDARY, + self.settings.KEY_DOWN_PRIMARY): return KeyValues.DOWN - elif key in (self.settings.KEY_LEFT_PRIMARY, self.settings.KEY_LEFT_SECONDARY): + elif key in (self.settings.KEY_LEFT_PRIMARY, + self.settings.KEY_LEFT_SECONDARY): return KeyValues.LEFT - elif key in (self.settings.KEY_RIGHT_PRIMARY, self.settings.KEY_RIGHT_SECONDARY): + elif key in (self.settings.KEY_RIGHT_PRIMARY, + self.settings.KEY_RIGHT_SECONDARY): return KeyValues.RIGHT - elif key in (self.settings.KEY_UP_PRIMARY, self.settings.KEY_UP_SECONDARY): + elif key in (self.settings.KEY_UP_PRIMARY, + self.settings.KEY_UP_SECONDARY): return KeyValues.UP def handle_key_pressed(self, key: KeyValues) -> None: diff --git a/dungeonbattle/menus.py b/dungeonbattle/menus.py index f2da237..904fa17 100644 --- a/dungeonbattle/menus.py +++ b/dungeonbattle/menus.py @@ -2,32 +2,32 @@ from enum import Enum, auto class Menu: - - values:list + values: list def __init__(self): self.position = 0 - - def go_up(self): - self.position = max(0, self.position -1) - - def go_down(self): - self.position = min(len(self.values)-1, self.position +1) - - def validate(self): + + def go_up(self) -> None: + self.position = max(0, self.position - 1) + + def go_down(self) -> None: + self.position = min(len(self.values) - 1, self.position + 1) + + def validate(self) -> None: return self.values[self.position] + class MainMenuValues(Enum): START = auto() SETTINGS = auto() EXIT = auto() -class MainMenu(Menu): - values = [ e for e in MainMenuValues ] +class MainMenu(Menu): + values = [e for e in MainMenuValues] + class ArbitraryMenu: - - def __init__(self, values:list): + def __init__(self, values: list): super().__init__(self) - self.values = values \ No newline at end of file + self.values = values diff --git a/dungeonbattle/texturepack.py b/dungeonbattle/texturepack.py index 3a4eb99..d9b5818 100644 --- a/dungeonbattle/texturepack.py +++ b/dungeonbattle/texturepack.py @@ -1,8 +1,8 @@ -#This is the base ascii texturepack +# This is the base ascii texturepack ascii = { - "EMPTY" : ' ', - "WALL" : '#', - "FLOOR" : '.', - "PLAYER" : '@' - } + "EMPTY": ' ', + "WALL": '#', + "FLOOR": '.', + "PLAYER": '@' +} From 60d6c7509a3079e2b5e9c902d3f034713ee1a11c Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Fri, 6 Nov 2020 18:25:02 +0100 Subject: [PATCH 12/16] Bind Return key to menus --- dungeonbattle/game.py | 2 ++ dungeonbattle/menus.py | 3 ++- dungeonbattle/settings.py | 2 ++ 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/dungeonbattle/game.py b/dungeonbattle/game.py index 1466dfd..0787f9c 100644 --- a/dungeonbattle/game.py +++ b/dungeonbattle/game.py @@ -68,6 +68,8 @@ class Game: elif key in (self.settings.KEY_UP_PRIMARY, self.settings.KEY_UP_SECONDARY): return KeyValues.UP + elif key == self.settings.KEY_ENTER: + return KeyValues.ENTER def handle_key_pressed(self, key: KeyValues) -> None: if self.state == GameMode.PLAY: diff --git a/dungeonbattle/menus.py b/dungeonbattle/menus.py index 904fa17..66dc199 100644 --- a/dungeonbattle/menus.py +++ b/dungeonbattle/menus.py @@ -1,4 +1,5 @@ from enum import Enum, auto +from typing import Any class Menu: @@ -13,7 +14,7 @@ class Menu: def go_down(self) -> None: self.position = min(len(self.values) - 1, self.position + 1) - def validate(self) -> None: + def validate(self) -> Any: return self.values[self.position] diff --git a/dungeonbattle/settings.py b/dungeonbattle/settings.py index 5c80b7c..db216df 100644 --- a/dungeonbattle/settings.py +++ b/dungeonbattle/settings.py @@ -27,6 +27,8 @@ class Settings: ['d', 'Touche principale pour aller vers la droite'] self.KEY_RIGHT_SECONDARY = \ ['KEY_RIGHT', 'Touche secondaire pour aller vers la droite'] + self.KEY_ENTER = \ + ['\n', 'Touche pour valider un menu'] self.TEXTURE_PACK = ['ASCII', 'Pack de textures utilisé'] def __getattribute__(self, item: str) -> Any: From 30a01108b802dbce648d93d65fff3bd171174b31 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Fri, 6 Nov 2020 18:32:51 +0100 Subject: [PATCH 13/16] Test main menu scrolling --- dungeonbattle/menus.py | 4 ++-- dungeonbattle/tests/menus_test.py | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 dungeonbattle/tests/menus_test.py diff --git a/dungeonbattle/menus.py b/dungeonbattle/menus.py index 66dc199..82d6d4e 100644 --- a/dungeonbattle/menus.py +++ b/dungeonbattle/menus.py @@ -28,7 +28,7 @@ class MainMenu(Menu): values = [e for e in MainMenuValues] -class ArbitraryMenu: +class ArbitraryMenu(Menu): def __init__(self, values: list): - super().__init__(self) + super().__init__() self.values = values diff --git a/dungeonbattle/tests/menus_test.py b/dungeonbattle/tests/menus_test.py new file mode 100644 index 0000000..6ad9df7 --- /dev/null +++ b/dungeonbattle/tests/menus_test.py @@ -0,0 +1,24 @@ +import unittest + +from dungeonbattle.menus import ArbitraryMenu, MainMenu, MainMenuValues + + +class TestMenus(unittest.TestCase): + def test_scroll_menu(self) -> None: + """ + Test to scroll the menu. + """ + arbitrary_menu = ArbitraryMenu([]) + self.assertEqual(arbitrary_menu.position, 0) + + main_menu = MainMenu() + self.assertEqual(main_menu.position, 0) + self.assertEqual(main_menu.validate(), MainMenuValues.START) + main_menu.go_up() + self.assertEqual(main_menu.validate(), MainMenuValues.START) + main_menu.go_down() + self.assertEqual(main_menu.validate(), MainMenuValues.SETTINGS) + main_menu.go_down() + self.assertEqual(main_menu.validate(), MainMenuValues.EXIT) + main_menu.go_down() + self.assertEqual(main_menu.validate(), MainMenuValues.EXIT) From a1731dc9f72d49eca6d3b13412f7684546d034fc Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Fri, 6 Nov 2020 18:38:10 +0100 Subject: [PATCH 14/16] Default coordinates are (1, 6) --- dungeonbattle/game.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dungeonbattle/game.py b/dungeonbattle/game.py index 687dfaf..8fdbfd4 100644 --- a/dungeonbattle/game.py +++ b/dungeonbattle/game.py @@ -39,7 +39,7 @@ class Game: # TODO generate a new map procedurally self.m = Map.load("example_map.txt") self.player = Player() - self.player.move(y, x) + self.player.move(1, 6) self.m.add_entity(self.player) self.d = MapDisplay(self.m, self.player) From dc0d478d1ebfadccb4f49bbe73fa10df424c52f8 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Fri, 6 Nov 2020 18:39:55 +0100 Subject: [PATCH 15/16] init -> __init__ --- dungeonbattle/game.py | 5 +---- main.py | 1 - 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/dungeonbattle/game.py b/dungeonbattle/game.py index 8fdbfd4..b1e8bc6 100644 --- a/dungeonbattle/game.py +++ b/dungeonbattle/game.py @@ -25,10 +25,7 @@ class KeyValues(Enum): class Game: - INSTANCE = None - - def init(self) -> None: - Game.INSTANCE = self + def __init__(self) -> None: self.state = GameMode.MAINMENU self.main_menu = menus.MainMenu() self.settings = Settings() diff --git a/main.py b/main.py index 56cc77c..2e44d9d 100755 --- a/main.py +++ b/main.py @@ -5,6 +5,5 @@ from dungeonbattle.term_manager import TermManager if __name__ == "__main__": with TermManager() as term_manager: game = Game() - game.init() game.new_game() game.run(term_manager.screen) From c97a9a2e6c6c5a1a0fa1a5ba47fd79a69ce05418 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Fri, 6 Nov 2020 19:12:56 +0100 Subject: [PATCH 16/16] Test some functions in the game --- dungeonbattle/game.py | 4 +- dungeonbattle/mapdisplay.py | 5 +- dungeonbattle/tests/game_test.py | 97 ++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+), 4 deletions(-) create mode 100644 dungeonbattle/tests/game_test.py diff --git a/dungeonbattle/game.py b/dungeonbattle/game.py index b1e8bc6..e0e707d 100644 --- a/dungeonbattle/game.py +++ b/dungeonbattle/game.py @@ -32,13 +32,13 @@ class Game: self.settings.load_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 self.m = Map.load("example_map.txt") self.player = Player() self.player.move(1, 6) self.m.add_entity(self.player) - self.d = MapDisplay(self.m, self.player) + self.d = MapDisplay(self.m, self.player, init_pad) @staticmethod def load_game(filename: str) -> None: diff --git a/dungeonbattle/mapdisplay.py b/dungeonbattle/mapdisplay.py index fdc87ba..095b7cb 100644 --- a/dungeonbattle/mapdisplay.py +++ b/dungeonbattle/mapdisplay.py @@ -6,10 +6,11 @@ from dungeonbattle.interfaces import Map class MapDisplay: - def __init__(self, m: Map, player: Player): + def __init__(self, m: Map, player: Player, init_pad: bool = True): self.map = m - self.pad = curses.newpad(m.height, m.width + 1) self.player = player + if init_pad: + self.pad = curses.newpad(m.height, m.width + 1) def update_pad(self) -> None: self.pad.addstr(0, 0, self.map.draw_string()) diff --git a/dungeonbattle/tests/game_test.py b/dungeonbattle/tests/game_test.py new file mode 100644 index 0000000..4183cbe --- /dev/null +++ b/dungeonbattle/tests/game_test.py @@ -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)