-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake.py
More file actions
92 lines (76 loc) · 1.86 KB
/
snake.py
File metadata and controls
92 lines (76 loc) · 1.86 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
import RPi.GPIO as gpio
import time
import max7219.led as led
from random import randint
gpio.setmode(gpio.BCM)
taster = [14,15,18,23]
matrix = led.matrix()
def startSpiel():
global snake, richtung, apfel
snake = [[randint(2,5),randint(2,5)]]
richtung = [0,0]
while richtung == [0,0]:
matrix.show_message("READY")
neuerApfel()
def steuerung(gpio):
global richtung
if(gpio == 14): #rechts
richtung = [1,0]
elif(gpio == 15): #oben
richtung = [0,-1]
elif(gpio == 18): #unten
richtung = [0,1]
elif(gpio == 23): #links
richtung = [-1,0]
for i in taster:
gpio.setup(i,gpio.IN,pull_up_down=gpio.PUD_UP)
gpio.add_event_detect(i, gpio.FALLING, callback=steuerung)
def neuerApfel():
global apfel, snake
apfelSnake = False
while apfelSnake == False:
apfelSnake = True
apfel = [randint(0,7),randint(0,7)]
for i in snake:
if(i == apfel):
apfelSnake = False
def endOfGame():
for i in range(0,2):
matrix.clear()
for i in range(0,8):
for j in range(0,8):
matrix.pixel(i,j,1)
time.sleep(0.01)
time.sleep(0.1)
matrix.show_message("GAME OVER")
punkte = len(snake)-1
matrix.show_message(str(punkte)+" PUNKTE", delay=0.1)
startSpiel()
startSpiel()
while True:
keinePause = False
newSnake = [snake[0][0]+richtung[0],
snake[0][1]+richtung[1]]
for i in snake:
if(i == newSnake):
endOfGame()
pass
if(newSnake == apfel):
neuerApfel()
keinePause = True
else:
snake.pop()
snake.insert(0,newSnake)
if(snake[0][0] >= 8 or snake[0][1] >=8
or snake[0][0] < 0 or snake[0][1] < 0 ):
endOfGame()
pass
matrix.clear()
for i in snake:
matrix.pixel(i[0],i[1], 1)
matrix.pixel(apfel[0],apfel[1],1)
if(keinePause == False):
newLength = (len(snake)-2)*0.01
time.sleep(0.8-newLength)
else:
time.sleep(0.4)