右键单击一个矩形内的圆圈

时间:2017-01-13 15:30:18

标签: python-2.7 button pygame geometry

我正在尝试创建一个游戏,我必须使这个丛由按钮组成。每次玩家点击按钮时,它都会在按下的按钮内显示一个小圆圈。我尝试制作draw_circle函数但是当我将它放在按钮内的action变量上时,我得到了这个错误。未定义全局名称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()

1 个答案:

答案 0 :(得分:0)

button("GO!",150,450,100,50,green,bright_green,draw_circle)行中,您没有为draw_circlex提供y值,因此事实上它们并未定义。您需要将这些传递给draw_circle函数。

button函数内部,将参数传递给draw_circle,如:

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