

Screen = _mode((450, 450))Ĭlock = () # A clock to limit the frame rate. # Takes a font object now instead of creating a new one. Return None # Return None if no button was clicked. """Return the clicked button or None if no button was clicked.""" The while loops and the corresponding variables could be put into different functions to make it easier to restart the game.ĭef _init_(self, color, dark_color, pos): It would be nice if the game could be restarted when it's over instead of having to execute the program again. I'm using the index variable in my example for this purpose and have removed the player_list.

I think the game should stop when a wrong button is clicked, so that you don't have to keep clicking (unless the original worked in this way).
SIMON SAYS MEMORY GAME WINDOWS
One more tip: If you set os.environ = '1', the pygame window will be centered and I think that also moves it to the front (if other windows are open in the foreground). However, for this particular game, I think it's okay and it probably doesn't need to be replaced. Time.sleep should usually not be called in games, since it stops the execution of the rest of the program and makes the game unresponsive. PEP 8 (capitalized words are for constants).ĭefine a instance and call clock.tick(frame_rate) every frame to limit the frame rate. In order to make that work, you have to append the button objects (references) to the correct_list instead of the indices. That makes it possible to iterate over the buttons with a for loop and you can shorten the click and press functions quite a bit.Īctually, I'd replace click and press with a function that checks which button was clicked and then check if it was the correct button. Pygame.Rects have handy collision detection methods ( collidepoint would be helpful here). You don't have to reassign the rect returned by, since the rect shape won't change anyway. I'd also assign the rect to a self.rect attribute and remove the pos and shape attributes. Then pass the desired color to the draw method. Instead of changing the colors in the Button's darken and lighten methods, you could just pass the light and dark color and make them attributes of the class.

Pass the variables to the functions that need them and then return something and bind the returned value to a variable. It would be a good idea to reduce the amount of global variables. In most games the event handling, game logic and the rendering are done separately, but here it's all intertwined. I think it would be better to restructure the main while loop.
