Pygame 无法加载背景图片?
Pygame can't get background image to load?
我得到的只是黑屏。
不确定我是否在目录中找不到图像,或者如果我没有正确调用某些图像...但它没有给我错误,所以我不确定该怎么做。
import pygame
# Intialize the pygame
pygame.init()
# Create the screen
screen = pygame.display.set_mode((300, 180))
#Title and Icon
pygame.display.set_caption("Fighting Game")
# Add's logo to the window
# icon = pygame.image.load('')
# pygame.display.set_icon(icon)
# Game Loop
running = True
while running:
# screen.fill((0, 0, 0))
# screen.blit(BackGround.image, BackGround.rect)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
class Background(pygame.sprite.Sprite):
def __init__(self, image_file, location):
pygame.sprite.Sprite.__init__(self) #call Sprite initializer
self.image = pygame.image.load("images/image.png")
self.rect = self.image.get_rect(300,180)
self.rect.left, self.rect.top = location
BackGround = Background('image.png', [0,0])
screen.fill((0, 0, 0))
screen.blit(BackGround.image, BackGround.rect)
您必须 blit
主应用程序循环中的图像,并且必须通过 pygame.display.flip
更新显示。
此外,没有必要将任何参数传递给self.image.get_rect()
。无论如何, get_rect()
的参数必须是关键字参数。
您可以做的是通过关键字参数 topleft
.
设置位置
class Background(pygame.sprite.Sprite):
def __init__(self, image_file, location):
pygame.sprite.Sprite.__init__(self) #call Sprite initializer
self.image = pygame.image.load("images/image.png")
self.rect = self.image.get_rect(topleft = location)
BackGround = Background('image.png', [0,0])
# Game Loop
running = True
while running:
# screen.fill((0, 0, 0))
# screen.blit(BackGround.image, BackGround.rect)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
#screen.fill((0, 0, 0)) # unnecessary because of the background image
screen.blit(BackGround.image, BackGround.rect)
pygame.display.flip()
注意,主应用程序循环必须:
- 处理事件
- 清除显示或 blit 背景图像
- 绘制场景
- 更新显示
我得到的只是黑屏。 不确定我是否在目录中找不到图像,或者如果我没有正确调用某些图像...但它没有给我错误,所以我不确定该怎么做。
import pygame
# Intialize the pygame
pygame.init()
# Create the screen
screen = pygame.display.set_mode((300, 180))
#Title and Icon
pygame.display.set_caption("Fighting Game")
# Add's logo to the window
# icon = pygame.image.load('')
# pygame.display.set_icon(icon)
# Game Loop
running = True
while running:
# screen.fill((0, 0, 0))
# screen.blit(BackGround.image, BackGround.rect)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
class Background(pygame.sprite.Sprite):
def __init__(self, image_file, location):
pygame.sprite.Sprite.__init__(self) #call Sprite initializer
self.image = pygame.image.load("images/image.png")
self.rect = self.image.get_rect(300,180)
self.rect.left, self.rect.top = location
BackGround = Background('image.png', [0,0])
screen.fill((0, 0, 0))
screen.blit(BackGround.image, BackGround.rect)
您必须 blit
主应用程序循环中的图像,并且必须通过 pygame.display.flip
更新显示。
此外,没有必要将任何参数传递给self.image.get_rect()
。无论如何, get_rect()
的参数必须是关键字参数。
您可以做的是通过关键字参数 topleft
.
class Background(pygame.sprite.Sprite):
def __init__(self, image_file, location):
pygame.sprite.Sprite.__init__(self) #call Sprite initializer
self.image = pygame.image.load("images/image.png")
self.rect = self.image.get_rect(topleft = location)
BackGround = Background('image.png', [0,0])
# Game Loop
running = True
while running:
# screen.fill((0, 0, 0))
# screen.blit(BackGround.image, BackGround.rect)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
#screen.fill((0, 0, 0)) # unnecessary because of the background image
screen.blit(BackGround.image, BackGround.rect)
pygame.display.flip()
注意,主应用程序循环必须:
- 处理事件
- 清除显示或 blit 背景图像
- 绘制场景
- 更新显示