Pong v. 1.0 – Pygame example

Pong 2.0

The new version of Pong now uses classes, instead of functions only and has a new way to update the page.

You will find the old code below. In this version both the bars are controlled by the mouse.

The github repository for pong.

This code is done after ArkaPyGame that is also a github repo and on itch.

# pong!
import pygame, sys
from pygame import gfxdraw

BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)

scorep1 = 0
scorep2 = 0

clock = pygame.time.Clock()
screen = pygame.display.set_mode((500, 500))

pygame.init()

class Bar(pygame.sprite.Sprite):
    "This is the bar class"

    def __init__(self, x, y, w=10, h=60):
        super().__init__()
        self.x = x
        self.y = y
        self.w = w
        self.h = h
        self.rect = pygame.Rect(self.x, self.y, self.w, self.h)

    def update(self):
        self.y = pygame.mouse.get_pos()[1]
        self.rect = pygame.Rect(self.x, self.y, self.w, self.h)
        pygame.draw.rect(screen, RED, self.rect)


bar1 = Bar(0, 0)
bar2 = Bar(490, 0)
class Ball:
    "Draw the ball"

    def __init__(self, x, y):
        self.dirh = 0
        self.dirv = 0
        self.speed = 5
        self.x = x
        self.y = y
        # self.rect = pygame.Rect(self.x, self.y, 10, 10)

    def update(self):
        
        if self.dirh == 0:
            self.x -= self.speed
        if self.dirv == 0:
            self.y += self.speed
            if self.y > 490:
                self.dirv = 1
        if self.dirv:
            self.y -= self.speed
            if self.y < self.speed:
                self.dirv = 0
        if self.dirh:
            self.x += self.speed
        gfxdraw.filled_circle(screen, self.x, self.y, 5, (0, 255, 0))
        self.rect = pygame.Rect(self.x, self.y, 10, 10)

ball = Ball(480, 20)



def collision():
    global scorep1, scorep2

    pygame.display.set_caption(
        f"Player 1: {scorep1} - Player 2: {scorep2}")

    if ball.rect.colliderect(bar2):
        ball.dirh = 0  
    if ball.rect.colliderect(bar1):
        ball.dirh = 1
    if ball.x > 500:
        ball.x, ball.y = 10, 20
    if ball.x < 0:
        gfxdraw.filled_circle(screen, ball.x, ball.y, 5, (0, 0, 0))
        ball.x, ball.y = 480, 20


pygame.mouse.set_visible(False)
pygame.event.set_grab(True)
loop = 1
while loop:
    keys = pygame.key.get_pressed()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            loop = 0
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                loop = 0
    gfxdraw.filled_circle(screen, ball.x, ball.y, 5, (0, 0, 0))
    ball.update()
    pygame.draw.rect(screen, BLACK, bar1.rect)
    bar1.update()
    pygame.draw.rect(screen, BLACK, bar2.rect)
    bar2.update()
    collision()
    pygame.display.update()
    # screen.fill((0, 0, 0))
    clock.tick(60)

pygame.quit()
sys.exit()

 

Pong game has been updated to v. 1.0 with the players to be controlled via keyboard and mouse.

Go here to see the previous post with this code from skratch.

The code is here:

# pong!
import pygame, sys


BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
# Coordinates p1, p2
x1 = (490)
y1 = 250
x2 = (0)
y2 = 250
# coordinates of the ball
xb = 500
yb = 300
speedball = 5
# horisontal and vertical direction of the ball
dbo = 0 # left
dbv = 0 # down
# Score of player 1 and 2
scorep1 = 0
scorep2 = 0

clock = pygame.time.Clock()
screen = pygame.display.set_mode((500, 500))
pygame.display.set_caption("My game" + "Score player 1: " + str(scorep1) + " - Score player 2: " + str(scorep2))

pygame.init()

def ball():
	"Draw the ball"
	global xb, yb
	pygame.draw.ellipse(screen, GREEN, (xb, yb, 10, 10))

def sprite1(y): # x and y are the mouse position
	"Draw Player 1"
	pygame.draw.rect(screen, RED, (x1, y, 10, 50))

def sprite2(y):
	"Draw Player 2"
	pygame.draw.rect(screen, GREEN, (x2, y, 10, 50))

def move_ball(x,y):
	"The ball moves"
	global xb, yb, dbo, dbv, speedball
	if dbo == 0:
		xb -= speedball
	if dbv == 0:
		yb += speedball
		if yb > 490:
			dbv = 1
	if dbv:
		yb -= speedball
		if yb < speedball:
			dbv = 0
	if dbo:
		xb += speedball
	
def collision():
	global x1, y1 # the player 1 x and y (on the right)
	global x2, y2 # the player 2 x and y (on the left)
	global xb, yb # the ball x and y
	global x, y
	global dbo
	global scorep1, scorep2
	
	def restart():
		global xb, yb, scorep1, scorep2
		global x, y
		scorep2 += 10
		pygame.display.set_caption("My game" + "Score player 1: " + str(scorep1) + " - Score player 2: " + str(scorep2))
	
	if dbo:
		if xb > 480:
			if yb >= y and  yb < y + 50:
				dbo = 0		
			else:
				xb, yb = 10, 20
				pygame.display.update()
				pygame.time.delay(500)
				restart()
	else:
		if xb < 10:
			if yb >= y2 and  yb < y2 + 50:
				dbo = 1
			else:
				xb, yb = 480, 20
				pygame.display.update()
				pygame.time.delay(500)
				restart()

