2017-12-12 9 views
0

私は問題があります。私は既にテキストとボタンでいっぱいの画面を持っており、ボタンを押して背景に色を塗りたいと思っています。 (私はこのトピックについて何百もの質問があることを知っていますが、私はそれらを理解できません)。これは私がボタンを持っているコードのセクションです。私は、ボタンを押したときに色を変える機能を作るのは簡単だと思ったが、ボタンを押すと、マウスボタンを押している間は色が変わる。私が持っていないときは、テキストとボタン付きの画面に戻ります。python pygame screen.fill

def game_loop(): 
    global display_text 
    global display_button_1 
    global display_button_2 

    gameExit = False 
    display_text = True 
    display_button_1 = True 
    display_button_2 = False 

    meie_font = pygame.font.SysFont("Arial", 36) 

    tekst = "This game will go as far as you choose!" 
    teksti_pilt1 = meie_font.render(tekst, False, (50,50,155)) 

    tekst2 = "You are the smith of your destiny" 
    teksti_pilt = meie_font.render(tekst2, False, (50,50,155)) 
######################################################################## 
    while not gameExit: 

     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       return 

     gameDisplay.fill(white) 
######################################################################## 
     if display_text: 
      gameDisplay.blit(teksti_pilt1, (100, 250)) 
      gameDisplay.blit(teksti_pilt, (100, 400)) 
######################################################################## 
     if display_button_1: 
      button("Start playing", 300,500,150,50,green,bright_green, hide_text) 
######################################################################## 
     if display_button_2: 
      #gameDisplay.fill(white) 
      bg = pygame.image.load("natsalo.jpg") 
      gameDisplay.blit(bg, (0, 0)) 

      tekst = "You, just have a friendly dinner with your family!" 
      meie_font = pygame.font.SysFont("Arial", 36) 
      teksti_pilt = meie_font.render(tekst, False, (25,25,155)) 
      gameDisplay.blit(teksti_pilt, (100, 30)) 

      tekst = "It wasn't so usualy as always, it was always something...!" 
      meie_font = pygame.font.SysFont("Arial", 36) 
      teksti_pilt = meie_font.render(tekst, False, (25,25,155)) 
      gameDisplay.blit(teksti_pilt, (50, 100)) 

      tekst = "You are Lee Everett, you are a professor who taught history" 
      meie_font = pygame.font.SysFont("Arial", 36) 
      teksti_pilt = meie_font.render(tekst, False, (25,25,155)) 
      gameDisplay.blit(teksti_pilt, (25, 170)) 

      tekst = "For over six years at the University of Georgia" 
      meie_font = pygame.font.SysFont("Arial", 36) 
      teksti_pilt = meie_font.render(tekst, False, (25,25,155)) 
      gameDisplay.blit(teksti_pilt, (100, 240)) 

      tekst = "You, just have a friendly dinner with your family!" 
      meie_font = pygame.font.SysFont("Arial", 36) 
      teksti_pilt = meie_font.render(tekst, False, (25,25,155)) 
      gameDisplay.blit(teksti_pilt, (100, 310)) 

      button("I am to lazy to wash hands, just sit", 100,500,600,50,green,bright_green, hide_text) 
      button("Wash your hands", 100,400,600,50,green,bright_green, next_screen) 

     def next_screen(): 
      gameDisplay.fill((0,0,0)) 


     pygame.display.update() 

これは必要な場合はフルコードです。

import pygame 
import sys 


pygame.init() 
############# 
pygame.mixer.music.load('Invincible.mp3') 
pygame.mixer.music.play() 

############# 
clock = pygame.time.Clock() 

display_width = 800 
display_height = 600 

black = (0,0,0) 
white = (255,255,255) 

red = (200,0,0) 
green = (0,200,0) 

bright_red = (255,0,0) 
bright_green = (0,255,0) 

block_color = (53,115,255) 


gameDisplay = pygame.display.set_mode((display_width,display_height)) 
pygame.display.set_caption('One Day After') 
clock = pygame.time.Clock() 

gameIcon = pygame.image.load('gameicon.jpg') 
pygame.display.set_icon(gameIcon) 

