forked from hhhrrrttt222111/PyBox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPong.txt
More file actions
96 lines (75 loc) · 1.86 KB
/
Pong.txt
File metadata and controls
96 lines (75 loc) · 1.86 KB
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
# This game is from the official documentation of freegames
# https://pypi.org/project/freegames/
# pip install freegames
from random import choice, random
import turtle as t
from freegames import vector
# Player 1 Controls - Move Up: 'w', Move Down: 's'
# Player 2 Controls - Move Up: 'i', Move Down: 'k'
# Set window title, color and icon
t.title("Pong")
root = t.Screen()._root
root.iconbitmap("logo-ico.ico")
t.bgcolor('#cc66ff')
#Functions
# Randomly generate value between (-5, -3) or (3, 5)
def value():
return (3 + random() * 2) * choice([1, -1])
ball = vector(0, 0)
aim = vector(value(), value())
state = {1: 0, 2: 0}
# Move player position by change
def move(player, change):
state[player] += change
# Draw rectangle at (x, y) with given width and height
def rectangle(x, y, width, height):
t.up()
t.goto(x, y)
t.down()
t.begin_fill()
for count in range(2):
t.forward(width)
t.left(90)
t.forward(height)
t.left(90)
t.end_fill()
# Draw game and move pong ball
def draw():
t.clear()
rectangle(-200, state[1], 10, 50)
rectangle(190, state[2], 10, 50)
ball.move(aim)
x = ball.x
y = ball.y
t.up()
t.goto(x, y)
t.dot(10)
t.update()
if y < -200 or y > 200:
aim.y = -aim.y
if x < -185:
low = state[1]
high = state[1] + 50
if low <= y <= high:
aim.x = -aim.x
else:
return
if x > 185:
low = state[2]
high = state[2] + 50
if low <= y <= high:
aim.x = -aim.x
else:
return
t.ontimer(draw, 50)
t.setup(420, 420, 370, 0)
t.hideturtle()
t.tracer(False)
t.listen()
# Set Keyboard Controls
t.onkey(lambda: move(1, 20), 'w')
t.onkey(lambda: move(1, -20), 's')
t.onkey(lambda: move(2, 20), 'i')
t.onkey(lambda: move(2, -20), 'k')
draw()
t.done()