07: the predecessor of mushroom - the realization of mushroom small square #python game programming # red umbrella

Statement: original is not easy, and cannot be reproduced without permission

0. Preface

hello, guys! I'll still take you to write games with sister cat.
Today is the seventh episode of this game! In the last episode, we let the little boy's feet move flexibly. In this episode, we come to realize the code of mushroom small square, the predecessor of mushroom.

1. Define mushrooms

Let's first define a MushRoom class. The class is called MushRoom. In parentheses, we pass in pyGame sprite. Sprite, let MushRoom inherit this sprite class. Then, in the class, we implement our own constructor def__ init__ (self), use the super function to call the constructor of the wizard parent class.

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

In the constructor, we use pygame's Surface function to generate a small box with a width of 30 pixels and a height of 30 pixels. Give this small box a name, self image. Use self image. The fill function fills the small square with red.
Through self Get of image_ Rect function can get the rectangular area of the small box. In the brackets, you need to set the display position of the rectangular area, that is, where the small box will be displayed after the program runs. Here, let's display the small square in the center at the top of the screen, center=(WIDTH//2, 15), the coordinate of x is the width of the screen divided by 2, and the coordinate of y 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 square. We call the screen dot blit function, and the name of the picture to be displayed is passed in brackets Image, and where to display the picture.

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

2. Display of squares

We now display the small box in the game window. We generate a mushroom object outside the while loop, and then call the draw function of the mushroom object in the while loop to draw the small box! Let's run the program to see the effect!

#...
player = Player()
# Instantiate the MushRoom object
mushroom = MushRoom()

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

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

The square here represents the mushroom. It can only be displayed. The mushroom should fall slowly.
Therefore, in the MushRoom class, you also need to write an update function to make the small box fall down automatically.

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

Here, I use a new variable self Speed. Therefore, in the init function, you need to set the initial value of speed to 10.
In the while loop, we just need to call mushroom Update function, the small box 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 automatically fall down.

There is another problem here. After the small box falls outside 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 block. When the y coordinate of the small block is greater than HEIGHT, the small block will disappear automatically.

class MushRoom(pygame.sprite.Sprite):
    #...
    def update(self):
        #...
        if self.rect.top >= HEIGHT:  # if the small square fell to the bottom
            self.kill()  # Just kill yourself

Here we won't run the game, and we can't see the difference. This kill function just releases the resources it occupies!
Pay attention to sister cat. In the next episode, we use timer technology to produce a lot of falling mushrooms!

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 the small square fell to the bottom
            self.kill()  # Just kill yourself


player = Player()
# Instantiate the MushRoom object
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()
    # Draw small squares
    mushroom.draw(screen)
    # Update small box
    mushroom.update()
    pygame.display.update()
    clock.tick(FPS)

Tags: Python pygame

Posted by hotdog on Tue, 10 May 2022 14:15:11 +0300