@anagenb/

TurtleEvents

Python (with Turtle)

LEarning Turtle Events

fork
loading
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import turtle
import random
import time

screen = turtle.Screen()
screen.setup(400, 600)
screen.bgcolor("lightgreen")

pacer = turtle.Turtle()
pacer.up()

catcher = turtle.Turtle()
catcher.up()
catcher.shape("turtle")
catcher.sety(-200)

spaceMenList=[]

def pacer_timeTick():
    pacer.forward(10)
    if(pacer.xcor() > 190):
        pacer.left(180)
    if(pacer.xcor() < -190):
        pacer.left(180)
    screen.ontimer(pacer_timeTick , 200)

def catcher_left():
    catcher.setx (  catcher.xcor() - 20)

def catcher_right():
    catcher.setx (  catcher.xcor() + 20)

def create_men():
    if(len(spaceMenList) < 10):
        newMen = turtle.Turtle()
        newMen.sety(290)
        newMen.setx(random.randint(-195, 195))
        spaceMenList.append(newMen)
    
def handle_spaceMen():
    create_men()
    #move_men()
    screen.ontimer(create_men, 400)
    
    
    
screen.ontimer(pacer_timeTick , 200)
screen.ontimer(handle_spaceMen, 400)


screen.onkey( catcher_left  ,   "Left")
screen.onkey( catcher_right  ,   "Right")

screen.listen()
screen.mainloop()


















result
console