SoFunction
Updated on 2024-10-30

python airplane wars pygame game background design details

This article example describes python airplane war pygame game background design. Shared for your reference, as follows:

goal

The idea of alternating background scrolling identifies

  • Show game background

01. Identification of ideas for alternate background scrolling

Run the lesson plan code and observe how the background image is displayed:

  • When the game starts, the background image will continuously move downwards.
  • Visually, it creates the illusion that the hero's plane is constantly flying upwards - a common trope in many parkour games.
    • The background of the game is constantly changing
    • The protagonist of the game The position remains unchanged

1.1 Analysis of realization ideas

在这里插入图片描述
method settle an issue

  1. Create two background image sprites
    • 1st picture Fully overlapped with the screen
    • The second one is at the top of the screen.
  2. The two images are moving downward together.
    • +=
  3. When any background sprite's >= screen height, it has moved to the bottom of the screen.
  4. Set this image, which was moved to the bottom of the screen, to the top of the screen.
    • = -

1.2 Designing contextual categories

在这里插入图片描述

  • initialization method
    • Specify background image directly
    • is_alt Determines if it is another image
      • False means the first image needs to overlap with the screen.
      • True means another image, directly above the screen.
  • The update() method
    • Determines whether to move off the screen, and if so, sets the image to the top of the screen to enable alternate scrolling.

Inheritance If the parent class provides a method that does not satisfy the needs of the child class:

  • Derive a subclass
  • Override parent class methods in subclasses for unique needs and extend them

02. Showing the background of the game

2.1 Basic implementation of background sprites

  • Create new Background in plane_sprites Inherited from GameSprite
class Background(GameSprite):
  """Game background sprites."""

  def update(self):

    # 1. call the method implementation of the parent class
    super().update()

    # 2. Determine whether to move off the screen, if so, set the image to the top of the screen
    if  >= SCREEN_RECT.height:
       = -

2.2 Displaying background sprites in plane_main.py

  1. Creating sprites and groups of sprites in the __create_sprites method
  2. In the __update_sprites method, have the sprite group call the update() and draw() methods

__create_sprites method

def __create_sprites(self):

  # Create background sprites and sprite groups
  bg1 = Background("./images/")
  bg2 = Background("./images/")
   = -
  
  self.back_group = (bg1, bg2)

__update_sprites method

def __update_sprites(self):

  self.back_group.update()
  self.back_group.draw()

2.3 Simplify background sprite creation with initialization methods

Reflections -- What kind of problems exist with the code completed in the previous tip? Can it be simplified?

  • In the main program, the two background sprites created are passed the same image file paths
  • When creating the second background sprite, set the image position of the background sprite in the main program.

Thoughts -- Should the main program be responsible for setting the initial position of the sprite? Or should the sprite itself be responsible?

Answer - by the elves themselves

  • According to the principles of object-oriented design, the responsibilities of an object should be encapsulated within the code of the class.
  • Minimize code calls on the calling side of the program

在这里插入图片描述

  • initialization method
    • Specify background image directly
    • is_alt Determines if it is another image
      • False means the first image needs to overlap with the screen.
      • True means another image, directly above the screen.

Initialize Background in plane_sprites.py

def __init__(self, is_alt=False):

  image_name = "./images/"
  super().__init__(image_name)
    
  # Determine if alternate images, if so, set the image to the top of the screen
  if is_alt:
     = -   

Modify the __create_sprites method of plane_main.

# Create background sprites and sprite groups
bg1 = Background()
bg2 = Background(True)

self.back_group = (bg1, bg2)

More about Python related content can be viewed on this site's topic: thePython Game Development Tips Summary》、《Python Data Structures and Algorithms Tutorial》、《Python Socket Programming Tips Summary》、《Summary of Python function usage tips》、《Summary of Python string manipulation techniques》、《Python introductory and advanced classic tutorialsand theSummary of Python file and directory manipulation techniques

I hope that what I have said in this article will help you in Python programming.