如何在 pygame 中显示一个带有可移动透明圆圈的大黑色矩形? [英] How do I display a large black rectangle with a moveable transparent circle in pygame?

查看:55
本文介绍了如何在 pygame 中显示一个带有可移动透明圆圈的大黑色矩形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

那个问题不是很清楚.

本质上,我正在尝试制作一个多人吃豆人游戏,玩家(当扮演幽灵时)只能看到他们周围一定的半径.我最好的猜测是有一个覆盖整个迷宫的矩形,然后以某种方式切出一个以鬼魂的矩形为中心的圆.但是,我不确定如何在 pygame 中完成最后一部分.

我只想补充一下,如果它甚至可以在 pygame 中实现,那么像素化圆而不是平滑圆将是理想的选择,但这不是必需的.

有什么建议吗?干杯.

解决方案

如果你只想在一个圆形区域内显示场景,那么你可以这样做:

  • 清除显示.

  • 将绘图区域限制在圆形区域周围的正方形区域

  • 绘制场景

  • 在正方形区域的顶部绘制一个透明的圆形表面

可以在运行时轻松创建圆曲面.定义圆形区域的半径 (areaRadius).创建一个正方形

That question wasn't very clear.

Essentially, I am trying to make a multi-player Pac-Man game whereby the players (when playing as ghosts) can only see a certain radius around them. My best guess for going about this is to have a rectangle which covers the whole maze and then somehow cut out a circle which will be centred on the ghost's rect. However, I am not sure how to do this last part in pygame.

I'd just like to add if it's even possible in pygame, it would be ideal for the circle to be pixelated and not a smooth circle, but this is not essential.

Any suggestions? Cheers.

解决方案

If you only want to show the scene inside a circular area, then you can do the following:

  • Clear the display.

  • Limit the drawing region to a square area around the circular area

  • Draw the scene

  • Draw a transparent circle surface on top of the square area

The circle surface can be created with ease on runtime. Define the radius of the circular area (areaRadius). Create a square pygame.Surface with the doubled radius of the circular area. Fill it with opaque black and draw a transparent circle in the middle:

circularArea = pygame.Surface((areaRadius*2, areaRadius*2), pygame.SRCALPHA)
circularArea.fill((0, 0, 0, 255))
pygame.draw.circle(circularArea, (0,0,0,0), (areaRadius, areaRadius), areaRadius)

The drawing region of a surface can be limited by .set_clip(). Calling the function with the parameter None removes the clipping area. In the following screen is the surface which represents the window and areaCenter is the center of the circular area on the screen:

while run:

    # [...]

    # remove clipping region and clear the entire screen
    screen.set_clip(None)
    screen.fill(0)

    # set the clipping region to square around the circular area
    areaTopleft = (areaCenter[0]-areaRadius, areaCenter[1]-areaRadius)
    clipRect = pygame.Rect(areaTopleft, (areaRadius*2, areaRadius*2))
    screen.set_clip(clipRect)

    # draw the scene
    # [...]

    # draw the transparent circle on top of the rectangular clipping region
    screen.blit(circularArea, areaTopleft)  

    # clear the dripping region and draw all the things which should be visible in any case
    screen.set_clip(None)
    # [...]

    pygame.display.flip()

这篇关于如何在 pygame 中显示一个带有可移动透明圆圈的大黑色矩形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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