Python 如何在pygame中添加背景图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28005641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add a background image into pygame?
提问by jy95
new to pygame just wondering how i would go about adding a background image into the game itself? this is my code so far, i've been using the bg as a way to import my image but the py file itself refuses to load up.
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()
回答by Malik Brahimi
First of all, none of this will work because you did not initialize Pygame after importing it. Also, the pictures won't be loaded because the backslash indicates an escape seqeunce. Lastly, you should fix your indentation.
首先,这一切都不起作用,因为您在导入 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()
回答by Anthony Pham
This problem can be easily solved. You will need an image the size of your screen for your background. Please remember to add pygame.init()
at the beginning of your game to be able to start it and its abilities. A function for this picture can be used like this:
这个问题很容易解决。您将需要一张与屏幕大小相同的图像作为背景。请记住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
This will allow the program to load your image through this function when you call it like this:
当您像这样调用它时,这将允许程序通过此函数加载您的图像:
BackGround = Background('background_image.png', [0,0])
And you will also need these two lines in your while
loop:
您还需要在while
循环中使用这两行:
screen.fill([255, 255, 255])
screen.blit(BackGround.image, BackGround.rect)
This will fill your screen white and put the background image over it but under your other sprites and objects.
Suggestions:You should make another class for your other sprite (maybe the reason why the image is not appearing). An example could be like:
这将填充您的屏幕白色并将背景图像放在它上面但在您的其他精灵和对象下面。
建议:你应该为你的另一个精灵创建另一个类(可能是图像没有出现的原因)。一个例子可能是这样的:
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
You could then "activate" it like this:
然后你可以像这样“激活”它:
ship = Ship("images\ship.png", [a, b])
Select the coordinates for a
and b
. You can then blit the image on to the screen like this but after your background blit statement:
选择的坐标a
和b
。然后你可以像这样将图像 blit 到屏幕上,但在你的背景 blit 语句之后:
screen.blit(ship.image, ship.rect)
I hope this helps you!
我希望这可以帮助你!
回答by HKVariant
For background I always make an image the size of my game window or smaller then before all of the images are displayed, I blit that image to 0,0.
对于背景,我总是制作一个与我的游戏窗口大小相同或更小的图像,然后在显示所有图像之前,将该图像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.
Hope this helps.
希望这可以帮助。