2

I am trying to blit a 'P' as text onto a chessboard. Even following previous help on how to blit squares, I am not able to do it with text.

This variation of a chess game is played with pikemen, archers, and knights. The 'P' stands for pikeman.

In future, I will display a sprite, but for the moment I would like to display letters (also, because it could be helpful for the GUI)

class Unit:
    def __init__(self):
        self.surface = pygame.Surface((100, 100))
        self.my_font = pygame.font.SysFont('Time New Roman', 18)
        self.img = self.my_font.render('P', 1, (255, 0, 0))
        self.surface.blit(self.img, (100, 100))

Basically what I am trying to do is to create a surface, set a font, render it, and blit it on the surface created.

Hereafter the full display code:

import pygame
import sys

from coordinator import coordinator

# Sets up the display

pygame.init()
window_size = (800, 800)
game_window = pygame.display.set_mode(size=window_size)
pygame.display.set_caption('My Game')




class WhiteSquare:
    def __init__(self):
        self.height = int(window_size[0] / 8)
        self.width = int(window_size[1] / 8)
        self.white_square = pygame.Surface((self.height, self.width))
        self.white_square.fill((255, 255, 255))


class BlackSquare:
    def __init__(self):
        self.height = int(window_size[0] / 8)
        self.width = int(window_size[1] / 8)
        self.black_square = pygame.Surface((self.height, self.width))
        self.black_square.fill((0, 0, 0))


class ChessBoard:
    def __init__(self):
        self.ws = ws
        self.bs = bs
        self.white_columns = white_columns
        self.black_columns = black_columns

    def draw(self):
        for columns in self.white_columns:
            game_window.blit(self.ws.white_square, columns)

        for columns in self.black_columns:
            for destination in columns:
                game_window.blit(self.bs.black_square, destination)


class Unit:
    def __init__(self):
        self.surface = pygame.Surface((100, 100))
        self.my_font = pygame.font.SysFont('Time New Roman', 18)
        self.img = self.my_font.render('P', 1, (255, 0, 0))
        self.surface.blit(self.img, (100, 100))


coordinator = coordinator()
black_columns = coordinator[2], coordinator[3]
white_columns = coordinator[0] + coordinator[1]

#print('coordinator' + str(coordinator))
#print('w_columns' + str(w_columns))


ws = WhiteSquare()
bs = BlackSquare()
cb = ChessBoard()
u = Unit()



# Event loop (outer)
while 1:

    # Event loop (inner)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

    cb.draw()

    pygame.display.update()

Hopefully, it makes sense.

Thank you

Techoplite
  • 605
  • 9
  • 23

2 Answers2

1

Create self.imag in the constructor of Unit. But blit it in a draw method. The instance method draw has a parameter, which is the Surface, where the text has to be blit on:

class Unit:
    def __init__(self):
        self.surface = pygame.Surface((100, 100))
        self.my_font = pygame.font.SysFont('Time New Roman', 18)
        self.img = self.my_font.render('P', 1, (255, 0, 0))

   def draw(self, surface):
        surface.blit(self.img, (100, 100))

Create an instance (e.g. u) of the class Unit. Call u.draw in the main loop. The parameter to the draw method is the Surface which is associated to the display (game_window), so the text is drawn on the display. e.g.:

u = Unit()
while 1:

    # Event loop (inner)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

    # draw the scene
    cb.draw()
    u.draw(game_window)

    # update the display
    pygame.display.update()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
1

You will need a draw function just like for the chess board. And then "paint" your custom font on the game_window. This could look like such:

def draw(self):
    game_window.blit(self.img, (100, 100))

Complete unit class:

class Unit:
def __init__(self):
    self.surface = pygame.Surface((100, 100))
    self.my_font = pygame.font.SysFont('Time New Roman', 18)
    self.img = self.my_font.render('P', 1, (255, 0, 0))

def draw(self):
    game_window.blit(self.img, (100, 100))

and then use u.draw() inside your main loop

Tobias O
  • 101
  • 5
  • Thanks, Tobias. Your solution works as well. I have just picked Rabbid76's one because it is shorter, that's all. – Techoplite Oct 16 '19 at 14:55