diff --git a/source/state/level.py b/source/state/level.py index cdf961e..f9ed135 100644 --- a/source/state/level.py +++ b/source/state/level.py @@ -634,13 +634,6 @@ class Level(tool.State): self.level_progress_flag_rect = self.level_progress_flag.get_rect() self.level_progress_flag_rect.x = self.level_progress_bar_image_rect.x - 78 self.level_progress_flag_rect.y = self.level_progress_bar_image_rect.y - 3 - - def inArea(self, rect, x, y): - if (x >= rect.x and x <= rect.right and - y >= rect.y and y <= rect.bottom): - return True - else: - return False # 用小铲子移除植物 def shovelRemovePlant(self, mouse_pos): diff --git a/source/state/mainmenu.py b/source/state/mainmenu.py index d552035..5eb9ae1 100644 --- a/source/state/mainmenu.py +++ b/source/state/mainmenu.py @@ -99,13 +99,6 @@ class Menu(tool.State): self.adventure_clicked = False self.option_button_clicked = False - def inArea(self, rect, x, y): - if (x >= rect.x and x <= rect.right and - y >= rect.y and y <= rect.bottom): - return True - else: - return False - def checkHilight(self, x, y): # 高亮冒险模式按钮 if self.inArea(self.adventure_rect, x, y): diff --git a/source/state/screen.py b/source/state/screen.py index 36afa79..e36c096 100644 --- a/source/state/screen.py +++ b/source/state/screen.py @@ -6,48 +6,65 @@ from .. import constants as c class Screen(tool.State): def __init__(self): tool.State.__init__(self) - self.end_time = 3000 def startup(self, current_time, persist): pass - - def getImageName(self): - pass - - def set_next_state(self): - pass def setupImage(self, name, frame_rect=(0, 0, 800, 600)): + # 背景图本身 self.image = tool.get_image(tool.GFX[name], *frame_rect) self.rect = self.image.get_rect() self.rect.x = 0 self.rect.y = 0 + # 按钮 + frame_rect = (0, 0, 111, 26) + ## 继续按钮 + self.next_button_image = tool.get_image_menu(tool.GFX[c.UNIVERSAL_BUTTON], *frame_rect) + self.next_button_image_rect = self.next_button_image.get_rect() + self.next_button_image_rect.x = 70 + ### 继续按钮上的文字 + font = pg.font.Font(c.FONT_PATH, 18) + next_text = font.render("继续", True, c.NAVYBLUE) + next_text_rect = next_text.get_rect() + next_text_rect.x = 37 + ## 主菜单按钮 + self.main_menu_button_image = tool.get_image_menu(tool.GFX[c.UNIVERSAL_BUTTON], *frame_rect) + self.main_menu_button_image_rect = self.main_menu_button_image.get_rect() + self.main_menu_button_image_rect.x = 620 + self.next_button_image_rect.y = self.main_menu_button_image_rect.y = 550 + ### 主菜单按钮上的文字 + main_menu_text = font.render("主菜单", True, c.NAVYBLUE) + main_menu_text_rect = main_menu_text.get_rect() + main_menu_text_rect.x = 29 + self.next_button_image.blit(next_text, next_text_rect) + self.main_menu_button_image.blit(main_menu_text, main_menu_text_rect) + self.image.blit(self.next_button_image, self.next_button_image_rect) + self.image.blit(self.main_menu_button_image, self.main_menu_button_image_rect) + def update(self, surface, current_time, mouse_pos, mouse_click): - if (current_time - self.start_time) < self.end_time: - surface.fill(c.WHITE) - surface.blit(self.image, self.rect) - else: - self.done = True + surface.fill(c.WHITE) + surface.blit(self.image, self.rect) + if mouse_pos: + # 点到继续 + if self.inArea(self.next_button_image_rect, *mouse_pos): + self.next = c.LEVEL + self.done = True + # 点到主菜单 + elif self.inArea(self.main_menu_button_image_rect, *mouse_pos): + self.next = c.MAIN_MENU + self.done = True class GameVictoryScreen(Screen): def __init__(self): Screen.__init__(self) - - def getImageName(self): - return c.GAME_VICTORY_IMAGE - - def set_next_state(self): - return c.LEVEL + self.image_name = c.GAME_VICTORY_IMAGE def startup(self, current_time, persist): self.start_time = current_time - self.next = c.LEVEL self.persist = persist self.game_info = persist - name = self.getImageName() - self.setupImage(name) - self.next = self.set_next_state() + self.setupImage(self.image_name) pg.display.set_caption("pypvz: 战斗胜利!") # 停止播放原来关卡中的音乐 pg.mixer.music.stop() @@ -55,21 +72,13 @@ class GameVictoryScreen(Screen): class GameLoseScreen(Screen): def __init__(self): Screen.__init__(self) - - def getImageName(self): - return c.GAME_LOSE_IMAGE - - def set_next_state(self): - return c.LEVEL + self.image_name = c.GAME_LOSE_IMAGE def startup(self, current_time, persist): self.start_time = current_time - self.next = c.LEVEL self.persist = persist self.game_info = persist - name = self.getImageName() - self.setupImage(name, (-15, 0, 800, 600)) - self.next = self.set_next_state() + self.setupImage(self.image_name, (-15, 0, 800, 600)) pg.display.set_caption("pypvz: 战斗失败!") # 停止播放原来关卡中的音乐 pg.mixer.music.stop() @@ -187,7 +196,6 @@ class AwardScreen(tool.State): pg.mixer.music.set_volume(self.game_info[c.SOUND_VOLUME]) def update(self, surface, current_time, mouse_pos, mouse_click): - surface.fill(c.WHITE) surface.blit(self.image, self.rect) if mouse_pos: # 检查主菜单点击 @@ -198,10 +206,3 @@ class AwardScreen(tool.State): if self.inArea(self.next_button_image_rect, *mouse_pos): self.next = c.LEVEL self.done = True - - def inArea(self, rect, x, y): - if (x >= rect.x and x <= rect.right and - y >= rect.y and y <= rect.bottom): - return True - else: - return False diff --git a/source/tool.py b/source/tool.py index 7426ffc..a50b8e6 100755 --- a/source/tool.py +++ b/source/tool.py @@ -29,6 +29,14 @@ class State(): # abstract method pass + # 工具:范围判断函数,用于判断点击 + def inArea(self, rect, x, y): + if (x >= rect.x and x <= rect.right and + y >= rect.y and y <= rect.bottom): + return True + else: + return False + # control this game. do event loops class Control(): def __init__(self):