Skip to content

setting up screens #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
1 change: 0 additions & 1 deletion .idea/PracticalPython-PyGame.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion common.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import pygame as pg
import matplotlib.colors as mcl

# Colors
COLORS = {"black": pg.Color('#000000')} #Perhaps more to come?
COLORS: dict = mcl.CSS4_COLORS

# Tiles and Screen
TILE_SIZE = 64
Expand Down
10 changes: 10 additions & 0 deletions game.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from common import COLORS, HEIGHT, WIDTH, FPS
from pygame_gui.core.interfaces.manager_interface import IUIManagerInterface


class Game:
def __init__(self, run):
self.run = run
print("Game starting.")
self.run.manager.clear_and_reset()
self.run.background.fill(COLORS["aqua"])
35 changes: 35 additions & 0 deletions home.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import pygame as pg
import pygame_gui as gui
from common import COLORS, HEIGHT, WIDTH, FPS
from game import Game
from settings import Settings
from other import Other


class HomeScreen:

def __init__(self, run):
self.run = run
self.home_screen()

def home_screen(self):
self.set_buttons()
if self.run.buttons:
self.run.btn_dict = dict(zip(self.run.buttons, self.run.btn_class))

def set_buttons(self):
def offset_button(top_left):
padding = 80
top_left[1] += padding
return top_left

btn_width, btn_height = 100, 50
top_left = [WIDTH // 2 - btn_width // 2, btn_height]
dimensions = (btn_width, btn_height)
button = pg.Rect(top_left, dimensions)
for btn_name in self.run.btn_names:
btn = gui.elements.UIButton(relative_rect=button,
text=btn_name,
manager=self.run.manager)
self.run.buttons.append(btn)
button = pg.Rect(offset_button(top_left), dimensions)
96 changes: 37 additions & 59 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,49 +1,31 @@
import pygame as pg
import pygame_gui

import pygame_gui as gui
from home import HomeScreen
from game import Game
from settings import Settings
from other import Other
from common import COLORS, HEIGHT, WIDTH, FPS
from pygame_gui.core.interfaces.manager_interface import IUIManagerInterface


class RunGame:
class Run:
def __init__(self):
pg.init()
self.game_state = HomeScreen
self.clock = pg.time.Clock()
self.screen_size = (WIDTH, HEIGHT)
self.btn_names = ['START', 'SETTINGS', 'OTHER']
self.btn_class = [Game, Settings, Other]
self.buttons = list()
self.init_ui()
self.events = self.get_events()
self.game_loop()


def init_ui(self):
pg.init()
pg.display.set_caption('Quick Start')
self.window_surface = pg.display.set_mode(self.screen_size)
self.background = pg.Surface(self.screen_size)
self.background.fill(COLORS["black"])

self.manager = pygame_gui.UIManager(self.screen_size)

self.set_buttons()


def set_buttons(self):
def offset_button(top_left):
padding = 80
top_left[1] += padding
return top_left

btn_width, btn_height = 100, 50
top_left = [WIDTH//2 - btn_width//2, btn_height]
dimensions = (btn_width, btn_height)

self.button = pg.Rect(top_left, dimensions)
self.btn_names = ['START', 'SETTINGS', 'OTHER']
for btn_name in self.btn_names:
btn = pygame_gui.elements.UIButton(relative_rect=self.button,
text=btn_name,
manager=self.manager)
self.buttons.append(btn)
self.button = pg.Rect(offset_button(top_left), dimensions)

self.manager = gui.UIManager(self.screen_size)

def get_events(self):
"""
Expand All @@ -55,7 +37,6 @@ def get_events(self):
mouse_pos = pg.mouse.get_pos()
raw_dt = self.clock.get_time()
dt = raw_dt * FPS

return {
"events": events,
"mouse press": mouse_press,
Expand All @@ -65,40 +46,37 @@ def get_events(self):
"dt": dt,
}


def event_handler(self, running):
for event in self.events["events"]:
if event.type == pg.QUIT:
return not running
if (event.type == gui.UI_BUTTON_PRESSED
and self.buttons):
for btn in self.buttons:
if event.ui_element == btn:
self.game_state = self.btn_dict[btn]
self.manager.process_events(event)

def game_loop(self):

is_running = True

while is_running:

#time_delta = clock.tick(FPS) / 1000.0

self.clock.tick(FPS)

events = self.get_events()

time_delta = events["dt"]

for event in events["events"]:
if event.type == pg.QUIT:
is_running = False

if event.type == pygame_gui.UI_BUTTON_PRESSED:
for btn in self.buttons:
if event.ui_element == btn:
print('Hello World!')

self.manager.process_events(event)

time_delta = self.events["dt"]
self.events = self.get_events()
tmp = self.event_handler(is_running)
# return: bool if event == self.pg.QUIT
if tmp is not None:
is_running = tmp
if self.game_state == HomeScreen:
HomeScreen(self)
else: # state changed on button press
run = self.game_state
run(self)
self.manager.update(time_delta)

self.window_surface.blit(self.background, (0, 0))
self.manager.draw_ui(self.window_surface)

pg.display.update()


RunGame()
# Close pygame
pg.quit()
Run()
pg.quit()
10 changes: 10 additions & 0 deletions other.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from common import COLORS, HEIGHT, WIDTH, FPS


class Other:

def __init__(self, run):
self.run = run
print("Hello from the other side.")
self.run.manager.clear_and_reset()
self.run.background.fill(COLORS["yellowgreen"])
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ pygame==2.5.1
pygame-ce==2.3.1
pygame-gui==0.6.9
python-i18n==0.3.9

colour~=0.1.5
matplotlib
10 changes: 10 additions & 0 deletions settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from common import COLORS, HEIGHT, WIDTH, FPS


class Settings:

def __init__(self, run):
self.run = run
print("Hello from settings.")
self.run.manager.clear_and_reset()
self.run.background.fill(COLORS["magenta"])