头图

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

xa

0. Preface

hello, friends! I'm the cat lady who took you guys to write the game.
Today we are going to make a little game for children to eat mushrooms!
In the last episode, we realized the free movement of the small squares, but the freedom is a bit too much. The small squares will run out of the window. Today we will solve this bug. By the way, we will also change the background of the game window to a picture. The effect is this.

窗口边界处理

1. Boundary processing of squares

If we don't want the block to go out of the window, we just need to control the range of the block's movement ! Add a few more if judgment and you're done! We can do this, in Player of the class move函数 , when the block moves to the far left , the x coordinate will be less than 0 , and we make the block x coordinate equal to 0; When the x coordinate of the left vertex of the block self.rect.left is less than 0, let the block's self.rect.left=0 .
Similarly, when the square runs to the far right, the x coordinate is greater than the width of the window. We set the window size to 600 above, so the maximum value of x cannot exceed 600 . That is, when the x-coordinate of the right vertex of the block self.rect.right>WIDTH , let the block's self.rect.right = WIDTH .
Let's run the program and see the effect.

When the block is moved to the far left, the block will not move any more; when the block is moved to the far right, the block will not move any more.
In the same way, the block moves up and down, and the same is true.

The code for boundary processing is as follows:

 def move(self):
    key_pressed = pygame.key.get_pressed()
    if key_pressed[K_UP]:
        self.rect.move_ip(0, -10)
    if key_pressed[K_DOWN]:
        self.rect.move_ip(0, 10)
    if key_pressed[K_LEFT]:
        self.rect.move_ip(-10, 0)
    if key_pressed[K_RIGHT]:
        self.rect.move_ip(10, 0)
    # 判断边界
    if self.rect.left < 0:
        self.rect.left = 0
    if self.rect.right > WIDTH:
        self.rect.right = WIDTH
    if self.rect.top < 0:
        self.rect.top = 0
    if self.rect.bottom > HEIGHT:
        self.rect.bottom = HEIGHT

Finally, run the program and see the effect. When we press the up, down, left and right keys of the keyboard, the small square will no longer go out of the border, which achieves the effect we want!

The game has been played for so long, and it still has a dark background . Isn't it a sense of achievement? We can use the 2行 code to change the background to the desired image .
In the image folder, look at the picture background, 宽为600 pixels, 高为800 pixels. Back in the part1-game file,
The image is loaded by the pygame.image.load function ("./image/bg.png"), the path of the image is passed in the parentheses, and then assigned to bg_image.

 bg_image = pygaem.image.load("./image/bg.png")

In the while loop, the picture needs to be drawn. Comment out the screen.fill code and replace it with screen.blit function (bg_image, (0, 0)), the bg_image passed in the parentheses, and the position of the image display.

 screen.blit(bg_image, (0, 0))

Run it to see the effect, how is it, do you feel that the game is almost finished in an instant? In the next episode, we will add background music to the game and it will feel even more!

窗口边界处理


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

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