-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake.py
More file actions
188 lines (176 loc) · 5.78 KB
/
Copy pathsnake.py
File metadata and controls
188 lines (176 loc) · 5.78 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import curses
import random
def joc(scr):
curses.curs_set(0)
scr.attron(curses.A_BOLD)
scr.clear()
LINES = scr.getmaxyx()[0]
COLS = scr.getmaxyx()[1]
velocitat = 125
class node_serp():
def __init__(self, y = None, x = None):
self.y = y
self.x = x
self.anterior = None
class serp(object):
def __init__(self, y, x):
self.primer = node_serp(y, x + 1)
self.ultim = node_serp(y, x)
self.ultim.anterior = self.primer
scr.addch(y, x, '#')
scr.addch(y, x+1, '#')
scr.refresh()
def moure(self, dire):
scr.addch(self.ultim.y, self.ultim.x, ' ')
self.ultim = self.ultim.anterior
nou_node = node_serp(self.primer.y, self.primer.x)
if dire == 'RIGHT':
nou_node.x += 1
elif dire == 'LEFT':
nou_node.x -= 1
elif dire == 'TOP':
nou_node.y -= 1
else:
nou_node.y += 1
self.primer.anterior = nou_node
self.primer = nou_node
scr.addch(nou_node.y, nou_node.x, '#')
scr.refresh()
def comprovar(self, dire):
xx = 0
yy = 0
if dire == 'RIGHT':
yy = self.primer.y
xx = self.primer.x + 1
elif dire == 'LEFT':
yy = self.primer.y
xx = self.primer.x - 1
elif dire == 'TOP':
yy = self.primer.y - 1
xx = self.primer.x
else:
yy = self.primer.y + 1
xx = self.primer.x
if yy < 1 or xx < 1 or yy >= LINES-1 or xx >= COLS-1:
return False
if (scr.inch(yy, xx) & curses.A_CHARTEXT) == ord('#'):
return False
return True
def trobaFruita(self, dire):
xx = 0
yy = 0
if dire == 'RIGHT':
yy = self.primer.y
xx = self.primer.x + 1
elif dire == 'LEFT':
yy = self.primer.y
xx = self.primer.x - 1
elif dire == 'TOP':
yy = self.primer.y - 1
xx = self.primer.x
else:
yy = self.primer.y + 1
xx = self.primer.x
if (scr.inch(yy, xx) & curses.A_CHARTEXT) == ord('@'):
return True
return False
def menja(self, dire):
nou_node = node_serp(self.primer.y, self.primer.x)
if dire == 'RIGHT':
nou_node.x += 1
elif dire == 'LEFT':
nou_node.x -= 1
elif dire == 'TOP':
nou_node.y -= 1
else:
nou_node.y += 1
self.primer.anterior = nou_node
self.primer = nou_node
scr.addch(nou_node.y, nou_node.x, '#')
scr.refresh()
def gen_fruita():
ch = ord('#')
x = 5
y = 5
while ch == ord('#'):
y = random.randint(1, LINES-2)
x = random.randint(1, COLS-2)
ch = scr.inch(y, x) & curses.A_CHARTEXT
scr.addch(y, x, '@')
def ajustar_v():
scr.timeout(-1)
scr.clear()
scr.addstr(int(LINES/2), int(COLS/2 -22), 'JOC CLASSIC: PREM UN NUMERO DEL 1 AL 8 (8 es el mes lent)')
while True:
ch = scr.getch()
if ch == ord('1'):
return 25
elif ch == ord('2'):
return 50
elif ch == ord('3'):
return 75
elif ch == ord('4'):
return 100
elif ch == ord('5'):
return 125
elif ch == ord('6'):
return 150
elif ch == ord('7'):
return 175
elif ch == ord('8'):
return 200
def jugar():
scr.clear()
scr.addstr(int(LINES/2 -1), int(COLS/2 -10), 'EN QUALSEVOL MOMENT POTS CLICAR [q] PER TORNAR AL MENU')
scr.refresh()
curses.napms(1000)
scr.clear()
scr.box(0, 0)
direccio = 'RIGHT'
s = serp(2, 2)
gen_fruita()
scr.timeout(velocitat)
ch = ''
while ch != ord('q'):
ch = scr.getch()
if ch == curses.KEY_UP and direccio != 'BOTTOM':
direccio = 'TOP'
elif ch == curses.KEY_DOWN and direccio != 'TOP':
direccio = 'BOTTOM'
elif ch == curses.KEY_RIGHT and direccio != 'LEFT':
direccio = 'RIGHT'
elif ch == curses.KEY_LEFT and direccio != 'RIGHT':
direccio = 'LEFT'
if not s.comprovar(direccio):
s.moure(direccio)
break
elif s.trobaFruita(direccio):
s.menja(direccio)
gen_fruita()
else: s.moure(direccio)
scr.timeout(-1)
scr.addstr(int(LINES/2 -1), int(COLS/2 -8), 'JOC CLASSIC: SERP')
scr.addstr(int(LINES/2 +1), int(COLS/2 -3), 'TIMTIM')
scr.refresh()
curses.napms(1000)
scr.timeout(-1)
ch = ''
while ch != ord('q'):
scr.addstr(int(LINES/2 -1), int(COLS/2 -11), 'PREM [ENTER] PER JUGAR')
scr.addstr(int(LINES/2), int(COLS/2 -16), 'PREM [v] PER AJUSTAR LA VELOCITAT')
scr.addstr(int(LINES/2 +1), int(COLS/2 -7), 'PREM [q] SORTIR')
scr.refresh()
ch = scr.getch()
if ch == ord('v'):
velocitat = ajustar_v()
scr.clear()
continue
if ch == ord('\n'):
jugar()
scr.clear()
scr.addstr(int(LINES/2 +1), int(COLS/2 -8), 'GRACIES PER JUGAR')
scr.timeout(1000)
scr.refresh()
scr.getch()
scr.attroff(curses.A_BOLD)
curses.wrapper(joc)