From 04a311ef0adc13055f882624af652d070c604abe Mon Sep 17 00:00:00 2001 From: thatretrodev Date: Mon, 20 Jun 2022 13:55:10 -0400 Subject: [PATCH 1/2] Add a GUI system --- .gitignore | 1 + src/__init__.py | 23 +++++++++++-- src/controls.py | 2 -- src/globals.py | 38 +++++++++------------ src/gmglobal.py | 86 ---------------------------------------------- src/gmplay.py | 1 - src/gui.py | 90 +++++++++++++++++++++++++++++++++++++++++++++++++ src/init.py | 1 - src/utils.py | 5 ++- 9 files changed, 128 insertions(+), 119 deletions(-) create mode 100644 .gitignore delete mode 100755 src/gmglobal.py create mode 100644 src/gui.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..600d2d3 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.vscode \ No newline at end of file diff --git a/src/__init__.py b/src/__init__.py index 7168fe7..145b692 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -1,8 +1,14 @@ from .actors import * from .init import * +from .gui import * def start_game(): + gui_screen = GUIScreen() + + gui_screen.widgets.append(Button(0, 0, sprite_button_fight, sprite_button_fight_dark)) + game.game_mode = game_play + p = GameMap("res/map/test_for_PGE.json") p.draw_tiles() @@ -13,24 +19,35 @@ def start_game(): new_actor(Slime, 300, 250, None, "actorlayer") - # new_actor(Slime, 500, 400, None, "actorlayer") - ############### Testing ############### ############ Main game loop ############## running = True + frame = 0 while running: clock.tick(FPS) for event in pygame.event.get(): keyboard.handle_event(event) + + if gui_screen.handle_event(window, frame, event): + continue + if event.type == pygame.QUIT: running = False window.fill(BLACK) + game.game_mode() game.run() - pygame.display.update() \ No newline at end of file + if game.game_mode == battle_mode: + gui_screen.widgets[0].x = (window.get_width() // 2) - gui_screen.widgets[0].image.get_rect().size[0] + gui_screen.widgets[0].y = (window.get_height() // 2) - gui_screen.widgets[0].image.get_rect().size[1] + + gui_screen.run(window) + + pygame.display.update() + frame += 1 \ No newline at end of file diff --git a/src/controls.py b/src/controls.py index 8f9965e..4b77f8d 100755 --- a/src/controls.py +++ b/src/controls.py @@ -1,7 +1,6 @@ from .globals import * import time - state = { "keys": {}, "left": {"press": False, "hold": 0, "held": False}, @@ -12,7 +11,6 @@ "accept": {"press": False, "hold": 0, "held": False}, } - def get_control(_control, _state): state["keys"] = pygame.key.get_pressed() for i in config["key"]: diff --git a/src/globals.py b/src/globals.py index 0360cb7..2fdd8aa 100644 --- a/src/globals.py +++ b/src/globals.py @@ -1,9 +1,7 @@ import pygame - pygame.init() - ################ Colors ################# RED = (255, 0, 0) @@ -23,27 +21,6 @@ clock = pygame.time.Clock() game_mode = None -# ############ Sprite Sheets ############# -# -# -# def draw_sprite(spr, frame, x, y): -# window.blit(spr, (x, y), frame) -# -# ########## Additional functions ########## -# -# -# def draw_text(font, x, y, text): -# window.blit(font.render(text, True, RED), (x, y)) -# -# -# def json_write(path, data): -# json.dump(data, open(path, "w")) -# -# -# def json_read(path): -# return json.loads(open(path, "r").read()) - - #################### Configurations ###################### config = { @@ -76,3 +53,18 @@ sprite_slime = pygame.image.load("res/gfx/tiles/slimes sheet.png").convert_alpha() sprite_soul = pygame.image.load("res/gfx/Soul/soul.png").convert_alpha() sprite_bullet = pygame.image.load("res/gfx/Soul/bullet.png").convert_alpha() + +######################## GUI Sprites ######################## + +sprite_cursor = pygame.image.load("res/gfx/engine/cursor.png").convert_alpha() +sprite_button_defend = pygame.image.load("res/gfx/Battle/button-defend.png").convert_alpha() +sprite_button_defend_dark = pygame.image.load("res/gfx/Battle/button-defend-dark.png").convert_alpha() +sprite_button_fight = pygame.image.load("res/gfx/Battle/button-fight.png").convert_alpha() +sprite_button_fight_dark = pygame.image.load("res/gfx/Battle/button-fight-dark.png").convert_alpha() +sprite_button_end = pygame.image.load("res/gfx/Battle/button-end.png").convert_alpha() +sprite_button_end_dark = pygame.image.load("res/gfx/Battle/button-end-dark.png").convert_alpha() + +######################## GUI Sounds ######################## + +# TODO: This sound is a placeholder (and I disabled it), replace it with something else +#sound_menu_select = pygame.mixer.Sound("res/sfx/menu-select.ogg") \ No newline at end of file diff --git a/src/gmglobal.py b/src/gmglobal.py deleted file mode 100755 index 133d7ea..0000000 --- a/src/gmglobal.py +++ /dev/null @@ -1,86 +0,0 @@ -import pygame -import json - - -pygame.font.init() - -############ Sprite Sheets ############# - - -def draw_sprite(spr, frame, x, y): - window.blit(spr, (x, y), frame) - - -################ Colors ################# - -RED = (255, 0, 0) -BLACK = (0, 0, 0) -GREEN = (0, 255, 0) -BLUE = (0, 0, 255) -YELLOW = (255, 255, 0) - -########### Window Properties ########### - -display_w, display_H = 400, 240 -Font = pygame.font.SysFont("comicsans", 40) -pygame.display.set_caption("Tuxtale [Python Edition]") - -window = pygame.display.set_mode((display_w, display_H), pygame.RESIZABLE | pygame.SCALED) -FPS = 60 -clock = pygame.time.Clock() -game_mode = None - - -########## Additional functions ########## - - -def draw_text(_font, _x, _y, text): - text = _font.render(text, 1, RED) - window.blit(text, (_x, _y)) - - -def json_write(path, data): - with open(path, "w") as fp: - json.dump(data, fp) - - -def json_read(path): - f = open(path, "r") - Data = f.read() - f.close() - return json.loads(Data) - - -#################### Configurations ###################### - -config = { - "key": { - "up": pygame.K_UP, - "down": pygame.K_DOWN, - "left": pygame.K_LEFT, - "right": pygame.K_RIGHT, - "pause": pygame.K_ESCAPE, - "accept": pygame.K_RETURN, - } -} - -RIGHT = config["key"]["right"] -LEFT = config["key"]["left"] -UP = config["key"]["up"] -DOWN = config["key"]["down"] -PAUSE = config["key"]["pause"] -ACCEPT = config["key"]["accept"] - -######################## Game Data ######################## - -game_data = dict(map=None, posX=64, posY=64, cam_x=0, cam_y=0, dialogResponses={}) - -############# Sprite Sheets ############# - -sprite_tux = pygame.image.load("res/gfx/Tux/taletuxNL.png").convert_alpha() -sprite_block = pygame.image.load("res/gfx/tiles/block.png").convert_alpha() -sprite_marbel = pygame.image.load("res/gfx/tiles/blue_marbel 2.5d_v1.0.png").convert_alpha() -sprite_slime = pygame.image.load("res/gfx/tiles/slimes sheet.png").convert_alpha() -sprite_soul = pygame.image.load("res/gfx/Soul/soul.png").convert_alpha() -sprite_bullet = pygame.image.load("res/gfx/Soul/bullet.png").convert_alpha() -sprite_tree = pygame.image.load("res/gfx/tiles/big tree.png").convert_alpha() diff --git a/src/gmplay.py b/src/gmplay.py index a2ead0e..96895ee 100644 --- a/src/gmplay.py +++ b/src/gmplay.py @@ -1,6 +1,5 @@ from .init import * - def game_play(): if game: # 1) UPDATE PHASE diff --git a/src/gui.py b/src/gui.py new file mode 100644 index 0000000..2f16777 --- /dev/null +++ b/src/gui.py @@ -0,0 +1,90 @@ +from .globals import * + +def convert_to_centered(display, size, x, y): + return (((display.get_width() // 2) - (size[0] // 2)) + x, ((display.get_height() // 2) - (size[1] // 2)) + y) + +class GUIScreen: + def __init__(self): + self.widgets = [] + def handle_event(self, display, frame, event): + for i in self.widgets: + if i.handle_event(display, frame, event): + return True + + return False + def run(self, display): + # Hide the original cursor + + pygame.mouse.set_visible(False) + + # Make a rect for the new cursor + + rect = sprite_cursor.get_rect() + rect.center = pygame.mouse.get_pos() + + # Render widgets + + for i in self.widgets: + i.run(display) + + # Show the new cursor on the screen + + display.blit(sprite_cursor, rect) + +class GUIWidget: + def __init__(self, x, y): + self.x = x + self.y = y + def handle_event(self, display, event): + pass + def run(self, display): + pass + +class Button(GUIWidget): + def __init__(self, x, y, image, image_dark): + if image.get_rect().size != image_dark.get_rect().size: + raise Exception("The normal Button image is not the same size as the dark Button image.") + + super().__init__(x, y) + + self.image = image + self.image_dark = image_dark + self.clicked = False + #self.cooldown = 0 + + def handle_event(self, display, frame, event): + button_image = self.image + button_size = button_image.get_rect().size + button_position = convert_to_centered(display, button_size, self.x, self.y) + button_rect = pygame.Rect(button_position[0], button_position[1], button_size[0], button_size[1]) + + if button_rect.collidepoint(pygame.mouse.get_pos()) and event.type == pygame.MOUSEBUTTONUP: + self.clicked = False + return True + + #if 0 > self.cooldown: + # self.cooldown = 0 + + #if self.cooldown != 0: + # if frame > self.cooldown: + # self.cooldown = 0 + # return False + + if button_rect.collidepoint(pygame.mouse.get_pos()) and event.type == pygame.MOUSEBUTTONDOWN: + #sound_menu_select.play() + self.clicked = True + #self.cooldown = frame + int(FPS * 0.7) + return True + + return False + + def run(self, display): + button_image = self.image + button_size = button_image.get_rect().size + button_position = convert_to_centered(display, button_size, self.x, self.y) + button_rect = pygame.Rect(button_position[0], button_position[1], button_size[0], button_size[1]) + + if self.clicked: + button_image = self.image_dark + + display.blit(button_image, button_position) \ No newline at end of file diff --git a/src/init.py b/src/init.py index 036ff5a..de9f4bf 100755 --- a/src/init.py +++ b/src/init.py @@ -1,7 +1,6 @@ from .globals import * from .utils import * - class Game: def __init__(self): self.game_mode = None diff --git a/src/utils.py b/src/utils.py index c9a4302..782af06 100644 --- a/src/utils.py +++ b/src/utils.py @@ -1,6 +1,5 @@ -import json - from .globals import window +import json def draw_sprite(spr, frame, x, y): @@ -16,4 +15,4 @@ def json_write(path, data): def json_read(path): - return json.loads(open(path, "r").read()) + return json.loads(open(path, "r").read()) \ No newline at end of file From 3a5a9368d7fd11a1bb39aaaab57e53b07414afe6 Mon Sep 17 00:00:00 2001 From: thatretrodev Date: Mon, 20 Jun 2022 14:03:21 -0400 Subject: [PATCH 2/2] Hide cursor on every game mode --- src/__init__.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/__init__.py b/src/__init__.py index 145b692..e32fe4f 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -39,6 +39,8 @@ def start_game(): running = False window.fill(BLACK) + + pygame.mouse.set_visible(False) game.game_mode() game.run()