From c3fe29494347ad3ef7ac98ee320469e0c7f680ec Mon Sep 17 00:00:00 2001 From: Sfever <136332266+Sfever@users.noreply.github.com> Date: Mon, 11 Mar 2024 21:12:21 +0800 Subject: [PATCH] Changed the imp,entation of title screen, add button implentation for future use, add comments to code files --- codes/button.py | 14 ++++++++++++++ codes/init.py | 3 +++ codes/startscreen.py | 43 +++++++++++++++++++++++++++++++++++++++++++ codes/textview.py | 9 +++++++-- main.py | 41 ++++++++++------------------------------- 5 files changed, 77 insertions(+), 33 deletions(-) create mode 100644 codes/button.py create mode 100644 codes/startscreen.py diff --git a/codes/button.py b/codes/button.py new file mode 100644 index 0000000..6b4dcc3 --- /dev/null +++ b/codes/button.py @@ -0,0 +1,14 @@ +import pygame as pg +class button: + def __init__(self,posx,posy,width,height): + self.x=posx + self.y=posy + self.width=width + self.height=height + def onclick(self,action,mousepos): + if mousepos[0] > self.x: + if mousepos[1] > self.y: + if mousepos[0] < self.x+self.width: + if mousepos[1] < self.y+self.height: + action() + diff --git a/codes/init.py b/codes/init.py index 4211bf5..71d8034 100644 --- a/codes/init.py +++ b/codes/init.py @@ -1,3 +1,6 @@ +''' +Codes used to initalize pygame and config +''' import pygame as pg import warnings import json diff --git a/codes/startscreen.py b/codes/startscreen.py new file mode 100644 index 0000000..028329c --- /dev/null +++ b/codes/startscreen.py @@ -0,0 +1,43 @@ +''' +Start screen codes, Nothing more +''' +import pygame as pg +from . import textview as tv +import os +import json +class startscr: + bgexist=0 + textview="" + font4title="" + def __init__(self): + self.bgexist=0 + self.textview=tv.textview() + self.font4title=self.textview.new_text() + if os.access("../res/bg.png",os.R_OK): + self.bg=pg.image.load("../res/bg.png").convert() + self.bgexist=1 + print("Initilization of start screen complete successfully") + #loading texts + def load_title(self,main): + if main.config_exist==1: + config={} + with open(main.config_path,'r') as config_reader:#read configs + config=json.load(config_reader) + #load title text properties from config.json + self.textview.textlist[self.font4title].set_color(config["font_color"]) + self.textview.textlist[self.font4title].set_font(config["font"]) + self.textview.textlist[self.font4title].set_content(config["title"]) + self.textview.textlist[self.font4title].set_size(config["font_size"]) + print("Text is load successfully") + def show_title(self,window,main): + self.textview.textlist[self.font4title].set_text() + self.textview.textlist[self.font4title].render_surface() + F_width,F_height=self.textview.textlist[self.font4title].get_dimensions() + window.blit(self.textview.textlist[self.font4title].get_surface(),((main.get_reso_horizontal()-F_width)/2,100)) + pg.display.update() + def show_background(self,window): + if self.bgexist==1: + window.blit(self.bg,(0,0)) + pg.display.update() + + diff --git a/codes/textview.py b/codes/textview.py index d8a1c45..3b16819 100644 --- a/codes/textview.py +++ b/codes/textview.py @@ -39,21 +39,26 @@ def get_dimensions(self): def delete_text(self): self.surface.set_alpha(0) pg.display.update() + print("Successed to remove text") class textview: - textlist=[] def __init__(self): self.textlist=[] def new_text(self): newtext=textobj() self.textlist.append(newtext) + print("A text is added successfully") return len(self.textlist)-1 def del_text(self,tgt): self.textlist[tgt].delete_text() del self.textlist[tgt]#remove it and make become not accessible anymore + print("Successed to remove text") ''' self.textlist will not be provided outside the class in order to prevent losting access to textobj object accidentally ''' #returns the instance of textobj for devs to work on + ''' def get_object(self,index): - return self.textlist[index] \ No newline at end of file + return self.textlist[index] + Just for idiots, i think don't give 'em text object either + ''' \ No newline at end of file diff --git a/main.py b/main.py index fe811c1..381e570 100644 --- a/main.py +++ b/main.py @@ -1,47 +1,26 @@ from codes import init as base +from codes import startscreen as ss +from codes import button import pygame as pg import pygame.locals as pgl -import os -import json -from codes import textview #intialize objects main=base.inital() main.init_window() mainwindow=main.get_screen_object() -bgexist=0 -textlist=textview.textview() -font4title_index=textlist.new_text() -font4title=textlist.get_object(font4title_index) - -if os.access("./res/bg.png",os.R_OK): - bg=pg.image.load("./res/bg.png").convert() - bgexist=1 - -if main.config_exist==1: - config={} - with open(main.config_path,'r') as config_reader:#read configs - config=json.load(config_reader) - #load title text properties from config.json - font4title.set_color(config["font_color"]) - font4title.set_font(config["font"]) - font4title.set_content(config["title"]) - font4title.set_size(config["font_size"]) - -font4title.set_text() - +start_screen=ss.startscr() +start_screen.load_title(main) +easter_egg_quit_button=button.button(10,10,100,100) while True: for event in pg.event.get(): if event.type == pgl.QUIT: main.onquit() + if event.type==pgl.MOUSEBUTTONDOWN: + mousepos=pg.mouse.get_pos() + easter_egg_quit_button.onclick(main.onquit(),mousepos) #show background if it's there - if bgexist==1: - mainwindow.blit(bg,(0,0)) - - font4title.render_surface() - #get title text size for show it in center - F_width,F_height=font4title.get_dimensions() + start_screen.show_background(mainwindow) #show title text - mainwindow.blit(font4title.get_surface(),((main.get_reso_horizontal()-F_width)/2,100)) + start_screen.show_title(mainwindow,main) #Update display pg.display.update()