forked from s-nez/falling-blocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
executable file
·73 lines (62 loc) · 2.26 KB
/
game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/python
"""Main game module"""
import curses
from time import time
from board import Board
from scoreboard import ScoreBoard
from keymap import KeyMap
STEP_TIME = 0.5
CONFIG_FNAME = 'falling_blocks.conf'
STDSCR = curses.initscr()
curses.start_color() # enable color support
curses.noecho() # don't display pressed KEYS
curses.cbreak() # don't wait for a newline to process input
STDSCR.keypad(1) # enable keypad mode (process special KEYS, like Home)
curses.curs_set(0) # make the cursor invisible
curses.halfdelay(5) # wait only half a second between each getch
# Overload colors to make blocks
curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_WHITE)
curses.init_pair(2, curses.COLOR_GREEN, curses.COLOR_GREEN)
curses.init_pair(3, curses.COLOR_BLUE, curses.COLOR_BLUE)
curses.init_pair(4, curses.COLOR_YELLOW, curses.COLOR_YELLOW)
curses.init_pair(5, curses.COLOR_RED, curses.COLOR_RED)
try:
KEYS = KeyMap()
KEYS.load_from_file(CONFIG_FNAME)
GAME_BOARD = Board(STDSCR, [0, 0], [20, 20])
SCORE_BOARD = ScoreBoard(GAME_BOARD.status, 5, 25)
GAME_QUIT = False
LAST_GAME_STEP = time()
while not GAME_QUIT and not GAME_BOARD.game_over():
CURRENT_TIME = time()
if CURRENT_TIME - LAST_GAME_STEP > STEP_TIME:
GAME_BOARD.advance_block()
LAST_GAME_STEP = CURRENT_TIME
USR_INPUT = STDSCR.getch()
if USR_INPUT == KEYS.quit:
GAME_QUIT = True
elif USR_INPUT == KEYS.lshift:
GAME_BOARD.lshift_block()
elif USR_INPUT == KEYS.rshift:
GAME_BOARD.rshift_block()
elif USR_INPUT == KEYS.land:
GAME_BOARD.land_block()
elif USR_INPUT == KEYS.rotate:
GAME_BOARD.rotate_block()
GAME_BOARD.draw()
SCORE_BOARD.draw()
# Update game SPEED
SPEED = GAME_BOARD.game_speed()
STEP_TIME = float(SPEED) / 10
curses.halfdelay(SPEED)
if GAME_BOARD.game_over():
STDSCR.addstr(10, 5, 'GAME OVER')
USR_INPUT = STDSCR.getch()
while USR_INPUT != KEYS.quit:
USR_INPUT = STDSCR.getch()
finally:
# Disable the curses-friendly terminal settings and close the window
curses.nocbreak()
STDSCR.keypad(0)
curses.echo()
curses.endwin()