I was creating some buttons for a menu being written using Pygame, when I noticed something that didn't seem to add up. Here's what the code currently looks like:
#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()
Like this, python continually prints "hi" while my mouse hovers over the play button which is exactly what I intend it to at this point. The thing is, line 65 if(x <= mouse[0]/screen_width <= x + width and y <= mouse[1]/screen_height <= y + height):
seems like a waste of space and time since the function my_test
should only be called up while line 58 (which is exactly the same) is true. That is to say that I'm evaluating the same condition twice.
For some reason though, if I comment that line out, "hi" is printed out continually regardless of the position of the mouse. This isn't a huge issue at present (considering how early it is) but I imagine having to evaluate the same conditions multiple times could reduce performance later down the line when is starts to matter.
What I need to know is why the my_test
function seems to be unconditionally called.
Thanks in advance for any help.