@PatrikSkarbo/

Turtle Animation test 2 (Använd denna// 12 mars 2019

Python (with Turtle)

No description

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import turtle 

window = turtle.Screen()
window.setup(500,500)
window.bgcolor('lightblue')
window.tracer(1)            # tell screen to not show automatically


# Playing field boundaries
play_top = window.window_height() / 2 - 100    # top of screen minus 100 units
play_bottom = -window.window_height() / 2 + 100   # 100 from bottom
play_left   = -window.window_width() / 2 + 50     # 50 from left
play_right  = window.window_width() / 2 - 50      # 50 from right


# Draw playing field
area = turtle.Turtle()
area.speed(8)
area.hideturtle()
area.penup()
area.goto(play_right, play_top)
area.pendown()
area.goto(play_left, play_top)
area.goto(play_left, play_bottom)
area.goto(play_right, play_bottom)
area.goto(play_right, play_top)


#Boll 1
boll1 = turtle.Turtle()

boll1.color('red')
boll1.penup()
boll1.shape('square')
boll1_radius = 10 * 0.5 
speed = 1

boll1.goto(-135,0)

def move():
 boll1.forward(speed)
 window.ontimer(move, 10)


window.onkey(lambda: boll1.setheading(90), 'W')
#window.onkey(lambda: boll1.setheading(180), 'A')
#window.onkey(lambda: boll1.setheading(0), 'D')
window.onkey(lambda: boll1.setheading(270), 'S')


#Boll 2
boll2 = turtle.Turtle()

boll2.color('green')
boll2.penup()
boll2.shape('square')
boll2_radius = 10 * 0.5 
speed = 1

boll2.goto(135,0)

boll2.setheading(90)

def move():
 boll2.forward(speed)
 window.ontimer(move, 10)

window.onkey(lambda: boll2.setheading(90), 'Up')
#window.onkey(lambda: boll2.setheading(180), 'Left')
#window.onkey(lambda: boll2.setheading(0), 'Right')
window.onkey(lambda: boll2.setheading(270), 'Down')


# Score
score_turtle = turtle.Turtle()
score_turtle.speed(8)
score_turtle.penup()
score_turtle.hideturtle()

score_L = 0
score_R = 0

def write_scores() :
    score_turtle.clear()
    score_turtle.goto(-window.window_width()/4, window.window_height()/2 - 80)
    score_turtle.write(score_L, align="center", font=("Arial", 32, "bold"))
    score_turtle.goto(window.window_width()/4, window.window_height()/2 - 80)
    score_turtle.write(score_R, align="center", font=("Arial", 32, "bold"))

write_scores()

window.listen()

move()

window.mainloop()
result
console