-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython.py
178 lines (145 loc) · 4.99 KB
/
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import pygame, asyncio
from pygame.locals import *
pygame.init()
#Clock
fpsclock=pygame.time.Clock()
fps=60
#Color
bg=(0,0,0) #bg=(50,25,50)
green=(0,255,0) #white=(255,255,255)
#Font
font=pygame.font.SysFont('constantia',30)
#Variables
live_ball=False
You_text_x_cor=500
margin=50
AI_score=0
player_score=0
winner=0
speed_increase=0
#Screen
screen_width=600
screen_height=500
screen=pygame.display.set_mode((screen_width,screen_height))
pygame.display.set_caption('Pong')
#Score board
def draw_board():
screen.fill(bg)
def draw_text(text,font,text_col,x,y):
img=font.render(text,True,text_col)
screen.blit(img,(x,y))
class paddle():
def __init__(self,x,y):
self.x=x
self.y=y
self.rect=Rect(self.x,self.y,20,100)
self.speed=5
def move(self):
key=pygame.key.get_pressed()
if (key[pygame.K_w] or key[pygame.K_UP]) and self.rect.top>margin:
self.rect.move_ip(0,-1*self.speed)
if (key[pygame.K_s] or key[pygame.K_DOWN]) and self.rect.bottom<screen_height:
self.rect.move_ip(0,self.speed)
def draw(self):
pygame.draw.rect(screen,green,self.rect)
def ai(self):
#Computer moves the paddle by itself
if self.rect.centery<pong.rect.top and self.rect.bottom<screen_height:
self.rect.move_ip(0,self.speed)
if self.rect.centery>pong.rect.bottom and self.rect.top>margin:
self.rect.move_ip(0,-1*self.speed)
class ball():
def __init__(self,x,y):
self.reset(x,y)
def move(self):
#Collision with top
if self.rect.top<margin:
self.speed_y*=-1
#Collision with bottom
if self.rect.bottom>screen_height:
self.speed_y*=-1
#Collision with paddles
if self.rect.colliderect(player_paddle) or self.rect.colliderect(AI_paddle):
self.speed_x*=-1
#Check who won
if self.rect.left<0:
self.winner=1
if self.rect.right>screen_width:
self.winner=-1
self.rect.x+=self.speed_x
self.rect.y+=self.speed_y
return self.winner
def draw(self):
pygame.draw.circle(screen,green,(self.rect.x+self.ball_rad,self.rect.y+self.ball_rad),self.ball_rad)
def reset(self,x,y):
self.x=x
self.y=y
self.ball_rad=8
self.rect=Rect(self.x,self.y,self.ball_rad*2,self.ball_rad*2)
self.speed_x=-4
self.speed_y=4
self.winner=0
#Paddles
player_paddle=paddle(screen_width-40,screen_height//2)
AI_paddle=paddle(20,screen_height//2)
#Pong ball
pong=ball(screen_width-60,screen_height//2+50)
async def main():
global live_ball, winner, player_score, AI_score, speed_increase
run=True
while run:
fpsclock.tick(fps)
draw_board()
draw_text('AI: ' +str(AI_score),font,green,20,15)
draw_text('You: ' +str(player_score),font,green,screen_width-100,15)
draw_text('BALL SPEED: ' +str(abs(pong.speed_x)),font,green,screen_width//2-100,15)
pygame.draw.line(screen,green,(0,margin),(screen_width,margin),2)
#Draw paddles and ball
player_paddle.draw()
AI_paddle.draw()
if live_ball:
winner=pong.move()
if winner==0:
player_paddle.move()
AI_paddle.ai()
pong.draw()
speed_increase+=1
else:
live_ball=False
if winner==1:
player_score+=1
elif winner==-1:
AI_score+=1
if speed_increase>500:
speed_increase=0
if pong.speed_x < 0:
pong.speed_x -= 1
if pong.speed_x > 0:
pong.speed_x += 1
if pong.speed_y < 0:
pong.speed_y -= 1
if pong.speed_y > 0:
pong.speed_y += 1
#Player instructions
if not live_ball:
if winner==0:
draw_text('Use "up/down" key or "W","S',font,green,100,screen_height//2-100)
draw_text('to go up and down dont allow',font,green,100,200)
draw_text('the ball slip behind your paddle.',font,green,100,250)
draw_text('CLICK ANYWHERE TO START',font,green,100,300)
elif winner==1:
draw_text('YOU SCORED!',font,green,220,screen_height//2-100)
draw_text('CLICK ANYWHERE TO START',font,green,100,screen_height//2-50)
elif winner==-1:
draw_text('AI SCORED!',font,green,220,screen_height//2-100)
draw_text('CLICK ANYWHERE TO START',font,green,100,screen_height//2-50)
for event in pygame.event.get():
if event.type==pygame.QUIT:
run=False
if event.type==pygame.MOUSEBUTTONDOWN and not live_ball:
live_ball=True
pong.reset(screen_width-60,screen_height//2+50)
pygame.display.update()
await asyncio.sleep(0)
pygame.quit()
asyncio.run(main())