Skip to content

Commit

Permalink
refactor world code - it should only draw the game scenario, not pann…
Browse files Browse the repository at this point in the history
…els and high scores
  • Loading branch information
duarte-pompeu committed Jul 14, 2015
1 parent 5d7ff72 commit 79ec8ea
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
16 changes: 6 additions & 10 deletions src/lib_common.py
Expand Up @@ -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)

Expand All @@ -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):
Expand Down Expand Up @@ -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:
Expand Down
26 changes: 22 additions & 4 deletions src/main.py
Expand Up @@ -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:
Expand All @@ -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__":
Expand Down
6 changes: 1 addition & 5 deletions src/world.py
Expand Up @@ -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()
Expand Down

0 comments on commit 79ec8ea

Please sign in to comment.