Python Pygame无法正确显示图像
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python Pygame无法正确显示图像,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1881字,纯文字阅读大概需要3分钟。
内容图文
![Python Pygame无法正确显示图像](/upload/InfoBanner/zyjiaocheng/691/deeaa67454564084be14d5d6ef2bc874.jpg)
我是Python的新手,我从Eric Matthes的“ Python崩溃课程”开始学习.我正处于Pygame一章的开头,并且遵循代码,但是我加载的图像总是看起来损坏,我也不知道为什么.代码来自本书.第一个文件:
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版本.问题不在于您的代码,而是与Mac OS上存在错误的SDL的较新版本有关.
要解决此问题,您需要将SDL降级到1.2版之前的版本(该版本在那里,忘记了确切的版本),或者在其他操作系统上工作.
真烦人..我最终在Mac上安装了virtualbox并运行Linux只是为了能够编码pygame!
内容总结
以上是互联网集市为您收集整理的Python Pygame无法正确显示图像全部内容,希望文章能够帮你解决Python Pygame无法正确显示图像所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。