python - 如何在 python-pygame 中添加背景图像

标签 python image pygame

我正在制作一款游戏,打砖 block 。 我尝试在背景上添加图像,但它没有显示。

我尝试了一些其他方法: 类背景(pygame.sprite.Sprite) - 我可以看到图像,但它隐藏了游戏

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()

最佳答案

我通过更改您的代码来获得背景图像

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

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

blit()有两个参数:图像和图像的位置。

关于python - 如何在 python-pygame 中添加背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56485518/

相关文章:

python - Matplotlib imshow 缩放功能?

css - 如何防止选择图像的边

python - 为什么我的程序停止响应?

python - 从导入的脚本中使用 Pygame 模块

python - 如何安装 LXML Python 3.3 Windows 8 64 位

python - 如何使用 Tensorflow 检测图像中的物体位置?

python - 使用 ctypes : passing vectors 从 python 调用 c

python - Pygame:将 .png 文件转换为 rect?

Python:将元组值放入单独的 df 列中?

python - Jnius安装错误, "Unable to determine JDK_HOME"