From 79ec8eaa8a58bd04e82708aa688f98a1fbe05fa9 Mon Sep 17 00:00:00 2001 From: duarte-pompeu Date: Tue, 14 Jul 2015 16:43:07 +0100 Subject: [PATCH] refactor world code - it should only draw the game scenario, not pannels and high scores --- src/lib_common.py | 16 ++++++---------- src/main.py | 26 ++++++++++++++++++++++---- src/world.py | 6 +----- 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/src/lib_common.py b/src/lib_common.py index 9e592f3..718b54e 100755 --- a/src/lib_common.py +++ b/src/lib_common.py @@ -7,23 +7,19 @@ # CURSES # ########## -X_LIM = 0 -Y_LIM = 0 X_TERM_LIM = 0 Y_TERM_LIM = 0 def init_curses(x_limit, y_limit): - global X_LIM, Y_LIM + global X_TERM_LIM, Y_TERM_LIM global stdscr stdscr = curses.initscr() curses.use_env(True) Y_TERM_LIM, X_TERM_LIM = stdscr.getmaxyx() - X_LIM, Y_LIM = X_TERM_LIM, Y_TERM_LIM - #~ X_LIM, Y_LIM = x_limit, y_limit time.sleep(1) @@ -32,7 +28,7 @@ def init_curses(x_limit, y_limit): stdscr.keypad(1) stdscr.nodelay(1) - return X_LIM, Y_LIM + return X_TERM_LIM, Y_TERM_LIM def check_input(world): @@ -79,19 +75,19 @@ def draw_window(x1,y1,x2,y2): def draw_window_y(y1, y2): x1 = 0 - x2 = X_LIM + x2 = X_TERM_LIM draw_window(x1, y1, x2-1, y2) def draw_cur(x, y, msg): real_x = x - real_y = Y_LIM - y + real_y = Y_TERM_LIM - y - if real_x < 0 or real_x > X_LIM: + if real_x < 0 or real_x > X_TERM_LIM: return - if real_y < 0 or real_y > Y_LIM: + if real_y < 0 or real_y > Y_TERM_LIM: return try: diff --git a/src/main.py b/src/main.py index 5b5fa26..22243d2 100755 --- a/src/main.py +++ b/src/main.py @@ -3,19 +3,22 @@ from world import * import time +X_LIMIT, Y_LIMIT = 0,0 + def main(): + global X_LIMIT, Y_LIMIT try: - xlimit, ylimit = init_curses(50,50) + X_LIMIT, Y_LIMIT = init_curses(50,50) global world - world = World(0,xlimit,0,ylimit) + world = World(1,X_LIMIT,6,Y_LIMIT) while(True): if check_input(world) == -1: break - world.update() - world.draw() + update() + draw() time.sleep(0.1) except Exception, e: @@ -24,6 +27,21 @@ def main(): finally: close_curses() + +def update(): + world.update() + +def draw(): + clear_screen() + draw_pannel() + world.draw() + +def draw_pannel(): + y = 5 + draw_window_y(1, y) + draw_cur(2, y-1, " use wasd to move or q to quit") + limits_msg = "WINDOW LIMITS: " + str(X_LIMIT) + ":" + str(Y_LIMIT) + draw_cur(2, y-2, limits_msg) if __name__ == "__main__": diff --git a/src/world.py b/src/world.py index 769768b..75af933 100644 --- a/src/world.py +++ b/src/world.py @@ -42,11 +42,7 @@ def update(self): self.spawn_food() def draw(self): - clear_screen() - draw_window_y(1, 5) - draw_cur(2, 4, " use wasd to move or q to quit") - limits_msg = " LIMITS: " + str(self.x2) + ":" + str(self.y2) - draw_cur(2, 3, limits_msg) + #~ draw_window(self.x1+1,self.x2,self.y1,self.y2) self.snake.draw() self.food.draw()