本文整理汇总了Python中turtle.onkey函数的典型用法代码示例。如果您正苦于以下问题:Python onkey函数的具体用法?Python onkey怎么用?Python onkey使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了onkey函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: top_layer
def top_layer():
turtle.clearscreen()
screen_setup()
stations()
sp = turtle.Turtle()
sp.color('white')
sp.penup()
global top_pos
sp.setpos(top_pos)
sp.showturtle()
sp.speed(1)
def sta1():
turn_and_go(sp, 'sta1')
def sta2():
turn_and_go(sp, 'sta2')
def sta3():
turn_and_go(sp, 'sta3')
def zoom1():
global top_pos
top_pos = sp.pos()
global middle_sta1_pos
middle_sta1_pos = 250,-150
if sp.pos() == (200,75):
middle_layer_sta1()
else:
zoom()
turtle.onkey(sta1, 'a')
turtle.onkey(sta2, 'b')
turtle.onkey(sta3, 'c')
turtle.onkey(zoom1, '=')
turtle.listen()
示例2: docking_layer
def docking_layer():
turtle.clearscreen()
screen_setup()
docking_port1()
sp3 = turtle.Turtle()
sp3.color('white')
sp3.penup()
sp3.speed(0)
sp3.setpos(300, 0)
sp3.seth(180)
sp3.showturtle()
speed = (10)
def turnleft():
sp3.left(30)
def turnright():
sp3.right(30)
def forward():
sp3.forward(speed)
def backward():
sp3.backward(speed)
turtle.onkey(forward, 'Up')
turtle.onkey(turnleft, 'Left')
turtle.onkey(turnright, 'Right')
turtle.onkey(backward, 'Down')
turtle.onkey(middle_layer_sta1, '-')
turtle.listen()
示例3: zoom
def zoom():
time.sleep(0.5)
turtle.clearscreen()
screen_setup()
sp2 = turtle.Turtle()
sp2.color('white')
sp2.penup()
sp2.speed(0)
sp2.setpos(250, -150)
sp2.seth(90)
sp2.showturtle()
speed = (10)
b = 2
def turnleft():
sp2.left(30)
def turnright():
sp2.right(30)
def forward():
sp2.forward(speed)
def backward():
sp2.backward(speed)
turtle.onkey(forward, 'Up')
turtle.onkey(turnleft, 'Left')
turtle.onkey(turnright, 'Right')
turtle.onkey(backward, 'Down')
turtle.onkey(top_layer, '-')
turtle.listen()
示例4: __init__
def __init__(self):
super(LaserCannon, self).__init__()
# Register events. Note the function we register for 'q' is
# a turtle function.
turtle.onscreenclick(self.aim,1)
turtle.onkey(self.shoot,"s")
turtle.onkey(seeYaLater,'q')
示例5: top_layer
def top_layer():
turtle.clearscreen()
screen_setup()
stations()
sp = turtle.Turtle()
sp.color('white')
sp.penup()
sp.setpos(200, 75)
sp.showturtle()
sp.speed(1)
def sta1():
sp.goto(200, 75)
def sta2():
sp.goto(-300, 45)
def sta3():
sp.goto(-100,-200)
def zoom1():
if sp.pos() == (200,75):
middle_layer_sta1()
else:
zoom()
turtle.onkey(sta1, 'a')
turtle.onkey(sta2, 'b')
turtle.onkey(sta3, 'c')
turtle.onkey(zoom1, '=')
turtle.listen()
示例6: hold
def hold(self):
""" holds the screen open until the user clicks or types 'q' """
# have the turtle listen for events
turtle.listen()
# hide the turtle and update the screen
turtle.ht()
turtle.update()
# have the turtle listen for 'q'
turtle.onkey( turtle.bye, 'q' )
# have the turtle listen for a click
turtle.onscreenclick( lambda x,y: turtle.bye() )
# start the main loop until an event happens, then exit
turtle.mainloop()
exit()
示例7: main
def main():
# use sys.argv if needed
print('generating spirograph...')
# create parser
descStr = """This program draws spirographs using the Turtle module.
When run with no arguments, this program draws random spirographs.
Terminology:
R: radius of outer circle.
r: radius of inner circle.
l: ratio of hole distance to r.
"""
parser = argparse.ArgumentParser(description=descStr)
# add expected arguments
parser.add_argument('--sparams', nargs=3, dest='sparams', required=False,
help="The three arguments in sparams: R, r, l.")
# parse args
args = parser.parse_args()
# set to 80% screen width
turtle.setup(width=0.8)
# set cursor shape
turtle.shape('turtle')
# set title
turtle.title("Spirographs!")
# add key handler for saving images
turtle.onkey(saveDrawing, "s")
# start listening
turtle.listen()
# hide main turtle cursor
turtle.hideturtle()
# checks args and draw
if args.sparams:
params = [float(x) for x in args.sparams]
# draw spirograph with given parameters
# black by default
col = (0.0, 0.0, 0.0)
spiro = Spiro(0, 0, col, *params)
spiro.draw()
else:
# create animator object
spiroAnim = SpiroAnimator(4)
# add key handler to toggle turtle cursor
turtle.onkey(spiroAnim.toggleTurtles, "t")
# add key handler to restart animation
turtle.onkey(spiroAnim.restart, "space")
# start turtle main loop
turtle.mainloop()
示例8: start
def start(x,y):
turtle.onscreenclick(None)
level_1()
tfood = turtle.Turtle()
tfood.hideturtle()
tfood.pu()
tfood.speed(0)
tfood.shape("square")
tfood.color("red")
tscore = turtle.Turtle()
tscore.hideturtle()
tscore.pu()
tscore.speed(0)
tscore.goto(100,-250)
tscore.write("Score:" + str(a[0]), align="center",font=(10))
while x > -210 and x < 210 and y > -210 and y <210:
if fcoord[2] == 0:
food(tfood)
fcoord[2] = 1
turtle.onkey(u,"Up")
turtle.onkey(l,"Left")
turtle.onkey(r,"Right")
turtle.onkey(d,"Down")
turtle.listen()
move()
x = turtle.xcor()
y = turtle.ycor()
if x > fcoord[0]*20-5 and x < fcoord[0]*20+5 and y > fcoord[1]*20-5 and y < fcoord[1]*20+5:
fcoord[2] = 0
tfood.clear()
a[0] += 1
tscore.clear()
tscore.write("Score:" + str(a[0]), align="center",font=(10))
if len(pos) > 1:
for i in range(1,len(pos)):
if x < pos[i][0]+5 and x > pos[i][0]-5 and y < pos[i][1]+5 and y > pos[i][1]-5:
tscore.clear()
tfood.clear()
gameover()
tscore.clear()
tfood.clear()
gameover()
示例9: tegn_romskip
def tegn_romskip():
turtle.penup()
turtle.shapesize(4)
turtle.setpos(200, 400)
turtle.setheading(90)
turtle.color('blue')
turtle.onkey(snu_hoyre, 'Right')
turtle.onkey(snu_venstre, 'Left')
turtle.onkey(bruk_motor, 'Up')
turtle.listen()
示例10: set_listeners
def set_listeners(self):
turtle.onscreenclick(self.clickcb, 1)
# In OSX, ctrl-click doesn't give a button-3 click. Instead,
# trackpad users have to enable secondary click in system
# preferences. Then, right-clicking on trackpad generates a
# button-2 click. I don't know what one-button mouse users can
# do?
turtle.onscreenclick(self.rightclickcb, 2)
# On Linux, right-click generates a button-3 click.
turtle.onscreenclick(self.rightclickcb, 3)
turtle.onkey(self.spacecb, "space")
turtle.onkey(self.savecb, "s")
turtle.onkey(self.redisplaycb, "r")
turtle.listen()
示例11: main
def main():
descStr = "This draws spirographs with turtle graphics, if no arguments are given, random spirographs will be drawn."
parser = argparse.ArgumentParser(description=descStr)
#add expected args
parser.add_argument('--sparams', nargs=3, dest='sparams', required=False,
help="The three arguments in sparams: R, r, l.")
#parse args
args = parser.parse_args()
#set width of window to 80% of screen width
turtle.setup(width=0.8)
#set cursor shape to turtle
turtle.shape('turtle')
#set title to Spirographs!
turtle.title("Spirographs!")
#add keyhandler to save drawings
turtle.onkey(saveDrawing, 's')
#start listening
turtle.listen()
#hide main turtle cursor
turtle.hideturtle()
#check for any arguments sent to --sparams and draw spirograph
if args.sparams:
params = [float(x) for x in args.sparams]
#draw spiro with given params
col = (0.0, 0.0, 0.0)
spiro = Spiro(0, 0, col, *params)
spiro.draw()
else:
#create animator objects
span = SpiroAnimator(6)
#add key handler to toggle cursor
turtle.onkey(span.toggleTurtles, "t")
#add keyhandler to restart
turtle.onkey(span.restart, "space")
#start main loop
turtle.mainloop()
示例12: up
import turtle
turtle.penup()
turtle.ht()
def up():
print("You pressed Up!")
def down():
print("You pressed Down!")
def left():
print("You pressed Left!")
def right():
print("You pressed Right!")
turtle.onkey(up, 'Up')
turtle.onkey(down, 'Down')
turtle.onkey(left, 'Left')
turtle.onkey(right, 'Right')
def repeat():
turtle.ontimer(repeat, 500)
turtle.listen() # Remember to put this after your listeners!
示例13: stamp
# functions to play with
def stamp():
t1.stamp()
t2.stamp()
def dot():
t1.dot()
t2.dot()
def testfun():
x = t2.pen()
t2.stamp()
t2.pendown()
t2.circle(100)
t2.pen(x)
# global key event for turtle window
turtle.onkey(dot, "o")
turtle.onkey(stamp, "space")
w.onkey(testfun, "t")
w.onkey(w.bye, "x")
# extra key events for oturtles on window
t2._screen.onkey(t2.testfun,"h")
h.show(turtle._CFG)
print "use turtlekeys to move, v b m o space h t for Actions and x for close the Window"
# bind a callback on time
oldColor = None
def gruen(e):
global oldColor
print e
示例14: colide
def colide(t1,t2):
d = math.sqrt(math.pow(ugv1.xcor()-goal.xcor(),2) + math.pow(ugv1.ycor()-goal.ycor(),2))
if d < 20:
return True
else:
return False
def obstacle(t1,t2):
d = math.sqrt(math.pow(ugv1.xcor()-green.xcor(),2) + math.pow(ugv1.ycor()-green.ycor(),2))
if d < 30:
ugv1.rt(30)
#Modo escucha de teclado
turtle.listen()
turtle.onkey(turnleft, "Left")
turtle.onkey(turnright, "Right")
turtle.onkey(speedup, "Up")
turtle.onkey(speeddown, "Down")
turtle.onkey(restart, "r")
#Inicio de movimiento
while speed>0:
obstacle(ugv1,green)
ugv1.forward(speed)
#Mantener UGV dentro del ambiente
if ugv1.xcor() > 300 or ugv1.xcor() < -300:
ugv1.rt(180)
if ugv1.ycor() > 300 or ugv1.ycor() < -300:
示例15: docking
def docking():
cr = turtle.Turtle()
cr.hideturtle()
cr.color('gray')
cr.speed(0)
cr.penup()
cr.setpos(0,250)
cr.seth(270)
cr.pendown()
cr.forward(500)
cr.penup()
cr.setpos(450,0)
cr.seth(180)
cr.pendown()
cr.forward(900)
def left():
global cenx
global x
x = x-10
cenx = cenx-10
def right():
global cenx
global x
x = x+10
cenx = cenx+10
def up():
global y
global ceny
y = y+10
ceny = ceny+10
def down():
global y
global ceny
y = y-10
ceny = ceny-10
for i in range(100):
global r
global x
global cens
ma.setx(cenx)
ma.sety(ceny)
al.setx(x)
al.sety(y)
al.pendown()
al.seth(90)
al.clear()
ma.clear()
al.circle(r)
ma.dot(cens, 'white')
al.penup()
r = r+1
x = x+1
if cens <= 10:
cens+0.5
turtle.onkey(left, 'Left')
turtle.onkey(right, 'Right')
turtle.onkey(up, 'Up')
turtle.onkey(down, 'Down')
turtle.listen()
turtle.delay(50)