2017-01-13 5 views
-1

ゲームを作成しようとしていますが、この叢はボタンで作られていなければなりません。プレーヤーがボタンをクリックするたびに、押したボタンの内側に小さな円が表示されます。私はdraw_circle関数を作成しようとしましたが、ボタン内のアクション変数に配置すると、このエラーが発生します。グローバル名xは定義されていません。ボタンをクリックすると、四角の内側に円が表示されます。

import pygame 

pygame.init() 

display_width = 900 
display_height = 600 

black = (0,0,0) 
white = (255,255,255) 
red = (250,0,0) 
green = (0,250,0) 
bright_red =(200,0,0) 
bright_green = (0,200,0) 

gameDisplay = pygame.display.set_mode((display_width,display_height)) 
pygame.display.set_caption("A MATH'S GAME") 


def draw_circle(): 
    pygame.draw.circle(gameDisplay,black,(x,y),100) 


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: 
     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 text_objects(text, font): 
    textSurface = font.render(text, True, black) 
    return textSurface, textSurface.get_rect() 

def messgae_display(text): 
    largeText = pygame.font.Font("Arial",60) 
    TextSurf, TextRect = text_objects(text, largeText) 
    TextRect.center = ((display_width/2),(display_height/2)) 
    gameDisplay.blit(TextSurf, TextRect) 

    pygame.display.update() 


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

def game_intro(): 

    intro = True 

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

     gameDisplay.fill(white) 
     largeText = pygame.font.SysFont("comicsansms",115) 
     TextSurf, TextRect = text_objects("A math's game", largeText) 
     TextRect.center = ((display_width/2),(display_height/2)) 
     gameDisplay.blit(TextSurf, TextRect) 

     button("GO!",150,450,100,50,green,bright_green,draw_circle) 
     button("Quit",550,450,100,50,red,bright_red,quitgame) 

     pygame.display.update() 

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

     gameDisplay.fill(white) 
     largeText = pygame.font.SysFont("comicsansms",50) 
     TextSurf, TextRect = text_objects("A math's game", largeText) 
     TextRect.center = ((display_width/2),100) 
     gameDisplay.blit(TextSurf, TextRect) 

     button(None,200,250,50,50,bright_red,red,None) 
     button(None,200,300,50,50,bright_red,red,None) 
     button(None,200,350,50,50,bright_red,red,None) 
     button(None,200,400,50,50,bright_red,red,None) 
     button(None,200,450,50,50,bright_red,red,None) 
     button(None,200,500,50,50,bright_red,red,None) 
     button(None,250,250,50,50,bright_red,red,None) 
     button(None,250,300,50,50,bright_red,red,None) 
     button(None,250,350,50,50,bright_red,red,None) 
     button(None,250,400,50,50,bright_red,red,None) 
     button(None,250,450,50,50,bright_red,red,None) 
     button(None,250,500,50,50,bright_red,red,None) 
     button(None,300,250,50,50,bright_red,red,None) 
     button(None,300,300,50,50,bright_red,red,None) 
     button(None,300,350,50,50,bright_red,red,None) 
     button(None,300,400,50,50,bright_red,red,None) 
     button(None,300,450,50,50,bright_red,red,None) 
     button(None,300,500,50,50,bright_red,red,None) 
     button(None,350,250,50,50,bright_red,red,None) 
     button(None,350,300,50,50,bright_red,red,None) 
     button(None,350,350,50,50,bright_red,red,None) 
     button(None,350,400,50,50,bright_red,red,None) 
     button(None,350,450,50,50,bright_red,red,None) 
     button(None,350,500,50,50,bright_red,red,None) 
     button(None,400,250,50,50,bright_red,red,None) 
     button(None,400,300,50,50,bright_red,red,None) 
     button(None,400,350,50,50,bright_red,red,None) 
     button(None,400,400,50,50,bright_red,red,None) 
     button(None,400,450,50,50,bright_red,red,None) 
     button(None,400,500,50,50,bright_red,red,None) 
     button(None,450,250,50,50,bright_red,red,None) 
     button(None,450,300,50,50,bright_red,red,None) 
     button(None,450,350,50,50,bright_red,red,None) 
     button(None,450,400,50,50,bright_red,red,None) 
     button(None,450,450,50,50,bright_red,red,None) 
     button(None,450,500,50,50,bright_red,red,None) 
     button(None,500,250,50,50,bright_red,red,None) 
     button(None,500,300,50,50,bright_red,red,None) 
     button(None,500,350,50,50,bright_red,red,None) 
     button(None,500,400,50,50,bright_red,red,None) 
     button(None,500,450,50,50,bright_red,red,None) 
     button(None,500,500,50,50,bright_red,red,None) 
     button(None,550,250,50,50,bright_red,red,None) 
     button(None,550,300,50,50,bright_red,red,None) 
     button(None,550,350,50,50,bright_red,red,None) 
     button(None,550,400,50,50,bright_red,red,None) 
     button(None,550,450,50,50,bright_red,red,None) 
     button(None,550,500,50,50,bright_red,red,None) 
     button(None,600,250,50,50,bright_red,red,None) 
     button(None,600,300,50,50,bright_red,red,None) 
     button(None,600,350,50,50,bright_red,red,None) 
     button(None,600,400,50,50,bright_red,red,None) 
     button(None,600,450,50,50,bright_red,red,None) 
     button(None,600,500,50,50,bright_red,red,None) 
     button(None,650,250,50,50,bright_red,red,None) 
     button(None,650,300,50,50,bright_red,red,None) 
     button(None,650,350,50,50,bright_red,red,None) 
     button(None,650,400,50,50,bright_red,red,None) 
     button(None,650,450,50,50,bright_red,red,None) 
     button(None,650,500,50,50,bright_red,red,None) 

     pygame.display.update() 

pygame.init() 
game_intro() 
+0

トレースバックを含む、完全な誤りをお願いします。非常に短いコードを除いて、問題の内容を知るには、エラーの場所が不可欠です。 – Douglas

答えて

0
あなたが xyため draw_circle値を与えていない button("GO!",150,450,100,50,green,bright_green,draw_circle)ラインで

ので、彼らは、実際には、定義されていません。これをdraw_circle関数に渡す必要があります。あなたのbutton関数の内部

、同じようdraw_circleまでの引数を渡す:

if click[0] == 1 and action != None: 
     action(x, y) 
+0

また、典型的なPythonコーディングスタイルでは、 'action!= None'を' action is not None'に変更する必要があります。 –

+1

ダング、7分で私を打ち負かしてください! = P – Douglas

+1

@Douglas lol私はちょっと奇妙だよ、基本的にはStackOverflowをブラウズするのは退屈だよlmao –

関連する問題