I have a basic pygame program that I use at the start of every project that involves pygame. The idea is every time I start a project that involves pygame I can copy and past and I can immediately start on my project without changing anything. The reason I am posting this is because of another question I posted. The answering user told me to shift some lines of my program to if __name__ == "__main__":
that were part of this basic pygame program. I am wondering what else I could do to improve this code.
import pygame
title = "Game"
size = (1000, 600)
def handle_events():
for e in pygame.event.get():
if e.type == pygame.QUIT:
return 1
# keys = pygame.key.get_pressed()
return 0
def draw():
screen.fill((0, 0, 0))
# screen.blit()
def game_logic():
pass
def run_game():
while 1:
game_logic()
if handle_events():
break
draw()
pygame.display.update(window_rect)
clock.tick(30)
pygame.quit()
if __name__ == "__main__":
pygame.init()
screen = pygame.display.set_mode(size)
window_rect = pygame.Rect((0, 0), size)
clock = pygame.time.Clock()
pygame.display.set_caption(title)
pygame.event.set_allowed([pygame.KEYDOWN, pygame.QUIT])
run_game()
2 Answers 2
I have put together bits and pieces of advice from some comments and an answer to end up with this:
import pygame
TITLE = "Game"
SIZE = (1000, 600)
def handle_events():
for event in pygame.event.get():
if event.type == pygame.QUIT:
return False
# keys = pygame.key.get_pressed()
return True
def draw():
screen.fill((0, 0, 0))
# screen.blit()
def game_logic():
pass
def run_game():
while handle_events():
game_logic()
draw()
pygame.display.update(window_rect)
clock.tick(30)
pygame.quit()
if __name__ == "__main__":
pygame.init()
screen = pygame.display.set_mode(SIZE)
window_rect = pygame.Rect((0, 0), SIZE)
clock = pygame.time.Clock()
pygame.display.set_caption(TITLE)
pygame.event.set_allowed([pygame.KEYDOWN, pygame.QUIT])
run_game()
- Changed
e
variable toevent
for clarity. - Changed
1
toTrue
for clarity. - Changed constants to uppercase.
- Put the
handle_events()
in the while loop condition, where it always should have been!
Feel free to comment below more improvements.
Name constants in uppercase. This is a common and useful naming convention.
Don't use one/zero if you mean true/false. Like the previous comment, this is focused on enhancing the code's readability and clarity of intent.
Put all code in functions. Code in functions can be tested, experimented with, and reorganized much more easily than code floating around at the top level. Floating code introduces the temptation to rely on global variables, which can create substantial headaches even at moderate program sizes. Putting everything in functions forces you to think clearly about how the behavior and data flow is organized. It's a time-tested practice worth embracing.
Organize the functions to help readability. People read English top to bottom. Arrange the functions in that order.
Caveat. I have no significant pygame
experience so I cannot offer advice on those details.
import pygame
import sys
TITLE = 'Game'
SIZE = (1000, 600)
def main(args):
pygame.init()
...
run_game(screen, window_rect, clock)
def run_game(screen, window_rect, clock):
while True:
game_logic()
if handle_events():
break
draw(screen)
...
...
def game_logic():
...
def handle_events():
for ...
if ...
return True
return False
def draw(screen):
...
if __name__ == '__main__':
main(sys.argv[1:])
True
instead of1
both inwhile 1
line and inreturn 1
statement.1
works fine in python, but I think it fits more with boolean. There is not much code to review \$\endgroup\$e
as name is bad habit :) I would use maybeev
for event orcurr_event
to indicate in that iteration, but that is very subjective, just use name that will make you sure in future what that variable is :D \$\endgroup\$e
means event \$\endgroup\$