Python Pygame不';无法正确显示图像
我是Python新手,从Eric Matthes的“Python速成课程”开始学习。我在Pygame一章的开头,我遵循代码,但我加载的图像总是看起来受损,我不知道为什么。代码来自于书中。第一个文件:Python Pygame不';无法正确显示图像,python,image,pygame,bmp,Python,Image,Pygame,Bmp,我是Python新手,从Eric Matthes的“Python速成课程”开始学习。我在Pygame一章的开头,我遵循代码,但我加载的图像总是看起来受损,我不知道为什么。代码来自于书中。第一个文件: import pygame class Ship(): def __init__(self, screen): """Initialize the ship and set its starting position.""" # Lo
import pygame
class Ship():
def __init__(self, screen):
"""Initialize the ship and set its starting position."""
# Load the ship image and get its rect.
self.image = pygame.image.load('ship.bmp')
self.screen = screen
self.rect = self.image.get_rect()
self.screen_rect = screen.get_rect()
# Start each new ship at the bottom center of the screen.
self.rect.centerx = self.screen_rect.centerx
self.rect.bottom = self.screen_rect.bottom
def blitme(self):
self.screen.blit(self.image, self.rect)
第二个文件:
import sys
import pygame
from settings import Settings
from ship import Ship
def run_game():
# Initialize game and create a screen object.
pygame.init()
ai_settings = Settings()
screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height))
pygame.display.set_caption("Alien Invasion")
ship = Ship(screen)
bg_color = (230, 230, 230)
# Start the main loop for the game.
while True:
# Watch for keyboard and mouse events.
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# Make the most recently drawn screen visible.
screen.fill(ai_settings.bg_color)
ship.blitme()
pygame.display.flip()
run_game()
设置文件:
class Settings():
"""A class to store all settings for Alien Invasion."""
def __init__(self):
"""Initialize the game's settings."""
# Screen settings
self.screen_width = 800
self.screen_height = 600
self.bg_color = (230, 230, 230)
我的bmp看起来是这样的:
我尝试添加不同的图像,但没有成功:
我怎样才能修好它 我想你是在Mac电脑上,有一个相对较新的SDL版本。问题不在于您的代码,而在于较新版本的SDL在Mac OS上存在缺陷 要解决这个问题,您需要将SDL降级到1.2版之前的版本(就在那里,忘记了确切的版本),或者在不同的操作系统上工作
这很烦人。。我最终在Mac上安装virtualbox并运行Linux,只是为了能够编写pygame 难道你不需要在你的图像上应用
convert()
吗?我不知道,书中的代码中没有这样的东西。我怎样才能用它修改我的代码呢?self.image=pygame.image.load('ship.bmp').convert()
-只要在末尾用convert()
试试就行了。没有任何变化,仍然是相同的变形图像。谢谢你,我想就是这样了,我可以在尝试修复代码的过程中挠头好长时间了。麦克和队长在这里,还有队长!如果换成其他方式对你有效,请告诉我。顺便说一句,这是我的消息来源。不幸的是,我无法降级SDL,所以我不能100%地说这就是问题所在,尽管很有可能。