为什么我需要两次设置相同的条件? (Python 3.4)

时间:2015-09-08 13:06:04

标签: python python-3.x if-statement pygame

我正在为使用Pygame编写的菜单创建一些按钮,当我注意到一些似乎没有加起来的东西时。以下是代码目前的样子:

    #Load up the modules
    import pygame, time, random, linecache
    pygame.init()

    #Let's get some basic colours stored.
    black = (0,0,0)
    gray = (123,123,123)
    white = (255,255,255)
    red = (200,0,0)
    bright_red = (255,0,0)
    green = (0,200,0)
    bright_green = (0,255,0)
    blue = (0,0,200)
    bright_blue = (0,0,255)
    yellow = (225,225,0)
    bright_yellow = (255,255,0)

    #Loading up a time obect function for later.
    clock = pygame.time.Clock()

    #This function is necessary all round.
    #n is the line to be read.
    def readline(file, n):
            current_line = linecache.getline(file , n)
            current_line = current_line[:-1]
            return current_line

    #Let's set the screen dimensions based on the options file.
    screen_width = int(readline('Options.txt', 2))
    screen_height = int(readline('Options.txt', 3))
    game_display = pygame.display.set_mode((screen_width,screen_height))
    pygame.display.set_caption('Le jeux')

    #Functions for displaying text.
    def text_objects(text, font, colour):
        textSurface = font.render(text, True, colour)
        return textSurface, textSurface.get_rect()

    def message_display(text, x_ordinate, y_ordinate, font_size, colour):
        #Consider letting the font be a paramter.
        largeText = pygame.font.Font("freesansbold.ttf",font_size)
        TextSurf, TextRect = text_objects(text, largeText, colour)
        TextRect.center = (x_ordinate,y_ordinate)
        game_display.blit(TextSurf, TextRect)


    def button(x, y, width, height, active_colour, inactive_colour, message, font_size, function):
        if(x <= mouse[0]/screen_width <= x + width and y <= mouse[1]/screen_height <= y + height):
            pygame.draw.rect(game_display, active_colour, (x * screen_width, y * screen_height, width * screen_width, height * screen_height))
            message_display(message, round(screen_width * (x + width/2)), round(screen_height * (y + height/2)), round(font_size * screen_height), black)
            function
        else:
            pygame.draw.rect(game_display, inactive_colour, (x * screen_width, y * screen_height, width * screen_width, height * screen_height))
            message_display(message, round(screen_width * (x + width/2)), round(screen_height * (y + height/2)), round(font_size * screen_height), black)

    def my_test(x, y, width, height):
        if(x <= mouse[0]/screen_width <= x + width and y <= mouse[1]/screen_height <= y + height):
        #The line above seems completely redundant yet for some reason is essential
            print("hi")

    #Creating a few variables for the loops to come.
    viewing_menu = True
    playing_game = False

    #Showing a very basic menu.
    while viewing_menu == True:

        for event in pygame.event.get():
            if(event.type == pygame.QUIT) :
                viewing_menu = False

        #Determining the mouse's current position
        mouse = pygame.mouse.get_pos()

        game_display.fill(white)


        button(0.25, 0.1, 0.5, 0.2, bright_green, green, 'Play', 0.15, my_test(0.25, 0.1, 0.5, 0.2))
        #button(0.25, 0.4, 0.5, 0.2, bright_yellow, yellow, 'Options', 0.15, my_test())
        #button(0.25, 0.7, 0.5, 0.2, bright_red, red, 'Quit', 0.15, my_test())

        pygame.display.update()
        clock.tick(60)

    pygame.quit()
    quit()

像这样,当我的鼠标盘旋在播放按钮上时,python会不断打印“hi”,这正是我此时想要的。问题是,第65行if(x <= mouse[0]/screen_width <= x + width and y <= mouse[1]/screen_height <= y + height):似乎浪费了空间和时间,因为函数my_test只应该被调用,而第58行(完全相同)才是真的。也就是说我两次评估相同的情况。

出于某种原因,如果我对该行进行评论,则无论鼠标的位置如何,都会连续打印出“hi”。目前这不是一个大问题(考虑到它的早期),但我认为必须多次评估相同条件可能会在开始变得重要时降低性能。

我需要知道的是为什么似乎无条件地调用my_test函数。

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

当您调用my_test(0.25, 0.1, 0.5, 0.2)函数时,您可以调用button()作为参数之一,因此每次迭代都会调用mytest()button()

button(0.25, 0.1, 0.5, 0.2, bright_green, green, 'Play', 0.15, my_test(0.25, 0.1, 0.5, 0.2))
                                                                ^^ right there

你应该传递函数本身:

button(0.25, 0.1, 0.5, 0.2, bright_green, green, 'Play', 0.15, my_test)

在内部按钮()中,您可以调用它:

def button(x, y, width, height, active_colour, inactive_colour, message, font_size, function):
    if(x <= mouse[0]/screen_width <= x + width and y <= mouse[1]/screen_height <= y + height):
        #...
        function(x,y,width,height)

这可以让你改变button()调用的功能。如果它始终是mytest() you could call mytest()directly from button() and omit the last argument to按钮()`。