Skip to content

Instantly share code, notes, and snippets.

@horstjens
Last active Jun 13, 2022
Embed
What would you like to do?
labyrinth1
import PySimpleGUI as sg
import random
import time
WIDTH, HEIGHT = 800,600
HELPTEXT = """Escape from the labyrinth. Reach the yellow goal.
You are the green rect.
user cursor keys to navigate. """
#COLORS = []
#for r in range(16,200,8):
# for g in range(16,200,8):
# for b in range(16,200,8):
# COLORS.append("#{}{}{}".format(str(hex(r).replace("0x","")),
# str(hex(g).replace("0x","")),
# str(hex(b).replace("0x","")),
# ))
#print(COLORS)
#random.shuffle(COLORS)
# -------- first gui: theme cooser --------------------------
sg.theme(random.choice(sg.theme_list()))
# ---------------------- theme selector layout -------------
layout1 = [
[
sg.Text("Select theme:"),
sg.Combo(
values=sg.theme_list(),
default_value=sg.theme(),
enable_events=True,
key="selector",
),
],
[sg.Text("# x-tiles:"),sg.Slider(range=(10,200), default_value=50,orientation="h", key="x_slider")],
[sg.Text("# y-tiles:"),sg.Slider(range=(10,200), default_value=20,orientation="h", key="y_slider")],
#[sg.Text("Pause in [ms]:"),sg.Slider(range=(0,1000), default_value=500,orientation="h", key="pause_slider")],
[sg.Button("play the game")],
]
window1 = sg.Window("theme selector", layout1)
# ---------------- main loop for first layout ---------------
while True:
event1, values1 = window1.read()
if event1 == "play the game" or event1 == sg.WIN_CLOSED:
# sg.theme("DarkBlue3")
break
if event1 == "selector":
old = sg.theme()
sg.theme(values1["selector"])
if sg.PopupYesNo("do you like this Theme?") == "Yes":
break
else:
sg.theme(old)
#NUMBER_OF_DISCS = int(values1["disc_slider"])
#PAUSE= int(values1["pause_slider"])/1000
x_tiles = int(values1["x_slider"])
y_tiles = int(values1["y_slider"])
window1.close()
# -----------------end of theme chooser -------------------
layout2 = [
[sg.Text("Time left (seconds):"), sg.Text("60", key="time_left")],
[sg.Multiline(default_text=HELPTEXT, size=(90,3), disabled=True)],
[sg.Graph(canvas_size=(WIDTH,HEIGHT),background_color="black",
key="canvas", graph_bottom_left=(0, y_tiles),
graph_top_right=(x_tiles,0 ))],
[sg.Button("Exit")],
]
window2 = sg.Window("enjoy the game", layout2, return_keyboard_events=True)
window2.finalize() # necessary to be able to draw on canvas
c = window2["canvas"]
lines = []
figures = []
# generate labyrinth
# little chance of empty labyrint
if random.random() < 0.01:
lines = [[0 for x in range(x_tiles)] for y in range(y_tiles)]
else:
for y in range(y_tiles):
line = []
for x in range(x_tiles):
if (x,y) == (0,0) :
line.append(0)
continue
if (x,y) == (x_tiles-1, y_tiles-1):
line.append(0)
continue
line.append(random.choice((0,0,0,0,0,1)))
if line[-1] == 1:
figures.append(c.draw_rectangle(top_left=(x,y), bottom_right=(x+1,y+1),
fill_color="white"))
lines.append(line)
duration = 60
player_pos_x = 0
player_pos_y = 0
figures.append(c.draw_rectangle(top_left=(0,0), bottom_right=(1,1), fill_color="green"))
player = c.draw_rectangle(top_left=(0,0), bottom_right=(1,1), fill_color="#00FF00")
goal = c.draw_rectangle(top_left=(x_tiles-1,y_tiles-1), bottom_right=(x_tiles, y_tiles), fill_color="yellow")
end_time = time.time() + duration
while not ((player_pos_x == x_tiles-1) and (player_pos_y != y_tiles+1)):
event2, values2 = window2.read(timeout=1000)
print(event2, values2)
if event2 in ("Exit", sg.WINDOW_CLOSED ):
break
if event2 == sg.TIMEOUT_EVENT:
# blink the player
#window2.finalize()
# update time left
if time.time() > end_time:
window2["time_left"].update(f"{end_time-time.time():.2f}")
else:
window2["time_left"].update("time is up - no bonus")
# TODO: windwos (every?) computers have different key-codes!
#if ":" in event2:
# print(player_pos_y, player_pos_x)
#if event2 == "Up:111":
if event2.startswith("Up:"):
if player_pos_y > 0 and lines[player_pos_y-1][player_pos_x] == 0:
player_pos_y -= 1
figures.append(c.draw_rectangle(
top_left=(player_pos_x, player_pos_y),
bottom_right=(player_pos_x+1, player_pos_y+1),
fill_color="green" ))
#if event2 == "Left:113":
if event2.startswith("Left:"):
if player_pos_x >0 and lines[player_pos_y][player_pos_x-1] == 0:
player_pos_x -= 1
figures.append(c.draw_rectangle(
top_left=(player_pos_x, player_pos_y),
bottom_right=(player_pos_x+1, player_pos_y+1),
fill_color="green" ))
#if event2 == "Down:116":
if event2.startswith("Down:"):
if (player_pos_y < (y_tiles-1)) and (lines[player_pos_y+1][player_pos_x] == 0):
player_pos_y += 1
figures.append(c.draw_rectangle(
top_left=(player_pos_x, player_pos_y),
bottom_right=(player_pos_x+1, player_pos_y+1),
fill_color="green" ))
#if event2 == "Right:114":
if event2.startswith("Right"):
if (player_pos_x < (x_tiles-1)) and (lines[player_pos_y][player_pos_x+1] == 0):
player_pos_x += 1
figures.append(c.draw_rectangle(
top_left=(player_pos_x, player_pos_y),
bottom_right=(player_pos_x+1, player_pos_y+1),
fill_color="green" ))
# ------ always ----
c.delete_figure(player)
player = c.draw_rectangle(top_left=(player_pos_x,player_pos_y), bottom_right=(player_pos_x+1,player_pos_y+1), fill_color="#00FF00")
window2.close()
print("bye")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment