-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake.py
More file actions
205 lines (168 loc) · 5.99 KB
/
snake.py
File metadata and controls
205 lines (168 loc) · 5.99 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
from uib_inf100_graphics.event_app import run_app
from snake_view import draw_board
import random as rd
def app_started(app):
# Modellen.
# Denne funksjonen kalles én gang ved programmets oppstart.
# Her skal vi __opprette__ variabler i som behøves i app.
app.direction = "east"
app.debug_mode = True
app.apples = 0 # Tellar for antall epler spist
app.board = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 2, 3, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
]
add_apple_at_random_location(app.board, app) # Startar med random eple
app.snake_size = 3
app.head_pos = (3, 4)
app.state = "active"
app.timer_delay = 200
def is_legal(pos: tuple, board: list): # Sjekker om neste pos er gyldig
a, b = pos
if (
a >= len(board) # Sjekker venstre og høgre
or a < 0
or b >= len(board[0]) # Sjekker topp og botn
or b < 0
or board[a][b] >= 1 # sjekker at nestre rute er gyldig
):
return False
return True
def add_apple_at_random_location(board: list, app): # Legger til nytt eple dersom ledig
counter = 0
max_attempts = len(board) * len(board[0])
a = rd.choice(range(len(board)))
b = rd.choice(range(len(board[0])))
while counter < max_attempts:
a = rd.choice(range(len(board)))
b = rd.choice(range(len(board[0])))
try:
if board[a][b] == 0:
board[a][b] = -1
app.apples += 1
return
except IndexError:
pass
counter += 1
raise RuntimeError("No available space for apple after max attempts")
for rows in range(len(board)):
for cols in range(len(board[0])):
if board[rows][cols] == -1:
no_apples = True
pass
if no_apples:
add_apple_at_random_location(app.board, app)
def timer_fired(app):
# En kontroller.
# Denne funksjonen kalles ca 10 ganger per sekund som standard.
# Funksjonen kan __endre på__ eksisterende variabler i app.
if app.debug_mode == False and app.state == "active": # kjører "automatisk"
move_snake(app)
def key_pressed(app, event): # Styrer taste trykk
# En kontroller.
# Denne funksjonen kalles hver gang brukeren trykker på tastaturet.
# Funksjonen kan __endre på__ eksisterende variabler i app.
if app.state == "active":
if app.debug_mode:
if event.key == "Space":
move_snake(app)
app.direction = "east"
if event.key == "d":
app.debug_mode = not app.debug_mode
elif event.key == "Up":
app.direction = "north"
elif event.key == "Down":
app.direction = "south"
elif event.key == "Left":
app.direction = "west"
elif event.key == "Right":
app.direction = "east"
elif app.state == "gameover":
if event.key == "d":
if app.debug_mode:
app.debug_mode = not app.debug_mode
if event.key == "r":
run_app(width=800, height=600, title="Snake")
def substract_one_from_all_positives(board): # oppdaterer brettet tilbake til 0
for rows in range(len(board)):
for cols in range(len(board[0])):
if board[rows][cols] > 0:
board[rows][cols] = board[rows][cols] - 1
# Henter neste head_pos
def get_next_head_pos(head_pos: tuple, direction: str, board: list):
r, c = head_pos
new_head_pos = head_pos
if direction == "east":
if c + 1 < len(board[0]):
new_head_pos = (r, c + 1)
elif direction == "west":
if c - 1 >= 0:
new_head_pos = (r, c - 1)
elif direction == "north":
if r - 1 >= 0:
new_head_pos = (r - 1, c)
elif direction == "south":
if r + 1 < len(board):
new_head_pos = (r + 1, c)
return new_head_pos
def move_snake(app): # Flyttar slangen
app.head_pos = get_next_head_pos(app.head_pos, app.direction, app.board)
a, b = app.head_pos
if not is_legal(app.head_pos, app.board):
app.state = "gameover"
return
if app.board[a][b] == -1:
app.board[a][b] = 0
app.snake_size += 1
if app.timer_delay > 80:
app.timer_delay -= 10
add_apple_at_random_location(app.board, app)
substract_one_from_all_positives(app.board)
app.board[a][b] = app.snake_size
def redraw_all(app, canvas): # Tegnar canvas
# Visningen.
# Denne funksjonen tegner vinduet. Funksjonen kalles hver gang
# modellen har endret seg, eller vinduet har forandret størrelse.
# Funksjonen kan __lese__ variabler fra app, men har ikke lov til
# å endre på dem.
if app.debug_mode: # Kun i debug_mode
canvas.create_text(
250,
10,
text=f"app.head_pos={app.head_pos}, app.snake_size={app.snake_size}, app.direction={app.direction}, app.state={app.state}",
)
else: # Ikkje debug mode
canvas.create_text(app.width / 2, 10, text=f"Apples eaten: {app.apples}")
if app.state == "active":
draw_board(
canvas,
25,
25,
app.width - 25,
app.height - 25,
app.board,
app.debug_mode,
)
elif app.state == "gameover":
canvas.create_text(
app.width / 2,
app.height / 2,
text=f"Game Over",
font=("Comic sans MS", 40),
)
canvas.create_text(
app.width / 2,
app.height / 1.5,
text=f"Press R to restart",
font=("Comic sans MS", 20),
)
run_app(width=800, height=600, title="Snake")