6/2/2023 0 Comments Online simon saysIt 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). 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. ![]() The most important (game breaking) problem is that the game will become unresponsive if you don't make some sort of call to the event queue regularly, and when that happens (for example when the correct_list becomes too large and sleep gets called too often) you can't see which buttons the AI is clicking anymore.Ī quick fix would be to call () in the ai_turn function: def ai_turn():.Global AI_turn, player_turn, correct_list ![]() I understand my use of globals in some functions is considered poor, but I couldn't think of any other way to write them. Please let me know if there's anything notable that I should improve on. I've been teaching myself python and I decided to make a Simon copy.
0 Comments
Leave a Reply. |