1

I'm making a game, brickbreaker. I'm tring to add an image on the background, but it doesn't show up.

I tried some other ways: Class background(pygame.sprite.Sprite) - I can see image, but it hides game

import pygame
from pygame.locals import *
import sys


class Brickbreaker:
    def __init__(self):
        self.screen = pygame.display.set_mode((600, 600))
        self.image = pygame.image.load("bg.png")
        self.blocks = []
        self.paddle = [[pygame.Rect(300, 500, 20, 10), 120],
                [pygame.Rect(320, 500, 20, 10),100],
                [pygame.Rect(340, 500, 20, 10),80],
                [pygame.Rect(360, 500, 20, 10),45],
        ]
        self.ball = pygame.Rect(300, 490, 12, 12) 
        self.direction = -1
        self.yDirection = -1
        self.angle = 80
        self.speeds = {
            120:(-10, -3),
            100:(-10, -8),
            80:(10, -8),
            45:(10, -3),
        }
        self.swap = {
            120:45,
            45:120,
            100:80,
            80:100,
        }
        pygame.font.init()
        self.font = pygame.font.SysFont("Italic", 40)
        self.score = 0

    def createBlocks(self):
        self.blocks = []
        y = 50
        for __ in range(90 // 10):
            x = 40
            for _ in range(400 // 25 - 6):
                block = pygame.Rect(x, y, 50, 20)
                self.blocks.append(block)
                x += 52 
            y += 22


    def ballUpdate(self):
        for _ in range(2):
            speed = self.speeds[self.angle]
            xMovement = True
            if _:
                self.ball.x += speed[0] * self.direction 
            else:
                self.ball.y += speed[1] * self.direction * self.yDirection 
                xMovement = False
            if self.ball.x <= 0 or self.ball.x >= 600:
                self.angle = self.swap[self.angle]
                if self.ball.x <= 0:
                    self.ball.x = 1
                else:
                    self.ball.x = 599
            if self.ball.y <= 0:
                self.ball.y = 1
                self.yDirection *= -1

            for paddle in self.paddle:
                if paddle[0].colliderect(self.ball):
                    self.angle = paddle[1]
                    self.direction = -1
                    self.yDirection = -1
                    break
            check = self.ball.collidelist(self.blocks)
            if check != -1:
                block = self.blocks.pop(check)
                if xMovement:
                    self.direction *= -1
                self.yDirection *= -1
                self.score += 1
            if self.ball.y > 600:
                self.createBlocks()
                self.score = 0
                self.ball.x = self.paddle[1][0].x
                self.ball.y = 490
                self.yDirection = self.direction = -1

    def paddleUpdate(self):
        pos = pygame.mouse.get_pos()
        on = -1
        for p in self.paddle:
            p[0].x = pos[0] + 20 * on
            on += 1

    def main(self):
        pygame.mouse.set_visible(False)
        clock = pygame.time.Clock()
        self.createBlocks()
        while True:
            clock.tick(60)
            for event in pygame.event.get():
                if event.type == QUIT:
                    sys.exit()
            self.screen.fill([255, 255, 255])
            self.screen.blit("bg.png")
            self.paddleUpdate()
            self.ballUpdate()

            for block in self.blocks:
                pygame.draw.rect(self.screen, (80,80,80), block)
            for paddle in self.paddle:
                pygame.draw.rect(self.screen, (255,255,255), paddle[0])
            pygame.draw.rect(self.screen, (120,200,255), self.ball)
            self.screen.blit(self.font.render(str(self.score), -1, (200,200,255)), (40, 540)) #스코어 색
            pygame.display.update()

if __name__ == "__main__":
    Brickbreaker().main()

1 Answer 1

1

I got your code to have a background image by changing

self.screen.blit("bg.png")

to

self.screen.blit(self.image, (0,0))

blit() takes two arugments the image and the position of it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.