在游戏画面之间移动

时间:2016-11-16 08:23:38

标签: python menu pygame

我对pygame很新,并且设计一个处理不同游戏屏幕的主循环时遇到问题。我正在用3种不同的屏幕编写一个跳棋游戏。

简介屏幕:获取所需的电路板尺寸N并将其传递到主循环。然后主循环调用game_screen,调用我的游戏类,调用N作为参数。

游戏画面:调用游戏类,所以整个游戏都在这里运行。它还可以从游戏类中获得经过时间和赢家等回报。我们的想法是将它们传递到主循环中,并将它们作为参数调用game_over_screen。

这导致问题,因为我的回归"选择"有时是一个元组,有时是一个int。当我关闭窗口时它是一个int,但是当赢得游戏时会返回一个元组。

游戏结束:基本上只需要获胜者和经过的时间并打印出来。单击按钮时也应返回选项。这应该回到介绍屏幕。

我需要帮助: 我的主循环中的逻辑看起来非常笨重。有时返回int,有时候是元组的想法对我来说似乎并不合适。我很感激有关如何设计这类简单游戏的主要循环的任何提示。

# Intro Screen
########################################################################### 
def game_intro_screen():                  
    intro  = True
    while intro:
        for event in pygame.event.get():
            if event.type==pygame.QUIT:     #quit by closing the window
                return 0  
            if event.type==pygame.KEYDOWN:
                if event.key==pygame.K_SPACE:
                    return 2                            
        screen.fill(BLACK)                     # Fill background with black
        pygame.display.set_caption("CHECKERS")
        message_to_screen("CHECKERS", GRAY, y_displace=-350, size="large")    
        message_to_screen("- Rules -", GRAY, y_displace=-170, size="medium")
        message_to_screen("- Peasants( x, o) can only move diagonally forward.", GRAY, y_displace=-120, size="small")
        message_to_screen("- Kings (X, O) can move diagonally both ", GRAY, y_displace= -90, size="small")
        message_to_screen(" forward and backwards. ", GRAY, y_displace= -60, size="small")#
        message_to_screen("- Tokens can only move to a free field.", GRAY, y_displace= -30, size="small")
        message_to_screen("- Eat the opponents pieces by jumping over them.", GRAY, y_displace= 0, size="small")
        message_to_screen("- Win the game by eating the opponent's tokens.", GRAY, y_displace= 30, size="small")

        message_to_screen("- Controls - ", GRAY, y_displace= 100, size="medium")
        message_to_screen("- Click a token to select it. ", GRAY, y_displace= 150, size="small")
        message_to_screen("- Click an empty field to move your selected token", GRAY, y_displace= 180, size="small")

        message_to_screen("- Chose the grid size - ", GRAY, y_displace= 300, size="medium")

        choice=button("8 X 8", 60,800,150,50, GRAY, BLUE, action="play8")
        if choice!=None: 
            return 8
        choice=button("10 X 10", 270,800,150,50, GRAY, BLUE, action="play10")
        if choice!=None: 
            return 10
        choice=button("12 X12", 485,800,150,50, GRAY, BLUE, action="play12")
        if choice!=None: 
            return 12

        pygame.display.update()
        clock.tick(10)

