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()
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\$