Sign InNuggetsCoursesCodePadCheatsheet

Game Skeleton

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.

import pygame # 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) class Game(object): def __init__(self): # Set up pygame, create screen and clock, get ready for a new game. pygame.init() 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 def start(self): # Start a new game self.all_sprites = pygame.sprite.Group() def process_events(self): # 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 def updates(self): # Update sprites self.all_sprites.update() def display(self): # Draw background and all the sprites self.screen.fill(BLACK) if self.game_over: self.end_screen() else: self.all_sprites.draw(self.screen) # Flip the display after drawing is done pygame.display.flip() def run(self): # Run the game logic self.game_over = False while self.game_over == False: self.process_events() self.updates() self.display() # Loop running at the specified speed self.clock.tick(FPS) def start_screen(self): pass def end_screen(self): pass def main(): # Create a new game object mygame = Game() # Display the start screen when a game starts. mygame.start_screen() # Main game loop while mygame.running: # Start to run the game. mygame.start() mygame.run() # Close the window and exit pygame.quit() # Call the main function and start up the game main()
© CS Wonders·Home·About·Contact·Classes·Gallery·Glossary·Fun Facts