I added some code to speed up the ball when the player bar is moving at the moment of the collision, with the function speed_up():
def speed_up():
global vel_bal
x, y = pygame.mouse.get_pos()
if startx == x:
vel_bal = 1
print("Normal speed")
else:
vel_bal = 2
print("Speed up")
We also added the bricks, but, at the moment they are just images and no collision is detected.
They are built with this sprite class:
class Sprite:
"Draw Player 2"
def __init__(self, x, y):
self.x = x
self.y = y
self.update()
def update(self):
pygame.draw.rect(screen, GREEN, (self.x, self.y, 50, 10))
And then all the bricks are create with this function:
def create_bricks():
blist = """
10111111
11111011
"""
bricks = []
h = 20
w = 0
for brick in blist[1:-1]:
if brick == "1":
bricks.append(Sprite(20 + w * 60, h))
if w > 7:
w = 0
h += 20
w += 1
return bricks
bricks = create_bricks()
So the bricks list contains all the sprites, that are displayed in the while loop with the update function of every sprite:
for brick in bricks:
brick.update()
The whole code
# pong!
import pygame
from random import choice, randrange
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("Game")
pygame.init()
def sprite1(x, y):
"Draw Player 1"
pygame.draw.rect(screen, RED, (x, y, 50, 10))
class Sprite:
"Draw Player 2"
def __init__(self, x, y):
self.x = x
self.y = y
self.update()
def update(self):
pygame.draw.rect(screen, GREEN, (self.x, self.y, 50, 10))
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"
pygame.draw.ellipse(screen, GREEN, (xb, yb, 10, 10))
def collision():
global startx
global x1, y1
global x2, y2
global xb, yb
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"
print(dbv)
print(yb)
speed_up()
else:
pygame.draw.ellipse(screen, BLACK, (xb, yb, 20, 20))
pygame.display.update()
xb, yb = 500, 300
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
def exit(event):
global loop
if event.type == pygame.QUIT:
loop = 0
if event.type == pygame.KEYUP:
if event.key == pygame.K_ESCAPE:
loop = 0
return loop
def speed_up():
global vel_bal
x, y = pygame.mouse.get_pos()
if startx == x:
vel_bal = 1
print("Normal speed")
else:
vel_bal = 2
print("Speed up")
pygame.mouse.set_visible(False)
loop = 1
# This is to increase ball speed
startx = 0
def create_bricks():
blist = """
10111111
11111011
"""
bricks = []
h = 20
w = 0
for brick in blist[1:-1]:
if brick == "1":
bricks.append(Sprite(20 + w * 60, h))
if w > 7:
w = 0
h += 20
w += 1
return bricks
bricks = create_bricks()
while loop:
keys = pygame.key.get_pressed()
for event in pygame.event.get():
loop = exit(event)
move1()
move2()
move_ball(xb, yb)
x, y = pygame.mouse.get_pos()
sprite1(x, y1)
collision()
startx = x
for brick in bricks:
brick.update()
pygame.display.update()
screen.fill((0, 0, 0))
clock.tick(120)
pygame.quit()