|
@@ -0,0 +1,167 @@
|
|
1
|
+#!/usr/bin/env python3
|
|
2
|
+
|
|
3
|
+# ----------------------------------------------------------------------------
|
|
4
|
+# "THE BEER-WARE LICENSE" (Revision 42):
|
|
5
|
+# <xythobuz@xythobuz.de> wrote this file. As long as you retain this notice
|
|
6
|
+# you can do whatever you want with this stuff. If we meet some day, and you
|
|
7
|
+# think this stuff is worth it, you can buy me a beer in return. Thomas Buck
|
|
8
|
+# ----------------------------------------------------------------------------
|
|
9
|
+
|
|
10
|
+from draw import ScrollText
|
|
11
|
+import time
|
|
12
|
+import random
|
|
13
|
+
|
|
14
|
+class Snake:
|
|
15
|
+ def __init__(self, g, i, sc = (0, 255, 0), d = (0, 0, 255), bg = (0, 0, 0), ts = 0.3, su = 0.75, to = 60.0):
|
|
16
|
+ self.gui = g
|
|
17
|
+ self.input = i
|
|
18
|
+ self.colors = [ bg, sc, d ]
|
|
19
|
+ self.timestep = ts
|
|
20
|
+ self.timeout = to
|
|
21
|
+ self.speedup = su
|
|
22
|
+
|
|
23
|
+ self.winText = ScrollText(self.gui, "You Won!", "uushi",
|
|
24
|
+ 2, 75, (0, 255, 0))
|
|
25
|
+ self.loseText = ScrollText(self.gui, "Game Over!", "uushi",
|
|
26
|
+ 2, 75, (255, 0, 0))
|
|
27
|
+ self.scoreText = ScrollText(self.gui, "Score:", "uushi",
|
|
28
|
+ 2, 75, sc)
|
|
29
|
+
|
|
30
|
+ random.seed()
|
|
31
|
+ self.restart()
|
|
32
|
+
|
|
33
|
+ def restart(self):
|
|
34
|
+ self.start = time.time()
|
|
35
|
+ self.last = time.time()
|
|
36
|
+ self.direction = "r"
|
|
37
|
+ self.directionTmp = "r"
|
|
38
|
+ self.score = 0
|
|
39
|
+ self.data = [[0 for y in range(self.gui.height)] for x in range(self.gui.width)]
|
|
40
|
+
|
|
41
|
+ self.player = [ (int(self.gui.width / 2), int(self.gui.height / 2)) ]
|
|
42
|
+ self.data[self.player[0][0]][self.player[0][1]] = 1
|
|
43
|
+
|
|
44
|
+ self.placeDot()
|
|
45
|
+
|
|
46
|
+ def finished(self):
|
|
47
|
+ if self.input == None:
|
|
48
|
+ # backup timeout for "AI"
|
|
49
|
+ if (time.time() - self.start) >= self.timeout:
|
|
50
|
+ return True
|
|
51
|
+
|
|
52
|
+ if self.direction == "":
|
|
53
|
+ # game over screen
|
|
54
|
+ return self.scoreText.finished()
|
|
55
|
+
|
|
56
|
+ return False
|
|
57
|
+
|
|
58
|
+ def placeDot(self):
|
|
59
|
+ d = (random.randrange(0, self.gui.width), random.randrange(0, self.gui.height))
|
|
60
|
+ while self.data[d[0]][d[1]] != 0:
|
|
61
|
+ d = (random.randrange(0, self.gui.width), random.randrange(0, self.gui.height))
|
|
62
|
+ self.data[d[0]][d[1]] = 2
|
|
63
|
+
|
|
64
|
+ def buttons(self):
|
|
65
|
+ keys = self.input.get()
|
|
66
|
+ if keys["left"]:
|
|
67
|
+ self.directionTmp = "l"
|
|
68
|
+ elif keys["right"]:
|
|
69
|
+ self.directionTmp = "r"
|
|
70
|
+ elif keys["up"]:
|
|
71
|
+ self.directionTmp = "u"
|
|
72
|
+ elif keys["down"]:
|
|
73
|
+ self.directionTmp = "d"
|
|
74
|
+
|
|
75
|
+ def step(self):
|
|
76
|
+ player = self.player[len(self.player) - 1]
|
|
77
|
+ if self.direction == "r":
|
|
78
|
+ player = (player[0] + 1, player[1])
|
|
79
|
+ elif self.direction == "l":
|
|
80
|
+ player = (player[0] - 1, player[1])
|
|
81
|
+ elif self.direction == "u":
|
|
82
|
+ player = (player[0], player[1] - 1)
|
|
83
|
+ elif self.direction == "d":
|
|
84
|
+ player = (player[0], player[1] + 1)
|
|
85
|
+
|
|
86
|
+ if (player[0] < 0) or (player[1] < 0) or (player[0] >= self.gui.width) or (player[1] >= self.gui.height):
|
|
87
|
+ return False
|
|
88
|
+
|
|
89
|
+ if self.data[player[0]][player[1]] == 0:
|
|
90
|
+ # remove last tail piece if not on dot
|
|
91
|
+ self.data[self.player[0][0]][self.player[0][1]] = 0
|
|
92
|
+ self.player.pop(0)
|
|
93
|
+ elif self.data[player[0]][player[1]] == 1:
|
|
94
|
+ # snake crashed into itself
|
|
95
|
+ return False
|
|
96
|
+ else:
|
|
97
|
+ # collected a dot
|
|
98
|
+ self.score += 1
|
|
99
|
+ if self.score >= self.gui.width * self.gui.height:
|
|
100
|
+ return False
|
|
101
|
+
|
|
102
|
+ self.timestep = self.timestep * self.speedup
|
|
103
|
+ self.placeDot()
|
|
104
|
+
|
|
105
|
+ self.player.append(player)
|
|
106
|
+ self.data[player[0]][player[1]] = 1
|
|
107
|
+ return True
|
|
108
|
+
|
|
109
|
+ def finishedEndScreen(self):
|
|
110
|
+ if self.score >= self.gui.width * self.gui.height:
|
|
111
|
+ return self.winText.finished()
|
|
112
|
+ else:
|
|
113
|
+ return self.loseText.finished()
|
|
114
|
+
|
|
115
|
+ def drawEndScreen(self):
|
|
116
|
+ if self.score >= self.gui.width * self.gui.height:
|
|
117
|
+ self.winText.draw()
|
|
118
|
+ else:
|
|
119
|
+ self.loseText.draw()
|
|
120
|
+
|
|
121
|
+ def drawScoreScreen(self):
|
|
122
|
+ self.scoreText.draw()
|
|
123
|
+
|
|
124
|
+ def draw(self):
|
|
125
|
+ if self.direction == "":
|
|
126
|
+ if self.finishedEndScreen():
|
|
127
|
+ self.drawScoreScreen()
|
|
128
|
+ else:
|
|
129
|
+ self.drawEndScreen()
|
|
130
|
+ self.scoreText.restart()
|
|
131
|
+ return
|
|
132
|
+
|
|
133
|
+ if self.input != None:
|
|
134
|
+ self.buttons()
|
|
135
|
+ else:
|
|
136
|
+ # TODO "AI"
|
|
137
|
+ pass
|
|
138
|
+
|
|
139
|
+ if (time.time() - self.last) >= self.timestep:
|
|
140
|
+ self.last = time.time()
|
|
141
|
+
|
|
142
|
+ # only allow valid inputs
|
|
143
|
+ tmp = self.direction + self.directionTmp
|
|
144
|
+ if (tmp != "lr") and (tmp != "rl") and (tmp != "ud") and (tmp != "du"):
|
|
145
|
+ self.direction = self.directionTmp
|
|
146
|
+
|
|
147
|
+ cont = self.step()
|
|
148
|
+ if cont == False:
|
|
149
|
+ self.direction = ""
|
|
150
|
+ self.scoreText.setText("Score: " + str(self.score), "uushi")
|
|
151
|
+ self.winText.restart()
|
|
152
|
+ self.loseText.restart()
|
|
153
|
+ self.scoreText.restart()
|
|
154
|
+
|
|
155
|
+ for x in range(0, self.gui.width):
|
|
156
|
+ for y in range(0, self.gui.height):
|
|
157
|
+ self.gui.set_pixel(x, y, self.colors[self.data[x][y]])
|
|
158
|
+
|
|
159
|
+if __name__ == "__main__":
|
|
160
|
+ import util
|
|
161
|
+ t = util.getTarget()
|
|
162
|
+
|
|
163
|
+ from gamepad import InputWrapper
|
|
164
|
+ i = InputWrapper()
|
|
165
|
+
|
|
166
|
+ d = Snake(t, i)
|
|
167
|
+ t.debug_loop(d.draw)
|