神经代码服了转个圈都转不明白
2024-11-17 15:14:59
发布于:浙江
import math
from turtle import *
OD = math.pi / 180
def Rotate(x, y, a, b, theta): # a,b为坐标,x,y为旋转中心,theta为角度
xn, yn = a - x, b - y
if xn == 0 and yn == 0:
return x, y
elif yn == 0:
n = 0.0 if xn > 0 else 180.0 * OD
elif xn == 0:
n = 90.0 * OD if yn > 0 else -90.0 * OD
else:
n = math.atan(yn / xn)
n = n + theta * OD
bn = math.sqrt(xn ** 2 + yn ** 2) * math.sin(n)
an = (bn / math.tan(n)) if n % math.pi != 0 else 0.0
return x + an, y + bn
SIZE = 50
pensize(5)
speed(0)
penup()
goto(Rotate(0, 0, SIZE, SIZE, -360))
pendown()
for i in range(720):
goto(Rotate(0, 0, SIZE, SIZE, i - 360))
done()
全部评论 2
我表示:大哥,能不能标注一下Python
3天前 来自 浙江
0人机 是个人都知道Python
4小时前 来自 广东
0
import math from turtle import * OD = math.pi / 180 def Rotate(x, y, a, b, theta): # a,b为坐标,x,y为旋转中心,theta为角度 xn, yn = a - x, b - y if xn == 0 and yn == 0: return a, b elif yn == 0: n = 0.0 if xn > 0 else 180.0 * OD elif xn == 0: n = 90.0 * OD if yn > 0 else -90.0 * OD else: n = math.atan(yn / xn) n = n - theta * OD Lng = math.sqrt(xn ** 2 + yn ** 2) bn = Lng * math.sin(n) an = Lng * math.cos(n) return x + an, y + bn def RotRec(x, y, z, a, b, c, xA, yA, zA): a, b = Rotate(x, y, a, b, zA) b, c = Rotate(y, z, b, c, yA) a, c = Rotate(x, z, a, c, xA) return a, b, c SIZE = 100 pensize(5) speed(0) penup() tracer(False) f = lambda z: min(0.5, (z + SIZE) / SIZE * 0.5) def setpoint(x, y, z, col=(0, 0, 0)): z *= 0.75 goto(x + z - 200, y + z - 200) pendown() dot(5, col) penup() for i in range(128): setpoint(i * 3, 0, 0, (f(0), f(0), f(0))) setpoint(0, i * 3, 0, (f(0), f(0), f(0))) setpoint(0, 0, i * 3, (f(i*3), f(i*3), f(i*3))) update() def setpoint(x, y, z, col=(0, 0, 0)): z *= 0.75 goto(x + z, y + z) pendown() dot(round(max((z + SIZE) * 0.025 + 3, 0.1)), col) penup() for i in range(365): x, y, z = RotRec(0, 0, 0, SIZE, 0, 0, i, 0, 0) setpoint(x, y, z, (1, f(z), f(z))) x, y, z = RotRec(0, 0, 0, 0, SIZE, 0, 0, i, 0) setpoint(x, y, z, (f(z), 1, f(z))) x, y, z = RotRec(0, 0, 0, SIZE, 0, 0, 0, 0, i) setpoint(x, y, z, (f(z), f(z), 1)) goto(999, 999) update() print("Done.") done()
4天前 来自 浙江
0flag = 'left' def right(): global flag flag = 'right' def up(): global flag flag = 'up' def left(): global flag flag = 'left' def down(): global flag flag = 'down' turtle.listen() turtle.onkeypress(left,'Left') turtle.onkeypress(right,'Right') turtle.onkeypress(up,'Up') turtle.onkeypress(down,'Down') length = 5 def eat(): eggX = egg.xcor() eggY = egg.ycor() headX = snakelist[0].xcor() headY = snakelist[0].ycor() if eggX==headX and eggY==headY: print('吃鸡蛋') eggX = random.randint(-13,13)*20 eggY = random.randint(-13,13)*20 egg.goto(eggX,eggY) t = turtle.Turtle() t.shape('body.gif') snakelist.append(t) global length length += 1 word.clear() word.write(length,font=('宋体',20)) turtle.update def bound(): headX = snakelist[0].xcor() headY = snakelist[0].ycor() if headX<-300 or headX>300 or headY>300 or headY<-300: return True while True: eat() if flag == 'left': goLeft() elif flag == 'right': goRight() elif flag == 'up': goUp() elif flag == 'down': goDown() time.sleep(0.1) if bound(): break word = turtle.Turtle() word.penup() word.goto(-175,30) word.color('white') word.write('GAME OVER',font=('宋体',40)) time.sleep(2) turtle.bye()
自己下个图片
3天前 来自 浙江
0###贪吃蛇游戏O(∩_∩)O哈哈~ import turtle,time,random turtle.setup(600,600) #设置窗口大小 turtle.bgpic('背景1.gif') turtle.tracer(0) #创建鸡蛋 turtle.addshape('egg.gif') #添加画笔形状 egg = turtle.Turtle() egg.shape('egg.gif') egg.penup() egg.goto(-180,-180) #创建小蛇 turtle.addshape('body.gif') turtle.addshape('head1.gif') turtle.addshape('head2.gif') turtle.addshape('head3.gif') turtle.addshape('head4.gif') snakelist = [] for i in range(10): t = turtle.Turtle() t.penup() t.goto(20*i,0) if i==0: t.shape('head1.gif') else: t.shape('body.gif') snakelist.append(t) #记录长度 word = turtle.Turtle() word.penup() word.goto(-225,265) word.color('white') word.hideturtle() word.write('100',font=('宋体',25)) turtle.update() #向左移动 def goLeft(): x = snakelist[0].xcor() y = snakelist[0].ycor() t = snakelist.pop() t.hideturtle() t1 = turtle.Turtle() t1.shape('head1.gif') snakelist.insert(0,t1) t1.penup() t1.goto(x-20,y) snakelist[1].shape('body.gif') turtle.update() ##while True: ## goLeft() ## time.sleep(0.1) def goRight(): x = snakelist[0].xcor() y = snakelist[0].ycor() t = snakelist.pop() t.hideturtle() t1 = turtle.Turtle() t1.shape('head3.gif') snakelist.insert(0,t1) t1.penup() t1.goto(x+20,y) snakelist[1].shape('body.gif') turtle.update() ##while True: ## goRight() ## time.sleep(0.1) def goUp(): x = snakelist[0].xcor() y = snakelist[0].ycor() t = snakelist.pop() t.hideturtle() t1 = turtle.Turtle() t1.shape('head2.gif') snakelist.insert(0,t1) t1.penup() t1.goto(x,y+20) snakelist[1].shape('body.gif') turtle.update() ##while True: ## goUp() ## time.sleep(0.1) def goDown(): x = snakelist[0].xcor() y = snakelist[0].ycor() t = snakelist.pop() t.hideturtle() t1 = turtle.Turtle() t1.shape('head4.gif') snakelist.insert(0,t1) t1.penup() t1.goto(x,y-20) snakelist[1].shape('
3天前 来自 浙江
0
有帮助,赞一个