pygame 定义不同的位置顺序产生不同的结果 [英] pygame define different position order make the different result
问题描述
我定义了对象的不同位置顺序,然后我发现它出错了.
我希望我的
button_surface
位于screen
的中心,所以我定义了self.button_surface_rect.center = self.setting.screen_rect.center
然后在
screen
的中心制作text_surface_rect
self.text_surface_rect.center = self.setting.screen_rect.center
将
text_rect
放在text_surface
的中心self.text_rect.center = self.text_surface_rect.center
当我 blit 屏幕时,我的 text_surface 上没有文字,为什么?
代码如下:
#!/usr/bin/python导入系统,操作系统导入pygame类设置():def __init__(self,width,height):self.w=宽度self.h=身高self.flag=pygame.RESIZABLEself.color=(255,255,255)self.screen=pygame.display.set_mode((self.w,self.h),self.flag)self.screen_rect=self.screen.get_rect()pygame.display.set_caption("哇哈哈")类按钮():def __init__(self,setting,text):self.setting = 设置self.text_color=(0,0,255)self.button_color=(0,100,100)self.rect=pygame.Rect(0,0,400,100)self.rect.center = self.setting.screen_rect.centerself.text=pygame.font.Font(None,80).render(text,True,self.text_color)self.text_surface=pygame.Surface((400,100))self.text_surface.set_colorkey((0,0,0))self.button_surface=pygame.Surface((400,100))self.button_surface.set_alpha(128)self.button_surface_rect = self.button_surface.get_rect()self.button_surface_rect.center = self.setting.screen_rect.center'''当我定义这个时,我看不到文本'''self.text_surface_rect = self.text_surface.get_rect()self.text_surface_rect.center = self.setting.screen_rect.centerself.text_rect = self.text.get_rect()打印(self.text_rect)打印(self.text_surface_rect)self.text_rect.center = self.text_surface_rect.center打印(self.text_rect)def blit_button(self):self.button_surface.fill(self.button_color)self.setting.screen.blit(self.button_surface,self.button_surface_rect)self.text_surface.blit(self.text,self.text_rect)self.setting.screen.blit(self.text_surface,self.button_surface_rect)定义游戏():pygame.init()设置=设置(1200,800)按钮=按钮(设置,'播放')为真:对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:系统退出()setting.screen.fill((255,0,0))button.blit_button()pygame.display.flip()游戏()
self.setting.screen_rect.center
是大屏幕的中心.但是文本不是 blit
到屏幕上,而是在小的 text_surface
上 blit.
self.text_surface.blit(self.text,self.text_rect)
text_surface
的大小是(400, 100),但是self.text_rect
的中心是(600, 400).
如果你这样做
<块引用>self.text_surface_rect.center = self.setting.screen_rect.center[...]self.text_rect.center = self.text_surface_rect.center
那么 self.text_surface_rect
的位置远远超出 self.text_surface
的范围.屏幕的中心 (self.setting.screen_rect.center
) 是 (600, 400).但是text_surface的大小是(400, 100).
+--------------+|文字表面|+--------------+尺寸:(400, 100)+-----------+|text_rect|中心:(600, 400)+-----------+
<小时>
注意text_surface_rect
是(400, 350, 400, 100)
,但是text_surface
是一个Surface 对象并且没有它刚刚拥有的位置大小为 (400, 100).你可以把它想象成一个矩形 (0, 0, 400, 100).
文本在 text_surface
上是 blit
而不是在 text_surface_rect
上.
您必须计算相对于 self.button_surface_rect
的位置,而不是 self.setting.screen
:
self.text_surface_rect.center = (self.setting.screen_rect.centerx - self.button_surface_rect.x,self.setting.screen_rect.centery - self.button_surface_rect.y)
I define the different position order of my object, then I found it makes an error.
I want to my
button_surface
on the center of thescreen
so I define
self.button_surface_rect.center = self.setting.screen_rect.center
Then make the
text_surface_rect
on the center of thescreen
self.text_surface_rect.center = self.setting.screen_rect.center
Put the
text_rect
on the center of thetext_surface
self.text_rect.center = self.text_surface_rect.center
When I blit the screen, there's no text on my text_surface, why?
Here's the code:
#!/usr/bin/python
import sys,os
import pygame
class Setting():
def __init__(self,width,height):
self.w=width
self.h=height
self.flag=pygame.RESIZABLE
self.color=(255,255,255)
self.screen=pygame.display.set_mode((self.w,self.h),self.flag)
self.screen_rect=self.screen.get_rect()
pygame.display.set_caption("Muhaha")
class Button():
def __init__(self,setting,text):
self.setting = setting
self.text_color=(0,0,255)
self.button_color=(0,100,100)
self.rect=pygame.Rect(0,0,400,100)
self.rect.center = self.setting.screen_rect.center
self.text=pygame.font.Font(None,80).render(text,True,self.text_color)
self.text_surface=pygame.Surface((400,100))
self.text_surface.set_colorkey((0,0,0))
self.button_surface=pygame.Surface((400,100))
self.button_surface.set_alpha(128)
self.button_surface_rect = self.button_surface.get_rect()
self.button_surface_rect.center = self.setting.screen_rect.center
'''when i define this i cant see the textz'''
self.text_surface_rect = self.text_surface.get_rect()
self.text_surface_rect.center = self.setting.screen_rect.center
self.text_rect = self.text.get_rect()
print(self.text_rect)
print(self.text_surface_rect)
self.text_rect.center = self.text_surface_rect.center
print(self.text_rect)
def blit_button(self):
self.button_surface.fill(self.button_color)
self.setting.screen.blit(self.button_surface,self.button_surface_rect)
self.text_surface.blit(self.text,self.text_rect)
self.setting.screen.blit(self.text_surface,self.button_surface_rect)
def game():
pygame.init()
setting=Setting(1200,800)
button=Button(setting,'PLAY')
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
setting.screen.fill((255,0,0))
button.blit_button()
pygame.display.flip()
game()
self.setting.screen_rect.center
is the center of the large screen. But the text is not blit
onto the screen, it is blit on the small text_surface
.
self.text_surface.blit(self.text,self.text_rect)
The size of text_surface
is (400, 100), but the center of self.text_rect
is (600, 400).
If you do
self.text_surface_rect.center = self.setting.screen_rect.center [...] self.text_rect.center = self.text_surface_rect.center
then self.text_surface_rect
has a location which is far out of the bounds of self.text_surface
. The center of the screen (self.setting.screen_rect.center
) is (600, 400). But the size of text_surface is (400, 100).
+--------------+
| text_surface |
+--------------+
size: (400, 100)
+----------+
| text_rect| center: (600, 400)
+----------+
Note text_surface_rect
is (400, 350, 400, 100)
, but text_surface
is a Surface object and has no location it has just a size of (400, 100). You can think about it as a rectangle (0, 0, 400, 100).
The text is blit
on text_surface
not on text_surface_rect
.
You have to compute the location relative to self.button_surface_rect
, rather than self.setting.screen
:
self.text_surface_rect.center = (self.setting.screen_rect.centerx - self.button_surface_rect.x,
self.setting.screen_rect.centery - self.button_surface_rect.y)
这篇关于pygame 定义不同的位置顺序产生不同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!