Python 带有圆角的Pygame按钮,border_radius参数不起作用
我目前正在学习pygame。当用圆角编程矩形时,我遇到了一个问题 pygame.draw.rect() 画一个长方形 rect(表面、颜色、rect)->rect 矩形(表面、颜色、矩形、宽度=0,边界半径=0,边界半径=-1,边界半径=-1,边界半径=-1,边界半径=-1,边界半径=-1,边界半径=-1)-> 在给定曲面上绘制矩形 边框半径(int)--(可选)用于绘制圆角矩形。支持的范围为[0,min(高度、宽度)/2],0表示没有圆角的矩形 这是pygame官方文档的描述Python 带有圆角的Pygame按钮,border_radius参数不起作用,python,pygame,rounded-corners,rect,Python,Pygame,Rounded Corners,Rect,我目前正在学习pygame。当用圆角编程矩形时,我遇到了一个问题 pygame.draw.rect() 画一个长方形 rect(表面、颜色、rect)->rect 矩形(表面、颜色、矩形、宽度=0,边界半径=0,边界半径=-1,边界半径=-1,边界半径=-1,边界半径=-1,边界半径=-1,边界半径=-1)-> 在给定曲面上绘制矩形 边框半径(int)--(可选)用于绘制圆角矩形。支持的范围为[0,min(高度、宽度)/2],0表示没有圆角的矩形 这是pygame官方文档的描述 username
username_rect = pg.rect.Rect(screenwidth/2 - 1/8*screenwidth, screenheight*1/5, 1/4*screenwidth, username_title_render.get_height() + 10)
pg.draw.rect(screen, (0,0,0), username_rect,border_radius = 15)
这里是主要部分。但是我得到了这个错误信息:
TypeError: 'border_radius' is an invalid keyword argument for this function
我已经尝试添加一个“width=2”参数,但是它说参数太多了。不幸的是,采用其他数字也无济于事。
我目前正在使用pygame版本2.0.0.dev6(SDL2.0.10,python 3.8.1)
如果你能帮助我,我会非常高兴,提前谢谢你 该文档是针对2.0.0.dev7的,并且根据,
border\u radius
是在2.0.0.dev6发布后添加的。所以你想用一些还没出来的东西。您可以尝试从源代码构建并使用关键字(尽管稳定性不是一个承诺)。或者你可以尝试使用矩形和圆形的组合来达到效果。我遇到了这个问题,不得不自己做,我不妨分享一下
试试这些,一个画圆角矩形,另一个画带边框的圆角矩形。它们使用抗锯齿圆来绘制角点,因为否则它们看起来非常参差不齐:
import pygame.gfxdraw
def draw_rounded_rect(surface, rect, color, corner_radius):
''' Draw a rectangle with rounded corners.
Would prefer this:
pygame.draw.rect(surface, color, rect, border_radius=corner_radius)
but this option is not yet supported in my version of pygame so do it ourselves.
We use anti-aliased circles to make the corners smoother
'''
if rect.width < 2 * corner_radius or rect.height < 2 * corner_radius:
raise ValueError(f"Both height (rect.height) and width (rect.width) must be > 2 * corner radius ({corner_radius})")
# need to use anti aliasing circle drawing routines to smooth the corners
pygame.gfxdraw.aacircle(surface, rect.left+corner_radius, rect.top+corner_radius, corner_radius, color)
pygame.gfxdraw.aacircle(surface, rect.right-corner_radius-1, rect.top+corner_radius, corner_radius, color)
pygame.gfxdraw.aacircle(surface, rect.left+corner_radius, rect.bottom-corner_radius-1, corner_radius, color)
pygame.gfxdraw.aacircle(surface, rect.right-corner_radius-1, rect.bottom-corner_radius-1, corner_radius, color)
pygame.gfxdraw.filled_circle(surface, rect.left+corner_radius, rect.top+corner_radius, corner_radius, color)
pygame.gfxdraw.filled_circle(surface, rect.right-corner_radius-1, rect.top+corner_radius, corner_radius, color)
pygame.gfxdraw.filled_circle(surface, rect.left+corner_radius, rect.bottom-corner_radius-1, corner_radius, color)
pygame.gfxdraw.filled_circle(surface, rect.right-corner_radius-1, rect.bottom-corner_radius-1, corner_radius, color)
rect_tmp = pygame.Rect(rect)
rect_tmp.width -= 2 * corner_radius
rect_tmp.center = rect.center
pygame.draw.rect(surface, color, rect_tmp)
rect_tmp.width = rect.width
rect_tmp.height -= 2 * corner_radius
rect_tmp.center = rect.center
pygame.draw.rect(surface, color, rect_tmp)
def draw_bordered_rounded_rect(surface, rect, color, border_color, corner_radius, border_thickness):
if corner_radius < 0:
raise ValueError(f"border radius ({corner_radius}) must be >= 0")
rect_tmp = pygame.Rect(rect)
center = rect_tmp.center
if border_thickness:
if corner_radius <= 0:
pygame.draw.rect(surface, border_color, rect_tmp)
else:
draw_rounded_rect(surface, rect_tmp, border_color, corner_radius)
rect_tmp.inflate_ip(-2*border_thickness, -2*border_thickness)
inner_radius = corner_radius - border_thickness + 1
else:
inner_radius = corner_radius
if inner_radius <= 0:
pygame.draw.rect(surface, color, rect_tmp)
else:
draw_rounded_rect(surface, rect_tmp, color, inner_radius)
import pygame.gfxdraw
def draw_rounded_rect(曲面、矩形、颜色、角半径):
''画一个圆角矩形。
我希望这样:
pygame.draw.rect(曲面、颜色、矩形、边框半径=角半径)
但是这个选项在我的pygame版本中还不受支持,所以我们自己做吧。
我们使用抗锯齿圆使拐角更平滑
'''
如果矩形宽度小于2*角半径或矩形高度小于2*角半径:
raise VALUERROR(f“高度(矩形高度)和宽度(矩形宽度)必须大于2*角半径({角半径})”)
#需要使用抗锯齿圆绘制例程来平滑角点
pygame.gfxdraw.aacircle(曲面、矩形、左+角半径、矩形、上+角半径、角半径、颜色)
pygame.gfxdraw.aacircle(曲面,矩形右拐角半径-1,矩形顶部+拐角半径,拐角半径,颜色)
pygame.gfxdraw.aacircle(曲面,矩形左+角_半径,矩形下角_半径-1,角_半径,颜色)
pygame.gfxdraw.aacircle(曲面、矩形右角_半径-1、矩形下角_半径-1、角_半径、颜色)
pygame.gfxdraw.filled_圆(曲面、矩形、左+角半径、矩形、上+角半径、角半径、颜色)
pygame.gfxdraw.filled_圆(曲面、矩形右角半径-1、矩形顶部+角半径、角半径、颜色)
pygame.gfxdraw.filled_圆(曲面、矩形左+角_半径、矩形底角_半径-1、角_半径、颜色)
pygame.gfxdraw.filled_圆(曲面、矩形右角半径-1、矩形下角半径-1、角半径、颜色)
rect_tmp=pygame.rect(rect)
矩形tmp.width-=2*拐角半径
rect_tmp.center=rect.center
pygame.draw.rect(表面、颜色、矩形)
rect_tmp.width=rect.width
矩形tmp.height-=2*拐角半径
rect_tmp.center=rect.center
pygame.draw.rect(表面、颜色、矩形)
def绘制带边框的圆角矩形(曲面、矩形、颜色、边框颜色、角半径、边框厚度):
如果角半径小于0:
raise VALUERROR(f“边界半径({corner_radius})必须大于等于0”)
rect_tmp=pygame.rect(rect)
中心=矩形tmp.center
如果边界厚度:
如果是拐角半径嘿shynjax287!谢谢你快速而有用的回答。你完全是对的,我完全错过了。不幸的是,PyGame2.0.0.dev7还不起作用,我将尝试在角落使用圆圈。我遇到了这个问题,不得不自己做。试试下面我的答案中的例行公事。