add exit button

This commit is contained in:
wcb_dell 2020-11-25 10:20:02 +08:00
parent 2e60881256
commit 291addb895
6 changed files with 44 additions and 25 deletions

16
main.py
View File

@ -1,8 +1,14 @@
import pygame as pg import pygame as pg
from source.main import main from source import tool
from source import constants as c
from source.state import mainmenu, screen, level
if __name__=='__main__': if __name__=='__main__':
# do everything in this main function # 控制状态机运行
main() game = tool.Control()
# don't forget quit for release all resource state_dict = {c.MAIN_MENU: mainmenu.Menu(),
pg.quit() c.GAME_VICTORY: screen.GameVictoryScreen(),
c.GAME_LOSE: screen.GameLoseScreen(),
c.LEVEL: level.Level()}
game.setup_states(state_dict, c.MAIN_MENU)
game.run()

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

View File

@ -24,6 +24,12 @@ GREEN = ( 0, 255, 0)
SIZE_MULTIPLIER = 1.3 SIZE_MULTIPLIER = 1.3
# 退出游戏按钮
EXIT = 'exit'
# 当想要一个特殊值时用
NULL = 'null'
#GAME INFO DICTIONARY KEYS #GAME INFO DICTIONARY KEYS
CURRENT_TIME = 'current time' CURRENT_TIME = 'current time'
LEVEL_NUM = 'level num' LEVEL_NUM = 'level num'

View File

@ -1,16 +0,0 @@
__author__ = 'wcb'
from . import tool
from . import constants as c
from .state import mainmenu, screen, level
# create a standard game
def main():
# 控制状态机运行
game = tool.Control()
state_dict = {c.MAIN_MENU: mainmenu.Menu(),
c.GAME_VICTORY: screen.GameVictoryScreen(),
c.GAME_LOSE: screen.GameLoseScreen(),
c.LEVEL: level.Level()}
game.setup_states(state_dict, c.MAIN_MENU)
game.run()

View File

@ -1,8 +1,10 @@
import pygame as pg import pygame as pg
from .. import tool from .. import tool
from .. import constants as c from .. import constants as c
from . import level
class Menu(tool.State): class Menu(tool.State):
def __init__(self): def __init__(self):
tool.State.__init__(self) tool.State.__init__(self)
@ -10,7 +12,6 @@ class Menu(tool.State):
self.next = c.LEVEL self.next = c.LEVEL
self.persist = persist self.persist = persist
self.game_info = persist self.game_info = persist
self.setupBackground() self.setupBackground()
self.setupOption() self.setupOption()
@ -38,6 +39,13 @@ class Menu(tool.State):
self.option_rect.x = 435 self.option_rect.x = 435
self.option_rect.y = 75 self.option_rect.y = 75
# 退出按钮
frame_rect = [0, 0, 500, 500]
self.option_exit = tool.get_image_menu(tool.GFX[c.EXIT], *frame_rect, c.BLACK, 1.1)
self.exit_rect = self.option_exit.get_rect()
self.exit_rect.x = 690
self.exit_rect.y = 400
self.option_start = 0 self.option_start = 0
self.option_timer = 0 self.option_timer = 0
self.option_clicked = False self.option_clicked = False
@ -50,6 +58,14 @@ class Menu(tool.State):
self.option_timer = self.option_start = self.current_time self.option_timer = self.option_start = self.current_time
return False return False
# 点击到按钮修改转态的done属性
def checkExitClick(self, mouse_pos):
x, y = mouse_pos
if(x >= self.exit_rect.x and x <= self.exit_rect.right and
y >= self.exit_rect.y and y <= self.exit_rect.bottom):
self.done = True
self.next = c.EXIT
def update(self, surface, current_time, mouse_pos, mouse_click): def update(self, surface, current_time, mouse_pos, mouse_click):
self.current_time = self.game_info[c.CURRENT_TIME] = current_time self.current_time = self.game_info[c.CURRENT_TIME] = current_time
@ -57,6 +73,7 @@ class Menu(tool.State):
if not self.option_clicked: if not self.option_clicked:
if mouse_pos: if mouse_pos:
self.checkOptionClick(mouse_pos) self.checkOptionClick(mouse_pos)
self.checkExitClick(mouse_pos)
else: else:
# 点到后播放动画 # 点到后播放动画
if(self.current_time - self.option_timer) > 200: if(self.current_time - self.option_timer) > 200:
@ -68,5 +85,7 @@ class Menu(tool.State):
if(self.current_time - self.option_start) > 1300: if(self.current_time - self.option_start) > 1300:
self.done = True self.done = True
surface.blit(self.bg_image, self.bg_rect) surface.blit(self.bg_image, self.bg_rect)
surface.blit(self.option_image, self.option_rect) surface.blit(self.option_image, self.option_rect)
surface.blit(self.option_exit, self.exit_rect)

View File

@ -64,6 +64,9 @@ class Control():
# 状态转移 # 状态转移
def flip_state(self): def flip_state(self):
if self.state.next == c.EXIT:
pg.quit()
os._exit(0)
previous, self.state_name = self.state_name, self.state.next previous, self.state_name = self.state_name, self.state.next
persist = self.state.cleanup() persist = self.state.cleanup()
self.state = self.state_dict[self.state_name] self.state = self.state_dict[self.state_name]
@ -98,6 +101,7 @@ def get_image(sheet, x, y, width, height, colorkey=c.BLACK, scale=1):
rect = image.get_rect() rect = image.get_rect()
image.blit(sheet, (0, 0), (x, y, width, height)) image.blit(sheet, (0, 0), (x, y, width, height))
if colorkey != c.NULL:
image.set_colorkey(colorkey) image.set_colorkey(colorkey)
image = pg.transform.scale(image, image = pg.transform.scale(image,
(int(rect.width*scale), (int(rect.width*scale),