M_Sea
M_Sea

Reputation: 491

pygame define different position order make the different result

I define the different position order of my object, then I found it makes an error.

  1. I want to my button_surface on the center of the screen so I define
    self.button_surface_rect.center = self.setting.screen_rect.center

  2. Then make the text_surface_rect on the center of the screen
    self.text_surface_rect.center = self.setting.screen_rect.center

  3. Put the text_rect on the center of the text_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()

Upvotes: 1

Views: 174

Answers (1)

Rabbid76
Rabbid76

Reputation: 210889

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)

Upvotes: 1

Related Questions