Skip to main content
 首页 » 编程设计

python之Pygame 中的白盒

2024年12月31日10pengyingh

非常不言自明。我的 Sprite 没有出现,只有一个白框。我正在使用 Ubuntu 18.04.1 LTS 和 pygame 版本 1.9.3 如果您想知道的话,我正在使用第 107 页上 Simon Monk 编写的树莓派编程中的代码

import pygame 
from pygame.locals import * 
from sys import exit 
 
spoon_x = 300 
spoon_y = 300 
 
pygame.init() 
 
screen = pygame.display.set_mode((600,400)) 
pygame.display.set_caption('Raspberry Catching') 
 
spoon = pygame.image.load('/home/john/PycharmProjects/pygame/spoon.png').convert() 
 
while True: 
    for event in pygame.event.get(): 
        if event.type == QUIT: 
                exit() 
 
    screen.fill((255,255,255)) 
    spoon_x, ignore = pygame.mouse.get_pos() 
    screen.blit(spoon, (spoon_x, spoon_y)) 
    pygame.display.update() 

spoon test

请您参考如下方法:

您的图像非常大并且包含很多白色区域,因此如果您在 y 坐标 300 处对其进行 blit,您将只能看到顶部的一些白色区域,而勺子将位于屏幕下方的某个位置。如果将 spoon_y 更改为 -300,您可以看到勺子。

我建议裁剪(去除勺子周围的大部分白色区域)并在图形编辑器中缩放图像。

你也可以使用 pygame.Surface.subsurface在 pygame 中裁剪表面:

spoon_cropped = spoon.subsurface((295, 357, 1208, 273)) 

或者创建另一个表面并将第一个表面 blit 到它上面:

spoon_cropped = pygame.Surface((1208, 273)) 
# The third argument is the area that comprises the spoon. 
spoon_cropped.blit(spoon, (0, 0), (295, 357, 1208, 273))