头图

Disclaimer: It is not easy to be original, and may not be reproduced without permission

xa

0. Preface

hello, friends! I'm still the cat lady who took you guys to write games.
Today is the 7th episode of this game! In the last episode, we made the little boy's feet move flexibly. In this episode, we will realize the code of the mushroom's predecessor - the mushroom cube .

1. Defining Mushrooms

Let's define a mushroom class first. The class name is MushRoom . In the parentheses, we pass in pygame.sprite.Sprite and let MushRoom inherit this sprite class. Then in the class, implement our own constructor def __init__(self) , and use the super function to call the constructor of the elf parent class.

 class MushRoom(pygame.sprite.Sprite):
    def __init__(self):
        super(MushRoom, self).__init__()

In the constructor, we use the pygame Surface function of ---7ecfd2fc544b3356692a5305e60d8fd8 self.image to generate a small square with a width of 30 pixels and a height of 30 pixels . self.image . Use the self.image.fill function to fill the small square with red.
Through the self.image的get_rect function, you can get the rectangular area of the small square. Inside the brackets, you need to set the display position of the rectangular area, that is, where the small square is displayed after the program runs. Here, we make the small square displayed at the top center of the screen, center=(WIDTH//2, 15) , the x coordinate is the width of the screen divisible by 2, and the y coordinate is 15.

 class MushRoom(pygame.sprite.Sprite):
    def __init__(self):
        super(MushRoom, self).__init__()
        self.image = pygame.Surface((30, 30))
        self.image.fill("red")
        self.rect = self.image.get_rect(center=(WIDTH//2, 15))

Next, we define a draw function to display the small squares. We call the screen点blit function, and the name of the picture to be displayed is passed in the parentheses self.image and where the picture is to be displayed .

 class MushRoom(pygame.sprite.Sprite):
    #...
    def draw(self, screen):
        screen.blit(self.image, self.rect)

2. Display of squares

We now display the small square in the game window, we are outside the while loop, generate a mushroom object, and then inside the while loop, call mushroom Object's draw function, draws small squares! Let's run the program to see the effect!

 #...
player = Player()
# 实例化MushRoom对象
mushroom = MushRoom()

while True:
    #...
    mushroom.draw(screen)

Well, we see that the little square has been displayed!

The squares here are the representative mushrooms, which are only displayed, but not enough, the mushrooms should slowly fall.
Therefore, in the MushRoom class, you also need to write a update function to make the small square fall down automatically.

 class MushRoom(pygame.sprite.Sprite):
    #...
    def update(self):
        self.rect.move_ip(0, self.speed)

Here, I used a new variable self.speed , so, in the init function, the initial value of speed needs to be set to 10 .
In the while loop, we only need to call mushroom.update function, and the small square will fall down.

 class MushRoom(pygame.sprite.Sprite):
    def __init__(self):
        #...
        self.speed = 10
while True:
    #...
    mushroom.update()

Let's run the game and see the effect. As you can see, the small square will fall down automatically.

There is another problem here. After the small square falls out of the screen , it should be destroyed, so we need to add two lines of code in the update function to deal with this situation!
The boundary processing of the small square, when the y coordinate of the small square is greater than HEIGHT, the small square will automatically disappear.

 class MushRoom(pygame.sprite.Sprite):
    #...
    def update(self):
        #...
        if self.rect.top >= HEIGHT:  # if小方块掉到最底部了
            self.kill()  # 就把自己干掉

Here we don't run the game, and we can't see any difference in running it. This kill function just releases the resources occupied by itself !
Pay attention to Sister Cat. In the next episode, we will use the timer technology to generate a lot of falling mushrooms!

The code involved in this article :

 class MushRoom(pygame.sprite.Sprite):
    def __init__(self):
        super(MushRoom, self).__init__()
        self.image = pygame.Surface((30, 30))
        self.image.fill("red")
        self.rect = self.image.get_rect(center=(WIDTH//2, 15))
        self.speed = 10

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

    def update(self):
        self.rect.move_ip(0, self.speed)
        if self.rect.top >= HEIGHT:  # if小方块掉到最底部了
            self.kill()  # 就把自己干掉


player = Player()
# 实例化MushRoom对象
mushroom = MushRoom()

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()
    # screen.fill("black")
    screen.blit(bg_image, (0, 0))
    player.update()
    player.move()
    # 绘制小方块
    mushroom.draw(screen)
    # 更新小方块
    mushroom.update()
    pygame.display.update()
    clock.tick(FPS)

猫姐_游戏编程
56 声望17 粉丝

😸猫姐,211硕士,定居成都的荆州人,5年IT生涯,教大家做益智小游戏,快乐学编程