add exit button
This commit is contained in:
parent
2e60881256
commit
291addb895
16
main.py
16
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()
|
||||
# 控制状态机运行
|
||||
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()
|
||||
BIN
resources/graphics/Screen/exit.png
Normal file
BIN
resources/graphics/Screen/exit.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 23 KiB |
@ -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'
|
||||
|
||||
@ -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()
|
||||
@ -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)
|
||||
surface.blit(self.option_image, self.option_rect)
|
||||
surface.blit(self.option_exit, self.exit_rect)
|
||||
@ -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)))
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user