python - 如何在 python-pygame 中添加背景图片
问题描述
我在做游戏,砖家。我想在背景上添加一个图像,但它没有显示出来。
我尝试了其他一些方法:类背景(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()接受两个参数图像和它的位置。
推荐阅读
- google-apps-script - 通过 Google Apps 脚本导出到 .xlsx 脚本错误
- tmux - 为什么提示符等待命令时 Tmux 不会停止更改终端标题?
- python - Django REST + Heroku + Amazon S3 Bucket:如何配置?
- oracle - 在oracle中创建索引时的表空间
- html - 在 R 中转义多个特殊字符和换行符
- java - java无法从文件中读取数据
- ios - 如何在同一个 App Swift iOS 中为两个不同的用户使用 Firebase 通知
- reactjs - 无法对未安装的组件执行 React 状态更新 - 即使清除间隔
- c - 我用 C 语言写了一个代码来解决一个问题。为什么它没有显示任何输出?
- c++ - 为什么 MSVC C++ 编译器将一个简单的 Hello World 扩展为 4000 行汇编?