diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..82aa3da Binary files /dev/null and b/.DS_Store differ diff --git a/.idea/PracticalPython-PyGame.iml b/.idea/PracticalPython-PyGame.iml index 9919c40..8e5446a 100644 --- a/.idea/PracticalPython-PyGame.iml +++ b/.idea/PracticalPython-PyGame.iml @@ -2,7 +2,6 @@ - diff --git a/common.py b/common.py index 75a6d7c..334db30 100644 --- a/common.py +++ b/common.py @@ -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 diff --git a/game.py b/game.py new file mode 100644 index 0000000..916e69d --- /dev/null +++ b/game.py @@ -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"]) diff --git a/home.py b/home.py new file mode 100644 index 0000000..edd5157 --- /dev/null +++ b/home.py @@ -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) diff --git a/main.py b/main.py index 70ad3c1..64afd57 100644 --- a/main.py +++ b/main.py @@ -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): """ @@ -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, @@ -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() \ No newline at end of file +Run() +pg.quit() diff --git a/other.py b/other.py new file mode 100644 index 0000000..e05de17 --- /dev/null +++ b/other.py @@ -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"]) diff --git a/requirements.txt b/requirements.txt index c597399..6bad4f8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 \ No newline at end of file diff --git a/settings.py b/settings.py new file mode 100644 index 0000000..bd48e6a --- /dev/null +++ b/settings.py @@ -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"])