Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用Zelle';创建循环以替代硬编码窗口坐标;s';graphics.py';图书馆?_Python_Loops_Coordinates - Fatal编程技术网

Python 如何使用Zelle';创建循环以替代硬编码窗口坐标;s';graphics.py';图书馆?

Python 如何使用Zelle';创建循环以替代硬编码窗口坐标;s';graphics.py';图书馆?,python,loops,coordinates,Python,Loops,Coordinates,问题:我浪费了大量的时间,手工编写坐标,造成了错误、臃肿的代码。我需要帮助学习如何用这个冗长的代码替换将得到相同结果的循环结构。目前,x和y坐标分别存储在名为xCoords和yCoords的单独数组中。我如何简化这个程序并编写一个更优雅的程序版本 [注意:这是我的第一篇StackOverflow帖子。请告知我风格、礼仪或帖子错误,我会纠正它们。这个论坛对我来说是不可或缺的,我感谢大家的帮助。] 相关详细信息: # dotmatrix.py # Dot Matrix Game

问题:我浪费了大量的时间,手工编写坐标,造成了错误、臃肿的代码。我需要帮助学习如何用这个冗长的代码替换将得到相同结果的循环结构。目前,x和y坐标分别存储在名为xCoords和yCoords的单独数组中。我如何简化这个程序并编写一个更优雅的程序版本

[注意:这是我的第一篇StackOverflow帖子。请告知我风格、礼仪或帖子错误,我会纠正它们。这个论坛对我来说是不可或缺的,我感谢大家的帮助。]

相关详细信息:

    # dotmatrix.py
    # Dot Matrix Game
    # Michael Morelli
    # mmorelli at live dot com
    # Created: 03-24-13 with Python 2.7.3 and PyScripter

    from graphics import *

    win = GraphWin("Dot Matrix", 500, 500)
    win.setCoords(0.0, 0.0, 10.0, 10.0)
    win.setBackground("white")

    xCoords = [1,1,1,1,1,1,1,1,1,
               2,2,2,2,2,2,2,2,2,
               3,3,3,3,3,3,3,3,3,
               4,4,4,4,4,4,4,4,4,
               5,5,5,5,5,5,5,5,5,
               6,6,6,6,6,6,6,6,6,
               7,7,7,7,7,7,7,7,7,
               8,8,8,8,8,8,8,8,8,
               9,9,9,9,9,9,9,9,9]

    yCoords = [1,2,3,4,5,6,7,8,9,
               1,2,3,4,5,6,7,8,9,
               1,2,3,4,5,6,7,8,9,
               1,2,3,4,5,6,7,8,9,
               1,2,3,4,5,6,7,8,9,
               1,2,3,4,5,6,7,8,9,
               1,2,3,4,5,6,7,8,9,
               1,2,3,4,5,6,7,8,9,
               1,2,3,4,5,6,7,8,9]

    for i in range(81):
        Text(Point(xCoords[i], yCoords[i]), "*").draw(win)
        i+=1 # This for loop iterates through each of the coordinate arrays
             # and plots an * asterisk at each coordinate locus. There is a plot()
             # method included with the graphics library, but it plots single-pixel
             # points and are hardly visible. I do not think this will affect the game.

    input("Press <Enter> to quit.")
    win.close()
  • 我正在创建一个名为“点阵”的两人GUI游戏。用户通过单击鼠标与窗口交互。可以找到此游戏的在线版本。绘制坐标点很重要,因为它可以为玩家定向,并且是整个游戏板的一部分。这个代码片段不包含实际的游戏逻辑,因为我还没有编写它
  • 我使用的是John Zelle的Python编程书中的一个
源代码:

    # dotmatrix.py
    # Dot Matrix Game
    # Michael Morelli
    # mmorelli at live dot com
    # Created: 03-24-13 with Python 2.7.3 and PyScripter

    from graphics import *

    win = GraphWin("Dot Matrix", 500, 500)
    win.setCoords(0.0, 0.0, 10.0, 10.0)
    win.setBackground("white")

    xCoords = [1,1,1,1,1,1,1,1,1,
               2,2,2,2,2,2,2,2,2,
               3,3,3,3,3,3,3,3,3,
               4,4,4,4,4,4,4,4,4,
               5,5,5,5,5,5,5,5,5,
               6,6,6,6,6,6,6,6,6,
               7,7,7,7,7,7,7,7,7,
               8,8,8,8,8,8,8,8,8,
               9,9,9,9,9,9,9,9,9]

    yCoords = [1,2,3,4,5,6,7,8,9,
               1,2,3,4,5,6,7,8,9,
               1,2,3,4,5,6,7,8,9,
               1,2,3,4,5,6,7,8,9,
               1,2,3,4,5,6,7,8,9,
               1,2,3,4,5,6,7,8,9,
               1,2,3,4,5,6,7,8,9,
               1,2,3,4,5,6,7,8,9,
               1,2,3,4,5,6,7,8,9]

    for i in range(81):
        Text(Point(xCoords[i], yCoords[i]), "*").draw(win)
        i+=1 # This for loop iterates through each of the coordinate arrays
             # and plots an * asterisk at each coordinate locus. There is a plot()
             # method included with the graphics library, but it plots single-pixel
             # points and are hardly visible. I do not think this will affect the game.

    input("Press <Enter> to quit.")
    win.close()
#dotmatrix.py
#点阵游戏
#迈克尔·莫雷利
#在live.com上的mmorelli
#创建日期:03-24-13,使用Python 2.7.3和PyScripter
从图形导入*
win=图形宽度(“点阵”,500,500)
win.setCoords(0.0,0.0,10.0,10.0)
胜利。挫折(“白色”)
xCoords=[1,1,1,1,1,1,1,1,1,
2,2,2,2,2,2,2,2,2,
3,3,3,3,3,3,3,3,3,
4,4,4,4,4,4,4,4,4,
5,5,5,5,5,5,5,5,5,
6,6,6,6,6,6,6,6,6,
7,7,7,7,7,7,7,7,7,
8,8,8,8,8,8,8,8,8,
9,9,9,9,9,9,9,9,9]
yCoords=[1,2,3,4,5,6,7,8,9,
1,2,3,4,5,6,7,8,9,
1,2,3,4,5,6,7,8,9,
1,2,3,4,5,6,7,8,9,
1,2,3,4,5,6,7,8,9,
1,2,3,4,5,6,7,8,9,
1,2,3,4,5,6,7,8,9,
1,2,3,4,5,6,7,8,9,
1,2,3,4,5,6,7,8,9]
对于范围(81)内的i:
文本(点(xCoords[i],yCoords[i]),“*”)。绘制(win)
i+=1#此for循环在每个坐标数组中迭代
#并在每个坐标轨迹上绘制一个*星号。有一个情节
#方法包含在图形库中,但它只打印单个像素
#点和点几乎看不见。我认为这不会影响比赛。
输入(“按退出”)
赢
谢谢你的帮助!
-Michael

代码逻辑似乎相当于使用两个嵌套循环:

for xCoord in xrange(1, 10):
    for yCoord in xrange(1, 10):
        Text(Point(xCoord , yCoord ), "*").draw(win)

我特别喜欢你的评论,因为它包含了xrange迭代函数,而不是range。我查阅了为什么xrange更可取,这是一个伟大的教学时刻。使用range()创建整个列表,因此存在延迟和内存问题;但使用xrange()时,它一次生成一个项目,然后丢弃并继续。多谢!让我成为一个更好的程序员!