-
Notifications
You must be signed in to change notification settings - Fork 2
/
juego_serpiente.py
202 lines (173 loc) · 6.27 KB
/
juego_serpiente.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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/python
# -*- coding: latin-1 -*-
import curses
import time
import random
import os
from curses import textpad
menu = ['Nuevo Juego', 'Salir']
hi_score = 0
def print_menu(stdscr, selected_row_idx):
stdscr.clear()
h, w = stdscr.getmaxyx()
texto = "S N A K E G A M E"
x = w//2 - len(texto)//2
stdscr.addstr(10, x, texto)
sh, sw = stdscr.getmaxyx()
box = [[3,3], [sh-3, sw-3]] # [[ul_y, ul_x], [dr_y, dr_x]]
textpad.rectangle(stdscr, box[0][0], box[0][1], box[1][0], box[1][1])
h, w = stdscr.getmaxyx()
for idx, row in enumerate(menu):
x = w//2 - len(row)//2
y = h//2 - len(menu)//2 + idx
if idx == selected_row_idx:
stdscr.attron(curses.color_pair(1))
stdscr.addstr(y, x, row)
stdscr.attroff(curses.color_pair(1))
else:
stdscr.addstr(y, x, row)
stdscr.refresh()
def center_text(stdscr,text):
h, w = stdscr.getmaxyx()
x = w//2 - len(text)//2
y = h//2
stdscr.addstr(y, x, text)
def print_center(stdscr, text):
stdscr.clear()
center_text(stdscr,text)
stdscr.refresh()
def pantalla(stdscr):
#stdscr.clear()
# turn off cursor blinking
curses.curs_set(0)
stdscr.nodelay(1)
stdscr.timeout(100)
# color scheme for selected row
curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_GREEN)
# specify the current selected row
current_row = 0
# print the menu
print_menu(stdscr, current_row)
while 1:
key = stdscr.getch()
if key == curses.KEY_UP and current_row > 0:
current_row -= 1
curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_GREEN)
elif key == curses.KEY_DOWN and current_row < len(menu)-1:
current_row += 1
curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_RED)
elif key == curses.KEY_ENTER or key in [10, 13]:
if current_row== len(menu)-1:
print_center(stdscr, "See you later".format(menu[current_row]))
#stdscr.getch()
time.sleep(2)
break
else:
stdscr.clear()
main(stdscr)
break
print_menu(stdscr, current_row)
def create_food(snake, box):
"""Simple function to find coordinates of food which is inside box and not on snake body"""
food = None
while food is None:
food = [random.randint(box[0][0]+1, box[1][0]-1),
random.randint(box[0][1]+1, box[1][1]-1)]
if food in snake:
food = None
return food
def main(stdscr):
global FT, hi_score
# initial settings
curses.curs_set(0)
# create a game box
sh, sw = stdscr.getmaxyx()
box = [[3,3], [sh-3, sw-3]] # [[ul_y, ul_x], [dr_y, dr_x]]
#stdscr.addstr(1,81,"'q' = QUIT SPACE BAR=PAUSE/CONTINUE")#Windows
if os.name == "posix": #for Linux
indication_text = "'q' = QUIT, <SPACE BAR>=PAUSE/CONTINUE"
x = sw//2 - len(indication_text)//2
stdscr.addstr(22,x,indication_text)
else: #for Windows
stdscr.addstr(1,81,"'q' = QUIT SPACE BAR=PAUSE/CONTINUE")
textpad.rectangle(stdscr, box[0][0], box[0][1], box[1][0], box[1][1])
# create snake and set initial direction
snake = [[sh//2, sw//2+1], [sh//2, sw//2], [sh//2, sw//2-1]]
direction = curses.KEY_RIGHT
# draw snake
for y,x in snake:
stdscr.addstr(y, x, '#')
# create food
food = create_food(snake, box)
stdscr.addstr(food[0], food[1], '*')
# print score
score = 0
#hi_score = 0
score_text = "Score: {}".format(score)####################################
hi_score_text = "Hi-Score: {}".format(hi_score)
stdscr.addstr(1, sw//2 - len(score_text)//2, score_text)
stdscr.addstr(1, 4, hi_score_text)
PAUSE=False
while 1:
# non-blocking input
key = stdscr.getch()
if key == ord(' '):
if PAUSE == False:
PAUSE = True
center_text(stdscr,"PAUSE")
#stdscr.addstr(15,57,"PAUSE")
#print_center(stdscr,"PAUSE")
else:
PAUSE = False
center_text(stdscr," ")
if key == ord('q') or key == ord('Q'):
break
# set direction if user pressed any arrow key
if PAUSE == False:
if key in [curses.KEY_RIGHT, curses.KEY_LEFT, curses.KEY_DOWN, curses.KEY_UP]:
direction = key
# find next position of snake head
head = snake[0]
if direction == curses.KEY_RIGHT:
new_head = [head[0], head[1]+1]
elif direction == curses.KEY_LEFT:
new_head = [head[0], head[1]-1]
elif direction == curses.KEY_DOWN:
new_head = [head[0]+1, head[1]]
elif direction == curses.KEY_UP:
new_head = [head[0]-1, head[1]]
# insert and print new head
#if PAUSE == False:
stdscr.addstr(new_head[0], new_head[1], '#')
snake.insert(0, new_head)
# if sanke head is on food
if snake[0] == food:
curses.beep()
score += 1
if score > hi_score:
hi_score+=1
hi_score_text = "Hi-Score: {}".format(hi_score)
score_text = "Score: {}".format(score)
stdscr.addstr(1, sw//2 - len(score_text)//2, score_text)
stdscr.addstr(1, 4, hi_score_text) #- (len(hi_score_text)//2)
# create new food
food = create_food(snake, box)
stdscr.addstr(food[0], food[1], '*')
# increase speed of game
stdscr.timeout(100 - (len(snake)//3)%90)
else:
# shift snake's tail
stdscr.addstr(snake[-1][0], snake[-1][1], ' ')
snake.pop()
# conditions for game over
if (snake[0][0] in [box[0][0], box[1][0]] or
snake[0][1] in [box[0][1], box[1][1]] or
snake[0] in snake[1:]):
msg = "GAME OVER"
stdscr.addstr(sh//2, sw//2-len(msg)//2, msg)
stdscr.getch()
stdscr.nodelay(0)
time.sleep(2)
break
pantalla(stdscr)
curses.wrapper(pantalla)