@PatrikSkarbo/

ping pong game

Python (with Turtle)

https://repl.it/@DARKLORD6474/ping-pong-game

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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from turtle import *
from random import randint, random
from time import sleep

start = 0

sliderH, sliderW = 60,20

totalX, totalY = 400, 400
ballX, ballY = 0.0, 0.0
collision = 0

dX = random() + 1#randint(1,2)
dY = random() + 1#randint(1,2)   

# Screen initia
screen = Screen()                        
screen.setup(totalX+50,totalY+50)
#screen.screensize(totalX,totalY)
#screen.title("Ping Pong Game!")
#print ('Screen size:', screen.screensize())
screen.bgcolor('black')

# Turtle 0 Creation => Draws boundaries for the Ping Pong Game
t0 = Turtle()
t0.hideturtle()
t0.color('white'); t0.speed(5)
t0.penup(); t0.goto(-75,totalY//2+10); t0.write("Press 'S' to start", True, font=("Arial", 10, "bold"), align="center")
t0.penup(); t0.goto(90,totalY//2+10); t0.write("Amount of bounces:", True, font=("Arial", 10, "bold") , align="center")
t0.penup(); t0.goto(160,totalY//2+10); t0.write('0', True, font=("Arial", 10, "bold") , align="center") #fixa så att den räknar uppåt med 1 varje gång bollen studsar på paddeln, kan använda räknar-koden i pong part 2 (sidan jag använde förut)
t0.speed(10) #denna rad gör så att ramen ritas snabbare
t0.penup(); t0.goto(-totalX//2,-totalY//2); t0.pendown(); t0.goto(-totalX//2,totalY//2); t0.goto(totalX//2,totalY//2); t0.goto(totalX//2,-totalY//2); t0.goto(-totalX//2,-totalY//2); #denna rad ritar ut spelets "ram"
t0.penup(); #t0.goto(-180,200); t0.pendown();t0.goto(-180, -200); t0.penup()


# Turtle 1 Creation => Acts as a slider controlled by 'Up' & 'Down' key
t1 = Turtle()
t1.hideturtle()
screen.register_shape("line", ((-sliderW//2,-sliderH//2), (-sliderW//2,sliderH//2), (sliderW//2,sliderH//2), (sliderW//2,-sliderH//2)))
t1.shape('line')
t1.speed(0)
t1.color('red')
t1.setheading(90) # face east at beginning
t1.penup(); t1.goto(-totalX//2 + 10, 0);
t1.showturtle()
 
def go_up():
    #print ('T1 position:',t1.pos())
    t1.fd(7)

def go_down():
    t1.bk(7)

def start_game():
    global start
    start = 1

screen.onkey(go_up, 'Up')
screen.onkey(go_down, 'Down')
screen.onkey(start_game, 'S')


# Turtle 2 Creation => Acts as the ping pong ball 
t2 = Turtle()
t2.shape('circle'); t2.speed(14) #t2.speed är hur snabbt bollen ska röra sig
t2.color('green')
t2.penup(); t2.goto(ballX,ballY);
t1.setheading(90)
#print ('Ball size:', t2.turtlesize())

def move_t2():
    global sliderH, sliderW
    global totalX, totalY
    global ballX, ballY
    global dX, dY
    global collision
    global start

    t2.goto(ballX, ballY)


##    if ballX < - totalX//2 + 10:
##        dX = -dX
    sliderX, sliderY = t1.pos()

    # Hit on the slider
    if ballY >= (sliderY - sliderH//2)and ballY <= (sliderY + sliderH//2) and ballX <= -totalX//2 + sliderW + 10:
        if collision == 0:
            dX = -dX
            collision = 1
    # Missed the slider
    if ballY <= (sliderY - sliderH//2) or ballY >= (sliderY + sliderH//2):
        if ballX <= -totalX//2 + sliderW + 10 -5:
            start = 0
            screen.bgcolor('red')
            #rad 94 angående t0 (kanterna) behöver fixas så att kanterna blir röda istället för vita
            t2.color('red')
                        
            # You lose text
            t0.penup(); t0.goto(0,totalY//10+1); t0.write("You Lose", True, font=("Arial", 32, "bold"), align="center")

  
    if ballX >= -totalX//2 + sliderW + 10:
        collision = 0
    
    if ballX > totalX//2 -10:
        dX = -dX
    if ballY > totalY//2 - 10:
        dY = -dY
    if ballY < -totalY//2 + 10:
        dY = -dY       

    if start == 1:
        ballX += dX
        ballY += dY
    else:
        
        ballX = 0
        ballY = 0
    
    screen.ontimer(move_t2,1)

move_t2()

screen.listen()
screen.mainloop()
result
console