English 中文(简体)
如果对象的坐标为( 0,0),如何处理在平局中的对象对象的环环绕矩形 。
原标题:How to deal with a wrap-around rectangle for an object in pygame if the object s coordinates are at (0,0)

我有一些代码可以环绕一个矩形 围绕一些文字在游戏中:

import pygame as pg
font = pg.font.SysFont("Comic Sans MS", 30)

def wrapRect(obj, color= Black ):
    rect = obj.get_rect()
    pg.draw.rect(obj, color, rect.inflate(10,10), 2)
    return obj

playButton = wrapRect(font.render( Play , True,  Black ))
screen.blit(playButton, (200, 170))

我做了圆形矩形以环绕作为参数给定对象的矩形。

此代码不起作用, 因为 Blit () 函数在将文本置于想要的坐标之前, 我假定播放按钮默认坐标是 0, 0, 所以当我使用充气() 函数时, 它使这些坐标为负。 结果, 周围矩形没有显示 。 我如何让折叠矩形考虑到我可能想要将对象重新定位到( 0, 0) 以外的事实?

问题回答

您可以将“ pg.draw.rect( 对象、 颜色[ r, g, b] 、 矩形[ x, y, w, h], 宽度) ” 的代码线解释为在对象的( x,y, w, h) 位置上绘制一个( w) 大小、 (r, g, b) 颜色、 ( width) 大小边框矩形 < 坚固 > 。

是的,它是在对象的( x,y) 位置上, 这意味着表面有一个与屏幕分开的坐标系统。 就像你看不到屏幕外画的部分一样, 你看不到表面外画的部分( 即负坐标 ) 。

在“ wrapRect” 函数中,您会将正则表面的正方形膨胀,这将使矩形的坐标变为负。您还会将边框宽度设置为 2,这样您就看不到文字表面上绘制的任何东西。如果您将宽度设定为 0,这意味着填充,您就会看到文字表面被填充为颜色。

要解决这个问题,您可以创建一个矩形表面(背面),然后在矩形表面绘制边框和文字表面。

def wrapRect(obj,color= Black ):
    obj_rect=obj.get_rect()
    inflated_rect=obj_rect.inflate((10,10))
    nw,nh=inflated_rect.size
    # create a new surface called rect_surf
    # you can think of it as the background of the button
    rect_surf=pygame.Surface((nw,nh))
    rect_surf.fill( White )
    # draw the border on rect_surf
    pygame.draw.rect(rect_surf,color,(0,0,nw,nh),2)
    # draw obj on the center of rect_surf
    obj_rect.center=nw//2,nh//2
    rect_surf.blit(obj,obj_rect)
    return rect_surf

现在,您不仅可以在 0,0 处,还可以在其他坐标处绘制按钮。 (只需更改“ 屏幕. blit () ” 的坐标)





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签