gpt4 book ai didi

python - 在pygame上的内存益智游戏中,我可以保持原来的显示速度很慢,然后在玩游戏时让它变快吗?

转载 作者:太空宇宙 更新时间:2023-11-03 23:55:05 27 4
gpt4 key购买 nike

这是游戏代码

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,我们看到动画是在 revealBoxesAnimationcoverBoxesAnimation 函数中处理的。他们使用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com