jy9*_*y95 6 python pygame background
pygame的新手只想知道如何在游戏中添加背景图片?这是我的代码到目前为止,我一直使用bg作为导入我的图像的方法,但py文件本身拒绝加载.
import pygame
import sys
from pygame.locals import *
clock = pygame.time.Clock()
screen = pygame.display.set_mode((600,500))
bg = pygame.image.load("images\space.png")
pygame.mouse.set_visible(0)
ship = pygame.image.load("images\ship.png")
ship_top = screen.get_height() - ship.get_height()
ship_left = screen.get_width()/2 - ship.get_width()/2
screen.blit(ship, (ship_left,ship_top))
shot = pygame.image.load("images\shot.png")
shoot_y = 0
pygame.display.set_caption('galaxy invaders')
while True:
clock.tick(60)
screen.fill((r,0,0))
screen.blit(bg.(0,0))
x,y = pygame.mouse.get_pos()
screen.blit(ship, (x-ship.get_width()/2, ship_top))
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == MOUSEBUTTONDOWN:
shoot_y = 500
shoot_x = x
if shoot_y > 0:
screen.blit(shot, (shoot_x, shoot_y))
shoot_y -= 10
pygame.display.update()
Run Code Online (Sandbox Code Playgroud)
这个问题很容易解决.您需要一个与屏幕大小相符的图像作为背景.请记住pygame.init()在游戏开始时添加,以便能够启动它和它的能力.这张图片的功能可以像这样使用:
class Background(pygame.sprite.Sprite):
def __init__(self, image_file, location):
pygame.sprite.Sprite.__init__(self) #call Sprite initializer
self.image = pygame.image.load(image_file)
self.rect = self.image.get_rect()
self.rect.left, self.rect.top = location
Run Code Online (Sandbox Code Playgroud)
这将允许程序在您调用它时通过此函数加载图像:
BackGround = Background('background_image.png', [0,0])
Run Code Online (Sandbox Code Playgroud)
你还需要while循环中的这两行:
screen.fill([255, 255, 255])
screen.blit(BackGround.image, BackGround.rect)
Run Code Online (Sandbox Code Playgroud)
这将使您的屏幕填满白色并将背景图像放在它上面,但在其他精灵和物体下面.
建议:
你应该为你的另一个精灵创建另一个类(可能是图像没有出现的原因).一个例子可能是:
class Ship(pygame.sprite.Sprite):
def __init__(self, image_file, speed, location):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(image_file)
self.rect = self.image.get_rect()
self.rect.left, self.rect.top = location
Run Code Online (Sandbox Code Playgroud)
然后你可以像这样"激活"它:
ship = Ship("images\ship.png", [a, b])
Run Code Online (Sandbox Code Playgroud)
选择的坐标a和b.然后你可以像这样将图像blit到屏幕上但是在你的后台blit声明之后:
screen.blit(ship.image, ship.rect)
Run Code Online (Sandbox Code Playgroud)
我希望这可以帮助你!
对于背景我总是将图像设置为我的游戏窗口的大小或更小,然后在显示所有图像之前,我将该图像blit为0,0.
bg = pygame.image.load("bg.png")
#INSIDE OF THE GAME LOOP
gameDisplay.blit(bg, (0, 0))
#REST OF ITEMS ARE BLIT'D TO SCREEN.
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助.
首先,这一切都不起作用,因为你在导入 Pygame 后没有初始化它。此外,图片不会被加载,因为反斜杠表示转义序列。最后,你应该修复你的缩进。
import pygame
import sys
from pygame.locals import *
pygame.init() # initialize pygame
clock = pygame.time.Clock()
screen = pygame.display.set_mode((600,500))
# os.path.join properly forms a cross-platform relative path
# by joining directory names
bg = pygame.image.load(os.path.join("images", "space.png"))
pygame.mouse.set_visible(0)
ship = pygame.image.load(os.path.join("images", "ship.png"))
ship_top = screen.get_height() - ship.get_height()
ship_left = screen.get_width()/2 - ship.get_width()/2
screen.blit(ship, (ship_left,ship_top))
shot = pygame.image.load(os.path.join("images", "space.png"))
shoot_y = 0
pygame.display.set_caption('galaxy invaders')
# fix indentation
while True:
clock.tick(60)
screen.blit(bg, (0,0))
x,y = pygame.mouse.get_pos()
screen.blit(ship, (x-ship.get_width()/2, ship_top))
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == MOUSEBUTTONDOWN:
shoot_y = 500
shoot_x = x
if shoot_y > 0:
screen.blit(shot, (shoot_x, shoot_y))
shoot_y -= 10
pygame.display.update()
Run Code Online (Sandbox Code Playgroud)