forked from AseanK/python-tools-and-games
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
66 lines (51 loc) · 1.41 KB
/
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
from turtle import Screen
from paddle import Paddle
from ball import Ball
from time import sleep
from score import score
from random import choice
# Global val
SPEEDS = [.08, .07, .06, .05, .04, .03, .02, .01]
screen = Screen()
screen.setup(width=800, height=600)
screen.bgcolor("black")
screen.title("Pong")
screen.tracer(0)
screen.listen()
# Paddle X-axis position
l_paddle = Paddle(-350)
r_paddle = Paddle(350)
ball = Ball()
scoreboard = score()
screen.onkeypress(r_paddle.up, "Up")
screen.onkeypress(r_paddle.down, "Down")
screen.onkeypress(l_paddle.up, "w")
screen.onkeypress(l_paddle.down, "s")
# Main game loop
game_on = True
speed = 0.07
while game_on:
# time.sleep() = speed of the ball / lower = faster
sleep(speed)
ball.move()
screen.update()
if ball.ycor() > 280 or ball.ycor() < -270:
ball.bounce_wall()
# When ball hits the either paddle/ flip the X-axis
# Random speed
if ball.distance(r_paddle) < 50 and ball.xcor() > 320 or ball.distance(l_paddle) < 50 and ball.xcor() < -320:
ball.bounce_paddle()
speed = choice(SPEEDS)
# Left paddle scores
if ball.xcor() > 360:
scoreboard.left_point()
ball.reset()
ball.bounce_paddle()
speed = 0.07
# Right paddle scores
if ball.xcor() < -360:
scoreboard.right_point()
ball.reset()
ball.bounce_paddle()
speed = 0.07
screen.exitonclick()