def move2():
	global y2
	if y2 <= 450:
		if keys[pygame.K_z]:
			y2 += 20
	if y2 > 0:
		if keys[pygame.K_a]:
			y2 -= 20

pygame.mouse.set_visible(False)
loop = 1
while loop:
	keys = pygame.key.get_pressed()
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			loop = 0
	x, y = pygame.mouse.get_pos()
	# move1()
	move2()
	move_ball(xb, yb)
	ball()
	sprite1(y)
	sprite2(y2)
	collision()
	pygame.display.update()
	screen.fill((0, 0, 0))
	clock.tick(60)

pygame.quit()
sys.exit()

The changes

The changes are in the fact that now the player one is controlled by the mouse

Mouse coordinates

We get the coordinates of the mouse here:

x, y = pygame.mouse.get_pos()

This is in the while loop.

Then we send the coordinates to the sprite1 function that draws the bar on the screen. We just need the y coordinate, as the bar need to go up and down.

	sprite1(y)

So, in sprite1 function, now, we just have to pass y and x1 is a global value that stays fixed.

def sprite1(y): # x and y are the mouse position
	"Draw Player 1"
	pygame.draw.rect(screen, RED, (x1, y, 10, 50))

Set the mouse invisible

To hide the mouse, before the while loop, we put this line of code

pygame.mouse.set_visible(False)

The video of the mouse controlled bar code

In this video I am trying to add the movement of the mouse to the game, having some issues to make the ball to restart when it went out of the right part of the screen. At the end the only problem is to make the ball bouce in a more interesting way, otherwise the ball is just bouncing in the same clockwise path.

Arkanoid ???

In the following code (with a video of the output) I made some changes to the code to transform pong into arkanoid. There is still the clockwise path that need to be fixed, in the next post.

# pong!
import pygame
from random import choice

BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
# Coordinates p1, p2 and ball
x1 = 490
y1 = 490
x2 = 0
y2 = 250
xb = 500
yb = 300

dbo = 'left'
dbv = 'down'

scorep1 = 0
scorep2 = 0

vel_bal = 2

clock = pygame.time.Clock()
screen = pygame.display.set_mode((500, 500))
pygame.display.set_caption("My game" + "Score player 1: " + str(scorep1) + " - Score player 2: " + str(scorep2))

pygame.init()

def ball():
	"Draw the ball"
	global xb, yb
	pygame.draw.ellipse(screen, GREEN, (xb, yb, 10, 10))

def sprite1(x,y):
	"Draw Player 1"
	pygame.draw.rect(screen, RED, (x, y, 50, 10))

def sprite2(x,y):
	"Draw Player 2"
	pygame.draw.rect(screen, GREEN, (x, y, 10, 50))

def move_ball(x,y):
	"The ball moves"
	global xb, yb, dbo, dbv
	if dbo == "left":
		xb -= vel_bal
		if xb < 10:
			dbo = "right"
	if dbv == 'down':
		yb += vel_bal
	if dbv == 'up':
		yb -= vel_bal
		if yb < 10:
			dbv = 'down'
	if dbo == "right":
		xb += vel_bal
		if xb > 480:
			dbo = "left"
	
def collision():
	global x1, y1 # the player 1 x and y (on the right)
	global x2, y2 # the player 2 x and y (on the left)
	global xb, yb # the ball x and y
	global x, y
	global dbo, dbv, vel_bal
	global scorep1, scorep2
	if dbo == "left":
		if yb > 480:
			if xb >= x and  xb < x + 50:
				print("Collision detected")
				dbv = "up"
				vel_bal = choice([1,2,3])
				print(dbv)
				print(yb)
			else:
				pygame.draw.ellipse(screen, BLACK, (xb, yb, 10, 10))
				pygame.display.update()
				xb, yb = 500, 300
				# scorep1 += 10
				pygame.display.set_caption("My game" + "Score player 1: " + str(scorep1) + " - Score player 2: " + str(scorep2))



def move1():
	global y2
	if y2 <= 450:
		if keys[pygame.K_z]:
			y2 += 20
	if y2 > 0:
		if keys[pygame.K_a]:
			y2 -= 20

def move2():
	global y1
	if y1 <= 450:
		if keys[pygame.K_m]:
			y1 += 20
	if y1 > 0:
		if keys[pygame.K_k]:
			y1 -= 20
pygame.mouse.set_visible(False)
loop = 1
while loop:
	keys = pygame.key.get_pressed()
	for event in pygame.event.get():
		if event.type == pygame.QUIT:
			loop = 0
	x, y = pygame.mouse.get_pos()
	move1()
	move2()
	move_ball(xb, yb)
	ball()
	sprite1(x,y1)
	collision()
	pygame.display.update()
	screen.fill((0, 0, 0))
	clock.tick(120)



pygame.quit()

This is a more recent version of Arkanoid made with pygame.

Particles in Arkapygame v. 10.0e

ArkaPyGame , the new version, has a github repo and it has an executable on itch.

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.