- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在编写一个基本上使用 Sprite 的游戏,我在这段代码中使用了 Sprite 表,最终得到了
"invalid destination for blit"
错误。
class spritesheet:
def __init__(self, filename, cols, rows):
self.sheet = pygame.image.load(filename).convert_alpha()
self.cols = cols
self.rows = rows
self.totalCellCount = cols * rows
self.rect = self.sheet.get_rect()
w = self.cellWidth = self.rect.width / cols
h = self.cellHeight = self.rect.height / rows
hw, hh = self.cellCenter = (w / 2, h / 2)
self.cells = list([(index % cols * w, index / cols * h, w, h) for index in range(self.totalCellCount)])
self.handle = list([
(0,0), (-hw, 0), (-w, 0),
(0, -hh), (-hw, -hh), (-w, -hh),
(0, -h), (-hw, -h), (-w, -h),])
def draw(self, surface, cellIndex, x, y, handle = 0):
surface.blit(self.sheet, (x + self.handle[handle][0], y + self.handle[handle][1], self.cells[cellIndex]))
s = spritesheet('Number18.png', 6, 58)
CENTER_HANDLE = 4
Index = 0
#mainloop
run = True
while run:
s.draw(DS, Index % s.totalCellCount, HW, HH, CENTER_HANDLE)
Index +=1
pygame.draw.circle(DS, WHITE, (HW, HW), 2, 0)
pygame.display.update()
CLOCK.tick(FPS)
DS.fill(BLACK)
这基本上是我的全部代码,但我遇到了问题
surface.blit(self.sheet, (x + self.handle[handle][0], y + self.handle[handle][1], self.cells[cellIndex]))
不断地给出错误,这是blit
的无效目标位置,我还注意到我的索引也对其有影响,但我不知道该怎么办。
最佳答案
第二个(目标)参数为 pygame.Surface
必须是一个位置(元组 (x, y)
)或矩形(元组 (x, y, h, w)
)。
你要做的就是传递一个元组(x, y, list)
:
surface.blit(self.sheet,
(x + self.handle[handle][0], y + self.handle[handle][1], self.cells[cellIndex]))
如果你想通过位置(x + self.handle[handle][0], y + self.handle[handle][1])
以及宽度和高度来设置目的地self.cells[cellIndex]
,那么它必须是:
hdl = self.handle[handle]
cell = self.cells[cellIndex]
surface.blit(self.sheet, (x + hdl[0], y + hdl[1], cell[2], cell[3]))
关于python - blit 错误的目标位置无效,不知道如何处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55199591/
使用 pygame,是否会更有效率: 每帧从 spritesheet 的一部分中 Blit 一个单独的 sprite 在启动时,将每个 Sprite 从 spritesheet 传输到它们自己的表面,
阅读 pygame 教程 here ,你会发现这个例子:(箭头是我的) for o in objects: screen.blit(background, o.pos, o.pos) # Re
我正在为 WinCE 6.0 设备开发一个应用程序,其中需要旋转屏幕,因此我们使用典型的 ChangeDisplaySettingsEx() API。我也在 View 上画一些东西。 从 memDC
我想将用户输入到屏幕上的文本传输到屏幕上。每次用户按回车键时,键入的文本都会被传输到屏幕上。对于文本输入,我使用这个 [text_input 模块] ( https://github.com/Near
我得到了一个不断变化/更新的缓冲区,我需要将这个缓冲区的像素 blit 到屏幕上。对于我的测试代码,我读取了一个位图并将其存储到缓冲区中。问题是,我想在使用 OpenGL 将特定颜色 blit 到屏幕
给定一张图片,如何将其“包裹”在屏幕上? 例如,如果您将图像的 rect 样式对象设置在屏幕边缘下方 - 不可见的一半会 blit 到屏幕顶部。 imageRect.top=800 #Below
我正在尝试为被 blit 的文本创建打字机效果。所谓打字机效应,我的意思是我试图避免整个文本同时出现在屏幕上。相反,我试图让每个字母单独出现,在字符串中的下一个字符出现之前稍有延迟。 要注意的是我没有
我正在寻求实现一个管理 blit 队列的模块。有一个表面,该表面的部分(由矩形包围)被复制到表面内的其他地方: add_blt(rect src, point dst); 可以有任意数量的操作按顺序发
我正在尝试让我的网络摄像头通过 pygame 显示视频。这是代码: # import the relevant libraries import time import pygame import p
我正在尝试在我的 python 应用程序中实时绘制多个子图。理想情况下,我还应该能够在每个子图中绘制多条线,但为了简单起见,我假设每个子图中绘制一条线。为了有效地做到这一点(我正在寻找快速绘图),我试
我正在尝试将多采样场景渲染为纹理,这是我正在使用的代码。我得到一个黑屏。我在 init 结束时检查了 fbo 的完整性,他们报告两个 fbo 都是完整的。 void init_rendered_FBO
我有 2 个 FBO + MRT,它们有相同的附件(每个附件有 4 个颜色)。使用 glBlitFrameBuffer 对深度缓冲区和 one color_attachment 按预期工作。但是,当我
我知道这个话题经常出现,但经过多次尝试、搜索和放弃,我又把它带回到你身边。 我有一个类,其中包含一个 matplotlib 图形。在此图中,我想要一个文本,当用户按下某个键时,文本会更新为某些内容,而
我正在尝试在 python 中为 pygame 制作一个脚本,以绘制一个文本居中的按钮,但是当我 blit 到屏幕上时,它 blits 到我给它的 x 和 y,而不是按比例居中的位置。我希望能够将它集
我正在尝试使用 SDL 制作一个非常小且简单的片段。这个就像一个魅力: SDL_Window * window = SDL_CreateWindow("SDLTest", 0, 0, SCREEN_W
我正在尝试让我的网络摄像头通过 pygame 显示视频。这是代码: # import the relevant libraries import time import pygame import p
我正在编写一个基本上使用 Sprite 的游戏,我在这段代码中使用了 Sprite 表,最终得到了 "invalid destination for blit" 错误。 class spriteshe
如果任何对 pygames 有基本了解的人可以帮助我解决我目前面临的问题,那就太好了。对于你们这些绝对的神来说,应该不会太难。 我现在的代码遇到了问题。 我正在尝试制作一个简单的游戏,其中程序显示一个
我目前正在尝试再次对已绘制图像的一部分进行 blit。为此,我读到一个可以在使用 blit 时使用可选的第三个区域参数。我不知道为什么,但这个参数对我来说很奇怪。 据我了解,区域参数是(pos_x、p
我想将一个数组复制到另一个大小不同的数组。我想要这样的功能: blit(destimg,src,dstlocation) 例如 blit(zeros((7,7)),ones((3,3)),(4,4))
我是一名优秀的程序员,十分优秀!