-
Notifications
You must be signed in to change notification settings - Fork 0
/
pong.py
110 lines (90 loc) · 3.35 KB
/
pong.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
import pygame as py
import time,random
py.init()
displaywidth=1500
displayheight=800
display=display=py.display.set_mode((displaywidth,displayheight))
clock=py.time.Clock()
black=[0,0,0]
white=[255,255,255]
gray=[200,200,200]
mincolour=150
def game():
gameexit=False
gameover=False
userheight=30
userwidth=200
userx=round(displaywidth/2-userwidth/2)
usery=displayheight-userheight*2
dx=0
ballsize=20
ballx=userx+round(userwidth/2)
bally=usery-ballsize
balldx=0
balldy=0
blockrows=3
blockcolumns=20
blockheight=20
blockwidth=round(displaywidth/blockcolumns)
blocks=[]
for i in range(blockrows):
blocks.append([])
for a in range(blockrows):
for b in range(blockcolumns):
colour=[random.randint(mincolour,255),random.randint(mincolour,255),random.randint(mincolour,255)]
blocks[a].append([b+b*blockwidth,a+a*blockheight,colour])
while not gameexit:
while not gameover:
display.fill(black)
for event in py.event.get():
if event.type==py.QUIT:
gameexit=True
gameover=True
if event.type==py.KEYDOWN:
if event.key==py.K_RIGHT or event.key==py.K_d:
if userx+userwidth<displaywidth:
userx+=2
dx=2
elif event.key==py.K_LEFT or event.key==py.K_a:
if userx>0:
userx-=2
dx=-2
if event.key==py.K_SPACE and balldx==0:
balldx=2
balldy=-2
elif event.type==py.KEYUP:
if event.key==py.K_RIGHT or event.key==py.K_d or event.key==py.K_LEFT or event.key==py.K_a:
dx=0
if userx<=0 or userx+userwidth>=displaywidth:
dx=0
userx+=dx
ballx+=balldx
bally+=balldy
if ballx+ballsize>=userx and ballx-ballsize<=userx+userwidth and bally+ballsize>=usery and bally-ballsize<=usery+userheight:
balldy*=-1
if ballx-ballsize<=0 or ballx+ballsize>=displaywidth:
balldx*=-1
if bally>=displayheight:
gameover=True
py.draw.circle(display,gray,[ballx,bally],ballsize)##ball
py.draw.rect(display,white,(userx,usery,userwidth,userheight))##user
dead=[]
for a in range(len(blocks)):
for b in range(len(blocks[a])):
block=blocks[a][b]
x=block[0]
y=block[1]
colour=block[2]
if ballx+ballsize>=x and ballx-ballsize<x+blockwidth and bally+ballsize>=y and bally-ballsize<=y+blockheight:
balldy*=-1
dead.append([a,b])
py.draw.rect(display,colour,(x,y,blockwidth,blockheight))
for i in dead:
a=i[0]
b=i[1]
del blocks[a][b]
py.display.update()
if not gameexit:
game()
py.quit()
game()