python - 在pygame上的记忆解谜游戏中,我可以在玩游戏时保持原来的显示速度慢,然后再加快吗?
问题描述
这是游戏代码
https://inventwithpython.com/pygame/chapter3.html
我目前将显示速度设置为 1,但这是用于初始显示和玩游戏时的显示。无论如何,我是否能够将初始显示速度保持在 1,然后在播放时将显示速度更改为更快的速度?
为了做到这一点,我觉得我需要在第12行的revealspeed下添加一行代码,我只是不知道新行应该说什么。
FPS = 30 # frames per second, the general speed of the program
WINDOWWIDTH = 640 # size of window's width in pixels
WINDOWHEIGHT = 480 # size of windows' height in pixels
REVEALSPEED = 2 # speed boxes' sliding reveals and covers
BOXSIZE = 60 # size of box height & width in pixels
GAPSIZE = 10 # size of gap between boxes in pixels
BOARDWIDTH = 8 # number of columns of icons
BOARDHEIGHT = 7 # number of rows of icons
assert (BOARDWIDTH * BOARDHEIGHT) % 2 == 0, 'Board needs to have an even number of boxes for pairs of matches.'
XMARGIN = int((WINDOWWIDTH - (BOARDWIDTH * (BOXSIZE + GAPSIZE))) / 2)
YMARGIN = int((WINDOWHEIGHT - (BOARDHEIGHT * (BOXSIZE + GAPSIZE))) / 2)
解决方案
所以我们想用两种不同的速度来做动画,所以让我们首先为它创建一个新的全局变量:
...
WINDOWHEIGHT = 480 # size of windows' height in pixels
# ADD THIS
INITIALREVEALSPEED = 1 # speed boxes' sliding reveals and covers AT THE START OF THE GAME
REVEALSPEED = 8 # speed boxes' sliding reveals and covers
BOXSIZE = 40 # size of box height & width in pixels
...
通过搜索REVEALSPEED
我们看到动画是在revealBoxesAnimation
andcoverBoxesAnimation
函数中处理的。他们使用REVEALSPEED
常量(不是真正的常量,但是嘿),但我们希望速度是动态的,所以让我们将我们想要使用的速度作为参数传递。将函数更改为:
def revealBoxesAnimation(board, boxesToReveal, speed=REVEALSPEED):
# Do the "box reveal" animation.
for coverage in range(BOXSIZE, (-speed) - 1, -speed):
drawBoxCovers(board, boxesToReveal, coverage)
def coverBoxesAnimation(board, boxesToCover, speed=REVEALSPEED):
# Do the "box cover" animation.
for coverage in range(0, BOXSIZE + speed, speed):
drawBoxCovers(board, boxesToCover, coverage)
我们仍然使用REVEALSPEED
作为默认值,因此我们不必更改每个方法调用。
由于我们只想在游戏开始时减慢动画速度,因此我们只需更改开始时发生的方法调用。如果我们搜索使用的地方revealBoxesAnimation
,我们会找到该startGameAnimation
功能。让我们将其更改为:
def startGameAnimation(board):
# Randomly reveal the boxes 8 at a time.
coveredBoxes = generateRevealedBoxesData(False)
boxes = []
for x in range(BOARDWIDTH):
for y in range(BOARDHEIGHT):
boxes.append( (x, y) )
random.shuffle(boxes)
boxGroups = splitIntoGroupsOf(8, boxes)
drawBoard(board, coveredBoxes)
for boxGroup in boxGroups:
revealBoxesAnimation(board, boxGroup, INITIALREVEALSPEED)
coverBoxesAnimation(board, boxGroup, INITIALREVEALSPEED)
就是这样。
推荐阅读
- javascript - 如何产生清晰的图像
- javascript - 自动提交存在于 javascript 中的表单
- windows - 我想使用 python 将 Windows 机器连接到 L2TP/IPsec vpn
- java - Android Studio 中的 Fragment 不稳定
- android - android studio 没有检测到我的手机,尽管 adb 检测到了
- python - 为 spacy 安装后端依赖项时出错
- laravel - 在laravel中单击html页面的链接打开pdf视图
- matlab - 将新值连接到单元格的特定单元格
- registry - 无法通过 Inno Setup Installer 更改注册表项
- php - 使用 php 访问和打印表单选项 name=""