Python Project Practice 1 (Alien Invasion Game) Part 2

Keywords: Python

Create a settings class
Every time a new feature is added to the game, new settings are usually introduced. Let's write a module called settings, which contains a class called Settings, to store all settings in one place, so as not to add settings everywhere in the code. In this way, we can pass a settings object instead of many different settings. Additionally, this makes function calls simpler and makes it easier to modify the appearance of the game as the project grows: to modify the game, you only need to modify some values in settings.py, rather than looking for different settings scattered in the file.
The following is the original Settings class:

If you are interested in python, I have a Python learning base, which has a lot of learning materials, interested in + Q group: 6882 44617

class Settings():
    '''Stores all the settings of Alien Invasion'''
 
    def __init__(self):
        '''Initialize game settings'''
        self.screen_width=1200
        self.screen_height=800
        self.bg_color = (230,230,230)

To create an instance of Settings and use it to access settings, modify alien_invasion.py to read as follows:

import sys
from settings import Settings
from ship import Ship
 
import pygame
 
def run_game():
    # Initialize the game and create a screen object
    pygame.init()
    # screen = pygame.display.set_mode((1200,800))
    ai_settings=Settings()
    screen = pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height))
    pygame.display.set_caption("Alien Invasion")
 
    #Start the main cycle of the game
    while True:
        # Monitor keyboard and mouse events
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
        screen.fill(ai_settings.bg_color)
         
        # Make the recently painted screen visible
        pygame.display.flip()
run_game()

2. Adding Image of Spacecraft

Load the spacecraft using ship.bm pictures as follows

Create Ship classes:

import pygame
 
class Ship():
    def __init__(self, screen):
        """Initialize the spacecraft and set its initial position"""
        self.screen = screen
        # Loading the spacecraft image and obtaining its outer rectangle
        self.image = pygame.image.load('images/ship.bmp')
        self.rect = self.image.get_rect()
        self.screen_rect = screen.get_rect()
        # Place each new spaceship in the center of the bottom of the screen
        self.rect.centerx = self.screen_rect.centerx
        self.rect.bottom = self.screen_rect.bottom
     
    def blitme(self):
        """Drawing a spacecraft at a specified position"""
        self.screen.blit(self.image, self.rect)

Notice several important functions:

(1) To load the image, we call pygame.image.load() (see). This function returns a surface representing the spaceship, and we store the surface in self.image.

(2) After loading the image, we use get_rect() to get the corresponding surface property rect

(3) When dealing with rect objects, the x and y coordinates of rectangular quadrangles and center can be used. These values can be set to specify the location of the rectangle.

(4) In Pygame, the origin (0, 0) is located in the upper left corner of the screen. When moving to the lower right, the coordinate value will increase. On a 1200 x 800 screen, the origin is in the upper left corner and the coordinates in the lower right corner are (1200, 800).

3. Draw the spacecraft on the screen

Next, update alien_invasion.py:

import sys
from settings import Settings
from ship import Ship
 
import pygame
 
def run_game():
    # Initialize the game and create a screen object
    pygame.init()
    # screen = pygame.display.set_mode((1200,800))
    ai_settings=Settings()
    screen = pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height))
    pygame.display.set_caption("Alien Invasion")
    # Create a spaceship
    ship = Ship(screen)
     
    #Start the main cycle of the game
    while True:
        # Monitor keyboard and mouse events
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
        screen.fill(ai_settings.bg_color)
        ship.blitme()
         
        # Make the recently painted screen visible
        pygame.display.flip()
run_game()

After running, the effect is as follows:
  

Posted by harley1387 on Sun, 11 Aug 2019 23:18:24 -0700