python - Pygame 中的白盒

标签 python python-3.x pygame

非常不言自明。我的 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))

关于python - Pygame 中的白盒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52167667/

相关文章:

检查 kwargs 中的所有值是否都是字符串 "false"的 Pythonic 方法

python - 创建具有条件的多列

python - 无论如何,我可以在 Google colaboratory 中下载该文件吗?

python - 在pygame中用鼠标获取世界坐标

python - 获取通过 os.system() 命令执行的命令的输出

python - Pandas:如何重用数据中的索引信息?

python-3.x - 如何在 Pandas 数据框列中找到第一个连续值并删除该行?

python - 我试图向我的基于 pygame 的游戏添加简单的碰撞,但它不起作用

python - 无法在 pygame 中改变颜色?

java - 如何将 python dict 对象转换为 java 等效对象?