From 0da6eb600ce360a39dc8b293298505e33c7ebe44 Mon Sep 17 00:00:00 2001 From: aditya desle <50102208+adityadesle@users.noreply.github.com> Date: Thu, 1 Oct 2020 11:32:15 +0530 Subject: [PATCH 1/2] Update README.md --- README.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c4ae2b8..10c7d4f 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,14 @@ # Python -All I do in Python. +All I do in Python : + +Go and check out the : + +Machine learing + +Data Preprocessing + +Data Analyzing + +And get some practise Examples of Python + +Intorduction to PyGame : Ping Pong game From 81a790e8c9ba71bac0adc5ff1353df7e02168795 Mon Sep 17 00:00:00 2001 From: aditya desle <50102208+adityadesle@users.noreply.github.com> Date: Thu, 1 Oct 2020 11:33:07 +0530 Subject: [PATCH 2/2] Ping Pong game Introduction to Py game : Ping Pong Game --- pingpong.py | 135 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 pingpong.py diff --git a/pingpong.py b/pingpong.py new file mode 100644 index 0000000..c982091 --- /dev/null +++ b/pingpong.py @@ -0,0 +1,135 @@ +# -*- coding: utf-8 -*- +""" +Created on Wed Apr 22 17:49:12 2020 + +@author: 66IN +""" + + +import pygame,random, sys #pygame is an inbuilt library for python , sys is module to access some functionality + + +def ball_animation(): + global ball_speed_x,ball_speed_y + ball.x += ball_speed_x + ball.y += ball_speed_y + + #to bounce the ball on the screen edge + if ball.top <= 0 or ball.bottom >= screen_height: #vertical or y axis + ball_speed_y *= -1 + + if ball.left <= 0 or ball.right >= screen_width: #horizontal or x axis + ball_restart() + + + if ball.colliderect(player) or ball.colliderect(opponent): + ball_speed_x *= -1 + + +def player_animation(): + player.y = player_speed + if player.top <=0: + player.top = 0 + if player.bottom >= screen_height: + player.bottom = screen_height + + + +def opponent_animation(): + if opponent.top < ball.y: + opponent.top += opponent_speed + + if opponent.bottom > ball.y: + opponent.bottom -= opponent_speed + if opponent.top <=0: + opponent.top = 0 + if opponent.bottom >= screen_height: + opponent.bottom = screen_height + + + +def ball_restart(): + global ball_speed_x,ball_speed_y + ball.center = (screen_width/2,screen_height/2) + ball_speed_y *= random.choice((1,-1)) + ball_speed_x *= random.choice((1,-1)) + + +pygame.init() #general setup , initiate pygame +clock = pygame.time.Clock() + + +#setting up window +screen_width = 1280 +screen_height = 960 +#display surface object , store in screen variable +screen = pygame.display.set_mode((screen_width,screen_height)) +pygame.display.set_caption("Pong")#title + + + +# now drawing in pygame + +#game rectangle +ball = pygame.Rect(screen_width/2 - 15,screen_height/2 - 15,30,30) #pygame.Rect(x,y,width,height) x,y is at top left(0,0) +player = pygame.Rect(screen_width - 20, screen_height/2 - 70,10,140) +opponent = pygame.Rect(10,screen_height/2 - 70,10,140) + + +#for color + +bg_color = pygame.Color('grey12') +light_grey = (200,200,200) + + +ball_speed_x = 7 * random.choice((1,-1)) +ball_speed_y = 7 * random.choice((1,-1)) +player_speed = 0 +opponent_speed = 7 +while True: #just to check the user has closed the exit window or not + + #handling event + for event in pygame.event.get(): #this loop will check all user actions + if event.type == pygame.QUIT: + pygame.quit() + sys.exit() #closes entire game + + if event.type == pygame.KEYDOWN: + if event.key == pygame.K_DOWN: + player_speed += 7 + + if event.key == pygame.K_UP: + player_speed -= 7 + + + if event.type == pygame.KEYUP: + if event.key == pygame.K_DOWN: + player_speed += 7 + + if event.key == pygame.K_UP: + player_speed -= 7 + + ball_animation() + player_animation() + opponent_animation() + + #visuals + screen.fill(bg_color) + pygame.draw.rect(screen,light_grey,player) #pygame.draw.rect(screen,color,rect) + pygame.draw.rect(screen,light_grey,opponent) + pygame.draw.ellipse(screen,light_grey,ball) + #line to seperate 2 sides + pygame.draw.aaline(screen,light_grey,(screen_width/2,0),(screen_width/2,screen_height)) + + + + + + + #updating window + pygame.display.flip() #used to draw things on screen + clock.tick(60) #sand clock defined in a code it limits how fast loop works , its 60fps + + + + \ No newline at end of file