私の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)
テキスト 'などのリストや辞書にループしておくと' '「使用...」'あなたは一度だけ、それらを生成することができ変えることはありません「WARPED!」 - 'の前にwhile'。 – furas
'button'の中の' useimage3'はローカル変数です。この関数の外にあるグローバル変数 'useimage3'は変更されません。 'button'の中に 'global useimage3'を追加して、' = 'を使うときにローカル/ローカル変数を使うのではなく、外部/グローバル変数を使うべきことを関数に通知する必要があります。 – furas
ところで: 'if'なしで' useimage3 = player3'を簡単にすることができます。そして、あなたが 'action()'を実行する前に、そしてあなたがボタンをクリックした場合にのみ行う必要があります。 – furas