From 8eb5e7dc31d4de1b5596f3c55bde5a540562d2f8 Mon Sep 17 00:00:00 2001 From: luxick Date: Wed, 26 Aug 2015 15:25:58 +0200 Subject: [PATCH] moved interface to its own class, minor interface updates --- cli.py | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++ config.py | 4 ++- kinggotchi.py | 5 +--- test.py | 50 ++++++------------------------------- 4 files changed, 80 insertions(+), 48 deletions(-) create mode 100644 cli.py diff --git a/cli.py b/cli.py new file mode 100644 index 0000000..9e7a331 --- /dev/null +++ b/cli.py @@ -0,0 +1,69 @@ +#!/usr/bin/python +# coding=UTF-8 + +from config import * +import curses +import time + +class cursesUI: + kingdomview = True + villagenr = 0 + percentage = True + running = True + + def __init__(self,start_paused): + self.kingdomview = True + self.villagenr = 0 + self.percentage = True + self.running = True + self.paused = start_paused + + self.stdscr = curses.initscr() + curses.noecho() + curses.curs_set(0) + self.stdscr.keypad(1) + self.stdscr.nodelay(1) + + height, width = self.stdscr.getmaxyx() + self.win = curses.newpad(16383, width) + + def build_screen(self, king, ticks): + + height, width = self.stdscr.getmaxyx() + self.win.move(0, 0) + self.win.addstr(0, 0, 'Tamagotchi Colony (alpha) - currently at tick '+str(ticks)+'.') + + if self.kingdomview: + self.win.addstr(1, 0, '-----------------------------------') + self.win.addstr(2, 0, '[q]Exit [p]Pause [v]Village View') + self.win.addstr(3, 0, '-----------------------------------') + self.win.addstr(5, 0, king.show_kingdom()) + else: + self.win.addstr(1, 0, '------------------------------------------------------------------') + self.win.addstr(2, 0, '[q]Exit [p]Pause [v]Kingdom View [n/m]Previous/Next [s]Percentage') + self.win.addstr(3, 0, '------------------------------------------------------------------') + self.win.addstr(5, 0, king.myvillages[self.villagenr].give_status(self.percentage)) + + self.win.clrtoeol() + self.win.clrtobot() + self.win.refresh(0, 0, 0, 0, height-1, width-1) + + key = self.stdscr.getch() + if key == ord('q'): + self.running = False + elif key == ord('p'): + self.paused = not self.paused + elif key == ord('v'): + self.kingdomview = not self.kingdomview + elif key == ord('n'): + self.villagenr -= 1 + elif key == ord('m'): + self.villagenr += 1 + elif key == ord('s'): + self.percentage = not self.percentage + elif key < 0: + time.sleep(ticklenght) + if not self.running: + curses.endwin() + + diff --git a/config.py b/config.py index 56beb43..ab75c06 100644 --- a/config.py +++ b/config.py @@ -4,7 +4,7 @@ # The lenght of one game cycle in milliseconds ticklenght = 0.005 # Number of Tamagotchis to be created at the start of the simulation -startnr = 100 +startnr = 60 # The amount of points a Tamagotchi loses per tick decayaspeed = 1 # If the hunger stat falls below this level Tamagotchis can be fed @@ -28,3 +28,5 @@ max_workpower = 6 # This defines the bounds of how much a Tamagotchi can recover when eating/sleeping/playing etc. min_recovery = 10 max_recovery = 20 + +start_paused = False diff --git a/kinggotchi.py b/kinggotchi.py index efa5a3b..4d52c06 100644 --- a/kinggotchi.py +++ b/kinggotchi.py @@ -18,7 +18,6 @@ class Kinggotchi: result = 'I am Kinggotchi '+str(self.name)+'.\nIn my Kingdom live '+str(self.all_tamagotchis())+' Tamagotichs.\nIn my Kingdom '+str(self.all_dead())+' Tamagotchis have died so far.\n\nThese are the '+str(len(self.myvillages))+' Villages in my Kingdom:\n' for n in self.myvillages: - # result += '-----------------------------------------------------------------------------------------\n' result += n.give_overview() return result @@ -37,6 +36,4 @@ class Kinggotchi: result = 0 for n in self.myvillages: result += n.graveyard - return result - - \ No newline at end of file + return result \ No newline at end of file diff --git a/test.py b/test.py index 386786d..bd15e4b 100644 --- a/test.py +++ b/test.py @@ -1,19 +1,16 @@ #!/usr/bin/python -# -*- coding: utf-8 -*- +# coding=UTF-8 from mayorgotchi import Mayorgotchi from kinggotchi import Kinggotchi from util import Util from config import * +from cli import * import time import curses # Temporary test section ticks = 0 -kingdomview = True -villagenr = 0 -percentage = True -paused = False def make_list(number): tmp = [] @@ -25,46 +22,13 @@ king = Kinggotchi() for n in range(0, 30): king.add_village(Mayorgotchi(make_list(startnr))) -stdscr = curses.initscr() -curses.noecho() -curses.curs_set(0) -stdscr.keypad(1) -stdscr.nodelay(1) - -height, width = stdscr.getmaxyx() -win = curses.newpad(16383, width) +ui = cursesUI(True) while True: - height, width = stdscr.getmaxyx() - win.move(0, 0) - win.addstr(0, 0, 'Tamagotchi Colony (alpha) - currently at tick '+str(ticks)+'.\n') + ui.build_screen(king,ticks) - if kingdomview: - win.addstr(2, 0, '[q]:Exit [p]:Pause [v]:Village View\n') - win.addstr(4, 0, king.show_kingdom()) - else: - win.addstr(2, 0, '[q]:Exit [p]:Pause [v]:Kingdom View [n/m]:Previous/Next Village\n') - win.addstr(4, 0, king.myvillages[villagenr].give_status(percentage)) - - if not paused: + if not ui.paused: king.step() ticks += 1 - win.clrtoeol() - win.clrtobot() - win.refresh(0, 0, 0, 0, height-1, width-1) - - key = stdscr.getch() - if key == ord('q'): - break - elif key == ord('p'): - paused = not paused - elif key == ord('v'): - kingdomview = not kingdomview - elif key == ord('n'): - villagenr -= 1 - elif key == ord('m'): - villagenr += 1 - elif key < 0: - time.sleep(ticklenght) - -curses.endwin() + if not ui.running: + break \ No newline at end of file