forked from VinayBelwal/Hactober-22-Programs-and-Projects-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
paddle ball game project using pygame python.py
111 lines (95 loc) · 2.87 KB
/
paddle ball game project using pygame python.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
import pygame as pg
import sys,time,random
pg.init()
SCREEN_WIDTH=600
SCREEN_HEIGHT=600
win=pg.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))
paddle=pg.Rect(250,575,100,13)
clock=pg.time.Clock()
font = pg.font.Font('arial.ttf', 30)
scoreText = font.render('Score: 0', True, (255,255,204), (0,0,0
scoreTextRect = scoreText.get_rect()
scoreTextRect.x=10
scoreTextRect.y=10
font2 = pg.font.Font('arial.ttf', 54)
gameOverText = font2.render('Game Over', True, (255,0,0), (0,0,
gameOverTextRect = gameOverText.get_rect()
gameOverTextRect.center=(SCREEN_WIDTH//2,SCREEN_HEIGHT//2)
GAME_RUNNING=True
BOX_SPEED=10
TARGET_FPS=50
MAX_BALL_SPEED=10
MIN_BALL_SPEED=5
BALL_X_SPEED=0
BALL_Y_SPEED=0
GAME_STARTED=False
BALL_X=paddle.x+50
BALL_Y=paddle.y-13
SCORE=0
dt=0
old=time.time()
def updateScore():
global SCORE,scoreText
SCORE+=1
scoreText = font.render(f'Score: {SCORE}', True, (255,255,2
def checkCollision():
global SCREEN_WIDTH,paddle,BALL_X,BALL_Y,BALL_X_SPEED,BALL_
if paddle.x<0:
paddle.x=0
if paddle.x+100>SCREEN_WIDTH:
paddle.x=SCREEN_WIDTH-100
if BALL_X-13<=0 or BALL_X+13>=SCREEN_WIDTH:
BALL_X_SPEED=-BALL_X_SPEED
if BALL_Y-13<=0:
BALL_Y_SPEED=-BALL_Y_SPEED
#Ball hits the paddle
if BALL_Y+13>=paddle.y-5 and GAME_STARTED==True and BALL_X>
BALL_Y-=15
BALL_Y_SPEED=-BALL_Y_SPEED
updateScore()
elif BALL_Y+13>paddle.y:
gameOver()
def gameOver():
global GAME_RUNNING
GAME_RUNNING=False
while True:
new=time.time()
dt=new-old
old=new
for event in pg.event.get():
if event.type==pg.QUIT:
pg.quit()
sys.exit()
if event.type==pg.KEYDOWN:
if event.key==pg.K_SPACE:
GAME_STARTED=True
BALL_Y-=10
sign=random.randint(0,1)
BALL_Y_SPEED=-random.randint(MIN_BALL_SPEED,MAX
BALL_X_SPEED=random.randint(MIN_BALL_SPEED,MAX_
if sign==0:
BALL_X_SPEED=-BALL_X_SPEED
if GAME_RUNNING==True:
checkCollision()
key=pg.key.get_pressed()
if key[pg.K_LEFT]:
paddle.x-=BOX_SPEED*dt*TARGET_FPS
if GAME_STARTED==False:
BALL_X=paddle.x+50
elif key[pg.K_RIGHT]:
paddle.x+=BOX_SPEED*dt*TARGET_FPS
if GAME_STARTED==False:
BALL_X=paddle.x+50
win.fill("black")
pg.draw.rect(win,(153,204,255),paddle)
if GAME_STARTED==False:
pg.draw.circle(win,(0,102,204),(BALL_X,BALL_Y),13)
else:
BALL_X+=BALL_X_SPEED*dt*TARGET_FPS
BALL_Y+=BALL_Y_SPEED*dt*TARGET_FPS
pg.draw.circle(win,(0,102,204),(BALL_X,BALL_Y),13)
else:
win.blit(gameOverText,gameOverTextRect)
win.blit(scoreText,scoreTextRect)
pg.display.update()
clock.tick(50)