首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用二维数据结构和循环的蜗牛游戏

问使用二维数据结构和循环的蜗牛游戏
EN

Stack Overflow用户
提问于 2021-12-07 02:11:25
回答 1查看 32关注 0票数 0

我试图创建这个游戏,蜗牛"@“将在这个方框内按你想要的方向移动。当它移动到一个空间时,它会留下一条小径。"_“。我对它进行了编码,这样它才能向右移动,但我不知道要改变什么,这样它才能向下移动。

代码语言:javascript
复制
column = int(input("column"))

gameField = []

for i in range(row):
    gameField.append([])
    for j in range(column):
        gameField[i].append(" ")
 
def printField(listField):      
    for i in listField:
        for j in i:
            print(j, end= " ")
        print()
    
curRow = 0
curCol = 0

gameField[curRow][curCol] = "@"

game = True
while game:
    direction = input("wasdq")
    if direction == "q":
        game = False
    elif direction == "d":
        if curCol+1 == column:
            print("try again")
        else:
            gameField[curRow][curCol] = "_"
            curCol += 1
            gameField[curRow][curCol] = "@"
        printField(gameField)
        
    elif direction == "s":
        if curCol+1 == column:
            print("try again")
        else:
            gameField[curRow][curCol] = "_"
            curCol = curCol - 1
            gameField[curRow][curCol] = "@"
        printField(gameField)
EN

回答 1

Stack Overflow用户

发布于 2021-12-07 02:35:44

对于初学者,您的用户提示不太清楚。我会说这样的话

代码语言:javascript
复制
columns = int(input("Enter Columns: "))

对于wasdq来说也是一样的,因为我不知道该如何处理它。

但是不管怎么说,如果改变了栏目,你的蜗牛左右移动,你如何上下移动蜗牛?

代码语言:javascript
复制
matrix = [[0, 1, 2], [3, 4, 5], [6, 7, 8]]
# This is a matrix that looks like
# [0, 1, 2]
# [3, 4, 5]
# [6, 7, 8]

zero = matrix[0][0]
one = matrix[0][1]
three = matrix[1][0]
four = matrix[1][1]

如果蜗牛生活在矩阵上,向右移动就相当于从单元格0移动到单元格1(举个例子),向下移动相当于从单元格0移动到单元格3(举个例子)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70254142

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档