Now we added a background to the game
I made it 500×500 and saved it into a folder called img.
I loaded with this function that makes it a surface
background = pygame.image.load("img\\background.png")
And I blitted it in the while loop at the start replacing screen.fill, because now this one deletes the previous blits on the screen so that it is all nice and clean.
while loop:
screen.blit(background, (0, 0))
I also used another way to draw the ball
I imported this
from pygame import gfxdraw
And substituted the ellipse, in the move_ball() function with this
# pygame.draw.ellipse(screen, GREEN, (xb, yb, 13, 13))
gfxdraw.filled_circle(screen, xb, yb, 5, GREEN)
The whole code
# pong!
import pygame
from random import choice, randrange
from pygame import gfxdraw
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, 13, 13))
gfxdraw.filled_circle(screen, xb, yb, 5, GREEN)
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))
xb, yb = 500, 300
def move1():
global y2
if y2 <= 450:
if keys[pygame.K_z]:
y2 += 10
if y2 > 0:
if keys[pygame.K_a]:
y2 -= 10
def move2():
global y1
if y1 <= 450:
if keys[pygame.K_m]:
y1 += 10
if y1 > 0:
if keys[pygame.K_k]:
y1 -= 10
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 = 2
print("Normal speed")
else:
vel_bal = 3
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()
background = pygame.image.load("img\\background.png")
while loop:
screen.blit(background, (0, 0))
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()
pygame.display.update()
clock.tick(120)
pygame.quit()
Now it looks a lot more the original
1.1 – Pong the father of Arkanoid
1.2 – Starting arkanoid… from pong
1.3 – Adding background
1.4 – Collision detection
1.5 – Bricks collisions
1.6 – Still on Collisions
1.7 – Fixed strange bouncing
1.8 – How to destroy the bricks
1.9 – More levels
2.1 – Infinite level generator
2.3 – Sounds and faster frame rate tecnique
2.5 – New nicer levels simmetric and in color and menus
2.6 – Keyboard control
2.7 – Mouse exclusive control
2.xxx – Tiny version
5.0 – Arkagame: 5 different versions
Github repository
https://github.com/formazione/arkapygame
Subscribe to the newsletter for updates
Tkinter templatesTwitter: @pythonprogrammi - python_pygame
Claude's Games
1. Memory gameVideos
Speech recognition gamePygame's Platform Game
