-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchuck_maze.py
executable file
·104 lines (89 loc) · 2.38 KB
/
chuck_maze.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
#!/usr/bin/python3
# This is a modified version of the raspberrypi.org marble maze.
# This reads in a file that has a maze that can be created by th chuck_pain program.
# nJoy Chuck
from sense_hat import SenseHat
from time import sleep
from tkinter import *
import tkinter as tk
from tkinter import filedialog
# File name to save and load from
file_name="maze.sav"
sense = SenseHat()
sense.clear()
r = (255,0,0)
g = (0,255,0)
b = (0,0,0)
w = (255,255,255)
x = 1
y = 1
def get_int(str_in): # Input string of integers, output tuple of integers.
ints_out = []
ch_str=str_in.split()
for e in ch_str:
ints_out.append(int(e))
return(tuple(ints_out))
root = Tk() # Get a TK window
ifile=filedialog.askopenfile(mode="r",initialfile=file_name)
if ifile is None:
maze = [[r,r,r,r,r,r,r,r],
[r,b,b,b,b,b,b,r],
[r,r,b,r,b,b,b,r],
[r,b,b,r,r,r,r,r],
[r,b,b,b,b,b,b,r],
[r,r,b,r,r,r,b,r],
[r,b,b,r,b,b,b,r],
[r,r,r,r,g,r,r,r]]
else:
maze = [[r,r,r,r,r,r,r,r],
[r,b,b,b,b,b,b,r],
[r,r,b,r,b,b,b,r],
[r,b,b,r,r,r,r,r],
[r,b,b,b,b,b,b,r],
[r,r,b,r,r,r,b,r],
[r,b,b,r,b,b,b,r],
[r,r,r,r,g,r,r,r]]
for y in range(8):
for x in range(8):
instr = ifile.readline()
maze[y][x] = get_int(instr)
root.destroy() # Close the window
x = 1
y = 1
def move_marble(pitch,roll,x,y):
new_x = x
new_y = y
if 1 < pitch < 179 and x != 0:
new_x -= 1
elif 359 > pitch > 179 and x != 7 :
new_x += 1
if 1 < roll < 179 and y != 7:
new_y += 1
elif 359 > roll > 179 and y != 0 :
new_y -= 1
x,y = check_wall(x,y,new_x,new_y)
return x,y
def check_wall(x,y,new_x,new_y):
if maze[new_y][new_x] != r:
return new_x, new_y
elif maze[new_y][x] != r:
return x, new_y
elif maze[y][new_x] != r:
return new_x, y
return x,y
game_over = False
def check_win(x,y):
global game_over
if maze[y][x] == g:
game_over = True
sense.show_message('You Win')
while not game_over:
pitch = sense.get_orientation()['pitch']
roll = sense.get_orientation()['roll']
x,y = move_marble(pitch,roll,x,y)
check_win(x,y)
maze[y][x] = w
sense.set_pixels(sum(maze,[]))
sleep(0.05)
maze[y][x] = b
exit()