Текст поверх друг друга - PullRequest
       5

Текст поверх друг друга

0 голосов
/ 13 декабря 2018

Я создал определение под названием message_display (text), но когда я использую его в своем коде, текст попадает друг на друга в левом нижнем углу вы видите, что привет и мир печатаются поверх каждогодругой .Ваша помощь приветствуется

import pygame
import random
import sys
pygame.init()
win =  pygame.display.set_mode((800,700))
pygame.display.set_caption('Knights Of Dungeons')
gb = pygame.image.load('background.png')
font= pygame.font.SysFont('Gabriola', 30, False,True)
game = True
roll_dice = font.render('Press Enter To roll the dice' ,10,(0,0,0))
def dice():
    x = random.randint(1,6)
    return x




def message_display(text):
    dis_text = font.render(text, 10, (0,0,0))
    win.blit(dis_text,(10,650))
    pygame.display.update()




player_1 = font.render('Name: Kaan                 '+ 'Health:   100           
' + 'Damage:   0            ' + 'Armour: 0         ')

while game:

    pygame.time.delay(50)
    for event in pygame.event.get():
       if event.type == pygame.QUIT:
           game = False
    keys = pygame.key.get_pressed()
    if keys[pygame.K_RETURN]:
      num_thrown = dice()
      roll_dice = font.render( 'The number you have throwen is: 
      '+str(0+num_thrown) ,20,(0,0,0))    
    win.blit(gb,(0,15))
    win.blit(player_1 ,(0,100))
    win.blit(roll_dice,(455,650))
    message_display('Hello')
    message_display('world')
    pygame.display.update()

pygame.quit()

1 Ответ

0 голосов
/ 13 декабря 2018

Когда я использую pygame, мне нравится, чтобы моя функция отображения текста была более общей.Примерно так:

import pygame as pg

def draw_text(surface, text, size, color, x, y):
    """Draw text to surface

       surface - Pygame surface to draw to
       text    - string text to draw
       size    - font size
       color   - color of text
       x       - x position of text on surface
       y       - y position of text on surface
    """
    font = pg.font.Font(font_name, size)
    text_surf = font.render(str(text), True, color)
    text_rect = text_surf.get_rect()
    text_rect.topleft = (x, y) # I use topleft here because that makes sense to me
                               # for English (unless you want it centered).
                               # But you can use any part of the rect to start drawing the text
    surface.blit(text_surf, text_rect)

Обратите внимание, что вам также нужно настроить font_name.Вы можете сделать это внутри или вне функции (если вы просто хотите).Я сделал это глобально для моего варианта использования, как это:

font_name = pg.font.match_font('arial')

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...