pause = False 

def text_objects(text, font): 
    textSurface = font.render(text, True, black) 
    return textSurface, textSurface.get_rect() 


def GameOver(): 
    #################################### 
    pygame.mixer.Sound.play("smb_gameover.wav") 
    pygame.mixer.music.stop() 
    #################################### 
    largeText = pygame.font.SysFont("comicsansms",115) 
    TextSurf, TextRect = text_objects("Game Over", largeText) 
    TextRect.center = ((display_width/2),(display_height/2)) 
    gameDisplay.blit(TextSurf, TextRect) 


    while True: 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       pygame.quit() 
       quit() 



     button("Play Again",150,450,100,50,green,bright_green,game_loop) 
     button("Quit",550,450,100,50,red,bright_red,quitgame) 

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

def button(msg,x,y,w,h,ic,ac,action=None): 
    mouse = pygame.mouse.get_pos() 
    click = pygame.mouse.get_pressed() 

    if x+w > mouse[0] > x and y+h > mouse[1] > y: 
     pygame.draw.rect(gameDisplay, ac,(x,y,w,h)) 
     if click[0] == 1 and action != None: 
      pygame.mixer.music.stop() 
      action() 

    else: 
     pygame.draw.rect(gameDisplay, ic,(x,y,w,h)) 
    smallText = pygame.font.SysFont("comicsansms",20) 
    textSurf, textRect = text_objects(msg, smallText) 
    textRect.center = ((x+(w/2)), (y+(h/2))) 
    gameDisplay.blit(textSurf, textRect) 


def quitgame(): 
    pygame.quit() 
    sys.exit() 
    quit() 

def hide_text(): 
    global display_text 
    global display_button_1 
    global display_button_2 

    display_text = False 
    display_button_1 = False 
    display_button_2 = True 

def unpause(): 
    global pause 
    pygame.mixer.music.unpause() 
    pause = False 


def paused(): 
    ############ 
    pygame.mixer.music.pause() 
    ############# 
    largeText = pygame.font.SysFont("comicsansms",115) 
    TextSurf, TextRect = text_objects("Paused", largeText) 
    TextRect.center = ((display_width/2),(display_height/2)) 
    gameDisplay.blit(TextSurf, TextRect) 


    while pause: 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       pygame.quit() 
       quit() 


     button("Continue",150,450,100,50,green,bright_green,unpause) 
     button("Quit",550,450,100,50,red,bright_red,quitgame) 

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


def game_intro(): 

    intro = True 

    while intro: 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       pygame.quit() 
       quit() 

     pilt1 = pygame.image.load('apoc2.jpg').convert() 
     gameDisplay.blit(pilt1, [0,0]) 

     button("Start",150,450,100,50,green,bright_green,game_loop) 
     button("Quit",550,450,100,50,red,bright_red,quitgame) 

     pygame.display.update() 

def game_loop(): 
    global display_text 
    global display_button_1 
    global display_button_2 

    gameExit = False 
    display_text = True 
    display_button_1 = True 
    display_button_2 = False 

    meie_font = pygame.font.SysFont("Arial", 36) 

    tekst = "This game will go as far as you choose!" 
    teksti_pilt1 = meie_font.render(tekst, False, (50,50,155)) 

    tekst2 = "You are the smith of your destiny" 
    teksti_pilt = meie_font.render(tekst2, False, (50,50,155)) 
######################################################################## 
    while not gameExit: 

     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       return 

     gameDisplay.fill(white) 
######################################################################## 
     if display_text: 
      gameDisplay.blit(teksti_pilt1, (100, 250)) 
      gameDisplay.blit(teksti_pilt, (100, 400)) 
######################################################################## 
     if display_button_1: 
      button("Start playing", 300,500,150,50,green,bright_green, hide_text) 
