-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple-snake.py
More file actions
190 lines (146 loc) · 5.37 KB
/
Copy pathsimple-snake.py
File metadata and controls
190 lines (146 loc) · 5.37 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
189
190
import os, time, random, sys
from pynput.keyboard import Key, Listener
last_key_pressed = ""
score = 0
height = 12
width = 20
refresh_time = 0.2
fruit_number = 1
def clear():
if os.name == "nt":
os.system('cls')
else:
os.system('clear')
def print_help():
print "Simple-Snake (Version 0.1) is a simple snake game written in python.\nUse wasd to play.\n"
print "Usage: python simple-snake.py [-h height] [-w width]\n [-r refresh-time (speed)] [-f number of fruit] [--help]\n"
print "Author: Wilhelm Roscher"
sys.exit(0)
# This will store the last key the player pressed. It's used to determine the direction of the snake.
def on_press(key):
global last_key_pressed
last_key_pressed = str(key)[2] # there may be a better way
# This will refresh the playing field on the display.
def refresh(field):
output = "--" * (width +1) + "- \n" # top part of the frame
for line in field:
output += "| " # left part of the frame
for position in line: output += position
output += "| \n" # right part of the frame
output += "--" * (width +1) + "- \n" # bottom part of the frame
output += "\nScore: " + str(score)
clear()
print output
# handle arguments (-h -w -r -f --help)
i_argument=0
for argument in sys.argv:
if argument[0] == "-":
if argument == "-h":
try:
height = int(sys.argv[i_argument + 1])
except:
print_help()
if argument == "-w":
try:
width = int(sys.argv[i_argument + 1])
except:
print_help()
if argument == "-r":
try:
refresh_time = float(sys.argv[i_argument + 1])
except:
print_help()
if argument == "-f":
try:
fruit_number = int(sys.argv[i_argument + 1])
except:
print_help()
if argument == "--help":
print_help()
i_argument+=1
#=== Start of the Game ===
# empty playing field
field = []
for i_line in range(0, height):
field += [[" "] * width]
# snake on starting position
field[0][0] = "@ "
# initial fruits generation
for i in range(0, fruit_number):
fruit_position = random.randint(0, width - 1)
fruit_line = random.randint(0, height - 1)
# if there allready is something on the field, find a new position
while field[fruit_line][fruit_position] <> " ":
fruit_position = random.randint(0, width - 1)
fruit_line = random.randint(0, height - 1)
field[fruit_line][fruit_position] = "* "
refresh(field)
# start keypress listener
with Listener(on_press=on_press) as listener:
# starting conditions
head_line = 0
head_position = 0
direction = "rightward"
length = 1
path = [[0, 0]]
end = False
while end <> True:
# Does the player want to change the direction?
if (last_key_pressed == "w") and (direction <> "downward"):
direction = "upward"
if (last_key_pressed == "a") and (direction <> "rightward"):
direction = "leftward"
if (last_key_pressed == "s") and (direction <> "upward"):
direction = "downward"
if (last_key_pressed == "d") and (direction <> "leftward"):
direction = "rightward"
# replacing the currend snakehead with a snake body-segment
field[head_line][head_position] = "O "
# determening the next position of the snake head
if direction == "upward":
head_line-=1
if head_line < 0: head_line = height-1
if direction == "leftward":
head_position-=1
if head_position < 0: head_position = width-1
if direction == "downward":
head_line+=1
if head_line == height: head_line = 0
if direction == "rightward":
head_position+=1
if head_position == width: head_position = 0
path += [[head_line, head_position]]
# Has the snake bitten itself?
if field[head_line][head_position] == "O ":
end = True
# Fruit found?
if field[head_line][head_position] == "* ":
score += 15
length += 1
# place a new fruit
fruit_position = random.randint(0, width - 1)
fruit_line = random.randint(0, height - 1)
# if there allready is something on the field, find a new position
while field[fruit_line][fruit_position] <> " ":
fruit_position = random.randint(0, width - 1)
fruit_line = random.randint(0, height - 1)
field[fruit_line][fruit_position] = "* "
# remove the last snake-segment from the field
[last_segment_line, last_segment_position] = path[len(path)-length - 1]
field[last_segment_line][last_segment_position] = " "
# place the sneaks head
field[head_line][head_position] = "@ "
refresh(field)
time.sleep(refresh_time)
# display ending text
output = "--" * (width +1) + "- \n" # top part of the frame
for line in field:
output += "| " # left part of the frame
for position in line: output += position
output += "| \n" # right part of the frame
output += "--" * (width +1) + "- \n" # bottom part of the frame
clear()
print output
print "Game Over.\n"
print "Score " + str(score)
print "\nlook at --help for more options"