2017-02-04 17 views
-2

私はwhileループを作成し、.blit()はうまく動作しますが、ifステートメントに到達すると、ロードサインが表示され、何も動作しないようです。 whileループを間違ってやっていますか?私はまた、私のmenu1 == Falseが次のwhileループをトリガーしたい。あなたはなぜ私のwhileループのif文は動作しませんか?

  • =代わりの代わりにwhile menu1 == True

menu1 = False

  • if menu1 == True==を必要とする。しかし、あなたがより良い方法(または使用中のコードを整理することができ

    #Importing Stuff 
    import pygame 
    import sys 
    import time 
    import random 
    from pygame.locals import* 
    pygame.init() 
    
    #Naming Variables 
    menu = 0 
    color = (65,105,225) 
    tcolor = (255,255,255) 
    pcolor = (255,255,255) 
    hcolor = (255,255,255) 
    width, height = 1920, 1080 
    screen = pygame.display.set_mode((width, height)) 
    hecolor = (255,255,255) 
    sys_font = pygame.font.SysFont \ 
          ("None", 60) 
    menu1 = True 
    
    #Initializing Screen 
    pygame.display.set_caption("TSA Trend Game") 
    screen.fill(((color))) 
    pygame.display.update() 
    
    #Making Menu 
    while 1 == 1 and menu == 0: 
        for event in pygame.event.get(): 
         if event.type == QUIT: 
          pygame.quit() 
          sys.exit() 
         #More Variables 
         rendered = sys_font.render \ 
          ("Welcome to Trends of 2016!", True, ((tcolor))) 
         play = sys_font.render \ 
          ("Play Game", True, ((pcolor))) 
         help = sys_font.render \ 
          ("Help", True, ((hcolor))) 
         play_r = play.get_rect() 
         play_r.x, play_r.y = 710, 500 
         help_r = help.get_rect() 
         help_r.x, help_r.y = 1170, 500 
         render_r = play.get_rect() 
         render_r.x, render_r.y = 710, 500 
         #Display Text 
        while menu1 == True: 
         screen.blit(rendered, (710, 440)) 
         screen.blit(help, (1170, 500)) 
         screen.blit(play, (710, 500)) 
         pygame.display.update() 
         if render_r.collidepoint(pygame.mouse.get_pos()): 
          pcolor = (255,255,0) 
         else: 
          pcolor = (255,255,255) 
         if help_r.collidepoint(pygame.mouse.get_pos()): 
          hcolor = (255,255,0) 
         else: 
          hcolor = (255,255,255) 
         if event.type == pygame.MOUSEBUTTONDOWN and help_r.collidepoint(pygame.mouse.get_pos()): 
          menu1 == False 
        while menu1 == False: 
         screen.fill(color) 
         pygame.display.update() 
    
    pygame.display.update() 
    
  • +1

    'MENU1 == false'のは、' '真!= false'のようfalse'を、**ない**割り当てに評価し、比較したものです。また、あなたはちょうど '中断することができます。 – jonrsharpe

    +1

    ゲームの各状態(メニュー、オプション、ゲーム、ヘルプなど)には、それ自身のループがあり、以下が含まれている必要があります。** 1 **。一定のフレームレートを維持するクロック。 ** 2 **。イベントループ。 ** 3 **。オブジェクトの更新。 ** 4 **。オブジェクトを描画/描画します。 ** 5 **。ディスプレイを更新します。 [Here](http://stackoverflow.com/documentation/pygame/3959/getting-started-with-pygame/14697/a-simple-game#t=201702041456298336222)は簡単なウォークスルーです。 –

    +0

    'menu1 == True'の代わりに' if menu1 == True'が必要かもしれません。分割されたループに分割することもできます:http://imgur.com/MT7tZ4s – furas

    答えて

    0

    は用mainloopを分離しましたメニュー、ゲーム、hエル・P)。

    import pygame 
    
    # --- constants --- (UPPER_CASE names) 
    
    BLACK = ( 0, 0, 0) 
    WHITE = (255, 255, 255) 
    
    WIDTH = 1920 
    HEIGHT = 1080 
    
    # --- main --- (lower_case namse) 
    
    bg_color = (65,105,225) 
    
    text_color = WHITE 
    play_color = WHITE 
    help_color = WHITE 
    
    menu_background = (65, 105, 225) 
    play_background = (255, 0, 0) 
    help_background = ( 0, 255, 255) 
    
    pygame.init() 
    
    screen = pygame.display.set_mode((WIDTH, HEIGHT)) 
    screen_rect = screen.get_rect() 
    
    pygame.display.set_caption("TSA Trend Game") 
    
    sys_font = pygame.font.SysFont(None, 60) 
    
    # create only once 
    render = sys_font.render("Welcome to Trends of 2016!", True, text_color) 
    render_rect = render.get_rect(x=710, y=440) 
    
    play = sys_font.render("Play Game", True, play_color) 
    play_rect = play.get_rect(x=710, y=500) 
    
    help = sys_font.render("Help", True, help_color) 
    help_rect = help.get_rect(x=1170, y=500) 
    
    other = sys_font.render("click mouse in any place", True, WHITE) 
    other_rect = other.get_rect(center=screen_rect.center) 
    
    # - mainloop - 
    
    state_menu = True 
    state_play = False 
    state_help = False 
    
    running = True 
    
    while running: 
    
        # - events - 
    
        for event in pygame.event.get(): 
         if event.type == pygame.QUIT: 
          running = False 
    
         elif event.type == pygame.KEYDOWN: 
          if event.key == pygame.K_ESCAPE: 
           running = False 
    
         if state_menu: 
          if event.type == pygame.MOUSEBUTTONDOWN: 
           if help_rect.collidepoint(event.pos): 
            print("Help") 
            state_menu = False 
            state_help = True 
           elif play_rect.collidepoint(event.pos): 
            print("Play") 
            state_menu = False 
            state_play = True 
    
         elif state_play: 
          if event.type == pygame.MOUSEBUTTONDOWN: 
           state_menu = True 
           state_play = False 
    
         elif state_help: 
          if event.type == pygame.MOUSEBUTTONDOWN: 
           state_menu = True 
           state_help = False 
    
        # - updates - 
    
        if state_menu: 
    
         mouse_pos = pygame.mouse.get_pos() 
    
         if play_rect.collidepoint(mouse_pos): 
          play_color = (255,255,0) 
         else: 
          play_color = WHITE 
    
         if help_rect.collidepoint(mouse_pos): 
          help_color = (255,255,0) 
         else: 
          help_color = WHITE 
    
         play = sys_font.render("Play Game", True, play_color) 
         help = sys_font.render("Help", True, help_color) 
    
        elif state_play: 
         pass 
         # do something 
    
        elif state_help: 
         pass 
         # do something 
    
        # - draws - 
    
        if state_menu: 
         screen.fill(menu_background) 
         screen.blit(render, render_rect) 
         screen.blit(help, help_rect) 
         screen.blit(play, play_rect) 
    
        elif state_play: 
         screen.fill(play_background) 
         screen.blit(other, other_rect) 
    
        elif state_help: 
         screen.fill(help_background) 
         screen.blit(other, other_rect) 
    
        pygame.display.update() 
    
    
    # - end - 
    
    pygame.quit() 
    exit() 
    
    関連する問題