moved interface to its own class, minor interface updates

This commit is contained in:
luxick
2015-08-26 15:25:58 +02:00
parent c21413c789
commit 8eb5e7dc31
4 changed files with 80 additions and 48 deletions

69
cli.py Normal file
View File

@@ -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()

View File

@@ -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

View File

@@ -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
return result

50
test.py
View File

@@ -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