Python Forum
[PyGame] printing integers on pygame.draw.rect
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] printing integers on pygame.draw.rect
#1
hi i am new to python prog arming. i want to boost my skills by making simple games using pygame and other tools.
i want to print integers on pygame.draw.rect from 1 to 100 such that the count start from left bottom and finishes on left upper rectangle.
The code so far:
 import pygame
BOARD_POS = ( 10,10 )
TILESIZE = 40

def create_board_surf():
    board_surf = pygame.Surface((TILESIZE*10, TILESIZE*10))
    dark = False
    for y in range(10):
        for x in range(10):
            rect = pygame.Rect(x*TILESIZE, y*TILESIZE, TILESIZE, TILESIZE)
            pygame.draw.rect(board_surf, pygame.Color('blue' if dark else 'white'), rect)
            dark = not dark
        dark = not dark
    return board_surf

def create_board():
    board = []
    for y in range(10):
        board.append([])
        for x in range(10):
            board[y].append(None)

    return board

def get_square_under_mouse(board):
    mouse_pos = pygame.Vector2(pygame.mouse.get_pos()) - BOARD_POS
    x, y = [int(v // TILESIZE) for v in mouse_pos]
    try: 
        if x >= 0 and y >= 0: return (board[y][x], x, y)
    except IndexError: pass
    return None, None, None

def main():
    screen = pygame.display.set_mode((640, 480))
    board = create_board()
    board_surf = create_board_surf()
    clock = pygame.time.Clock()
    while True:
        events = pygame.event.get()
        for e in events:
            if e.type == pygame.QUIT:
                return

        piece, x, y = get_square_under_mouse(board)

        screen.fill(pygame.Color('grey'))
        screen.blit(board_surf, BOARD_POS)

        if x != None:
            rect = (BOARD_POS[0] + x * TILESIZE, BOARD_POS[1] + y * TILESIZE, TILESIZE, TILESIZE)
            pygame.draw.rect(screen, (255, 0, 0, 50), rect, 2)
        pygame.display.flip()
        clock.tick(60)

if __name__ == '__main__':
    main()

i think i add piece of code for that in in create_board and in main program for calling.
Reply
#2
I think it would be easier to create a Box class. In which based on its index in the alignment of a 10 by 10 grid would identify what color it is and what number it is. Then create a text object and its box index determines its number.
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] Rect object penetrates wall on one side Jan_97 4 2,532 Dec-30-2021, 11:08 PM
Last Post: Jan_97
  rect object not loading BlueClaw 2 4,148 Dec-11-2019, 04:25 PM
Last Post: BlueClaw
  [PyGame] PYGAME draw ball gean 1 2,631 Nov-08-2019, 11:16 AM
Last Post: metulburr
  [PyGame] pygame.draw.rect function stretches across the screen instead of moving BubblesTheGiraffe 2 3,652 Jun-11-2019, 08:32 PM
Last Post: metulburr
  [PyGame] assigning rect to sprite pfaber11 1 2,180 May-18-2019, 05:39 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020