StarCraft: The Little Overlord and the Little Bee (2)--The use of classes

Table of contents

foreword

1. Write the setting content in a class

 2. Set the shape of the little bee

 3. Set the parameters of the cat and bee

 4. Draw the cat and bee to the window

 Summarize


foreword

We set up the window yesterday, now we need to add elements to the window.

1. Write the setting content in a class

 I personally understand that the meaning of the book is to create a class and put all the properties that need to be set here, so that when you need to upgrade and change later, you can modify the parameters here. For example, the window background color we set yesterday can be changed to This parameter setting is placed in the setting class and called when needed. This is the object-oriented programming thinking. So we now create a settings file and create some parameters in it. code show as below:

class Settings():
    
    def __init__(self):
        
        self.screen_width = 800
        self.screen_height = 600
        self.bg_color = (220,220,220)

 It can be seen that we created the length and width of the window, and then set the color. I modified the color, and I feel that the cyan color yesterday is not good-looking. Now we need to modify the code in the alien_invasion file, import this class, and call these parameters, let's look at the code.

import sys
import pygame
import settings

def run_game():
    pygame.init()
    new_setting=settings.Settings()
    screen = pygame.display.set_mode((new_setting.screen_width,new_setting.screen_height))
    pygame.display.set_caption("Alien Invasion")

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

        screen.fill(new_setting.bg_color)
        pygame.display.flip()

run_game()

 It can be seen that we first use import to import the class we created, then instantiate the setting class, the instance is new_setting, and finally call the parameters set in advance through new_setting. Looking at this piece of code alone, it seems to be a little more complicated than before, and it is also a little difficult to understand, but when your program reaches a certain level of complexity, it will be easier to maintain by classifying and writing in this way. Let's take a look at the effect.

 

 The operation is successful, and the color has become gray (I think it is gray)

 2. Set the shape of the little bee

 According to the book, pygame uses bmp bitmap by default, and other file types. Is it so troublesome to install the library? Look for instructions online.

pygame.image.load can load various types of image files, including but not limited to:

  • BMP
  • GIF
  • JPG/JPEG
  • PNG
  • TGA
  • TIFF
  • WebP

It should be noted that if the loaded image file is not in a format supported by pygame, an exception will be thrown.

 According to what is said on the Internet, there are many formats supported by pygame, so we just try to use the JPG format (I personally like the jpg format)

 Now we need to pay attention to two points: one is a jpg image with a transparent background, and the other is free. I searched for a long time but couldn't find it. In order not to waste time, I found a cat. Let's make do with it. Let's appreciate it.

 

 3. Set the parameters of the cat and bee

 According to the previous programming thinking, we need to create a separate class to define various attributes of the cat bee, including size, position, action, etc. Just like in the book, we create a ship file. I don’t know why it is named ship. Doesn’t ship mean ship?

 First, let's show the code inside the class:

import pygame

class Ship():
    def __init__(self,screen):
        self.screen = screen
        self.image = pygame.image.load('cat.webp')
        self.rect = self.image.get_rect()
        self.screen_rect = screen.get_rect()
        self.rect.centerx = self.screen_rect.centerx
        self.rect.bottom=self.screen_rect.bottom

    def blitme(self):
        self.screen.blit(self.image,self.rect)

 This piece of code is not very easy to understand, and the book is not very clear. I will first talk about my initial understanding, and if I find something wrong later, I will explain it.

First, the screen passed in in the initialization method is our window, and then we import the downloaded picture "cat.webp" (let's see if this format can be used directly).

self.rect = self.image.get_rect() This code is to get the border of the picture. Similarly, the following code is to get the border of the window, and then use the centerx and bottom attributes to align the picture at the bottom of the screen.

Finally, we define the blitme method, which calls the blit function of pygame. The function of the blit function is to draw the picture on the screen, and the position of self.rect has been set.

 4. Draw the cat and bee to the window

 Next, we only need to call the blitme method of the Ship class in the main program to draw the cat and bee to the specified position.

import sys
import pygame
import settings
from ship import Ship

def run_game():
    pygame.init()
    new_setting=settings.Settings()
    screen = pygame.display.set_mode((new_setting.screen_width,new_setting.screen_height))
    ship = Ship(screen)
    pygame.display.set_caption("Alien Invasion")

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

        screen.fill(new_setting.bg_color)
        ship.blitme()
        pygame.display.flip()

run_game()

 This code is not easy to explain, it just calls the function. Let's see the effect directly.

 

 It is successful, and it is also possible to prove that the format of the picture is not bmp, but our cat and bee is a bit too big, we need to make it smaller, preferably like a small fly in the window, so I won’t go into details about how to change it here. Let's see the effect directly.

 

 Hmm, that works a lot better.

 Summarize

 Today I mainly learn how to set the position of drawing images and how to draw them.

 

Guess you like

Origin blog.csdn.net/m0_49914128/article/details/132348355