如何从矩形/直线获取表面 [英] How to get the surface from a rect/line

查看:58
本文介绍了如何从矩形/直线获取表面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我制作的打砖块中找到一条线与一块砖相撞的点.我发现的最合乎逻辑的方法是从行中获取掩码并在返回点时使用碰撞掩码.正如我尝试过的那样:

I am trying to find the point where a line collides with a brick in the arkanoid that i am making. The most logical way i found is getting the mask from the line and use collidemask as it returns the point. Well as i tried with this:

linemask = pygame.mask.from_surface(pygame.draw.line(screen, (0,0,0), bola.line[0], bola.line[1], 2)) 

它给了我这个错误:

TypeError: 参数 1 必须是 pygame.Surface,而不是 pygame.Rect

TypeError: argument 1 must be pygame.Surface, not pygame.Rect

意味着输入(在本例中为线)不能是矩形,而必须是曲面.您知道如何从矩形或任何替代解决方案中获取表面吗?

meaning that the input(in this case the line) can't be a rect but needs to be a surface. Do you know how to get the surface from a rect or any alternative solution ?

推荐答案

pygame.draw.line 在 Surface 上绘制并以 Rect 对象的形式返回受影响的区域.

pygame.draw.line draws on a Surface and returns the affected area in form of a Rect object.

你画的表面是screen.所以它是 screen 你想从它创建一个遮罩.或者,创建一个使用 pygame.draw 的新 Surface 并从中创建蒙版.或者从屏幕的次表面创建一个遮罩(这样你就不必从整个屏幕创建一个遮罩),像这样:

The Surface you drew on is screen. So it's screen you want to create a mask from. Alternatively, create a new Surface that you use pygame.draw on and create a mask from it. Or create a mask from the subsurface of the screen (so you don't have to create a mask from the whole screen), like this:

rect = pygame.draw.line(screen, (0,0,0), bola.line[0], bola.line[1], 2)
surface = screen.subsurface(rect)
mask = pygame.mask.from_surface(surface)

这篇关于如何从矩形/直线获取表面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