2017-01-13 4 views
0

私のPyGameの要件の1つは、「3 Player Mode」を含み、ボタンを使ってモードを区別することです。私は "プレイ"ボタンを2プレイヤー(デフォルトプレイヤー数)にし、 "3P"ボタンを3プレイヤーにするために追加プレイヤーを追加したい(CPUボタンを無視する)。私はそれを動作させるためにいくつかのコードを実装しました。現時点では、3Pボタンをクリックするたびに2人のプレイヤーしか表示されません。追加または削除する必要があるものはありますか?私もゲームのループがありますが、私はこの問題に関しては何も変更しませんでした。ボタン機能を使ってPyGameに「3 Player Mode」を追加するのが難しい

shipImg = pygame.image.load('ship1.png') 
shipImg = pygame.transform.scale(shipImg, (73,73)) 

shipImg2 = pygame.image.load('ship2.png') 
shipImg2 = pygame.transform.scale(shipImg2, (73,73)) 

shipImg3 = pygame.image.load('ship3.png') 
shipImg3 = pygame.transform.scale(shipImg3, (73,73)) 
useimage3 = False #3rd player is initially set to false 

これは私のボタン機能:私は私の選手たちを識別どこ

です。表示されているように、パラメータ内にリストされている項目の1つは「player3」です。関数の条件文の中で述べられているように、後で(ボタンが配置されている)ゲーム導入関数では、プレイヤー3を表示させるためにその値を "True"に設定します。

def button(msg,x,y,w,h,ic,ac, player3,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: 
     action() 

if player3 == True: 
    useimage3 = True 

else: 
    pygame.draw.rect(gameDisplay,ic, (x,y,w,h)) 

smallText = pygame.font.Font('freesansbold.ttf',20) 
textSurf, textRect = text_objects(msg,smallText, white) 
textRect = ((x+(w/2-25)), (y+(h/2+-10))) 
gameDisplay.blit(textSurf, textRect) 

私の紹介機能では、すべての選手の変数を「True」または「False」に設定しました。私の最初の2人のプレイヤー(p1とp2)はTrueに設定されています(私のゲームではデフォルトのプレイヤー数です)。プレーヤー3は "False"に設定されています。その結果、私はボタンの機能を実装し、ボタンの残りのための「3P」ボタンと「偽」の「player3」のためのパラメータの範囲内で、「真」に置く。クリック。button()イジーデ

def game_intro(): 

intro = True 

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

    gameDisplay.blit(background,(0,0)) 
    font = pygame.font.Font('freesansbold.ttf',75) 
    TextSurf, TextRect = text_objects('WARPED!',font, white) 
    TextRect.center = ((display_width/2),(display_height-500)) 
    gameDisplay.blit(TextSurf, TextRect) 

    font = pygame.font.Font('freesansbold.ttf',40) 
    TextSurf, TextRect = text_objects('Use the arrow keys to avoid the aliens!',font, white) 
    TextRect.center = ((display_width/2),(display_height/2)) 
    gameDisplay.blit(TextSurf, TextRect) 

    shipImg = True 
    shipImg2 = True 
    shipImg3 = False  


    button("PLAY",50,450,100,50,green,bright_green,False,game_loop) 
    button("QUIT",650,450,100,50,red,bright_red,False,quitgame) 
    button("CPU",250,450,100,50,blue,bright_blue,False,game_loop) 
    button("3P",450,450,100,50,purple,bright_purple, True, game_loop) 

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

テキスト 'などのリストや辞書にループしておくと' '「使用...」'あなたは一度だけ、それらを生成することができ変えることはありません「WARPED!」 - 'の前にwhile'。 – furas

+0

'button'の中の' useimage3'はローカル変数です。この関数の外にあるグローバル変数 'useimage3'は変更されません。 'button'の中に 'global useimage3'を追加して、' = 'を使うときにローカル/ローカル変数を使うのではなく、外部/グローバル変数を使うべきことを関数に通知する必要があります。 – furas

+0

ところで: 'if'なしで' useimage3 = player3'を簡単にすることができます。そして、あなたが 'action()'を実行する前に、そしてあなたがボタンをクリックした場合にのみ行う必要があります。 – furas

答えて

0

userImage3ローカル変数であり、外部関数を持つグローバル変数は変更されません。このグローバル変数を使用する必要があり、使用する際にローカル変数を作成しないようにする必要があります。=

私はこの前に何度もこの機能を何度も見ていました。フォント、テキスト、イメージなどを何度も繰り返し作成しますが(時間とCPUだけを浪費します)、while

def generate_button(msg, button_rect): 

    font = pygame.font.Font('freesansbold.ttf',20) 
    text_image, text_rect = text_objects(msg, font, white) 
    text_rect.center = button_rect.center 

    return text_image, text_rect 

# --- 

button_play = ["PLAY", pygame.Rect(50, 450, 100, 50), green, bright_green, False, game_loop] 

text_image, text_rect = generate_button(button_play[0], button_play[1]) 

button_play + = [text_image, text_rect] 

# ... other buttons ... 

while intro: 

    button(button_play) 
    button(button_quit) 
    button(button_cpu) 
    button(button_3p) 

def button(button_data): 
    # inform function to use external/global variable 
    # and not create local one when you use `=` 
    global useimage3 

    msg, button_rect, ic, ac, player3, action, text_image, text_rect = button_data 

    mouse = pygame.mouse.get_pos() 
    click = pygame.mouse.get_pressed() 

    if button_rect.colidepoint(mouse): 
     pygame.draw.rect(gameDisplay, ac, button_rect) 
     if click[0] and action: 
      # now we doesn't create local variable but we change global one 
      useimage3 = player3 
      action() 
    else: 
     pygame.draw.rect(gameDisplay, ic, button_rect) 

    gameDisplay.blit(text_image, text_rect) 
+0

あなたの提案をありがとう!できます。 – HOFBrINCl

関連する問題