这是游戏代码
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
和coverBoxesAnimation
函数中处理的。他们使用 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)
就是这样。
关于python - 在pygame上的内存益智游戏中,我可以保持原来的显示速度较慢,然后在玩游戏时加快速度吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58115470/