Python Project Practice One (alien invasion mini-game) second article

Source: Internet
Author: User

Then the last time to continue to study.

Create a settings class

When new features are added to the game, some new settings are often introduced. Here's a module named settings that contains a class named settings that stores all settings in one place so that settings are not added everywhere in your code. In this way, we can pass a set object instead of many different settings. In addition, this makes function calls easier, and it's easier to modify the appearance of the game as the project grows: To modify a game, you only need to modify some of the values in the settings.py without looking for different settings scattered in the file
The following is the original settings class:

Class Settings ():    "Storage for all settings of alien invasion" '    def __init__ (self): '        Initialize game settings '        self.screen_width=        self.screen_height=800        self.bg_color = (230,230,230)

To create a settings instance and use it to access the settings, change the alien_invasion.py to the following:

Import sysfrom settings Import settingsfrom ship import shipimport pygamedef run_game ():    # Initialize the game and set up 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 ")    #开始游戏的主循环 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)                # Let the recently painted screen be visible        pygame.display.flip () run_ Game ()

Second, add the spaceship image.

To load a spaceship with a SHIP.BM picture:

To create a ship class:

Import Pygameclass ship ():    def __init__ (self, screen): "" "        initializes the spaceship and sets its initial position" ""        self.screen = Screen        # Load the spaceship image and get the outer connector 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 at the bottom of the screen at the center        Self.rect.centerx = Self.screen_rect.centerx        Self.rect.bottom = Self.screen_rect.bottom        def blitme (self): "" "        draws the spaceship at the specified location" ""        self.screen.blit ( Self.image, Self.rect)

Note A few important functions:

(1) To load the image, we call the Pygame.image.load () (see?). This function returns a surface that represents the ship, and we store the surface in Self.image.

(2) After loading the image, we use Get_rect () to get the properties of the corresponding surface rect

(3) When working with a Rect object, you can use the X and Y coordinates of the rectangle's Four Corners and center. You can specify the position of the rectangle by setting these values.

(4) in Pygame, the origin (0, 0) is in the upper-left corner of the screen, and the coordinate value will increase when moving down to the right. On the 1200x800 screen, the origin is in the upper-left corner, and the lower-right corner coordinates are (1200, 800).

Three on the screen to draw the ship

Update alien_invasion.py below:

Import sysfrom settings Import settingsfrom ship import shipimport pygamedef run_game ():    # Initialize the game and set up 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    = ships        #开始游戏的主循环 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 ()

Post-run effects such as:

Python Project Practice One (alien invasion mini-game) second article

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.