# Game Over Screen
########################################################################### 
def game_over_screen(winner, end_time):                    # pass winner, time here


    second= end_time
    minute=0
    hour=0    
    minute, second=divmod(second, 60)
    hour, minute=divmod(minute, 60)
    time=(hour, minute, second)


    intro  = True
    while intro:
        for event in pygame.event.get():
            if event.type==pygame.QUIT:     #quit by closing the window
                return 0  
            if event.type==pygame.KEYDOWN:
                if event.key==pygame.K_SPACE:
                    return 2             

        screen.fill(BLACK)                     # Fill background with black
        pygame.display.set_caption("CHECKERS")
        message_to_screen("GAME OVER", GRAY, y_displace=-350, size="large")  
        #message_to_screen("%s wins!" % winner, GRAY, y_displace=-100, size="medium")
        message_to_screen("Player %s won!" % winner , GRAY, y_displace=-100, size="medium")  
        #message_to_screen(" Elapsed time: 00:12:43" , GRAY, y_displace=50, size="medium")
        message_to_screen(" Elapsed time: "+str("%d" %time[0] + " : "+"%d" %time[1] + " : "+"%d" %time[2]) , GRAY, y_displace=50, size="medium")
        message_to_screen("-------------------------------" , GRAY, y_displace=120, size="small")
        #message_to_screen(" Current High score: 00:07:11" , GRAY, y_displace=150, size="small")
        #text=smallfont.render("Elapsed time: "+str("%d" %hour + " : "+"%d" %minute + " : "+"%d" %second), True, WHITE) 
        #screen.blit(text, [0+MARGIN,350])  
        choice=button("Play again?", 220,800,250,50, GRAY, BLUE, action="play")
        if choice!=None: 
            return 3

        pygame.display.update()
        clock.tick(10)


# Game Screen
########################################################################### 

def game_screen(N):

    game = Game(N)
    validmove= None

    done= False
    while not done:
        #Keyboard and mouse event handling
        for event in pygame.event.get(): 
            if event.type == pygame.QUIT: 
                return 0

            if event.type == pygame.KEYDOWN:
                entry = str(event.key)

            if event.type == pygame.MOUSEBUTTONDOWN:
                mouse_x, mouse_y = pygame.mouse.get_pos()
                game.evaluate_click(pygame.mouse.get_pos())



        # Game over calls, get winner, get_end time get saved_seconds
        ########################################################## 
        if game.status == 'game over':
            winner= game.winner
            end_time= timer()[0]
            sec=getScore()
            if sec < end_time:
                saveScore(sec)

            print (winner)
            print(end_time)
            return 2, winner, end_time

        # Draw calls
        ############################# 
        screen.fill(BLACK)
        # Draw the game board and tokens:
        game.draw()
        # Display whose turn is it
        message=str(pygame.display.get_caption()[0])
        message_to_screen(message, WHITE, y_displace=310, size="small")
        #print (timer()[0])
        time=timer()[1]
        # Display Game timer
        text=smallfont.render("Elapsed time: "+str(time[0]) + " : " +str(time[1])+ " : " +str(time[2]), True, WHITE) 
        screen.blit(text, [200,800])
        # Display invalid move
        if game.valid_move==False:
            validmove = "Invalid Move"
            text=smallfont.render(validmove, True, WHITE) 
            screen.blit(text, [250,850])

        pygame.display.flip()
        clock.tick(10)


        pygame.display.flip()     
        clock.tick(30)


########################################################################### 



choice= game_intro_screen()
while choice !=0:
    # active_board = 8x8
    if choice==8:
        a= game_screen(choice)
        if isinstance(a, tuple):
            choice=a[0]
        else:
            choice=game_screen(choice)
    # active_board = 10x10
    if choice==10:
        a= game_screen(choice)
        if isinstance(a, tuple):
            choice=a[0]
        else:
            choice=game_screen(choice)
    # active_board = 12x12
    if choice==12:
        a= game_screen(choice)
        if isinstance(a, tuple):
            choice=a[0]
        else:
            choice=game_screen(choice)  
    # Exit to game_over screen
    if choice==2:
        if isinstance(a, tuple):
            W=a[1]
            ET=a[2]
        choice=game_over_screen(W,ET)       # Pass winner into game_over_screen
    # From game_over_screen, Play again, call intro screen
    if choice==3:
        choice=game_intro_screen()



pygame.QUIT
quit()

1 个答案:

答案 0 :(得分:0)

解决此问题的一种方法是返回包含信息的字典。 添加“原因”,您可以根据需要存储该原因,以存储游戏结束的原因(取消,游戏结束)和其他条目。

这还将使您以后可以返回其他信息,同时仍返回相同的数据类型。