Created
February 26, 2022 10:40
-
-
Save Rishbah-76/6aaeb0c08e2ee7d030c5464c78449ad2 to your computer and use it in GitHub Desktop.
Main function for pong game using python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def main(): | |
run= True | |
clock=pygame.time.Clock() | |
left_paddle=Paddle(10,HEIGHT//2-PADDLE_HEIGHT//2,PADDLE_WIDTH,PADDLE_HEIGHT) | |
right_paddle=Paddle(WIDTH-10-PADDLE_WIDTH,HEIGHT//2-PADDLE_HEIGHT//2,PADDLE_WIDTH,PADDLE_HEIGHT) | |
#Getting Ball | |
ball=Ball(WIDTH//2,HEIGHT//2,BALL_RADIUS) | |
#Setting score | |
left_score=0 | |
right_score=0 | |
while run: | |
# Maintaining constant FPS | |
clock.tick(FPS) | |
# Drwaing the ball, paddles and score | |
draw(WIN,[left_paddle,right_paddle],ball,left_score,right_score) | |
for event in pygame.event.get(): #getting the events | |
if event.type == pygame.QUIT: | |
run= False | |
break | |
elif event.type == pygame.KEYDOWN: | |
if event.key == pygame.K_q: | |
run= False | |
break | |
#Storing the keys to handle paddle movement | |
keys =pygame.key.get_pressed() | |
#MAking the ball move intially in x direction | |
ball.move_ball() | |
#Handling Paddle Movement | |
handle_paddle_movement(keys,left_paddle, right_paddle) | |
#Handling colision | |
handle_collision(ball,left_paddle,right_paddle) | |
#Adjusting score | |
if ball.x<0: | |
right_score+=1 | |
#Reseting the ball and paddle | |
ball.reset_ball() | |
left_paddle.reset_paddle() | |
right_paddle.reset_paddle() | |
sleep(1/2-0.09) | |
ball.x_vel*=-1 | |
elif ball.x> WIDTH: | |
left_score+=1 | |
#Reseting the ball and paddle | |
ball.reset_ball() | |
left_paddle.reset_paddle() | |
right_paddle.reset_paddle() | |
sleep(1/2-0.09) | |
ball.x_vel*=-1 | |
#Checking if anyone WON | |
won=False | |
if left_score>=WINING_SCORE: | |
won=True | |
win_text="Left Player Won!!" | |
elif right_score>=WINING_SCORE: | |
won=True | |
win_text="Right Player Won!!" | |
if won: | |
text=SCORE_FONT.render(f"{win_text}",1,SADDLE_BROWN) | |
WIN.blit(text,(WIDTH//2-text.get_width()//2,HEIGHT//2-text.get_height()//2)) | |
pygame.display.update() | |
sleep(3) | |
ball.reset_ball() | |
left_paddle.reset_paddle() | |
right_paddle.reset_paddle() | |
left_score=0 | |
right_score=0 | |
pygame.quit() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment