Pygame определить другой порядок позиции сделать другой результат - PullRequest
1 голос
/ 19 января 2020

Я определяю другой порядок расположения моего объекта, затем я обнаружил, что он делает ошибку.

  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

Когда я перетаскиваю экран, на моей текстовой поверхности нет текста, почему?

Вот код:

#!/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()

1 Ответ

1 голос
/ 19 января 2020

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)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...