######################################################################## 
     if display_button_2: 
      #gameDisplay.fill(white) 
      bg = pygame.image.load("natsalo.jpg") 
      gameDisplay.blit(bg, (0, 0)) 

      tekst = "You, just have a friendly dinner with your family!" 
      meie_font = pygame.font.SysFont("Arial", 36) 
      teksti_pilt = meie_font.render(tekst, False, (25,25,155)) 
      gameDisplay.blit(teksti_pilt, (100, 30)) 

      tekst = "It wasn't so usualy as always, it was always something...!" 
      meie_font = pygame.font.SysFont("Arial", 36) 
      teksti_pilt = meie_font.render(tekst, False, (25,25,155)) 
      gameDisplay.blit(teksti_pilt, (50, 100)) 

      tekst = "You are Lee Everett, you are a professor who taught history" 
      meie_font = pygame.font.SysFont("Arial", 36) 
      teksti_pilt = meie_font.render(tekst, False, (25,25,155)) 
      gameDisplay.blit(teksti_pilt, (25, 170)) 

      tekst = "For over six years at the University of Georgia" 
      meie_font = pygame.font.SysFont("Arial", 36) 
      teksti_pilt = meie_font.render(tekst, False, (25,25,155)) 
      gameDisplay.blit(teksti_pilt, (100, 240)) 

      tekst = "You, just have a friendly dinner with your family!" 
      meie_font = pygame.font.SysFont("Arial", 36) 
      teksti_pilt = meie_font.render(tekst, False, (25,25,155)) 
      gameDisplay.blit(teksti_pilt, (100, 310)) 

      button("I am to lazy to wash hands, just sit", 100,500,600,50,green,bright_green, hide_text) 
      button("Wash your hands", 100,400,600,50,green,bright_green, next_screen) 

     def next_screen(): 
      gameDisplay.fill((0,0,0)) 


     pygame.display.update() 



game_intro() 
game_loop() 
pygame.quit() 
quit() 
+1

