当你打电话给另一个方法时,如何阻止一个方法运行?

时间:2016-12-01 22:05:45

标签: python pygame

调用win.instructionScreen(screen, win)时,会出现说明屏幕,但startScreen中的文本仍然存在。使用screen.fill(BLACK)不起作用,因为主循环导致startScreen中的文字重新出现,并且使用return停止startScreen方法不起作用。

import pygame

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)

class Game:
    def __init__(self):
        self.tickets = 0

    def startScreen(self, screen, win):
        titleText = pygame.font.SysFont('Showcard Gothic', 60)
        subText = pygame.font.SysFont('Showcard Gothic', 20)

        text = titleText.render("Our Game", True, WHITE)
        cs = subText.render("Final Project", True, WHITE)
        names = subText.render("Name 1, Name 2, Name 3, Name 4", True, WHITE)

        screen.blit(text, [220, 200])
        screen.blit(cs, [310, 265])
        screen.blit(names, [150, 290])

        mouse = pygame.mouse.get_pos()

        if 493 > mouse[0] > 343 and 461 > mouse[1] > 411:
            pygame.draw.rect(screen, RED, (343, 411, 150, 50))
        else:
            pygame.draw.rect(screen, GREEN, (343, 411, 150, 50))

        buttonText = pygame.font.SysFont('Showcard Gothic', 30)

        start = buttonText.render("Start!", True, WHITE)
        screen.blit(start, [365, 425])

        for event in pygame.event.get():
             if 494 > mouse[0] > 343 and 461 > mouse[1] > 411:
                if event.type == pygame.MOUSEBUTTONDOWN:
                    win.instructionScreen(screen, win)
                    pygame.display.update()
                    return

        pygame.display.update()

    def instructionScreen(self, screen, win):

        background = pygame.image.load("background.png").convert()
        screen.blit(background, [0, 0])
        caption = pygame.image.load("caption.png").convert()
        oak = pygame.image.load("oak.png").convert()
        oak.set_colorkey(BLACK)
        screen.blit(oak, [570, 130])
        titleText = pygame.font.SysFont('Showcard Gothic', 60)
        subText = pygame.font.SysFont('Showcard Gothic', 25)

        text = titleText.render("Instructions", True, WHITE)
        captionText = subText.render("Hey! Welcome to our game! Start by walking up", True, BLACK)
        captionText2 = subText.render("to and playing Higher or Lower and racking up", True, BLACK)
        captionText3 = subText.render("tickets. Then, when you get enough tickets,", True, BLACK)
        captionText4 = subText.render("different games will be unlocked. Have fun!", True, BLACK)

        screen.blit(text, [200, 80])

        mouse = pygame.mouse.get_pos()

        if 480 > mouse[0] > 325 and 550 > mouse[1] > 500:
            pygame.draw.rect(screen, RED, (325, 500, 150, 50))
        else:
            pygame.draw.rect(screen, GREEN, (325, 500, 150, 50))

        buttonText = pygame.font.SysFont('Showcard Gothic', 30)

        screen.blit(caption, [3, 300])
        pygame.draw.rect(screen, WHITE, (45, 320, 670, 110))

        screen.blit(captionText, [45, 325])
        screen.blit(captionText2, [45, 350])
        screen.blit(captionText3, [45, 375])
        screen.blit(captionText4, [45, 400])

        play = buttonText.render("Play!", True, WHITE)
        screen.blit(play, [357, 515])

        #if 480 > mouse[0] > 325 and 550 > mouse[1] > 500:
        #    if event.type == pygame.MOUSEBUTTONDOWN:
        #        from gamescreen.py import gamescreen

        pygame.display.update()

    def clearScreen(self, screen):
        screen.fill(WHITE)

def main():
    pygame.init()
    size = [800, 600]
    screen = pygame.display.set_mode(size)
    pygame.display.set_caption("Arcade City")
    done = False
    clock = pygame.time.Clock()
    win = Game()
    while not done:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                done = True

        win.startScreen(screen, win)
        clock.tick(60)
if __name__ == "__main__":
    main()

1 个答案:

答案 0 :(得分:2)

如果您不使用嵌套功能,这会更容易。从主函数调用win.InstructionScreen(),而不是从方法win.startScreen()调用。使用状态变量来控制流量,并从方法中返回它应该处于哪种状态。

win = Game()
current_screen = 'start'
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            done = True
    if current_screen == 'start':
        current_screen = win.startScreen(screen, win)
    elif current_screen == 'instruction':
        current_screen = win.instructionScreen(self, screen, win)
    clock.tick(60)

这只是一个模型,所以你必须在自己的配件后改变方法。在两种方法中都放置一个return语句,以便它们始终返回当前屏幕应该是什么。

提示 您可以在__init__方法中加载它们,而不是在每个帧中加载图像和字体,并将它们保存在属性变量中。

修改 要回答您的实际问题:您无法停止外部方法运行。可以这样想:你在一个房间里有一个盒子;你可以在不进入房间的情况下拿到箱子吗?不。你在外部方法中有一个内部方法;你可以在不进入外部方法的情况下获得内部方法吗?否。

如果你只想运行内部方法而不运行外部方法,你必须直接调用inner方法而不是调用外部方法。