私は自分のゲームにある種のメニューナビゲーションシステムを作成しました。 「再生」と「終了」と「コントロール」ボタンは正常に機能しますが、コントロール画面からメニューを押すと何も起こりません。コントロール画面では、最初のメニュー画面をかすかに見ることができます。それは問題かもしれません。メニューボタンへのリターンは前のコントロールのページボタンの上にあるので、何とか前からコントロールボタンを押していると思います。私のコードのボタンとメニューセグメントがここに貼り付けられ、すべてがペーストビンに貼り付けられます。ゲームのボタンが動作しない
def text_to_button(msg,color,buttonx,buttony,buttonwidth,buttonheight,size = "small"):
textSurf, textRect = text_objects(msg,color,size)
textRect.center = ((buttonx + buttonwidth/2)), buttony+(buttonheight/2)
gameDisplay.blit(textSurf, textRect)
def button(text,x,y,width,height,inactive_color,active_color,size = "small",action = None):
cur = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
#print(click)
if x + width > cur[0] > x and y + height > cur[1] > y:
pygame.draw.rect(gameDisplay, active_color,(x,y,width,height))
if click[0] == 1 and action != None:
if action == "quit":
pygame.quit()
quit()
if action == "controls":
game_controls()
if action == "play":
gameLoop()
if action == "main":
game_intro()
else:
pygame.draw.rect(gameDisplay, inactive_color,(x,y,width,height))
text_to_button(text,black,x,y,width,height,size)
def game_controls():
gcont = True
while gcont:
gameDisplay.blit(cont,(0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
button("Play",150,500,100,50,white,gray,"small",action = "play")
button("Main Menu",320,500,150,50,white,gray,"tiny", action = "main")
button("Quit",550,500,100,50,white,gray,"small", action = "quit")
pygame.display.update()
clock.tick(15)
def game_intro():
intro = True
while intro:
gameDisplay.blit(imggg,(0,0))
button("Play",150,500,100,50,white,gray,"small",action = "play")
button("ControLs",320,500,150,50,white,gray,"tiny", action = "controls")
button("Quit",550,500,100,50,white,gray,"small", action = "quit")
pygame.display.update()
clock.tick(15)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_p:
intro = False
全コード:https://pastebin.com/jrd82gkJ
をコーディングする楽しみを持ってありがとうございました。私はまだ2ヶ月前からコーディングを始めたばかりです。なぜ私はそれが働いていないのかを知りました。私はそれが働くようになっているが、時にはボタンを2〜3回押す必要がある。私は今すぐコードをデバッグしようとしています – PikachuPopcorn
あなたは大歓迎です。奇妙な振る舞いは、ボタンの押下で発生する再帰呼び出しのランダムなタイミングに由来します(したがって、時には2,3回、ランダムのみ)。それは適切に動作するように見えますが、十分に頻繁にビューを切り替えると、「再帰深度を超過しています」というエラーが発生します(実際には起こりません - 前にビューを切り替えるのが疲れます)。 – Claudio