[mcveページ](https://stackoverflow.com/help/mcve)をご覧ください。関連するコードだけを含んでおり、コピー、貼り付け、実行が可能な最小限の例が好きです。あなたがしようとしていることは私には分かりません。私はあなたを正しく理解していれば、次のシーンに切り替えて表示をクリア/塗りつぶしたいが、以前のグラフィックスは引き続き表示されます。あれは正しいですか? – skrx

+0

@skrxはい、それが計画でした。次のシーン(ボタンを押す)に切り替えるときに、画面に色が塗りつぶされていますが、マウスボタンを放しても前のグラフィックスは表示されます。 –

+0

どのボタンを意味しますか?あなたの手を洗う? 'while'ループの仕組みがわからないようです - すべてのコードを何度も何度も繰り返します - ボタンをクリックすると'(0,0,0) 'に色が変わりますが、whileループは最初に戻ります。すべての要素を再び描画します。どの要素を表示するかを制御するには変数 'True/False'を使用し、' fill(white) 'で使用する変数' background_color'を使用する必要があります。そして、あなたは 'background_color'に新しい色を割り当てて、次にそれを使用します。 – furas

答えて

0

コードを再編成する必要があります。

fill()、テキスト、ボタン、update()"subscenes"を作成します。

そして変数番号display_subsceneをnumberと使用して、どのサブディスプレイを表示するかを決定します。

クラスを使用して整理しやすくすることができます。

今の主な問題は、mouse.get_pressed()を使用するボタンです。そのため、異なるサブセンスの同じ場所にあるボタンを押し続けます。私はボタンを別の場所に移動しなければならなかった。

あなたはそれがすべてをクリックしていない私は、私は(新しい引数として)button()で使う変数mouse_buttonを設定するためにevent.type == MOUSEBUTTONDOWNを使用し、すべてのループと、このようにevent.type == MOUSEBUTTONDOWN


EDITを使用する必要があります押さえたままの時間。変数mouse_buttonは、すべてfor eventの前に0に設定する必要があります。

Pauseの半透明の背景を追加します。pgame_loopを押すと表示されます。

import pygame 
import sys 

# --- constants --- 

display_width = 800 
display_height = 600 

black = (0,0,0) 
white = (255,255,255) 

red = (200,0,0) 
green = (0,200,0) 

bright_red = (255,0,0) 
bright_green = (0,255,0) 

block_color = (53,115,255) 

# --- functions --- 

def text_objects(text, font, color=black): 
    textSurface = font.render(text, True, color) 
    return textSurface, textSurface.get_rect() 

def button(msg, x, y, w, h, ic, ac, click, action=None): 

    mouse = pygame.mouse.get_pos() 

    if x+w > mouse[0] > x and y+h > mouse[1] > y: 
     pygame.draw.rect(gameDisplay, ac,(x,y,w,h)) 
     if click == 1 and action != None: # <-- click instead of click[0] 
      pygame.mixer.music.stop() 
      action() 
    else: 
     pygame.draw.rect(gameDisplay, ic,(x,y,w,h)) 

    smallText = pygame.font.SysFont("comicsansms",20) 
    textSurf, textRect = text_objects(msg, smallText) 
    textRect.center = ((x+(w/2)), (y+(h/2))) 

    gameDisplay.blit(textSurf, textRect) 

def quitgame(): 
    pygame.quit() 
    sys.exit() 
    quit() 

# --- scenes --- 

# - scene Paused - 

def unpause(): 
    global pause 
    #pygame.mixer.music.unpause() 

    pause = False 

def paused(): 
    global pause 

    pause = True 

    background_image = pygame.Surface((display_width, display_height)).convert() 
    background_rect = background_image.get_rect(center=(display_width//2, display_height//2)) 
    background_image.set_alpha(220) 

    gameDisplay.blit(background_image, background_rect) 

    largeText = pygame.font.SysFont("comicsansms", 115) 
    TextSurf, TextRect = text_objects("Paused", largeText, white) 
    TextRect.center = ((display_width/2),(display_height/2)) 
    gameDisplay.blit(TextSurf, TextRect) 

    while pause: 

     mouse_button = 0 # reset in every loop 

     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       pygame.quit() 
       quit() 
      elif event.type == pygame.MOUSEBUTTONDOWN: 
       mouse_button = event.button 

     button("Continue",150,450,100,50,green,bright_green, mouse_button, unpause) 
     button("Quit",550,450,100,50,red,bright_red, mouse_button, quitgame) 

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

# - scene GameOver - 

def GameOver(): 

    largeText = pygame.font.SysFont("comicsansms",115) 
    TextSurf, TextRect = text_objects("Game Over", largeText) 
    TextRect.center = ((display_width/2),(display_height/2)) 

    gameDisplay.blit(TextSurf, TextRect) 

    while True: 

     mouse_button = 0 # reset in every loop 

     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       pygame.quit() 
       quit() 
      elif event.type == pygame.MOUSEBUTTONDOWN: 
       mouse_button = event.button 

     button("Play Again",150,450,100,50,green,bright_green, mouse_button, game_loop) 
     button("Quit",550,450,100,50,red,bright_red, mouse_button, quitgame) 

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

# - scene GameIntro - 

def game_intro(): 

    # - objects - 

    #pilt1 = pygame.image.load('apoc2.jpg').convert() 

    # - loop - 

    intro = True 

    while intro: 

     # - events - 

     mouse_button = 0 # reset in every loop 

     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       pygame.quit() 
       quit() 
      elif event.type == pygame.MOUSEBUTTONDOWN: 
       mouse_button = event.button 
      elif event.type == pygame.KEYDOWN: 
       if event.key == pygame.K_p: 
        paused() 

     # - updates - 

     button("Start",150,450,100,50,green,bright_green, mouse_button, game_loop) 
     button("Quit",550,450,100,50,red,bright_red, mouse_button, quitgame) 

     # - draws - 

     #gameDisplay.blit(pilt1, [0,0]) 
     pygame.display.update() 

# - scene GameLoop - 

def goto_subscene(number): 
    global display_subscene 

    display_subscene = number 

def game_loop(): 
    global display_subscene 

    # at start display subscene 1 
    display_subscene = 1 

    #bg = pygame.image.load("natsalo.jpg") 

    meie_font = pygame.font.SysFont("Arial", 36) 

    # - subscene 1 - 

    tekst = "This game will go as far as you choose!" 
    subscene_1_text_1 = meie_font.render(tekst, False, (50,50,155)) 

    tekst = "You are the smith of your destiny" 
    subscene_1_text_2 = meie_font.render(tekst, False, (50,50,155)) 

    # - subscene 2 - 

    tekst = "You, just have a friendly dinner with your family!" 
    subscene_2_text_1 = meie_font.render(tekst, False, (25,25,155)) 

    tekst = "It wasn't so usualy as always, it was always something...!" 
    subscene_2_text_2 = meie_font.render(tekst, False, (25,25,155)) 

    tekst = "You are Lee Everett, you are a professor who taught history" 
    subscene_2_text_3 = meie_font.render(tekst, False, (25,25,155)) 

    tekst = "For over six years at the University of Georgia" 
    subscene_2_text_4 = meie_font.render(tekst, False, (25,25,155)) 

    tekst = "You, just have a friendly dinner with your family!" 
    subscene_2_text_5 = meie_font.render(tekst, False, (25,25,155)) 

    # - subscene 3 - 

    tekst = "You killed by viruses !" 
    subscene_3_text_1 = meie_font.render(tekst, False, white) 

    # - subscene 4 - 

    tekst = "You lost searching bathroom !" 
    subscene_4_text_1 = meie_font.render(tekst, False, white) 

    # - subscene 5 - 

    tekst = "Good Bye !" 
    subscene_5_text_1 = meie_font.render(tekst, False, white) 

    # - loop - 

    gameExit = False 

    while not gameExit: 

     mouse_button = 0 # reset in every loop 

     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: 
       return 
      elif event.type == pygame.MOUSEBUTTONDOWN: 
       mouse_button = event.button 
      elif event.type == pygame.KEYDOWN: 
       if event.key == pygame.K_p: 
        paused() 

     if display_subscene == 1: 
      gameDisplay.fill(white) 

      gameDisplay.blit(subscene_1_text_1, (100, 250)) 
      gameDisplay.blit(subscene_1_text_2, (100, 400)) 

      button("Start playing", 300,500,150,50,green,bright_green, mouse_button, lambda:goto_subscene(2)) 

      pygame.display.update() 

     elif display_subscene == 2: 
      gameDisplay.fill(white) 

      gameDisplay.blit(subscene_2_text_1, (100, 30)) 
      gameDisplay.blit(subscene_2_text_2, (50, 100)) 
      gameDisplay.blit(subscene_2_text_3, (25, 170)) 
      gameDisplay.blit(subscene_2_text_4, (100, 240)) 
      gameDisplay.blit(subscene_2_text_5, (100, 310)) 

      button("Wash your hands", 100,400,600,50,green,bright_green, mouse_button, lambda:goto_subscene(4)) 
      button("I am to lazy to wash hands, just sit", 100,500,600,50,green,bright_green, mouse_button, lambda:goto_subscene(3)) 

      pygame.display.update() 

     elif display_subscene == 3: 
      gameDisplay.fill(black) 

      gameDisplay.blit(subscene_3_text_1, (100, 30)) 
      button("Go forward", 100,500,600,50,green,bright_green, mouse_button, lambda:goto_subscene(5)) 

      pygame.display.update() 

     elif display_subscene == 4: 
      gameDisplay.fill(black) 

      gameDisplay.blit(subscene_4_text_1, (100, 30)) 
      button("Go forward", 100,500,600,50,green,bright_green, mouse_button, lambda:goto_subscene(5)) 

      pygame.display.update() 

     elif display_subscene == 5: 
      gameDisplay.fill(black) 

      gameDisplay.blit(subscene_5_text_1, (100, 30)) 
      button("Exit", 100,500,600,50,green,bright_green, mouse_button, quitgame) 

      pygame.display.update() 

# --- main --- 

# - init - 

pygame.init() 
gameDisplay = pygame.display.set_mode((display_width,display_height)) 
pygame.display.set_caption('One Day After') 

# - m 

clock = pygame.time.Clock() 
pause = False 

game_intro() 
game_loop() 
pygame.quit() 
+0

@ 0x5453私はコードを入れて、私は説明を書くことを開始:) – furas