-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.py
executable file
·147 lines (124 loc) · 2.77 KB
/
snake.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
#!/usr/bin/python
# Snake console graphics game
import time
import random
import os
import kbhit
def clearScreen():
if os.name == 'nt':
os.system('cls') # clear screen using DOS command
else:
print '\x1b[2J',
print '\x1bc',
print '\x1bH',
print '\x1bF',
class Tile:
blinkState = False
invert = False
def __init__(self):
self.state = 'empty'
def set(self,state):
self.state = state
def getString(self):
tile = ' '
if self.state == 'empty':
if self.invert:
tile = '\xdb'
elif self.state == 'head':
if self.blinkState:
tile = '\xb1'
else:
tile = '\xb2'
elif self.state == 'alien':
if self.blinkState:
tile = '\xe8'
else:
tile = '\xf8'
elif self.state == 'tail':
tile = '\x69' #'\xb0'
return tile
class Board:
def __init__(self,rows,cols):
Tile.invert = False
self.rows = rows
self.cols = cols
self.board = [[Tile() for j in range(cols)] for i in range(rows)]
def draw(self):
clearScreen()
print
for row in self.board:
line = ''
for cell in row:
line += cell.getString()
print line
print
def setTile(self,row, col,state):
self.board[row][col].set(state)
def getTile(self,row, col):
return self.board[row][col].state
def setRandom(self, state):
while True:
newRow = random.randint(0,self.rows-1)
newCol = random.randint(0,self.cols-1)
if self.getTile(newRow,newCol) == 'empty':
self.setTile(newRow,newCol,state)
break
return (newRow,newCol)
def Snake():
boardSize = 10
board = Board(boardSize,boardSize)
pos = board.setRandom('head')
row = pos[0]
col = pos[1]
board.setRandom('alien')
tail = []
gameOver = False
while not gameOver:
board.draw()
print "Score = %d" % len(tail)
#action = raw_input("move?")
if kb.kbhit():
action = kb.getch()
lastRow = row
lastCol = col
if action=='w' and row > 0:
row -= 1
elif action=='s' and row < boardSize-1:
row += 1
elif action=='a' and col > 0:
col -= 1
elif action=='d' and col < boardSize-1:
col += 1
elif action =='q':
return True
else:
continue
board.setTile(lastRow,lastCol,'tail')
tail.insert(0,(lastRow,lastCol))
if board.getTile(row,col) == 'tail':
print '\a'*4, # bell
for i in range(4):
Tile.invert = not Tile.invert
board.draw()
print
print "Game Over!"
time.sleep(.5)
gameOver = True
elif board.getTile(row,col) == 'alien':
print '\a'*2, # bell
board.setRandom('alien')
else:
c = tail.pop()
board.setTile(c[0],c[1],'empty')
board.setTile(row,col,'head')
print '\a', # bell
else:
time.sleep(.3)
Tile.blinkState = not Tile.blinkState
kb.set_normal_term()
return False
kb = kbhit.KBHit()
done = False
while (not done):
done = Snake()
kb.set_normal_term()