@PatrikSkarbo/

Turtle Shapesize och collision field test

Python (with Turtle)

Rektangel försöker jag skapa

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
98
99
100
import turtle
#Rektangel (rad:3-14)
#turtle.speed(5)
#turtle.Turtle()
#turtle.color("red","blue")
#turtle.left(30)
#turtle.hideturtle()
#turtle.fd(100)
#turtle.left(90)
#turtle.fd(200)
#turtle.left(90)
#turtle.fd(100)
#turtle.left(90)
#turtle.fd(200)

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)


#boll2
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"))

#https://learn.wecode24.com/pong-with-turtle-graphics-part-2/ (rad: 84-91)
def update_boll2_position () :
    global boll2_move_horiz, boll2_move_vert
    if boll2.ycor() + boll2_radius >= play_top :       # top of ball at or above top of field
        boll2_move_vert *= -1
    elif play_bottom >= boll2.ycor() - boll2_radius :  # bottom of ball at or below bottom of field
        boll2_move_vert *= -1
    boll2.setx(boll2.xcor() + boll2_move_horiz)
    boll2.sety(boll2.ycor() + boll2_move_vert)


write_scores()

window.listen()

move()

window.mainloop()
result
console