-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw.py
125 lines (101 loc) · 3.21 KB
/
draw.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/python
#import curses
from curses import initscr, noecho, curs_set, cbreak, nocbreak, echo, endwin
import curses
WIDTH = 50
HEIGHT = 25
class Screen:
def __init__(self):
# Curses initialization
self.stdscr = initscr()
#self.stdscr.notimeout(0)
# Colors
curses.resize_term(HEIGHT, WIDTH)
curses.start_color()
self.br = str(curses.baudrate())
noecho()
cbreak()
self.stdscr.keypad(0) # Changed to 0
self.stdscr.nodelay(1)
curs_set(0)
self.stdscr.clear()
self.stdscr.border(0)
# stdscr functions made accessible through the Screen object
self.addstr = self.stdscr.addstr
self.getch = self.stdscr.getch
self.flushinp = curses.flushinp
self.refresh = self.stdscr.refresh
# Colours
self.init_pair = curses.init_pair
self.color_pair = curses.color_pair
self.COLOR_GREEN = curses.COLOR_GREEN
self.COLOR_BLACK = curses.COLOR_BLACK
# Effects
self.A_BLINK = curses.A_BLINK
self.A_UNDERLINE = curses.A_UNDERLINE
# List of all words to be drawn
# words: [[word, x, y, speed], ...]
self.words = []
# Buffer of current pressed keys
self.keys = []
# Score
self.score = 0
# Life
self.life = 3
# Game Over
self.game_over = False
def __del__(self):
# Ending...
nocbreak()
self.stdscr.keypad(0)
echo()
endwin()
def addWord(self, word):
""" Adds a word to the list of printable words"""
self.words.append(word)
def addKey(self, key):
if key == '\n':
ret = ''.join(self.keys)
self.keys = []
return ret
else:
self.keys.append(key)
return False
def remWord(self, name):
for i, w in enumerate(self.words):
if w.name == name:
del self.words[i]
return
def scoreWord(self, name):
for i, w in enumerate(self.words):
if w.name == name:
self.score += len(w.name)
del self.words[i]
def remLife(self):
self.life -= 1
if self.life == 0:
self.game_over = True
def drawPump(self):
# Draw static data
# Print current score
self.addstr(1, 1, 'Score: ' + str(self.score))
# Print current life
self.addstr(1, 20, 'Life: ' + str(self.life))
# Draw dynamic data
for i, w in enumerate(self.words):
self.addstr(int(round(-w.pos_y)), int(round(w.pos_x)), w.name)
def dynPump(self):
for i, w in enumerate(self.words):
# Dynamics
# Pos = Pos + Speed*Dt
self.words[i].pos_y = self.words[i].pos_y + self.words[i].speed
# Check the edge
if self.words[i].pos_y < -HEIGHT + 2:
self.remWord(self.words[i].name)
self.remLife()
#for word in self.words:
# self.addstr(-word[2], word[1], word[0])
#self.addstr(20, 20, self.br)
self.stdscr.refresh()
self.stdscr.erase()
self.stdscr.border(0)