How Could I Optimise This Simple Python Pygame Code
I've been set a challenge to make a game using pygame ( I am making snake so it should be easy... but I've never used pygame) so using a more efficient language isn't an option.So
Solution 1:
To improve the game's performance, draw the grid on a pygame.Surface
object with the size of the screen, before the application loop:
defdrawGrid(surf):
surf.fill(grey)
blockSize = 10for x inrange(display_width):
for y inrange(display_height):
rect = pygame.Rect(x*blockSize, y*blockSize,blockSize, blockSize)
pygame.draw.rect(surf, darkgrey, rect, 1)
grid_surf = pygame.Surface(display.get_size())
drawGrid(grid_surf)
blit
the surface once per frame on the display instead of drawing the grid once per frame, in the application loop:
defSnakeGameLoop():
# [...]whilenot game_over:
# [...]
display.blit(grid_surf, (0, 0))
pygame.draw.rect(display,puregreen,[X,Y,10,10])
pygame.display.update()
Post a Comment for "How Could I Optimise This Simple Python Pygame Code"