Skip to content

Instantly share code, notes, and snippets.

@horstjens
Last active Jun 20, 2022
Embed
What would you like to do?
draft3
import PySimpleGUI as sg
import random
import time
RESOLUTIONS = ["100x100", "200x200", "320x200", "400x400", "600x600", "640x400","800x600","1024x800", "1024x960","1280x1024"]
WIDTH, HEIGHT = 400,400
MIN_TILES = 10
MAX_TILES = 50
TX = 40
TY = 40
HELPTEXT = """Write here instructions for your game.
You can use several lines.
"""
#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=(MIN_TILES,MAX_TILES), default_value=10,orientation="h", key="x_slider", enable_events=True,)],
[sg.Text("# y-tiles:"),sg.Slider(range=(MIN_TILES,MAX_TILES), default_value=10,orientation="h", key="y_slider", enable_events=True)],
[sg.Text("10 x 10 = 100 tiles", key="tiles")],
[sg.Text("canvas resolution in Pixel:"),
sg.Combo(values=RESOLUTIONS, key="resolution", default_value="400x400", enable_events=True)],
#[sg.Text("Pause in [ms]:"),sg.Slider(range=(0,1000), default_value=500,orientation="h", key="pause_slider")],
[sg.Text("tile size in pixel:"), sg.Text("40 x 40",key="tile_size")],
[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)
if event1 in ("x_slider", "y_slider"):
window1["tiles"].update(f'{int(values1["x_slider"])} x {int(values1["y_slider"])} = {int(values1["x_slider"])*int(values1["y_slider"])} tiles')
if event1 in ("x_slider", "y_slider", "resolution"):
WIDTH, HEIGHT = [int(v) for v in values1["resolution"].split("x")]
TX = WIDTH / int(values1["x_slider"])
TY = HEIGHT / int(values1["y_slider"])
tile_size_string = ""
if TX == int(TX):
tile_size_string += f"{TX:.0f} x "
else:
tile_size_string += f"{TX:.1f} x "
if TY == int(TY):
tile_size_string += f"{TY:.0f}"
else:
tile_size_string += f"{TY:.1f}"
window1["tile_size"].update(tile_size_string)
WIDTH, HEIGHT = [int(v) for v in values1["resolution"].split("x")]
X_TILES = int(values1["x_slider"])
Y_TILES = int(values1["y_slider"])
window1.close()
# -----------------end of theme chooser -------------------
# --------------- start of labyrinth game ------------------
class Game:
number = 0
figures = []
lines = []
def is_free(x,y):
"""check if there is a 0 in Game.lines[y][x]"""
print("is free ",x,y)
if x < 0 or y < 0:
return False
try:
result = Game.lines[y][x]
except IndexError:
return False
if Game.lines[y][x] == 0:
return True
else:
return False
def create_blocks(min_number=0, max_number=7, ):
"""create random numbers in array Game.lines[]"""
for y, line in enumerate(Game.lines):
for x, number in enumerate(line):
#new_value = random.randint(min_number, max_number)
new_value=random.choice((0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,7))
#print(x,y,"old value:", lines[y][x], "new value:", new_value)
Game.lines[y][x]= new_value
def delete_old_figures():
"""delete all figures from canvas (except player and cursor)"""
for f in Game.figures:
c.delete_figure(f)
Game.figures = []
def delete_old_blocks():
"""delete all old blocks"""
Game.lines = [[0 for x in range(X_TILES)] for y in range(Y_TILES)]
#for f in Game.figures:
# c.delete_figure(f)
#Game.figures = []
def paint_blocks():
"""create colorful blocks, depending on the value in lines
adds the Tkinter figure into the list figures"""
#print(lines)
colordict = {
0: "#000000", # black,
1: "#FF0000", # red
2: "#00FF00", # green
3: "#0000FF", # blue
4: "#FFFF00", # yellow
5: "#FF00FF", # violet
6: "#00FFFF", # cyan
7: "#FFFFFF", # white
}
for y, line in enumerate(Game.lines):
for x, number in enumerate(line):
color = colordict[number]
# do not paint black, just leave it
if color == 0:
continue
Game.figures.append(c.draw_rectangle(top_left=(x,y),
bottom_right=(x+1,y+1),
fill_color = color))
layout2 = [
#[sg.Text("Playce your ships:", font=["System",32]),
# sg.Text("Battleship", key="head1", font=["System",32]),
[ sg.Text("Cursor:"),
sg.Text("mouse xy",key="mousepos"),
sg.Text("Player:"),
sg.Text("player xy", key="playerpos"),
],
[sg.Text("show:"),
sg.Radio("instructions", group_id="showgroup", default=True, enable_events=True, key="instructions"),
sg.Radio("lines", group_id="showgroup", default=False,enable_events=True, key="lines"),
sg.Radio("figures", group_id="showgroup", default=False, enable_events=True, key="figures"),
sg.ColorChooserButton("fg color:", "fg_color_input", key="button_fg"),
sg.Input(default_text="#FFFFFF", key="fg_color_input", size=(8,1), enable_events=True, disabled=True),
sg.ColorChooserButton("bg color:", "bg_color_input", key="button_bg"),
sg.Input(default_text="#FFFFFF", key="bg_color_input", size=(8,1), enable_events=True, disabled = True),
],
# sg.Checkbox("show grid", default=False, change_submits=True, key="show_grid")],
[sg.Multiline(default_text=HELPTEXT, size=(90,6), disabled=True, key="multi"),],
[sg.Button("new blocks"),sg.Button("delete blocks"),sg.Button("delete figures"), sg.Button("paint"), sg.Button("Exit"), ],
[sg.Graph(canvas_size=(WIDTH,HEIGHT),
key="canvas", graph_bottom_left=(0, Y_TILES),
graph_top_right=(X_TILES,0 ),
background_color="#333333",
enable_events=True, drag_submits=True, motion_events=True)],
]
window2 = sg.Window("enjoy the game", layout2, return_keyboard_events=True)
window2.finalize() # necessary to be able to draw on canvas
c = window2["canvas"]
# ----- create labyrinth -----
# fill labyrinth with zeros
Game.lines = [[0 for x in range(X_TILES)] for y in range(Y_TILES)]
Game.figures = [] # for Tkinter
#delete_old_blocks()
create_blocks()
paint_blocks()
# ------------- main loop -----------
duration = 60
player_pos_x = 0
player_pos_y = 0
cat_pos_x = X_TILES // 2
cat_pos_y = Y_TILES // 2
cursor_pos_x = 0
cursor_pos_y = 0
#figures.append(make_trail())
player = c.draw_rectangle(top_left=(0,0), bottom_right=(1,1), fill_color="#55FF55")
cursor = c.draw_rectangle(top_left=(0,0), bottom_right=(1,1), line_width=5, line_color="#D1D1D1", fill_color=None)
cat = c.draw_rectangle(top_left=(cat_pos_x,cat_pos_y),
bottom_right=(cat_pos_x+1,cat_pos_y+1), line_width=5, fill_color="#FF0000")
#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 True:
event2, values2 = window2.read(timeout=100)
#print(event2, values2)
# print all values except the multiline field
#print("Event:",event2, "Values:", {k:v for k,v in values2.items() if k != "multi"})
moved = False
if event2 in ("Exit", sg.WINDOW_CLOSED ):
break
if event2 == sg.TIMEOUT_EVENT:
# ----- move cat -----
possible_directions = []
for (dx,dy) in [(1,0),(1,-1),(0,-1),(-1,-1),(-1,0),(-1,1),(0,1),(1,1)]:
if is_free(cat_pos_x+dx, cat_pos_y+dy):
if (cat_pos_x+dx, cat_pos_y+dy) != (player_pos_x, player_pos_y):
possible_directions.append((dx,dy))
if len(possible_directions) == 0:
print("cat catched")
break
dx, dy = random.choice(possible_directions)
cat_pos_x += dx
cat_pos_y += dy
c.move_figure(cat, dx, dy )
if (player_pos_x == cat_pos_x) and (player_pos_y == cat_pos_y):
sg.PopupOK("you caught the cat")
break
#----- color choosen ----
if event2 == "fg_color_input":
text = values2["fg_color_input"]
print("new fg:", values2["fg_color_input"])
# text it IS already in the format #RRGGBB because of the color chooser buttons
window2["button_fg"].update(button_color=text)
#window2["button_fg"].update(default_color=text)
#window2.refresh()
#window2["fg_color_input"].update(background_color=text)
# --- toggle showgroup ---
if event2=="instructions":
window2["multi"].update(HELPTEXT)
if event2 == "lines":
window2["multi"].update("\n".join([str(line) for line in Game.lines]))
if event2 == "figures":
window2["multi"].update(f"# of figures: {len(Game.figures)}\n{Game.figures}")
if event2 == "new blocks":
#delete_old_blocks()
create_blocks()
#paint_blocks()
c.bring_figure_to_front(player)
if event2 == "delete blocks":
delete_old_blocks()
if event2 == "delete figures":
delete_old_figures()
if event2 == "paint":
paint_blocks()
# sg.PopupScrolled(Game.figures)
# mouse movement event
if event2.endswith("+MOVE"):
mousepos = values2["canvas"]
window2["mousepos"].update(mousepos)
cursor_pos_x, cursor_pos_y = mousepos
c.relocate_figure(cursor, cursor_pos_x, cursor_pos_y)
c.bring_figure_to_front(cursor)
# mouse click down event
if event2 == "canvas":
mousepos = values2["canvas"]
print("mouseclick down at: ", mousepos)
# mouse click up event
if event2 == "canvas+UP":
mousepos = values2["canvas"]
print("mouseclick up at: ", mousepos)
moved = False
#if event2 == "Up:111":
if event2.startswith("Up:"):
if is_free(x=player_pos_x, y=player_pos_y-1):
player_pos_y -= 1
#figures.append(make_trail())
moved = True
#if event2 == "Left:113":
if event2.startswith("Left:"):
if is_free(x=player_pos_x-1, y=player_pos_y):
player_pos_x -= 1
#figures.append(make_trail())
moved = True
#if event2 == "Down:116":
if event2.startswith("Down:"):
if is_free(x=player_pos_x, y=player_pos_y+1):
player_pos_y += 1
#figures.append(make_trail())
moved = True
#if event2 == "Right:114":
if event2.startswith("Right"):
if is_free(x=player_pos_x+1, y=player_pos_y):
player_pos_x += 1
#figures.append(make_trail())
moved = True
# ------ always ----
if moved:
print("moving player")
# --------- move player ----
c.relocate_figure(player, player_pos_x, player_pos_y)
c.bring_figure_to_front(player)
window2["playerpos"].update(f"{player_pos_x},{player_pos_y}")
# ----- move cat -----
#possible_directions = []
#if is_free(cat_pos_x+1,cat_pos_y): # east
# possible_directions.append((1,0))
#if is_free(cat_pos_x+1,cat_pos_y-1): # north-east
# possible_directions.append((1,-1))
#if is_free(cat_pos_x,cat_pos_y-1): # north
# possible_directions.append((0,-1))
#if is_free(cat_pos_x-1,cat_pos_y-1): # north-west
# possible_directions.append((-1,-1))
#if is_free(cat_pos_x-1,cat_pos_y): # west
# possible_directions.append((-1,0))
#if is_free(cat_pos_x-1,cat_pos_y+1): # south-west
# possible_directions.append((-1,1))
#if is_free(cat_pos_x,cat_pos_y+1): # south
# possible_directions.append((0,1))
#if is_free(cat_pos_x+1,cat_pos_y+1): # south-east
# possible_directions.append((1,1))
#if len(possible_directions) == 0:
# print("cat catched")
# break
#dx, dy = random.choice(possible_directions)
#cat_pos_x += dx
#cat_pos_y += dy
#c.move_figure(cat, dx, dy )
if (player_pos_x == cat_pos_x) and (player_pos_y == cat_pos_y):
sg.PopupOK("you caught the cat")
break
window2.close()
print("bye")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment