【问题标题】:How to add a background image into pygame?如何在pygame中添加背景图片?
【发布时间】:2015-01-17 23:49:22
【问题描述】:

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

【问题讨论】:

    标签: python background pygame


    【解决方案1】:

    对于背景,我总是将图像设置为游戏窗口大小或更小,然后在显示所有图像之前,我将该图像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.
    

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      这个问题很容易解决。您将需要一个屏幕大小的图像作为背景。请记住在游戏开始时添加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
      

      当你像这样调用它时,这将允许程序通过这个函数加载你的图像:

      BackGround = Background('background_image.png', [0,0])
      

      您还需要在 while 循环中使用这两行代码:

      screen.fill([255, 255, 255])
      screen.blit(BackGround.image, BackGround.rect)
      

      这会将您的屏幕填充为白色并将背景图像放在它上面但在您的其他精灵和对象下面。
      建议: 你应该为你的另一个精灵制作另一个类(也许是图像没有出现的原因)。一个例子可能是:

      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
      

      然后你可以像这样“激活”它:

      ship = Ship("images\ship.png", [a, b])
      

      选择ab 的坐标。然后,您可以像这样将图像blit到屏幕上,但在您的背景blit语句之后:

      screen.blit(ship.image, ship.rect)
      

      希望对你有帮助!

      【讨论】:

        【解决方案3】:

        首先,这些都不起作用,因为您在导入 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()
        

        【讨论】:

        • 实际上,至少在 Windows 上,反斜杠似乎由 Pygame 处理。使用os 仍然更合适,并确保所有平台上的功能。
        • 如果后面没有字符,反斜杠也是一个续行序列;它也适用于我的 Mac。
        猜你喜欢
        • 2022-01-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多