移动椭圆的板绘图代码
我正在为大学开发一款 Python 跳棋游戏。我使用 tk 绘制了棋盘,但我似乎无法实现棋子的移动功能。如果有人发现我的代码中有任何错误,或者可以提供帮助,我将不胜感激。这是完整的源代码。提前致谢。
我知道这会绘制棋子。我不知道如何在不删除其他部分的情况下重新绘制这些部分。我在网上查看了 move 函数,并尝试了有效的简单测试,但我无法在我的代码中使用它。
我确实知道递归,但是,在实现更多功能之前,我需要基本功能才能工作,即在屏幕上实际移动一块。
lst2 = []
#counter variable
i=0
#board variable is what stores the X/O/- values.
# It's a 2D list. We iterate over it, looking to see
# if there is a value that is X or O. If so, we draw
# text to the screen in the appropriate spot (based on
# i and j.
while i < len(board):
j=0
while j < len(board[i]):
if board[i][j] == 2:
lst2.append(canvas.create_oval((i+1)*width + width/2 + 15,
(j+1)*height + height/2 +15,(i+1)*width + width/2 - 15,
(j+1)*width + width/2 - 15, fill="Red",outline='Black'))
elif board[i][j] == 4:
lst2.append(canvas.create_oval((i+1)*width + width/2 + 15,
(j+1)*height + height/2 +15,(i+1)*width + width/2 - 15,
(j+1)*width + width/2 - 15, fill="Red",outline='Black'))
elif board[i][j] == 1:
lst2.append(canvas.create_oval((i+1)*width + width/2 + 15,
(j+1)*height + height/2 +15,(i+1)*width + width/2 - 15,
(j+1)*width + width/2 - 15, fill="Black",outline='Black'))
elif board[i][j] == 3:
lst2.append(canvas.create_oval((i+1)*width + width/2 + 15,
(j+1)*height + height/2 +15,(i+1)*width + width/2 - 15,
(j+1)*width + width/2 - 15, fill="Black",outline='Black'))
j+=1
i+=1
I am working on a python checkers game for college. I have the board drawn, using tk, but I can't seem to implement a movement function for the pieces. If anyone see any errors in my code, or can offer help, I would appreciate. Here is the complete source. Thanks in advance.
I know that this draws the checker pieces. I don't know how to redraw the pieces, without deleting the other pieces. I have looked online at the move function, and tried simple test that worked, yet I have not been able to use it in my code.
I do know of recursion, however, I need the basic function to work, i.e. actually moving a piece on the screen, before I implement any more functions.
lst2 = []
#counter variable
i=0
#board variable is what stores the X/O/- values.
# It's a 2D list. We iterate over it, looking to see
# if there is a value that is X or O. If so, we draw
# text to the screen in the appropriate spot (based on
# i and j.
while i < len(board):
j=0
while j < len(board[i]):
if board[i][j] == 2:
lst2.append(canvas.create_oval((i+1)*width + width/2 + 15,
(j+1)*height + height/2 +15,(i+1)*width + width/2 - 15,
(j+1)*width + width/2 - 15, fill="Red",outline='Black'))
elif board[i][j] == 4:
lst2.append(canvas.create_oval((i+1)*width + width/2 + 15,
(j+1)*height + height/2 +15,(i+1)*width + width/2 - 15,
(j+1)*width + width/2 - 15, fill="Red",outline='Black'))
elif board[i][j] == 1:
lst2.append(canvas.create_oval((i+1)*width + width/2 + 15,
(j+1)*height + height/2 +15,(i+1)*width + width/2 - 15,
(j+1)*width + width/2 - 15, fill="Black",outline='Black'))
elif board[i][j] == 3:
lst2.append(canvas.create_oval((i+1)*width + width/2 + 15,
(j+1)*height + height/2 +15,(i+1)*width + width/2 - 15,
(j+1)*width + width/2 - 15, fill="Black",outline='Black'))
j+=1
i+=1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 坐标 和/在画布上移动项目或 move 方法来更改坐标它们是什么就是你想要它们是什么。
下面是一个简单的示例,展示了如何在画布上创建和移动项目:
You can move an item on a canvas using the coords and/or move methods to change the coordinates from what they are to what you want them to be.
Here's a simple example showing how to create and move an item on a canvas:
第六次编辑:这里有两个解决方案供您使用:(
第五次编辑:好的,谢谢您剥离代码。
准确解释一下你的画板代码有什么问题? “移动的片段未从旧位置删除”? “所有部件都绘制在错误的坐标或颜色上”? ...?
仅仅不断地转储代码并说“此代码不起作用”是不可接受的。
让我建议您如何清理现有的绘图代码,在这个过程中您几乎肯定会发现错误。
显然,每次有移动(或升级)时,您都需要清除并重绘屏幕,您真的这样做了吗?为此声明一个 fn
redrawBoard()
。如果你不做清除,那么在移动之后,该棋子将显示在它的旧位置和新位置,这显然是错误的?(关于帧速率的评论是画布每秒更新的频率。让我想知道当您重绘时,您不需要每秒重绘10次,除非您还有时钟或其他变化但是,嘿,这也有效。)
首先,强烈建议您使用枚举来自我记录 board[][] 中使用的值
接下来,您可以极大地清理板绘制代码。
由于所有 4 个绘图用例都调用公共用例,因此将其设为 fn,并使该 fn 整洁:
现在,严格调用这些绘图代码实际上只有两种情况:(2,4) 或 (1, 3)假设你的枚举是正确的:
顺便说一句,永远不要使用更清晰的 for 循环可以做的 while 循环:
这种分解不是更容易阅读和调试吗?它是自我记录的。现在你的 bug 应该会突然出现在你面前了。
(顺便说一句,您目前绘制的国王与棋子完全相同,但我想您稍后会解决这个问题。)
第四次编辑:您让我们看到了错误的 fns,grr...您说您的错误实际上是在板绘图代码中。您能更正一下标题吗?它仍然是“实现移动功能”?
原回复:
机器向往所说的,这不是一个问题 - 还不是:告诉我们您目前正在尝试什么,以及为什么它不起作用。另外,删除所有不相关的代码。
看起来您在使用函数
moveTo(i,j)
时遇到了困难 - 但到底是什么?(全局变量 SecondPass、SecondPosition 表明你可能遇到了麻烦......你知道递归吗?如果不知道,不用担心。)
另外,作为一种风格,为了让你的生活变得轻松,这个实现不是面向对象的,全局变量尖叫分解不良。尝试重写为类
Checkers
,使棋盘等成为成员,编写init()
方法。我会将函数grid(x,y)
重命名为initialize(nrows,ncols)
。(咳咳!有迹象表明你是从别人那里改编的……)
6TH EDIT: Here are two solutions for you:
5TH EDIT: Ok thanks for stripping the code down.
Explain exactly what is the problem with your board-drawing code? 'Moved piece is not deleted from old location'? 'All pieces are drawn at wrong coordinates or colors'? ...?
It's not acceptable to just keep dumping code and saying "This code doesn't work".
Let me suggest you how to clean up your existing board-drawing code, and in that process you will almost surely find your bug.
Obviously you need to clear and redraw the screen every time there is a move (or promotion), do you actually do that? Declare a fn
redrawBoard()
for that. If you do not do a clear, then after a move the piece will be displayed in its old AND new locations, which would be wrong obviously?(The comment about Frame rate is how often canvas will be updated each second. makes me wonder when you redraw, you do not need to redraw 10 times a second, unless you also have a clock or other changing data. But, hey, that also works.)
First, strongly suggest you use an enum to self-document the values used in board[][]
Next, you can greatly clean up the board-drawing code.
Since all 4 piece-drawing cases call a common-case, make it a fn, and make that fn uncluttered:
Now, the board-drawing code that calls these strictly really only has two cases: (2,4) or (1,3) assuming you got the enum right:
and by the way, never use a while-loop where a more legible for-loop would do:
Is that decomposition not infinitely easier to read and debug? It's self-documenting. Now your bug should practically leap out at you.
(By the way, you're currently drawing kings the exact same as pieces, but I guess you'll fix that later.)
4TH EDIT: You had us looking at the wrong fns, grr... you say your bug is actually in the board-drawing code. Would you please correct the title which still says "Implement a movement function"?
ORIGINAL REPLY:
What machine yearning said, this is not a question - not yet: Tell us what you currently try, and why it doesn't work. Also, remove all unrelated code.
Looks like you're having difficulties with function
moveTo(i,j)
- but what exactly?(The globals secondPass, secondPosition signal you might be having trouble... do you know recursion? If not, no worries.)
Also, as a stylistic thing, and to make your life easy, this implementation is not OO, the globals scream bad decomposition. Try rewriting as a class
Checkers
, make board etc. a member, write aninit()
method. I would rename the functiongrid(x,y)
toinitialize(nrows,ncols)
.(And cough, cough! Signs you adapted this from someone else...)