文章目录
- 前文
- 运行环境
- 效果
- 原理
- 代码实现
- 垂直移动(背景往上移动)
- 垂直移动(背景往下移动)
- 水平移动(左)
- 水平移动(右)
- 结尾
前文
本文价绍用pygame实现背景的移动。游戏背景移动,如跑跑卡丁车、飞翔的小鸟等游戏,游戏背景移动带来的效果,相当于游戏人物一直在前进,而没有原地踏步。今天就带大家来实现一下。
运行环境
编译器:PyCharm 2021.2.1
解释器:Anaconda 3.8
安装模块
pip install pygame
效果
游戏背景移动效果
原理
背景移动的原理很简单,就是将将两张相同的图片紧紧的贴在一起,朝同一个方向移动,如第一张图片铺满整个屏幕,第二张图片在第一张图片的后面(屏幕中看不到),第一张图片往上移动,第二张图片就紧紧跟着移动。当第一张图片超过了屏幕的高度时,又重新回到屏幕的底部,如此反复就达到了游戏背景的移动。
代码实现
垂直移动(背景往上移动)
import pygame
import syspygame.init()class Background():def __init__(self):self.bgimage = pygame.image.load('background.png')self.rectBGimg = self.bgimage.get_rect()# 游戏画面左上角坐标self.bgY1 = 0self.bgX1 = 0# 游戏画面左下角坐标self.bgY2 = self.rectBGimg.heightself.bgX2 = 0# 画面移动速度self.moving_speed = 0.05# 更新坐标def update(self):self.bgY1 -= self.moving_speedself.bgY2 -= self.moving_speedif self.bgY1 <= -self.rectBGimg.height:self.bgY1 = self.rectBGimg.heightif 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()
首先我们先初始化函数,并用pygame.image.load方法读取图片,再用get_rect()获取矩形对象。再定义两组坐标,分别为游戏画面的左上角坐标,游戏画面的左下角坐标。最后我们再定义一个背景的移动速度self.moving_speed,如果你希望加快移动速度,可以将它的值调大一点。
def __init__(self):# 读取图片self.bgimage = pygame.image.load('background.png')self.rectBGimg = self.bgimage.get_rect()# 游戏画面左上角坐标self.bgY1 = 0self.bgX1 = 0# 游戏画面左下角坐标self.bgY2 = self.rectBGimg.heightself.bgX2 = 0# 画面移动速度self.moving_speed = 0.05
update()方法是用来更新坐标,self.bgY1和self.bgY2不断减去移动速度,改变了两个点坐标,背景图片将往上移动,后面的俩个if语句保证俩个变量的值不能超过屏幕本身的高度,如果超过了就重新赋值,让图片回到屏幕的底部。保证游戏背景的持续移动。
def update(self):self.bgY1 -= self.moving_speedself.bgY2 -= self.moving_speedif self.bgY1 <= -self.rectBGimg.height:self.bgY1 = self.rectBGimg.heightif 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))
垂直移动(背景往下移动)
游戏背景向下移动
import pygame
import syspygame.init()class Background():def __init__(self):self.bgimage = pygame.image.load('background.png')self.rectBGimg = self.bgimage.get_rect()# 第一组坐标self.bgY1 = -self.rectBGimg.heightself.bgX1 = 0# 第二组坐标self.bgY2 = 0self.bgX2 = 0# 画面移动速度self.moving_speed = 0.05# 更新def update(self):self.bgY1 += self.moving_speedself.bgY2 += self.moving_speed# 超过原来了就回到原来if self.bgY1 >= self.rectBGimg.height:self.bgY1 = -self.rectBGimg.heightif 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()
水平移动(左)
游戏背景向左移动
import pygame
import syspygame.init()class Background():def __init__(self):self.bgimage = pygame.image.load('background.png')self.rectBGimg = self.bgimage.get_rect()self.bgY1 = 0self.bgX1 = 0self.bgY2 = 0self.bgX2 = self.rectBGimg.width# 画面移动速度self.moving_speed = 0.05# 更新def update(self):self.bgX1 -= self.moving_speedself.bgX2 -= self.moving_speedif self.bgX1 <= -self.rectBGimg.width:self.bgX1 = self.rectBGimg.widthif 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()
水平移动(右)
游戏背景向右移动
import pygame
import syspygame.init()class Background():def __init__(self):self.bgimage = pygame.image.load('background.png')self.rectBGimg = self.bgimage.get_rect()self.bgY1 = 0self.bgX1 = -self.rectBGimg.widthself.bgY2 = 0self.bgX2 = 0# 画面移动速度self.moving_speed = 0.05# 更新def update(self):self.bgX1 += self.moving_speedself.bgX2 += self.moving_speedif self.bgX1 >= self.rectBGimg.width:self.bgX1 = -self.rectBGimg.widthif 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()
结尾
文章到此就结束了,主要介绍了pygame实现游戏背景的移动,最后再说几句,终有时,我们也会像那位登高望远的诗人一样,站在高处,俯视下方的一切,尽情享受那份自由与无邪。但不管我们站在哪个角度,重要的是我们要时刻保持朴素与谦卑!
——————2023.8.23 13.11