The movement of the pygame game background

Preamble

This article introduces the use of pygame to realize the movement of the background. The game background moves, such as running karts, flying birds and other games, the effect brought by the game background movement is equivalent to that the game characters are always moving forward instead of standing still. Today I will take you to realize it.

operating environment

Compiler: PyCharm 2021.2.1
Interpreter: Anaconda 3.8
install module
pip install pygame

Effect

Game background moving effect

principle

The principle of background movement is very simple, that is, stick two identical pictures tightly together and move them in the same direction . For example, the first picture covers the entire screen, and the second picture is behind the first picture ( cannot be seen on the screen), the first picture moves up, and the second picture follows closely. When the first picture exceeds the height of the screen, it returns to the bottom of the screen, and so on repeatedly to achieve the movement of the game background.

Code

Move vertically (the background moves up)

import pygame
import sys

pygame.init()


class Background():
    def __init__(self):
        self.bgimage = pygame.image.load('background.png')
        self.rectBGimg = self.bgimage.get_rect()

        # 游戏画面左上角坐标
        self.bgY1 = 0
        self.bgX1 = 0

        # 游戏画面左下角坐标
        self.bgY2 = self.rectBGimg.height
        self.bgX2 = 0

        # 画面移动速度
        self.moving_speed = 0.05
    # 更新坐标
    def update(self):
        self.bgY1 -= self.moving_speed
        self.bgY2 -= self.moving_speed
        if self.bgY1 <= -self.rectBGimg.height:
            self.bgY1 = self.rectBGimg.height
        if self.bgY2 <= -self.rectBGimg.height:
            self.bgY2 = self.rectBGimg.height

    # 绘制背景图片
    def render(self):
        DISPLAYSURF.blit(self.bgimage, (self.bgX1, self.bgY1))
        DISPLAYSURF.blit(self.bgimage, (self.bgX2, self.bgY2))


background = Background()
DISPLAYSURF = pygame.display.set_mode((512, 768))
while True:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    background.render()
    background.update()
    pygame.display.update()

First, we initialize the function first, and use the pygame.image.load method to read the image, and then use get_rect() to get the rectangle object. Then define two sets of coordinates, which are the coordinates of the upper left corner of the game screen and the coordinates of the lower left corner of the game screen. Finally, we define a background moving speed self.moving_speed. If you want to speed up the moving speed, you can increase its value a little.

def __init__(self):
		# 读取图片
        self.bgimage = pygame.image.load('background.png')
        self.rectBGimg = self.bgimage.get_rect()

        # 游戏画面左上角坐标
        self.bgY1 = 0
        self.bgX1 = 0

        # 游戏画面左下角坐标
        self.bgY2 = self.rectBGimg.height
        self.bgX2 = 0

        # 画面移动速度
        self.moving_speed = 0.05

The update() method is used to update the coordinates. Self.bgY1 and self.bgY2 continuously subtract the moving speed. If the coordinates of two points are changed, the background image will move upwards. The following two if statements ensure that the values ​​of the two variables cannot It exceeds the height of the screen itself. If it exceeds, it will be reassigned to make the picture return to the bottom of the screen. Guaranteed continuous movement of the game background.

 def update(self):
        self.bgY1 -= self.moving_speed
        self.bgY2 -= self.moving_speed
        if self.bgY1 <= -self.rectBGimg.height:
            self.bgY1 = self.rectBGimg.height
        if self.bgY2 <= -self.rectBGimg.height:
            self.bgY2 = self.rectBGimg.height

Finally draw the two pictures to the game screen.

 def render(self):
        DISPLAYSURF.blit(self.bgimage, (self.bgX1, self.bgY1))
        DISPLAYSURF.blit(self.bgimage, (self.bgX2, self.bgY2))

Move vertically (the background moves down)

Game background moves down

import pygame
import sys

pygame.init()


class Background():
    def __init__(self):
        self.bgimage = pygame.image.load('background.png')
        self.rectBGimg = self.bgimage.get_rect()
		# 第一组坐标
        self.bgY1 = -self.rectBGimg.height
        self.bgX1 = 0
		# 第二组坐标
        self.bgY2 = 0
        self.bgX2 = 0

        # 画面移动速度
        self.moving_speed = 0.05

    # 更新
    def update(self):
        self.bgY1 += self.moving_speed
        self.bgY2 += self.moving_speed
        # 超过原来了就回到原来
        if self.bgY1 >= self.rectBGimg.height:
            self.bgY1 = -self.rectBGimg.height
        if self.bgY2 >= self.rectBGimg.height:
            self.bgY2 = -self.rectBGimg.height

    # 绘制背景图片
    def render(self):
        DISPLAYSURF.blit(self.bgimage, (self.bgX1, self.bgY1))
        DISPLAYSURF.blit(self.bgimage, (self.bgX2, self.bgY2))


background = Background()
DISPLAYSURF = pygame.display.set_mode((512, 768))
while True:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    background.render()
    background.update()
    pygame.display.update()

horizontal movement (left)

The game background moves to the left

import pygame
import sys

pygame.init()


class Background():
    def __init__(self):
        self.bgimage = pygame.image.load('background.png')
        self.rectBGimg = self.bgimage.get_rect()

        self.bgY1 = 0
        self.bgX1 = 0

        self.bgY2 = 0
        self.bgX2 = self.rectBGimg.width

        # 画面移动速度
        self.moving_speed = 0.05

    # 更新
    def update(self):
        self.bgX1 -= self.moving_speed
        self.bgX2 -= self.moving_speed
        if self.bgX1 <= -self.rectBGimg.width:
            self.bgX1 = self.rectBGimg.width
        if self.bgX2 <= -self.rectBGimg.width:
            self.bgX2 = self.rectBGimg.width

    # 绘制背景图片
    def render(self):
        DISPLAYSURF.blit(self.bgimage, (self.bgX1, self.bgY1))
        DISPLAYSURF.blit(self.bgimage, (self.bgX2, self.bgY2))


background = Background()
DISPLAYSURF = pygame.display.set_mode((512, 768))
while True:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    background.render()
    background.update()
    pygame.display.update()

horizontal movement (right)

Game background moves to the right

import pygame
import sys

pygame.init()


class Background():
    def __init__(self):
        self.bgimage = pygame.image.load('background.png')
        self.rectBGimg = self.bgimage.get_rect()

        self.bgY1 = 0
        self.bgX1 = -self.rectBGimg.width

        self.bgY2 = 0
        self.bgX2 = 0

        # 画面移动速度
        self.moving_speed = 0.05

    # 更新
    def update(self):
        self.bgX1 += self.moving_speed
        self.bgX2 += self.moving_speed
        if self.bgX1 >= self.rectBGimg.width:
            self.bgX1 = -self.rectBGimg.width
        if self.bgX2 >= self.rectBGimg.width:
            self.bgX2 = -self.rectBGimg.width

    # 绘制背景图片
    def render(self):
        DISPLAYSURF.blit(self.bgimage, (self.bgX1, self.bgY1))
        DISPLAYSURF.blit(self.bgimage, (self.bgX2, self.bgY2))


background = Background()
DISPLAYSURF = pygame.display.set_mode((512, 768))
while True:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    background.render()
    background.update()
    pygame.display.update()

end

This is the end of the article. It mainly introduces pygame to realize the movement of the game background. Finally, I will say a few words. Sometimes, we will stand on a high place, look down on everything below, and enjoy ourselves That freedom and innocence. But no matter which angle we stand on, the important thing is that we must always remain simple and humble!
————— 2023.8.23 13.11

Guess you like

Origin blog.csdn.net/qq_65898266/article/details/132449157