Skip to main content
 首页 » 编程设计

python之如何在调用另一个方法时停止运行一个方法

2024年10月17日7duanxz

调用 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() 

请您参考如下方法:

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

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__ 方法中加载它们并将它们保存在属性变量中,而不是每一帧都加载图像和字体。

编辑: 回答您的实际问题:您不能停止运行外部方法。可以这样想:房间里有一个盒子;不进房间能拿到箱子吗?不,你在外部方法中有一个内部方法;不进入外层方法能得到内层方法吗?没有。

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