2
\$\begingroup\$

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()
\$\endgroup\$
4
  • 1
    \$\begingroup\$ You could use True instead of 1 both in while 1 line and in return 1 statement. 1 works fine in python, but I think it fits more with boolean. There is not much code to review \$\endgroup\$ May 16 at 20:24
  • \$\begingroup\$ what about pep8?, naming variables? \$\endgroup\$
    – coder
    May 16 at 21:22
  • 1
    \$\begingroup\$ That is small loop, but if it would be bigger, using e as name is bad habit :) I would use maybe ev for event or curr_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\$ May 17 at 15:28
  • \$\begingroup\$ ok, i sometimes have to remind myself that e means event \$\endgroup\$
    – coder
    May 17 at 21:07

2 Answers 2

2
\$\begingroup\$

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 to event for clarity.
  • Changed 1 to True 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.

\$\endgroup\$
0
1
\$\begingroup\$

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:])
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.