[PyGame] printing integers on pygame.draw.rect - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Game Development (https://python-forum.io/forum-11.html) +--- Thread: [PyGame] printing integers on pygame.draw.rect (/thread-29192.html) |
printing integers on pygame.draw.rect - Shyckh - Aug-22-2020 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. RE: printing integers on pygame.draw.rect - metulburr - Aug-22-2020 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. |