python - pygame定义不同的位置顺序产生不同的结果
问题描述
我定义了我的对象的不同位置顺序,然后我发现它出错了。
我想我
button_surface
的中心screen
所以我定义
self.button_surface_rect.center = self.setting.screen_rect.center
然后
text_surface_rect
在中心screen
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
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
是大屏幕的中心。但是文字并没有出现blit
在屏幕上,它是在小的text_surface
.
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)。
+--------------+
| text_surface |
+--------------+
size: (400, 100)
+----------+
| text_rect| center: (600, 400)
+----------+
注意text_surface_rect
是(400, 350, 400, 100)
, 但是text_surface
是一个 Surface 对象并且没有位置它只有 (400, 100) 的大小。您可以将其视为一个矩形(0、0、400、100)。
文本是blit
on text_surface
not on 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)
推荐阅读
- laravel - 如何在 Laravel 6 中创建来自不同表名的下拉列表数据
- c# - 在更新实体中的大量记录时面临问题
- node.js - 如何解析从 Pub/sub 触发的 Cloud Function 中的 Json 有效负载
- react-native - 如何在 Expo 中使用 Cordova 插件?
- javascript - Javascript promise(Async/Await) 在 Node api 上实现
- ubuntu - HTK 32 位问题 -> apt-get install gcc-multilib -> “包 'gcc-multilib' 没有安装候选”
- python - 单击时更改 QCandlestickSet 中“蜡烛”的颜色
- swift - 如何确保为子类调用我的 init?
- asp.net-core - 在我的 Web Api 中,如何为单个移动项目具有 AccessoryItems(Child) 的 MobileItems(Parent Class) 编写代码?
- nancy - 在 Nancy 2.x 中处理请求之前,我该怎么做?