2

不言自明。我的精灵没有出现,只有一个白框。我正在使用 Ubuntu 18.04.1 LTS 和 pygame 版本 1.9.3 如果您想知道的话,我正在使用 Simon Monk 在第 107 页的 Programming the raspberry pi 中的代码

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()

勺子 测试

4

1 回答 1

1

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

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

您还可以使用pygame.Surface.subsurface在 pygame 中裁剪表面:

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

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

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))
于 2018-09-04T14:59:32.567 回答