如何在Pygame中制作圆形表面 [英] how to make circular surface in pygame
问题描述
我需要创建一个具有边界圆的曲面.在该表面上绘制的任何内容在该边界圆之外均不可见.我曾尝试使用遮罩,地下,srcalpha等,但似乎无济于事.
我的尝试:
w = ss.get_width()h = ss.get_height()透明=(255,255,255,0)不透明=(0,0,0,225)作物= pygame.Surface((w,h),pygame.SRCALPHA,ss)crop.fill(透明)c =舍入(w/2),舍入(h/2)r = 1pygame.gfxdraw.aacircle(crop,* c,r,OPAQUE)pygame.gfxdraw.filled_circle(crop,* c,r,OPAQUE)ss = crop.subsurface(crop.get_rect())App.set_subsurface(self,ss)
以后...
self.ss.fill(黑色)self.ss.blit(self.background,来源)
背景是正方形图像.应该将其裁剪为圆形并在屏幕上呈现
基于Rabbid76的注释的解决方案:
def draw_scene(self,temp = None):如果temp为None:temp = self.ss#1.在与窗口大小相同的表面上绘制所有内容(背景和场景).大小= temp.get_size()temp = pygame.Surface(大小)self.draw_cropped_scene(临时)#2.用白色圆圈创建表面.self.cropped_background = pygame.Surface(大小,pygame.SRCALPHA)self.crop()#3.在白色圆圈上涂抹前一个表面.self.cropped_background.blit(温度,来源,特殊标志= pygame.BLEND_RGBA_MIN)#4.把整个东西都抹在窗户上.self.ss.blit(self.cropped_background,来源)def draw_cropped_scene(自我,临时):App.draw_scene(自我,临时)
crop()的示例实现为:
def作物(个体):o,界限= self.boundsbounds = tr(bounds)#元组的圆形元素pygame.gfxdraw.aaellipse(self.cropped_background,* bounds,* bounds,OPAQUE)pygame.gfxdraw.filled_ellipse(self.cropped_background,* bounds,* bounds,OPAQUE)
背景是正方形图像.应该将其裁剪为圆形并在屏幕上呈现
您可以使用混合模式 BLEND_RGBA_MIN
(请参见
import pygamepygame.init()窗口= pygame.display.set_mode((250,250))背景= pygame.Surface(window.get_size())对于范围x中的x(5):对于范围(5)中的y:颜色=(255,255,255)如果(x + y)%2 == 0否则(255,0,0)pygame.draw.rect(背景,颜色,(x * 50,y * 50,50,50))大小= background.get_size()cropped_background = pygame.Surface(size,pygame.SRCALPHA)pygame.draw.ellipse(cropped_background,(255,255,255,255),(0,0,* size))cropped_background.blit(背景(0,0),special_flags = pygame.BLEND_RGBA_MIN)运行=真运行时:对于pygame.event.get()中的事件:如果event.type == pygame.QUIT:运行=错误window.fill(0)window.blit(cropped_background,(0,0))pygame.display.flip()
I need to create a surface that has a bounding circle. Anything drawn on that surface should not be visible outside that bounding circle. I've tried using masks, subsurfaces, srcalpha, etc., but nothing seems to work.
My attempt:
w = ss.get_width ()
h = ss.get_height ()
TRANSPARENT = (255, 255, 255, 0)
OPAQUE = ( 0, 0, 0, 225)
crop = pygame.Surface ((w, h), pygame.SRCALPHA, ss)
crop.fill (TRANSPARENT)
c = round (w / 2), round (h / 2)
r = 1
pygame.gfxdraw. aacircle (crop, *c, r, OPAQUE)
pygame.gfxdraw.filled_circle (crop, *c, r, OPAQUE)
ss = crop.subsurface (crop.get_rect ())
App.set_subsurface (self, ss)
Later...
self.ss.fill (BLACK)
self.ss.blit (self.background, ORIGIN)
The background is a square image. It should be cropped into the shape of a circle and rendered on screen
Solution based on notes from Rabbid76:
def draw_scene (self, temp=None):
if temp is None: temp = self.ss
# 1. Draw everything on a surface with the same size as the window (background and scene).
size = temp.get_size ()
temp = pygame.Surface (size)
self.draw_cropped_scene (temp)
# 2. create the surface with the white circle.
self.cropped_background = pygame.Surface (size, pygame.SRCALPHA)
self.crop ()
# 3. blit the former surface on the white circle.
self.cropped_background.blit (temp, ORIGIN, special_flags=pygame.BLEND_RGBA_MIN)
# 4. blit the whole thing on the window.
self.ss.blit (self.cropped_background, ORIGIN)
def draw_cropped_scene (self, temp): App.draw_scene (self, temp)
An example implementation of crop() is:
def crop (self):
o, bounds = self.bounds
bounds = tr (bounds) # round elements of tuple
pygame.gfxdraw. aaellipse (self.cropped_background, *bounds, *bounds, OPAQUE)
pygame.gfxdraw.filled_ellipse (self.cropped_background, *bounds, *bounds, OPAQUE)
The background is a square image. It should be cropped into the shape of a circle and rendered on screen
You can achieve this by using the blend mode BLEND_RGBA_MIN
(see pygame.Surface.blit
).
Create a transparent pygame.Surface
with the same size as self.background
. Draw a whit circle in the middle of the Surface and blend the background on this Surface using the blend mode BLEND_RGBA_MIN
. Finally you can blit it on the screen:
size = self.background.get_size()
self.cropped_background = pygame.Surface(size, pygame.SRCALPHA)
pygame.draw.ellipse(self.cropped_background, (255, 255, 255, 255), (0, 0, *size))
self.cropped_background.blit(self.background, (0, 0), special_flags=pygame.BLEND_RGBA_MIN)
self.ss.fill(BLACK)
self.ss.blit(self.cropped_background, ORIGIN)
Minimal example:
import pygame
pygame.init()
window = pygame.display.set_mode((250, 250))
background = pygame.Surface(window.get_size())
for x in range(5):
for y in range(5):
color = (255, 255, 255) if (x+y) % 2 == 0 else (255, 0, 0)
pygame.draw.rect(background, color, (x*50, y*50, 50, 50))
size = background.get_size()
cropped_background = pygame.Surface(size, pygame.SRCALPHA)
pygame.draw.ellipse(cropped_background, (255, 255, 255, 255), (0, 0, *size))
cropped_background.blit(background, (0, 0), special_flags=pygame.BLEND_RGBA_MIN)
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
window.fill(0)
window.blit(cropped_background, (0, 0))
pygame.display.flip()
See Also How to fill only certain circular parts of the window in pygame?
这篇关于如何在Pygame中制作圆形表面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!