The first step to draw a picture is to set up its structure, by sketching quick line strokes with no details - the skeleton. Similarly, a common practice to start a computer project is to write a skeleton code. When you design your project, you should first think of its structure and layout. Starting at the main function, ask yourself what classes and functions you will need for your task. You should leave implementation details behind and focus on the relationship of the structures. Call those empty functions as if they are already implemented.
To start a Pygame project, you may always start with the following skeleton. It includes some common constants, the game class, and the main function. In the main function, a new game is first created. After a game start screen is displayed, the program enters the main game loop. The game class processes the user input events in method process_event, run the game logic in method run, and handles drawing in method display. Note that methods start_screen() and end_screen() are empty with only pass in it. It is okay to leave them like that. Later on, you may want to fill in the code to display start screen and game over screen. Create a file called game.py and write the skeleton code. You will get an empty black screen when you run it. Now you are ready to start your platform game project.
# Define some constants
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
FPS = 60
# Define some colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
# Set up pygame, create screen and clock, get ready for a new game.
self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("My Platform Game")
self.clock = pygame.time.Clock()
self.running = True
# Start a new game
self.all_sprites = pygame.sprite.Group()
# Process input events
for event in pygame.event.get():
# If the close button is clicked, game ends and quit running.
if event.type == pygame.QUIT:
self.game_over = True
self.running = False
# Update sprites
# Draw background and all the sprites
# Flip the display after drawing is done
# Run the game logic
self.game_over = False
while self.game_over == False:
# Loop running at the specified speed
# Create a new game object
mygame = Game()
# Display the start screen when a game starts.
# Main game loop
# Start to run the game.
# Close the window and exit
# Call the main function and start up the game