我正在关注由Clear Code发布的YouTube上的Pygame教程。到目前为止,它已经走得不错,但是我遇到了视频中的演示与代码行为之间的不一致之处,我很确定我正在做教程的准确指示,但结果是不同的。
我试图在矩形周围绘制边框,该矩形是从包含一些文本的表面创建的。
test_font = pygame.font.Font('font\Pixeltype.ttf',50)
score_surf = test_font.render('My Game', False, 'Black')
score_rect = score_surf.get_rect(center = (400,50))
#Later in the main loop
pygame.draw.rect(screen,'Pink',score_rect)
pygame.draw.rect(screen,'Pink',score_rect, 6)
我的理解是,第一个pygame.draw.Rect应该在Score_Rect区域上进行颜色,第二个则应创建一个略微超出Score_Rect区域的边框。这应该在文本上留下一些粉红色的信息。在视频中,我可以看到这种情况发生了,但是当我在系统上运行代码时,第二个pygame.draw。指定边框宽度似乎没有任何效果。
我已经通过删除第一个pygame.draw.Rect进行了一些实验,这很大程度上可以正如预期的那样,我的文本周围有一个粉红色的矩形边框,但是该边框严格score_rect是score_rect。
根据指定宽度参数的PYGAME文档,应导致边框稍微走出Score_Rect。但是我没有看到这种行为。
链接到我正在阅读的Pygame文档
链接到我关注的YouTube视频,以及视频中的位置
https://youtu.be/ay9mnq4x3zk?t
我的软件版本
Pygame:2.1.2
Python:3.10.2
OS Windows 10
任何帮助将不胜感激。
I'm following a Pygame tutorial on YouTube published by Clear Code. So far it's gone well but I've run into an inconsistency between the demo on the video and the behaviour of my code, I'm pretty sure I'm doing exactly what the tutorial instructs, but my results are different.
I'm attempting to draw a border around a rectangle, the rectangle was created from a surface that contains some text as follows.
test_font = pygame.font.Font('font\Pixeltype.ttf',50)
score_surf = test_font.render('My Game', False, 'Black')
score_rect = score_surf.get_rect(center = (400,50))
#Later in the main loop
pygame.draw.rect(screen,'Pink',score_rect)
pygame.draw.rect(screen,'Pink',score_rect, 6)
My understanding is that the first pygame.draw.rect should colour in the area of the score_rect, and the second should create a border that goes slightly outside the area of the score_rect. This should leave a bit of pink visible all the way around the text. In the video I can see this happening, but when I run the code on my system the second pygame.draw.rect that specifies a border width doesn't seem to have any effect.
I've experimented a bit by removing the first pygame.draw.rect, this works mostly as expected I get a pink rectangular border around my text, but this border is strictly insisde the score_rect.
According to the Pygame documentation specifying the width argument should cause the border to go slightly outside the score_rect. However I'm not seeing this behaviour.
Link to Pygame documentation I'm reading
https://www.pygame.org/docs/ref/draw.html#pygame.draw.rect
Link to Youtube video I'm following, and location in video
https://youtu.be/AY9MnQ4x3zk?t=4879
Edit: Sorry I forgot to note my software versions
Pygame: 2.1.2
Python: 3.10.2
OS Windows 10
Any help would be appreciated.
发布评论
评论(1)
我遇到有人在其他网站上问这个确切的问题(不是很容易搜索,请放心),所以我主要是复制粘贴我的最后一个答案。
在最近版本的Pygame中,Draw.Rect更改为“实际矩形”。在许多情况下,这具有看起来更清洁的优势,现在对它们的算法更快,有助于性能。
实际上,我与一个与您完全相同的问题(例如来自同一教程)的人进行了交谈,我们决定使用rect.inflate()调用矩形在文本后面绘制之前将矩形扩展出来。
例如,您可以执行
pygame.draw..rect(屏幕,'pink',score_rect.inflate(10,10))
之类的事情而不是两个Clear的Rect呼叫。
或者,如果要保留轻微的角落,则可以进行
pygame.draw..rect(屏幕,'pink',score_rect.inflate(10,10),border_radius = 3)
,所以这只是使用返回rect.inflate调用的值,而不是原始rect本身。膨胀需要X边距和AY边缘,并以这些数量返回更大/较小的矩形,但仍以同一位置为中心。
I've come across people asking this exact question on other sites (not in a way that's very searchable, don't worry), so I'm mainly copy pasting my last answer.
In a recent version of pygame, draw.rect was changed to give "actual rectangles." This has an advantage of looking cleaner in many situations, and the algorithm for them is now significantly faster, helping performance.
I actually talked to someone with your exact same issue (like coming from the same tutorial) on discord, and we decided to use a rect.inflate() call to grow the rectangle out before drawing it behind the text.
For example, you could do something like
pygame.draw.rect(screen, 'Pink', score_rect.inflate(10,10))
Instead of both Clear's rect calls.
Or if you want to preserve the slight corner rounding you could do
pygame.draw.rect(screen, 'Pink', score_rect.inflate(10,10), border_radius=3)
So this just uses the return value of a Rect.inflate call instead of the original Rect itself. Inflate takes an x margin and a y margin, and returns a Rect larger/smaller by those amounts, but still centered in the same location.