我在做游戏,打砖块。 我正在尝试在背景上添加图片,但是它没有显示。
我尝试了其他方法: 类background(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()
答案 0 :(得分:1)
我通过更改
使您的代码具有背景图片self.screen.blit("bg.png")
到
self.screen.blit(self.image, (0,0))
blit()对图像和其位置进行两个折痕处理。