Building a Ping-Pong game using Python Programming Photo

Python Programming

Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Its high-level built-in data structures, combined with dynamic typing and dynamic binding, make it very attractive for Rapid Application Development, as well as for use as a scripting or glue language to connect existing components together. Python's simple, easy-to-learn syntax emphasizes readability and therefore reduces the cost of program maintenance. Python supports modules and packages, which encourages program modularity and code reuse. The Python interpreter and the extensive standard library are available in source or binary form without charge for all major platforms and can be freely distributed.

Often, programmers fall in love with Python because of the increased productivity it provides. Since there is no compilation step, the edit-test-debug cycle is incredibly fast. Debugging Python programs is easy: a bug or bad input will never cause a segmentation fault. Instead, when the interpreter discovers an error, it raises an exception. When the program doesn't catch the exception, the interpreter prints a stack trace. A source-level debugger allows inspection of local and global variables, evaluation of arbitrary expressions, setting breakpoints, stepping through the code a line at a time, and so on. The debugger is written in Python itself, testifying Python's introspective power. On the other hand, often the quickest way to debug a program is to add a few print statements to the source: the fast edit-test-debug cycle makes this simple approach very effective.

We often love playing games and being a python programmer here, I had built the ping-pong basic game using python programming skills with the package called pygame.

So, let's begin the programming

Step1. Install python on your system either it's mac, windows, or Linux.

Step2. After installation of python on the system, make sure that you install the PIP

Step2. Install the package called pygame through the terminal by using the command line below:-

pip install pygame

Step3. Create the file and save it as  ping_pong.py

step4. Then do the following programming as given below:-

 

 

import pygame

#constants and variables

WHITE = (255, 255, 255)

BLACK = (0, 0, 0)

WIDTH = 600

HEIGHT = 600

pygame.init()

game_font = pygame.font.SysFont('ubuntu', 40)

delay = 30

paddle_speed = 20

paddle_width = 10

paddle_height = 100

#pos means the position

p1_x_pos = 10

p1_y_pos = HEIGHT / 2 - paddle_height / 2

p2_x_pos = WIDTH - paddle_width -10

p2_y_pos = HEIGHT / 2 - paddle_height / 2

p1_score = 0

p2_score = 0

p1_up = False

p1_down = False

p2_up = False

p2_down = False

ball_x_pos = WIDTH / 2

ball_y_pos = HEIGHT / 2

ball_width = 8

ball_x_vel = -10

ball_y_vel = 0

#vel means the value

screen = pygame.display.set_mode((WIDTH, HEIGHT))

#drawing the object

def draw_objects():

pygame.draw.rect(screen, WHITE, (int(p1_x_pos), int(p1_y_pos), paddle_width, paddle_height))

pygame.draw.rect(screen, WHITE, (int(p2_x_pos), int(p2_y_pos), paddle_width, paddle_height))

pygame.draw.circle(screen, WHITE, (ball_x_pos, ball_y_pos), ball_width)

score = game_font.render(f"{str(p1_score)} - {str(p2_score)}", False, WHITE)

screen.blit(score, (WIDTH / 2, 30))

def apply_player_movement():

global p1_y_pos

global p2_y_pos

if p1_up:

p1_y_pos = max(p1_y_pos - paddle_speed, 0)

elif p1_down:

p1_y_pos = min(p1_y_pos +paddle_speed, HEIGHT)

if p2_up:

p2_y_pos = max(p2_y_pos - paddle_speed, 0)

elif p2_down:

p2_y_pos = min(p2_y_pos +paddle_speed, HEIGHT)

def apply_ball_movemoment():

global ball_x_pos

global ball_y_pos

global ball_x_vel

global ball_y_vel

global p1_score

global p2_score

if(ball_x_pos + ball_x_vel < p1_x_pos + paddle_width) and (p1_y_pos < ball_y_pos + ball_y_vel + ball_width <p1_y_pos + paddle_height):

ball_x_vel = -ball_x_vel

ball_y_vel = (p1_y_pos + paddle_height / 2 - ball_y_pos) / 15

ball_y_vel = -ball_y_vel

elif ball_x_pos + ball_x_vel <0:

p2_score += 1

ball_x_pos = WIDTH / 2

ball_y_pos = HEIGHT / 2

ball_x_vel = 10

ball_y_vel = 0

if(ball_x_pos + ball_x_vel > p2_x_pos - paddle_width) and (p2_y_pos < ball_y_pos + ball_y_vel + ball_width < p2_y_pos + paddle_height):

ball_x_vel = -ball_x_vel

ball_y_vel = (p2_y_pos + paddle_height / 2 - ball_y_pos) /15

ball_y_vel = -ball_y_vel

elif ball_x_pos + ball_x_vel > HEIGHT:

p1_score += 1

ball_x_pos = WIDTH / 2

ball_y_pos = HEIGHT / 2

ball_x_vel = -10

ball_y_vel = 0

if ball_y_pos + ball_y_vel > HEIGHT or ball_y_pos + ball_y_vel <0:

ball_y_vel = -ball_y_vel

 

ball_x_pos += ball_x_vel

ball_y_pos += ball_y_vel

pygame.display.set_caption("FlarePong v0.1 Alpha")

screen.fill(BLACK)

pygame.display.flip()

running= True

while running:

for event in pygame.event.get():

if event.type == pygame.QUIT:

running = False

if event.type == pygame.KEYDOWN:

if event.key == pygame.K_ESCAPE:

running = False

if event.key == pygame.K_w:

p1_up = True

if event.key == pygame.K_s:

p1_down =True

if event.key == pygame.K_UP:

p2_up = True

if event.key == pygame.K_DOWN:

p2_down =True

if event.type == pygame.KEYUP:

if event.key == pygame.K_w:

p1_up = False

if event.key == pygame.K_s:

p1_down =False

if event.key == pygame.K_UP:

p2_up = False

if event.key == pygame.K_DOWN:

p2_down = False

 

screen.fill(BLACK)

apply_player_movement()

apply_ball_movemoment()

draw_objects()

pygame.display.flip()

pygame.time.wait(delay)

Then bang the game is ready. Result:-

python

Menu