diff --git a/main.py b/main.py index a75e9ba..f4fb648 100644 --- a/main.py +++ b/main.py @@ -1,8 +1,14 @@ 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__': - # do everything in this main function - main() - # don't forget quit for release all resource - pg.quit() \ No newline at end of file + # 控制状态机运行 + 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() \ No newline at end of file diff --git a/resources/graphics/Screen/exit.png b/resources/graphics/Screen/exit.png new file mode 100644 index 0000000..2a9e218 Binary files /dev/null and b/resources/graphics/Screen/exit.png differ diff --git a/source/constants.py b/source/constants.py index 09bfc9c..76fccba 100644 --- a/source/constants.py +++ b/source/constants.py @@ -24,6 +24,12 @@ GREEN = ( 0, 255, 0) SIZE_MULTIPLIER = 1.3 +# 退出游戏按钮 +EXIT = 'exit' +# 当想要一个特殊值时用 +NULL = 'null' + + #GAME INFO DICTIONARY KEYS CURRENT_TIME = 'current time' LEVEL_NUM = 'level num' diff --git a/source/main.py b/source/main.py deleted file mode 100644 index b1badeb..0000000 --- a/source/main.py +++ /dev/null @@ -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() \ No newline at end of file diff --git a/source/state/mainmenu.py b/source/state/mainmenu.py index 168af18..4f17dc8 100644 --- a/source/state/mainmenu.py +++ b/source/state/mainmenu.py @@ -1,8 +1,10 @@ import pygame as pg from .. import tool from .. import constants as c +from . import level class Menu(tool.State): + def __init__(self): tool.State.__init__(self) @@ -10,7 +12,6 @@ class Menu(tool.State): self.next = c.LEVEL self.persist = persist self.game_info = persist - self.setupBackground() self.setupOption() @@ -38,6 +39,13 @@ class Menu(tool.State): self.option_rect.x = 435 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_timer = 0 self.option_clicked = False @@ -49,7 +57,15 @@ class Menu(tool.State): self.option_clicked = True self.option_timer = self.option_start = self.current_time 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): 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 mouse_pos: self.checkOptionClick(mouse_pos) + self.checkExitClick(mouse_pos) else: # 点到后播放动画 if(self.current_time - self.option_timer) > 200: @@ -67,6 +84,8 @@ class Menu(tool.State): self.option_image = self.option_frames[self.option_frame_index] if(self.current_time - self.option_start) > 1300: self.done = True + surface.blit(self.bg_image, self.bg_rect) - surface.blit(self.option_image, self.option_rect) \ No newline at end of file + surface.blit(self.option_image, self.option_rect) + surface.blit(self.option_exit, self.exit_rect) \ No newline at end of file diff --git a/source/tool.py b/source/tool.py index ed5aa53..b34ec32 100644 --- a/source/tool.py +++ b/source/tool.py @@ -64,6 +64,9 @@ class Control(): # 状态转移 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 persist = self.state.cleanup() self.state = self.state_dict[self.state_name] @@ -98,7 +101,8 @@ def get_image(sheet, x, y, width, height, colorkey=c.BLACK, scale=1): rect = image.get_rect() image.blit(sheet, (0, 0), (x, y, width, height)) - image.set_colorkey(colorkey) + if colorkey != c.NULL: + image.set_colorkey(colorkey) image = pg.transform.scale(image, (int(rect.width*scale), int(rect.height*scale)))