Python切换代码在While循环中捕获

时间:2017-03-25 13:36:39

标签: python-2.7 loops

我尝试过并且没有使用转义按钮创建菜单切换,因为看起来python解释器进入while循环并且无法跟踪切换代码,因为它卡在while循环中(这是打开菜单时,我想要达到的目的是必要的。是否有一个特定的模块或方法可以帮助我克服这个问题?

编辑:

这是代码的简化版本:

import sys
import pygame
import os
from pygame.constants import RESIZABLE
from win32api import GetSystemMetrics
background = ([0, 0, 0])
screen = pygame.display.set_mode([600, 600], RESIZABLE)
startup_ended = False
startup_alpha = 1024
player_image = pygame.image.load('resources/game/entities/Player.png')
close_image = pygame.image.load('resources/game/Banner/MouseUp_Close.png')
menu_image = pygame.image.load('resources/game/Menu/Base.png')
BannerExtension_width = 1280
menu_on = False
menu_feedback = False
menu_override = False
player_xpos = 610
player_ypos = 482
player_left = 0
player_right = 0
speed = 0.75
resume_image = pygame.image.load('resources/game/Menu/MouseUp_Resume.png')
quick_save_image = pygame.image.load('resources/game/Menu/MouseUp_QuickSave.png')
save_image = pygame.image.load('resources/game/Menu/MouseUp_Save.png')
options_image = pygame.image.load('resources/game/Menu/MouseUp_Options.png')
return_to_start_screen_image = pygame.image.load('resources/game/Menu/MouseUp_ReturnToStartScreen.png')
class Game():
    def __init__(self):
        print 'initiated...'
        pygame.init()
        screen.fill(background)
    def run(self):
        pygame.init()
        def startup():
            global background
            global startup_ended
            logo = pygame.image.load('resources/startup/Logo.png')
            logo.set_alpha(startup_alpha)
            pygame.display.set_caption('Loading...')
            screen.blit(logo, [10, 10])
            pygame.display.flip()
            pygame.time.delay(1250)
            background = ([50, 50, 50])
            pygame.draw.rect(screen, background, [10, 10, 385, 322], 1)
            screen.fill(background)
            pygame.display.flip()
            pygame.time.delay(1000)
            pygame.display.set_caption('Rydlle')
            pygame.display.set_icon(logo)
            startup_ended = True
        def game():
            global screen, background
            global menu_feedback, resume_image,close_image
            os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (0, 0)
            screen = pygame.display.set_mode([GetSystemMetrics(0), GetSystemMetrics(1)])
            background = ([150, 150, 150])
            screen.fill(background)
            pygame.display.flip()
            class Player(pygame.sprite.Sprite):
                def __init__(self):
                    self.rect = player_image.get_rect()
                    self.rect.topleft = [player_xpos, player_ypos]
                    self.rect.width = 100
                    self.rect.height = 100
                    pygame.sprite.Sprite.__init__(self)
            class Close(pygame.sprite.Sprite):
                def __init__(self):
                    self.rect = close_image.get_rect()
                    self.rect.topleft = [(GetSystemMetrics(0)) - 50, 0]
                    self.rect.width = 50
                    self.rect.height = 50
                    pygame.sprite.Sprite.__init__(self)
            class Base(pygame.sprite.Sprite):
                def __init__(self):
                    self.rect = menu_image.get_rect()
                    self.rect.topleft = [(GetSystemMetrics(0)/8), (GetSystemMetrics(1)/8)]
                    self.rect.width = (GetSystemMetrics(0))/3
                    self.rect.height = (GetSystemMetrics(1))/3
                    menu_image.set_alpha(256)
                    pygame.sprite.Sprite.__init__(self)
            class Resume(pygame.sprite.Sprite):
                def __init__(self):
                    self.rect = resume_image.get_rect()
                    self.rect.topleft = [280, 200]
                    self.rect.width = 400
                    self.rect.height = 96
                    pygame.sprite.Sprite.__init__(self)
            screen.blit(player_image, [610, 482])
            screen.blit(close_image, [1230, 0])
            pygame.display.flip()
            timer_event = pygame.USEREVENT + 1
            pygame.time.set_timer(timer_event, 1)
            while startup_ended:
                for event in pygame.event.get():
                    global player_xpos, player_ypos, player_left, player_right, player_up, player_down, speed
                    global menu_on
                    pygame.event.pump()
                    if event.type == pygame.QUIT:
                        sys.exit()
                    close_i = Close()
                    if close_i.rect.collidepoint(pygame.mouse.get_pos()):
                        close_image = pygame.image.load('resources/game/Banner/MouseOver_Close.png')
                        screen.blit(close_image, [1230, 0])
                        pygame.display.flip()
                        if event.type == pygame.MOUSEBUTTONDOWN:
                            close_image = pygame.image.load('resources/game/Banner/MouseDown_Close.png')
                            screen.blit(close_image, [1230, 0])
                            pygame.display.flip()
                            pygame.time.wait(100)
                            sys.exit()
                    else:
                        close_image = pygame.image.load('resources/game/Banner/MouseUp_Close.png')
                        screen.blit(close_image, [1230, 0])
                        pygame.display.flip()
                    if menu_on:
                        resume_i = Resume()
                        screen.blit(resume_image, [280, 200])
                        pygame.display.flip()
                        if resume_i.rect.collidepoint(pygame.mouse.get_pos()):
                            resume_image = pygame.image.load('resources/game/Menu/buttons/MouseOver_Resume.png')
                            screen.blit(resume_image, [280, 200])
                            pygame.display.flip()
                            if event.type == pygame.MOUSEBUTTONDOWN:
                                resume_image = pygame.image.load('resources/game/Menu/buttons/MouseDown_Resume.png')
                                screen.blit(resume_image, [280, 200])
                                pygame.display.flip()
                                pygame.draw.rect(screen, background, [0, 0, GetSystemMetrics(0), GetSystemMetrics(1)], 0)
                                menu_on = False
                        else:
                            resume_image = pygame.image.load('resources/game/Menu/buttons/MouseUp_Resume.png')
                            screen.blit(resume_image, [280, 200])
                            screen.blit(quick_save_image, [280, 332])
                            screen.blit(save_image, [280, 464])
                            screen.blit(options_image, [280, 596])
                            screen.blit(return_to_start_screen_image, [280, 728])
                            pygame.display.flip()
                    else:
                        if event.type == pygame.KEYUP:
                            global direction_override, menu_override
                            if event.key == pygame.K_LEFT and (left_override or right_override or up_override or
                                                                   down_override):
                                left_override = False
                                up_override = False
                                right_override = False
                                down_override = False
                            if event.key == pygame.K_RIGHT and (left_override or right_override or up_override or
                                                                    down_override):
                                left_override = False
                                up_override = False
                                right_override = False
                                down_override = False
                        if event.type == pygame.KEYDOWN:
                            if event.key == pygame.K_LEFT:
                                pygame.draw.rect(screen, background, [player_xpos, player_ypos, 50, 50], 0)
                                player_left = True
                                player_right = False
                                screen.blit(player_image, [player_xpos, player_ypos])
                                pygame.display.flip()
                            if event.key == pygame.K_RIGHT:
                                pygame.draw.rect(screen, background, [player_xpos, player_ypos, 50, 50], 0)
                                player_right = True
                                player_left = False
                                screen.blit(player_image, [player_xpos, player_ypos])
                                pygame.display.flip()
                            if event.key == pygame.K_SPACE:
                                pygame.draw.rect(screen, background, [player_xpos, player_ypos, 50, 50], 0)
                                player_down = False
                                player_up = False
                                player_right = False
                                player_left = False
                                screen.blit(player_image, [player_xpos, player_ypos])
                                pygame.display.flip()
                            if event.key == pygame.K_ESCAPE:
                                print 'menu_on'
                                if menu_on:
                                    menu_override = True
                                menu_on = False
                        if event.type == timer_event:
                            screen.blit(close_image, [1230, 0])
                            pygame.display.flip()
                            if player_left:
                                pygame.draw.rect(screen, background, [player_xpos, player_ypos, 50, 50], 0)
                                player_xpos -= speed
                                screen.blit(player_image, [player_xpos, player_ypos])
                                pygame.display.flip()
                            if player_right:
                                pygame.draw.rect(screen, background, [player_xpos, player_ypos, 50, 50], 0)
                                player_xpos += speed
                                screen.blit(player_image, [player_xpos, player_ypos])
                                pygame.display.flip()

        startup()
        if startup_ended:
            game()
Game().run()

0 个答案:

没有答案