-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake-ai.py
114 lines (90 loc) · 2.28 KB
/
snake-ai.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
#!/usr/bin/env python3
import game
import random
s = game.Spiel(debug=False, size=20)
s.pos = 90
s.dir = 1
s.length = 5
s.kipos = 210
s.kidir = 1
s.kilength = 5
s.create()
s.level[random.randint(0,399)] = random.randint(-3,-1)
def alleme():
for ind in range(400):
if s.level[ind] > 0 and s.level[ind] < 400:
s.level[ind] = s.level[ind] - 1
def level1():
s.level[9]=400
s.level[10]=400
s.level[390]=400
s.level[389]=400
s.level[200]=400
s.level[180]=400
s.level[219]=400
s.level[199]=400
s.level[189]=400
s.level[190]=400
s.level[209]=400
s.level[210]=400
s.level[168]=400
s.level[171]=400
s.level[228]=400
s.level[231]=400
level1()
def newpos(pos, dir):
np = pos + dir
if np > 400:
np = np - 400
if np < 0:
np = np + 400
if (np % 20 == 0) and (dir == 1):
np = np - 20
if (np % 20 == 19) and (dir == -1):
np = np + 20
return np
def newkidir():
simpos = newpos(s.kipos, s.kidir)
if s.level[simpos] > 0:
for dir in [-1, 1, 20, -20]:
searchpos = newpos(s.kipos, dir)
if s.level[searchpos] == 0:
return dir
return s.kidir
def schritt():
s.pos = newpos(s.pos, s.dir)
if s.level[s.pos] > 0:
print("Spieler tot!")
print("Punkte: ", s.length)
print("Punkte KI: ", s.kilength)
s.exit()
if s.level[s.pos] < 0:
print("yummy")
s.length = s.length + (s.level[s.pos] * -1)
s.level[random.randint(0,399)] = random.randint(-3,-1)
s.level[s.pos] = s.length
s.kidir = newkidir()
s.kipos = newpos(s.kipos, s.kidir)
if s.level[s.kipos] > 0:
print("KI tot!")
print("Punkte: ", s.length)
print("Punkte KI: ", s.kilength)
s.exit()
if s.level[s.kipos] < 0:
print("yummy")
s.kilength = s.kilength + (s.level[s.kipos] * -1)
s.level[random.randint(0,399)] = random.randint(-3,-1)
s.level[s.kipos] = s.kilength
def tastendruck(taste):
if taste == "Up":
s.dir = -20
if taste == "Down":
s.dir = 20
if taste == "Left":
s.dir = -1
if taste == "Right":
s.dir = 1
s.addStep(alleme)
s.addStep(schritt)
s.addKeylistener(tastendruck)
s.start()