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 dcfb259..28406df 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -1,7 +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() game.game_mode = game_play @@ -14,24 +21,38 @@ def start_game(): new_actor(Sprite, 700, 1050, [sprite_tree, 0, (46, 59)], "actorlayer") #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) + + pygame.mouse.set_visible(False) + game.game_mode() game.run() + + 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) + textbox.load_dialogue() - #my_font.render(window, "My favourite song is Bohemian Rhapsody by Queen.", (0, 220)) - pygame.display.update() \ No newline at end of file + + pygame.display.update() + frame += 1 diff --git a/src/globals.py b/src/globals.py index a72ee84..28b6699 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 = { @@ -77,3 +54,18 @@ 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() + +######################## 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") 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 29a6596..41285fd 100755 --- a/src/init.py +++ b/src/init.py @@ -2,7 +2,6 @@ from .utils import * from .controls import* - class Game: def __init__(self): self.game_mode = None diff --git a/src/utils.py b/src/utils.py index e34edd3..c3ae8a8 100644 --- a/src/utils.py +++ b/src/utils.py @@ -1,7 +1,6 @@ -import json -import pygame - from .globals import window +import pygame +import json def draw_sprite(spr, frame, x, y): @@ -54,4 +53,4 @@ def ninepatch(surface: pygame.Surface, rect: tuple): ) result.blit(c3, (rect.w - subsurf_w, rect.h - subsurf_h)) - return result \ No newline at end of file + return result