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
당신이 x 또는 y에 대한 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

또한 전형적인 파이썬 코딩 스타일은'action! = None'을'action is not None'으로 바꿔야한다고 지시합니다. –

+1

Dang, 당신은 7 분 정도 나를 이겼습니다! = P – Douglas

+1

@Douglas lol 나는 괴상하다, 나는 기본적으로 StackOverflow를 탐색한다. lmao를 지루하다. –