Created
June 25, 2022 12:51
-
-
Save horstjens/462b540701ac2bad8b1d0912db8e1c5e to your computer and use it in GitHub Desktop.
breakout / pong
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import PySimpleGUI as sg | |
import random | |
import time | |
RESOLUTIONS = ["640x400","800x600","1024x800", "1024x960","1200x1024"] | |
WIDTH, HEIGHT = 800,600 | |
MIN_TILES = 10 | |
MAX_TILES = 200 | |
HELPTEXT = """catch the ball | |
User cursor keys to move left - right""" | |
# -------- 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=60,orientation="h", key="x_slider")], | |
[sg.Text("# y-tiles:"),sg.Slider(range=(MIN_TILES,MAX_TILES), default_value=40,orientation="h", key="y_slider")], | |
[sg.Text("canvas resolution in Pixel:"), | |
sg.Combo(values=RESOLUTIONS, key="resolution", default_value="600x400")], | |
#[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) | |
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: | |
dt = 10 | |
lines = [] | |
number = 0 | |
player_pos_x = X_TILES /2 | |
player_pos_y = Y_TILES -2 | |
player_width = 3 | |
ball_pos_x = X_TILES /2 | |
ball_pos_y = Y_TILES -5 | |
ball_dx = -0.2 | |
ball_dy = 0.8 | |
class Block: | |
def __init__(self): | |
self.number = Game.number | |
Game.number += 1 | |
Game.stones[self.number] = self | |
self.value = random.randint(1,7) | |
self.x = x | |
self.y = y | |
self.figure = c.draw_rectangle( | |
top_left=(self.x, self.y), | |
bottom_right=(self.x+1, self.y+1), | |
fill_color="red") | |
#def cmp(a, b): | |
# """returns 0, -1 or 1""" | |
# return (a > b) - (a < b) | |
layout2 = [ | |
[sg.Text("Time left (seconds):", font=["System",32]), | |
sg.Text("60", key="time_left", font=["System",32]), | |
], | |
[sg.Text("player: x,y ", key="playerpos"), | |
sg.Text("ball: x,y ", key="ballpos")], | |
#sg.Text("mouse xy",key="mousepos")], | |
[sg.Multiline(default_text=HELPTEXT, size=(90,3), disabled=True), | |
sg.Button("Exit"), ], | |
[sg.Graph(canvas_size=(WIDTH,HEIGHT),background_color="grey", | |
key="canvas", graph_bottom_left=(0, Y_TILES), | |
graph_top_right=(X_TILES,0 ), | |
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 ----- | |
# ------------- main loop ----------- | |
duration = 60 | |
# create player sprite and save it into Game.player | |
Game.player = c.draw_rectangle(top_left=(Game.player_pos_y,Game.player_pos_y), | |
bottom_right=(Game.player_pos_x+Game.player_width,Game.player_pos_y+1), | |
fill_color="#00FF00") | |
# create Ball | |
Game.ball = c.draw_circle( | |
center_location=(Game.ball_pos_x+0.5,Game.ball_pos_y+0.5), | |
radius = 0.4, | |
fill_color = "#0000FF", | |
line_color = "black", | |
line_width = 1) | |
# create Blocks | |
#create_blocks() | |
end_time = time.time() + duration | |
while True: | |
event2, values2 = window2.read(timeout=Game.dt) | |
#print(event2, values2) | |
moved = False | |
if event2 in ("Exit", sg.WINDOW_CLOSED ): | |
break | |
if event2 == sg.TIMEOUT_EVENT: | |
now = time.time() | |
window2["time_left"].update(f"{(end_time - now):.1f}") | |
window2["playerpos"].update(f"player:{Game.player_pos_x:.1f},{Game.player_pos_y:.1f}") | |
window2["ballpos"].update(f"ball:{Game.ball_pos_x:.1f},{Game.ball_pos_y:.1f}") | |
# ---- move the ball --- | |
# calculate new pos for ball, THEN move the figure | |
Game.ball_pos_x += Game.ball_dx * Game.dt/1000 | |
Game.ball_pos_y += Game.ball_dy * Game.dt/1000 | |
# check if bounce from wall | |
# west | |
if Game.ball_pos_x < 0: | |
Game.ball_pos_x = 0 # stop at wall | |
Game.ball_dx *= -1 # bounce | |
# east | |
if Game.ball_pos_x > X_TILES-1: | |
Game.ball_pos_x = X_TILES-1 | |
Game.ball_dx *= -1 # bonce | |
# north | |
if Game.ball_pos_y > Y_TILES-1: | |
Game.ball_pos_y = Y_TILES-1 | |
Game.ball_dy *= -1 # bonce | |
# south | |
if Game.ball_pos_y < 0: | |
Game.ball_pos_y = 0 | |
Game.ball_dy *= -1 # bonce | |
# bounce from player: (only check if moving south) | |
if Game.ball_dy > 0: | |
print("player:",Game.player_pos_y, Game.player_pos_x, "ball:", Game.ball_pos_y, Game.ball_pos_x) | |
if int(Game.player_pos_y) == int(Game.ball_pos_y)+1: | |
if Game.player_pos_x <= Game.ball_pos_x < Game.player_pos_x + Game.player_width: | |
print("bounce from player") | |
# stop and bounce from player | |
#Game.ball_pos_y = Game.player_pos_y - 1 # above player! | |
Game.ball_pos_y = int(Game.player_pos_y)-1 | |
Game.ball_dy *= -1 | |
# give a bit extra dx if ball is near the edge of player bat | |
# near left edge: | |
#if Game.ball_pos_x < Game.player_pos_x + 0.25 * Game.player_width: | |
# Game.ball_dx *= random.uniform(1.1,1.5) | |
#if Game.ball_pos_x > Game.player_pos_x + 0.75 * Game.player_width: | |
# Game.ball_dx *= random.uniform(1.1,1.5) | |
c.relocate_figure(Game.ball, Game.ball_pos_x, Game.ball_pos_y) | |
# mouse event | |
#if event2.endswith("+MOVE"): | |
# mousepos = values2["canvas"] | |
# window2["mousepos"].update(mousepos) | |
# c.relocate_figure(cursor, mousepos[0], mousepos[1]) | |
# c.bring_figure_to_front(cursor) | |
# --------- key handler ---------- | |
if event2.startswith("Left:"): | |
if Game.player_pos_x > 0: | |
Game.player_pos_x -= 1 | |
moved = True | |
if event2.startswith("Right"): | |
if Game.player_pos_x < X_TILES -1 - Game.player_width : | |
Game.player_pos_x += 1 | |
moved = True | |
# ------ always ---- | |
if moved: | |
# --------- move player ---- | |
c.relocate_figure(Game.player, Game.player_pos_x, Game.player_pos_y) | |
c.bring_figure_to_front(Game.player) | |
# --------- move monsters ---- | |
#for m in Game.monsters.values(): | |
# dx,dy = m.hunt(player_pos_x, player_pos_y) | |
# if is_free(m.x+dx, m.y+dy): | |
# m.x +=dx | |
# m.y +=dy | |
# c.relocate_figure(m.figure, m.x, m.y) | |
#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