python - 图像不会在 Pygame 中加载,但 Python IDLE 中没有错误
问题描述
无论我做什么,我的图像都不会加载到 pygame 中。我尝试使用带有正斜杠和反斜杠的绝对路径来制作图像。在 pygame 中,屏幕只是加载并且没有给我任何错误,但图像也没有加载。
这是代码:
import pygame
window = pygame.display.set_mode((1000,1000))
BGImage = pygame.image.load('Plat.jpg')
window.blit(BGImage(0,0))
Eggshell = (240,235,220)
vel = 15
x = 3
y = 450
width = 50
height= 60
isJump = False
jumpCount = 10
run = True
while run:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run == False
pressed = pygame.key.get_pressed()
if pressed[pygame.K_LEFT] and x > vel:
x-= vel
if pressed[pygame.K_RIGHT] and x < 920 :
x+=vel
if not (isJump):
if pressed[pygame.K_UP] and y > vel:
isJump = True
else:
if jumpCount >= -10:
neg = 1
if jumpCount < 0:
neg = -1
y -= (jumpCount ** 2) * 0.5 * neg
jumpCount -= 1
else:
isJump = False
jumpCount = 10
window.fill((0,0,0))
pygame.draw.rect(window,Eggshell,(x,y,width,height))
pygame.display.update()
pygame.quit()
解决方案
pygame.Surface
使用该fill
方法清除图像后,您需要将图像/ s blit到显示表面上。如果BGImage
覆盖了整个屏幕,则不需要在对图像进行 blit 之前填充屏幕。
window.fill((0, 0, 0))
window.blit(BGImage, (0, 0)) # Blit the image at the top left coords (0, 0).
我还建议使用该convert
方法转换图像(或者convert_alpha
如果图像具有透明部分),因为这将大大提高 blit 性能。
BGImage = pygame.image.load('Plat.jpg').convert()
推荐阅读
- c# - 如何在 ASP.NET MVC C# 项目的新选项卡中打开 PowerPoint 演示文稿
- java - 更改 sharedpreferences 数据后如何自动更改 android menuItem 数据?
- python - 如何对密码实施 3-try 授权?
- css - 如何使用css在悬停时翻转div
- node.js - 在 Typescript 中为同步函数添加超时
- python-3.x - 证书验证失败:无法获取本地颁发者证书 (_ssl.c:1051) Python
- c# - NullReferenceException 但我无法确定未分配的对象
- laravel - Laravel Eloquent:具有 3 级深度的多个嵌套关系
- asp.net - 有没有办法保存 Visual Studio .net Web 应用程序,以便旧版本的 Visual Studio 能够运行该项目?
- c++ - 未指定的常量表达式值和 ODR