Skip to content

Commit

Permalink
Changed the imp,entation of title screen, add button implentation for…
Browse files Browse the repository at this point in the history
… future use, add comments to code files
  • Loading branch information
Sfever authored Mar 11, 2024
1 parent e8982be commit c3fe294
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 33 deletions.
14 changes: 14 additions & 0 deletions codes/button.py
Original file line number Diff line number Diff line change
@@ -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()

3 changes: 3 additions & 0 deletions codes/init.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
'''
Codes used to initalize pygame and config
'''
import pygame as pg
import warnings
import json
Expand Down
43 changes: 43 additions & 0 deletions codes/startscreen.py
Original file line number Diff line number Diff line change
@@ -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()


9 changes: 7 additions & 2 deletions codes/textview.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
return self.textlist[index]
Just for idiots, i think don't give 'em text object either
'''
41 changes: 10 additions & 31 deletions main.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit c3fe294

Please sign in to comment.