Python 带有列表的贪吃蛇游戏单元格位置
Python Snake Game Cell Positions with Lists
我正在尝试创建一个可能有 100 个单元格(蛇的部分或长度)的蛇游戏。在主 while 循环的每次迭代中,每个单元格位置都采用它前面的单元格位置。当我尝试这样做时 returns:
cellPosX[100 - x] = cellPosX[100 - (x + 1)]
IndexError: list index out of range
这是我的代码,我认为没有必要。
cellPosX = [100]
cellPosY = [100]
cellPosX[0] = 400
cellPosY[0] = 300
#Change Cell Positions
for x in range(0, 100):
cellPosX[100 - x] = cellPosX[100 - (x + 1)]
cellPosY[100 - x] = cellPosY[100 - (x + 1)]
我只是python编程的初学者,还没有习惯。任何帮助将不胜感激。
您只是在初始化其中包含一项的列表:
最好初始化全部 100 个位置:
cellposX = [100] # is a list with only one '100' inside
cellposX = [100]*100 # is a list with 100 '100' inside
无论如何,在我看来,您尝试实现此目的的方式并不合适。
查看 Snake Pygame Example 上的代码进行学习。
我正在尝试创建一个可能有 100 个单元格(蛇的部分或长度)的蛇游戏。在主 while 循环的每次迭代中,每个单元格位置都采用它前面的单元格位置。当我尝试这样做时 returns:
cellPosX[100 - x] = cellPosX[100 - (x + 1)]
IndexError: list index out of range
这是我的代码,我认为没有必要。
cellPosX = [100]
cellPosY = [100]
cellPosX[0] = 400
cellPosY[0] = 300
#Change Cell Positions
for x in range(0, 100):
cellPosX[100 - x] = cellPosX[100 - (x + 1)]
cellPosY[100 - x] = cellPosY[100 - (x + 1)]
我只是python编程的初学者,还没有习惯。任何帮助将不胜感激。
您只是在初始化其中包含一项的列表: 最好初始化全部 100 个位置:
cellposX = [100] # is a list with only one '100' inside
cellposX = [100]*100 # is a list with 100 '100' inside
无论如何,在我看来,您尝试实现此目的的方式并不合适。 查看 Snake Pygame Example 上的代码进行学习。