pygame定义不同的位置顺序以得到不同的结果

问题描述 投票:1回答:1

我定义了对象的不同位置顺序,然后发现它出错了。

  1. 我想将我的button_surface放在screen的中心,所以我定义self.button_surface_rect.center = self.setting.screen_rect.center

  2. 然后将text_surface_rect放在screen的中心self.text_surface_rect.center = self.setting.screen_rect.center

  3. text_rect放在text_surface的中心self.text_rect.center = self.text_surface_rect.center

当我关闭屏幕时,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()
python pygame
1个回答
1
投票

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上的text_surface,而不是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)
© www.soinside.com 2019 - 2024. All rights reserved.