python - 我在用 Python 乌龟图形制作程序时遇到了一些问题
问题描述
我对我的代码有一些疑问。
我是 Python 的初学者。然后我开始用 Python 海龟图形制作一个绘图程序并上传到 Github。
我想在单击鼠标左键时画线。当我只是移动鼠标而不单击左键时,乌龟只会跟随我的光标。
所以我用谷歌搜索并找到了一些有用的代码。但它的工作原理很奇怪。我的意思是,它效果不好。
喜欢,当我点击按钮时,乌龟不动。或者我可以通过单击按钮移动乌龟,但线条不画...
无论如何,这里都是我的代码(我认为显示整个代码对你来说会更好......)。
from turtle import Screen
screen = Screen()
while True:
print('===========================')
print('*** Welcome to Pyint!!! ***')
print('___________________________')
print('*** Drawing With Turtle ***')
print('___________________________')
print('******* ver. 2.1.0 ********')
print('___________________________')
print('* Press Enter For Start!! *')
print('___________________________')
print('===========================')
import turtle as t
t.color('white')
t.bgcolor('black')
t.speed(0)
t.shape('circle')
t.pensize(5)
t.shapesize(2,2,2)
break
def colorsettings():
while True:
print('------------------------')
print('**** Color Settings ****')
print('------------------------')
chgpencolor = t.color(str(input('Color?:')))
savecolorsettings = str(input('Save the color settings? [y/n]:'))
if savecolorsettings == 'n':
continue
elif savecolorsettings == 'y':
print('-----------------------')
print('******** Saved ********')
print('-----------------------')
break
def thicknesssettings():
while True:
print('------------------------')
print('** Thickness Settings **')
print('------------------------')
chgpenthickness = t.pensize(int(input('Thickness?:')))
savethicknesssettings = str(input('Save the thickness settings? [y/n]:'))
if savethicknesssettings == 'n':
continue
elif savethicknesssettings == 'y':
print('-----------------------')
print('******** Saved ********')
print('-----------------------')
break
Moving, Dragging = range(2) # Codes about control the turtle start here.
def move_pen(x, y):
if state != Moving:
return
onmove(screen, None)
t.penup()
t.setheading(t.towards(x, y))
t.goto(x, y)
onmove(screen, move_pen)
def click_pen(x, y):
global state
t.onclick(None)
onmove(screen, None)
t.onrelease(release_handler)
t.ondrag(drag_handler)
state = Dragging
def release_handler(x, y):
global state
t.onrelease(None)
t.ondrag(None)
t.onclick(click_pen)
onmove(screen, move_pen)
state = Moving
def drag_handler(x, y):
if state != Dragging:
return
t.ondrag(None)
t.pendown()
t.setheading(t.towards(x, y))
t.goto(x, y)
t.ondrag(drag_handler)
def onmove(self, fun, add=None):
if fun is None:
self.cv.unbind('<Motion>')
else:
def eventfun(event):
fun(self.cv.canvasx(event.x) / self.xscale, -self.cv.canvasy(event.y) / self.yscale)
self.cv.bind('<Motion>', eventfun, add)
onmove(screen, move_pen)
t.onclick(click_pen) # End of codes about control the turtle
state = Moving
while True:
ipt = str(input())
if ipt == 'pencolor':
colorsettings()
elif ipt == 'penthickness':
thicknesssettings()
elif ipt == 'h':
t.goto(0,0)
elif ipt == 'c':
t.clear()
elif ipt == 'z':
t.undo()
elif ipt =='pu':
t.penup()
elif ipt =='pd':
t.pendown()
elif ipt == 'gg':
print('Are you really want to close the program? [y/n]')
elif ipt == 'n':
continue
elif ipt == 'y':
break
这是我第一次来这里。也是我的第一个问题。如果我的问题对你很粗鲁或不友好,我想说声抱歉。
谢谢 :)
解决方案
推荐阅读
- c# - 在服务器端 Blazor 中使用 SignInManager
- python - 计算不卖的天数
- excel - 在 Excel 中使用 vba 从“查找和替换”框或搜索框中复制和导出数据
- scheduled-tasks - 当 Sleuth 存在时,ShedLock 不锁定
- c# - LAB 到 XYZ 和 XYZ 到 RGB 颜色空间转换算法
- r - 在 R 中,如何在列名中包含正斜杠?
- django - 由于 django 中的 loaddata 导致的迁移错误,单元测试失败
- android - Xamarin Forms Android - AppCenter 推送通知按设备 ID 工作,但不按用户 ID
- java - 如何发送从java中的字符串创建的邮件附件?
- javascript - 如何在基于 JavaScript 的游戏代码中使用 jQuery 追加新跨度