minor optimizations
Code is now a little simpler and new menus are easier to implement. Before you had to create a new function and all adjustments just to change the state. Now you use the same function which does it all for you. You use self.changeMenu("theNextState") and it switches the state for you and does all the stuff you had to code before.
This commit is contained in:
parent
4efd981e0d
commit
68a88548c0
106
core.py
106
core.py
@ -35,9 +35,9 @@ class Game:
|
|||||||
self.clear()
|
self.clear()
|
||||||
self.run()
|
self.run()
|
||||||
else:
|
else:
|
||||||
self.mainMenu()
|
self.changeMenu("mainMenu")
|
||||||
else:
|
else:
|
||||||
self.mainMenu()
|
self.changeMenu("mainMenu")
|
||||||
|
|
||||||
# >>> HERE'S THE GOD MODE, FOR THE GOD DAMN CREATOR ! >>>
|
# >>> HERE'S THE GOD MODE, FOR THE GOD DAMN CREATOR ! >>>
|
||||||
def debug(self):
|
def debug(self):
|
||||||
@ -110,48 +110,20 @@ class Game:
|
|||||||
self.mainMenuMusic = pg.mixer.Sound(os.path.join(musicFolder, 'mainMenu.ogg'))
|
self.mainMenuMusic = pg.mixer.Sound(os.path.join(musicFolder, 'mainMenu.ogg'))
|
||||||
self.playMusic = pg.mixer.Sound(os.path.join(musicFolder, 'play.ogg'))
|
self.playMusic = pg.mixer.Sound(os.path.join(musicFolder, 'play.ogg'))
|
||||||
|
|
||||||
# >>> SHOW THE MAIN MENU >>>
|
# >>> CHANGE THE MENU YOU ARE INTO >>>
|
||||||
def mainMenu(self):
|
def changeMenu (self, nextState):
|
||||||
self.washTheScreen(); pg.key.set_repeat(0, 100); self.previousState = self.state
|
|
||||||
pg.mouse.set_visible(True)
|
|
||||||
if not pg.mixer.Channel(0).get_busy() or pg.mixer.Channel(0).get_sound() != self.mainMenuMusic:
|
|
||||||
if self.musicState == "ON":
|
|
||||||
pg.mixer.Channel(0).play(self.mainMenuMusic, -1)
|
|
||||||
self.state = 'mainMenu'
|
|
||||||
self.run()
|
|
||||||
|
|
||||||
# >>> SHOW THE PLAY MENU >>>
|
|
||||||
def playMenu(self):
|
|
||||||
self.washTheScreen(); pg.key.set_repeat(0, 100); self.previousState = self.state
|
|
||||||
self.state = 'playMenu'
|
|
||||||
self.run()
|
|
||||||
|
|
||||||
# >>> SHOW THE NEW GAME MENU >>>
|
|
||||||
def newGame(self):
|
|
||||||
self.washTheScreen(); pg.key.set_repeat(0, 100); self.previousState = self.state
|
|
||||||
self.state = 'newGame'
|
|
||||||
self.run()
|
|
||||||
|
|
||||||
# >>> SHOW THE HELP MENU >>>
|
|
||||||
def helpMenu(self):
|
|
||||||
self.washTheScreen(); pg.key.set_repeat(0, 100); self.previousState = self.state
|
|
||||||
self.state = 'helpMenu'
|
|
||||||
self.run()
|
|
||||||
|
|
||||||
# >>> SHOW THE SETTINGS MENU >>>
|
|
||||||
def settingsMenu(self):
|
|
||||||
self.washTheScreen(); pg.key.set_repeat(0, 100); self.previousState = self.state
|
|
||||||
self.state = 'settingsMenu'
|
|
||||||
self.run()
|
|
||||||
|
|
||||||
# >>> PAUSE THE GAME >>>
|
|
||||||
def pause(self):
|
|
||||||
pg.key.set_repeat(0, 100); self.previousState = self.state; pg.mouse.set_visible(True)
|
pg.key.set_repeat(0, 100); self.previousState = self.state; pg.mouse.set_visible(True)
|
||||||
save.saveGame(self.gender, self.lifeLevel, self.gameLevel)
|
if nextState == "pause":
|
||||||
self.screen.blit(self.dimScreen,(0,0))
|
self.screen.blit(self.dimScreen,(0,0))
|
||||||
if self.musicState == "ON":
|
if self.musicState == "ON" and pg.mixer.Channel(0).get_busy():
|
||||||
pg.mixer.Channel(0).pause()
|
pg.mixer.Channel(0).pause()
|
||||||
self.state = 'pause'
|
else:
|
||||||
|
self.washTheScreen()
|
||||||
|
if nextState == "mainMenu":
|
||||||
|
if not pg.mixer.Channel(0).get_busy() or pg.mixer.Channel(0).get_sound() != self.mainMenuMusic:
|
||||||
|
if self.musicState == "ON":
|
||||||
|
pg.mixer.Channel(0).play(self.mainMenuMusic, -1)
|
||||||
|
self.state = nextState
|
||||||
self.run()
|
self.run()
|
||||||
|
|
||||||
# >>> LET THE MAGIC HAPPEN >>>
|
# >>> LET THE MAGIC HAPPEN >>>
|
||||||
@ -164,7 +136,7 @@ class Game:
|
|||||||
pg.mixer.Channel(0).unpause()
|
pg.mixer.Channel(0).unpause()
|
||||||
actualGame = save.readGame()
|
actualGame = save.readGame()
|
||||||
self.gender = actualGame[0]; self.lifeLevel = actualGame[1]; self.gameLevel = actualGame[2]
|
self.gender = actualGame[0]; self.lifeLevel = actualGame[1]; self.gameLevel = actualGame[2]
|
||||||
print("i'm a {} and i have {} percent life and i'm in level {}".format(actualGame[0],self.lifeLevel, self.gameLevel))
|
print("I'm a {} and I have {} percent life and I'm in level {}".format(actualGame[0],self.lifeLevel, self.gameLevel))
|
||||||
if self.gender == 'male':
|
if self.gender == 'male':
|
||||||
self.playerImg = self.playerImgMale
|
self.playerImg = self.playerImgMale
|
||||||
else:
|
else:
|
||||||
@ -303,7 +275,7 @@ class Game:
|
|||||||
if self.mainMenuBlit.collidepoint(pg.mouse.get_pos()) == True:
|
if self.mainMenuBlit.collidepoint(pg.mouse.get_pos()) == True:
|
||||||
self.textColorMainMenu = self.colorWhite; self.actualChoicePreview = self.malePreview
|
self.textColorMainMenu = self.colorWhite; self.actualChoicePreview = self.malePreview
|
||||||
if event.type == pg.MOUSEBUTTONDOWN and event.button == 1:
|
if event.type == pg.MOUSEBUTTONDOWN and event.button == 1:
|
||||||
print("main menu"); self.mainMenu()
|
print("main menu"); self.changeMenu("mainMenu")
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
self.textColorMainMenu = self.colorGrey
|
self.textColorMainMenu = self.colorGrey
|
||||||
@ -320,7 +292,7 @@ class Game:
|
|||||||
if event.key == pg.K_r or event.key == pg.K_ESCAPE:
|
if event.key == pg.K_r or event.key == pg.K_ESCAPE:
|
||||||
print("resume game"); self.continueGame()
|
print("resume game"); self.continueGame()
|
||||||
if event.key == pg.K_SEMICOLON:
|
if event.key == pg.K_SEMICOLON:
|
||||||
print("main menu"); self.mainMenu()
|
print("main menu"); self.changeMenu("mainMenu")
|
||||||
if event.type == pg.QUIT:
|
if event.type == pg.QUIT:
|
||||||
self.quit()
|
self.quit()
|
||||||
# ========================================== NEW GAME ======================================================== #
|
# ========================================== NEW GAME ======================================================== #
|
||||||
@ -329,7 +301,7 @@ class Game:
|
|||||||
if self.returnBlit.collidepoint(pg.mouse.get_pos()) == True:
|
if self.returnBlit.collidepoint(pg.mouse.get_pos()) == True:
|
||||||
self.textColorReturn = self.colorWhite
|
self.textColorReturn = self.colorWhite
|
||||||
if event.type == pg.MOUSEBUTTONDOWN and event.button == 1:
|
if event.type == pg.MOUSEBUTTONDOWN and event.button == 1:
|
||||||
print('return'); self.playMenu()
|
print('return'); self.changeMenu("mainMenu")
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
self.textColorReturn = self.colorGrey
|
self.textColorReturn = self.colorGrey
|
||||||
@ -353,7 +325,7 @@ class Game:
|
|||||||
if event.key == pg.K_SEMICOLON:
|
if event.key == pg.K_SEMICOLON:
|
||||||
print("male")
|
print("male")
|
||||||
if event.key == pg.K_r or event.key == pg.K_ESCAPE:
|
if event.key == pg.K_r or event.key == pg.K_ESCAPE:
|
||||||
print("return"); self.playMenu()
|
print("return"); self.changeMenu("mainMenu")
|
||||||
if event.type == pg.QUIT:
|
if event.type == pg.QUIT:
|
||||||
self.areYouSure()
|
self.areYouSure()
|
||||||
# ========================================== SETTINGS MENU =================================================== #
|
# ========================================== SETTINGS MENU =================================================== #
|
||||||
@ -397,7 +369,7 @@ class Game:
|
|||||||
if self.returnBlit.collidepoint(pg.mouse.get_pos()) == True:
|
if self.returnBlit.collidepoint(pg.mouse.get_pos()) == True:
|
||||||
self.textColorReturn = self.colorWhite
|
self.textColorReturn = self.colorWhite
|
||||||
if event.type == pg.MOUSEBUTTONDOWN and event.button == 1:
|
if event.type == pg.MOUSEBUTTONDOWN and event.button == 1:
|
||||||
print('return'); save.saveSettings(self.musicState, self.fullscreenState, self.displayFPS); self.mainMenu()
|
print('return'); save.saveSettings(self.musicState, self.fullscreenState, self.displayFPS); self.changeMenu("mainMenu")
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
self.textColorReturn = self.colorGrey
|
self.textColorReturn = self.colorGrey
|
||||||
@ -425,7 +397,7 @@ class Game:
|
|||||||
if self.noBlit.collidepoint(pg.mouse.get_pos()) == True:
|
if self.noBlit.collidepoint(pg.mouse.get_pos()) == True:
|
||||||
self.textColorNo= self.colorWhite
|
self.textColorNo= self.colorWhite
|
||||||
if event.type == pg.MOUSEBUTTONDOWN and event.button == 1:
|
if event.type == pg.MOUSEBUTTONDOWN and event.button == 1:
|
||||||
print('no'); self.mainMenu()
|
print('no'); self.changeMenu("mainMenu")
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
self.textColorNo = self.colorGrey
|
self.textColorNo = self.colorGrey
|
||||||
@ -433,7 +405,7 @@ class Game:
|
|||||||
if event.key == pg.K_y:
|
if event.key == pg.K_y:
|
||||||
print('yes'); self.quit()
|
print('yes'); self.quit()
|
||||||
if event.key == pg.K_n or event.key == pg.K_ESCAPE:
|
if event.key == pg.K_n or event.key == pg.K_ESCAPE:
|
||||||
print('no'); self.mainMenu()
|
print('no'); self.changeMenu("mainMenu")
|
||||||
if event.type == pg.QUIT:
|
if event.type == pg.QUIT:
|
||||||
self.quit()
|
self.quit()
|
||||||
# ========================================== SURE TO DESTROY SCREEN ========================================== #
|
# ========================================== SURE TO DESTROY SCREEN ========================================== #
|
||||||
@ -446,14 +418,14 @@ class Game:
|
|||||||
if self.lastDestroy == "settings":
|
if self.lastDestroy == "settings":
|
||||||
save.resetSettings(); self.restartGame()
|
save.resetSettings(); self.restartGame()
|
||||||
elif self.lastDestroy == "saves":
|
elif self.lastDestroy == "saves":
|
||||||
save.resetSave(); self.settingsMenu()
|
save.resetSave(); self.changeMenu("settingsMenu")
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
self.textColorYes = self.colorGrey
|
self.textColorYes = self.colorGrey
|
||||||
if self.noBlit.collidepoint(pg.mouse.get_pos()) == True:
|
if self.noBlit.collidepoint(pg.mouse.get_pos()) == True:
|
||||||
self.textColorNo= self.colorWhite
|
self.textColorNo= self.colorWhite
|
||||||
if event.type == pg.MOUSEBUTTONDOWN and event.button == 1:
|
if event.type == pg.MOUSEBUTTONDOWN and event.button == 1:
|
||||||
print('no'); self.settingsMenu()
|
print('no'); self.changeMenu("settingsMenu")
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
self.textColorNo = self.colorGrey
|
self.textColorNo = self.colorGrey
|
||||||
@ -463,9 +435,9 @@ class Game:
|
|||||||
if self.lastDestroy == "settings":
|
if self.lastDestroy == "settings":
|
||||||
save.resetSettings(); self.restartGame()
|
save.resetSettings(); self.restartGame()
|
||||||
elif self.lastDestroy == "saves":
|
elif self.lastDestroy == "saves":
|
||||||
save.resetSave(); self.settingsMenu()
|
save.resetSave(); self.changeMenu("settingsMenu")
|
||||||
if event.key == pg.K_n or event.key == pg.K_ESCAPE:
|
if event.key == pg.K_n or event.key == pg.K_ESCAPE:
|
||||||
print('no'); self.settingsMenu()
|
print('no'); self.changeMenu("settingsMenu")
|
||||||
if event.type == pg.QUIT:
|
if event.type == pg.QUIT:
|
||||||
self.areYouSure()
|
self.areYouSure()
|
||||||
# ========================================== PLAY MENU ======================================================= #
|
# ========================================== PLAY MENU ======================================================= #
|
||||||
@ -483,14 +455,14 @@ class Game:
|
|||||||
if self.newGameBlit.collidepoint(pg.mouse.get_pos()) == True:
|
if self.newGameBlit.collidepoint(pg.mouse.get_pos()) == True:
|
||||||
self.textColorNewGame= self.colorWhite
|
self.textColorNewGame= self.colorWhite
|
||||||
if event.type == pg.MOUSEBUTTONDOWN and event.button == 1:
|
if event.type == pg.MOUSEBUTTONDOWN and event.button == 1:
|
||||||
print('new game'); self.newGame()
|
print('new game'); self.changeMenu("newGame")
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
self.textColorNewGame = self.colorGrey
|
self.textColorNewGame = self.colorGrey
|
||||||
if self.returnBlit.collidepoint(pg.mouse.get_pos()) == True:
|
if self.returnBlit.collidepoint(pg.mouse.get_pos()) == True:
|
||||||
self.textColorReturn = self.colorWhite
|
self.textColorReturn = self.colorWhite
|
||||||
if event.type == pg.MOUSEBUTTONDOWN and event.button == 1:
|
if event.type == pg.MOUSEBUTTONDOWN and event.button == 1:
|
||||||
print('return'); self.mainMenu()
|
print('return'); self.changeMenu("mainMenu")
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
self.textColorReturn = self.colorGrey
|
self.textColorReturn = self.colorGrey
|
||||||
@ -498,9 +470,9 @@ class Game:
|
|||||||
if event.key == pg.K_c:
|
if event.key == pg.K_c:
|
||||||
print('continue')
|
print('continue')
|
||||||
if event.key == pg.K_n:
|
if event.key == pg.K_n:
|
||||||
print('new game'); self.newGame()
|
print('new game'); self.changeMenu("newGame")
|
||||||
if event.key == pg.K_r or event.key == pg.K_ESCAPE:
|
if event.key == pg.K_r or event.key == pg.K_ESCAPE:
|
||||||
print('return'); self.mainMenu()
|
print('return'); self.changeMenu("mainMenu")
|
||||||
if event.type == pg.QUIT:
|
if event.type == pg.QUIT:
|
||||||
self.areYouSure(); print('quit')
|
self.areYouSure(); print('quit')
|
||||||
# ========================================== MAIN MENU ======================================================= #
|
# ========================================== MAIN MENU ======================================================= #
|
||||||
@ -509,21 +481,21 @@ class Game:
|
|||||||
if self.playOptionBlit.collidepoint(pg.mouse.get_pos()) == True:
|
if self.playOptionBlit.collidepoint(pg.mouse.get_pos()) == True:
|
||||||
self.textColorPlay = self.colorWhite
|
self.textColorPlay = self.colorWhite
|
||||||
if event.type == pg.MOUSEBUTTONDOWN and event.button == 1:
|
if event.type == pg.MOUSEBUTTONDOWN and event.button == 1:
|
||||||
print('play'); self.playMenu()
|
print('play'); self.changeMenu("playMenu")
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
self.textColorPlay = self.colorGrey
|
self.textColorPlay = self.colorGrey
|
||||||
if self.settingsOptionBlit.collidepoint(pg.mouse.get_pos()) == True:
|
if self.settingsOptionBlit.collidepoint(pg.mouse.get_pos()) == True:
|
||||||
self.textColorSettings = self.colorWhite
|
self.textColorSettings = self.colorWhite
|
||||||
if event.type == pg.MOUSEBUTTONDOWN and event.button == 1:
|
if event.type == pg.MOUSEBUTTONDOWN and event.button == 1:
|
||||||
print('settings'); self.settingsMenu()
|
print('settings'); self.changeMenu("settingsMenu")
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
self.textColorSettings = self.colorGrey
|
self.textColorSettings = self.colorGrey
|
||||||
if self.helpOptionBlit.collidepoint(pg.mouse.get_pos()) == True:
|
if self.helpOptionBlit.collidepoint(pg.mouse.get_pos()) == True:
|
||||||
self.textColorHelp = self.colorWhite
|
self.textColorHelp = self.colorWhite
|
||||||
if event.type == pg.MOUSEBUTTONDOWN and event.button == 1:
|
if event.type == pg.MOUSEBUTTONDOWN and event.button == 1:
|
||||||
print('help'); self.helpMenu()
|
print('help'); self.changeMenu("helpMenu")
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
self.textColorHelp = self.colorGrey
|
self.textColorHelp = self.colorGrey
|
||||||
@ -536,11 +508,11 @@ class Game:
|
|||||||
self.textColorQuit = self.colorGrey
|
self.textColorQuit = self.colorGrey
|
||||||
if event.type == pg.KEYDOWN:
|
if event.type == pg.KEYDOWN:
|
||||||
if event.key == pg.K_p:
|
if event.key == pg.K_p:
|
||||||
print('play'); self.playMenu()
|
print('play'); self.changeMenu("playMenu")
|
||||||
if event.key == pg.K_s:
|
if event.key == pg.K_s:
|
||||||
print('settings'); self.settingsMenu()
|
print('settings'); self.changeMenu("settingsMenu")
|
||||||
if event.key == pg.K_h:
|
if event.key == pg.K_h:
|
||||||
print('help'); self.helpMenu()
|
print('help'); self.changeMenu("helpMenu")
|
||||||
if event.key == pg.K_a or event.key == pg.K_ESCAPE:
|
if event.key == pg.K_a or event.key == pg.K_ESCAPE:
|
||||||
print('quit'); self.areYouSure()
|
print('quit'); self.areYouSure()
|
||||||
if event.type == pg.QUIT:
|
if event.type == pg.QUIT:
|
||||||
@ -551,13 +523,13 @@ class Game:
|
|||||||
if self.returnBlit.collidepoint(pg.mouse.get_pos()) == True:
|
if self.returnBlit.collidepoint(pg.mouse.get_pos()) == True:
|
||||||
self.textColorReturn = self.colorWhite
|
self.textColorReturn = self.colorWhite
|
||||||
if event.type == pg.MOUSEBUTTONDOWN and event.button == 1:
|
if event.type == pg.MOUSEBUTTONDOWN and event.button == 1:
|
||||||
print('return'); self.mainMenu()
|
print('return'); self.changeMenu("mainMenu")
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
self.textColorReturn = self.colorGrey
|
self.textColorReturn = self.colorGrey
|
||||||
if event.type == pg.KEYDOWN:
|
if event.type == pg.KEYDOWN:
|
||||||
if event.key == pg.K_r or event.key == pg.K_ESCAPE:
|
if event.key == pg.K_r or event.key == pg.K_ESCAPE:
|
||||||
print('return'); self.mainMenu()
|
print('return'); self.changeMenu("mainMenu")
|
||||||
if event.type == pg.QUIT:
|
if event.type == pg.QUIT:
|
||||||
self.areYouSure()
|
self.areYouSure()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user