Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
PyGame Window Creation | Building Arcade Game
Building Arcade Game
course content

Course Content

Building Arcade Game

bookPyGame Window Creation

Creating the Main Window

With main_display = pygame.display.set_mode((WIDTH, HEIGHT)), we summon the primary canvas of our adventure, setting it to the dimensions we've previously etched into our constants, WIDTH and HEIGHT. This magic incantation, courtesy of pygame.display.set_mode, breathes life into our game window, preparing a stage for the epic tales to unfold.

Setting the Stage with Background

By invoking bg = pygame.transform.scale(pygame.image.load('space.png'), (WIDTH, HEIGHT)), we delve into the art of scenery. This spell fetches an image from the depths of our files, space.png, transforming it to fit the grand scale of our window. The result is bg, a surface mirroring the dimensions of our game window, ready to serve as the backdrop for our saga.

Positioning the Horizon with bg_x1 and bg_x2

With bg_x1 = 0, we anchor our background to the game's left edge, setting the stage for our story's beginning. Meanwhile, bg_x2 = bg.get_width() casts a second instance of our background just off the right edge, preparing for a seamless scroll. This technique lays the groundwork for an eternal canvas, where one scene fluidly gives way to the next.

Note

There is also get_height() method on par with get_width().

Animating the Scene with bg_move

Through bg_move = 1, we dictate the rhythm of our background's journey across the screen, a single pixel per frame, breathing motion into our static universe. This subtle shift creates an illusion of movement, propelling our game into a realm of dynamism, perfect for space odysseys or high-speed chases.

As the narrative progresses, bg_x1, bg_x2, and bg_move become the conductors of our background's endless scroll, infusing our game with the essence of motion and bringing the illusion of life to our digital cosmos.

Task

  1. Define the main game window: use main_display = pygame.display.set_mode((WIDTH, HEIGHT)) to create the main window for the game with the predefined WIDTH and HEIGHT dimensions;
  2. Load and scale the background image: with bg = pygame.transform.scale(pygame.image.load('space.png'), (WIDTH, HEIGHT)), load the 'space.png' image, scaling it to fit the game window;
  3. Initialize background positions: set bg_x1 = 0 to start the background at the left edge of the window, and use bg_x2 = bg.get_width() to position a second background image right after the first, preparing for a continuous scrolling effect;
  4. Set background movement speed: assign bg_move = 1 to control how fast the background images move across the screen, creating the illusion of motion in the game.

Mark tasks as Completed
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Creating the Main Window

With main_display = pygame.display.set_mode((WIDTH, HEIGHT)), we summon the primary canvas of our adventure, setting it to the dimensions we've previously etched into our constants, WIDTH and HEIGHT. This magic incantation, courtesy of pygame.display.set_mode, breathes life into our game window, preparing a stage for the epic tales to unfold.

Setting the Stage with Background

By invoking bg = pygame.transform.scale(pygame.image.load('space.png'), (WIDTH, HEIGHT)), we delve into the art of scenery. This spell fetches an image from the depths of our files, space.png, transforming it to fit the grand scale of our window. The result is bg, a surface mirroring the dimensions of our game window, ready to serve as the backdrop for our saga.

Positioning the Horizon with bg_x1 and bg_x2

With bg_x1 = 0, we anchor our background to the game's left edge, setting the stage for our story's beginning. Meanwhile, bg_x2 = bg.get_width() casts a second instance of our background just off the right edge, preparing for a seamless scroll. This technique lays the groundwork for an eternal canvas, where one scene fluidly gives way to the next.

Note

There is also get_height() method on par with get_width().

Animating the Scene with bg_move

Through bg_move = 1, we dictate the rhythm of our background's journey across the screen, a single pixel per frame, breathing motion into our static universe. This subtle shift creates an illusion of movement, propelling our game into a realm of dynamism, perfect for space odysseys or high-speed chases.

As the narrative progresses, bg_x1, bg_x2, and bg_move become the conductors of our background's endless scroll, infusing our game with the essence of motion and bringing the illusion of life to our digital cosmos.

Task

  1. Define the main game window: use main_display = pygame.display.set_mode((WIDTH, HEIGHT)) to create the main window for the game with the predefined WIDTH and HEIGHT dimensions;
  2. Load and scale the background image: with bg = pygame.transform.scale(pygame.image.load('space.png'), (WIDTH, HEIGHT)), load the 'space.png' image, scaling it to fit the game window;
  3. Initialize background positions: set bg_x1 = 0 to start the background at the left edge of the window, and use bg_x2 = bg.get_width() to position a second background image right after the first, preparing for a continuous scrolling effect;
  4. Set background movement speed: assign bg_move = 1 to control how fast the background images move across the screen, creating the illusion of motion in the game.

Mark tasks as Completed
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 1. Chapter 3
AVAILABLE TO ULTIMATE ONLY
some-alt