Python Forum
use the print function in pygame - 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: use the print function in pygame (/thread-19615.html)



use the print function in pygame - rwahdan - Jul-07-2019

Hi,

I am learning python and I took the code from GitHub but it is printing the results in the screen as print command, I need to attach this printing function so I can it can placed as buttons using pygame. is there a hint or tutorial how to achieve that?

Here is the code:

def printSudoku(sudoku):
    '''Prints out a sudoku in a format that is easy for a human to read'''
    row1 = []
    row2 = []
    row3 = []
    row4 = []
    row5 = []
    row6 = []
    row7 = []
    row8 = []
    row9 = []
    for i in range(81):
        if i in range(0,9):
            row1.append(sudoku[i].returnSolved())
        if i in range(9,18):
            row2.append(sudoku[i].returnSolved())
        if i in range(18,27):
            row3.append(sudoku[i].returnSolved())
        if i in range(27,36):
            row4.append(sudoku[i].returnSolved())
        if i in range(36,45):
            row5.append(sudoku[i].returnSolved())
        if i in range(45,54):
            row6.append(sudoku[i].returnSolved())
        if i in range(54,63):
            row7.append(sudoku[i].returnSolved())
        if i in range(63,72):
            row8.append(sudoku[i].returnSolved())
        if i in range(72,81):
            row9.append(sudoku[i].returnSolved())
    print(row1[0:3],row1[3:6],row1[6:10])
    print(row2[0:3],row2[3:6],row2[6:10])
    print(row3[0:3],row3[3:6],row3[6:10])
    print('')
    print(row4[0:3],row4[3:6],row4[6:10])
    print(row5[0:3],row5[3:6],row5[6:10])
    print(row6[0:3],row6[3:6],row6[6:10])
    print('')
    print(row7[0:3],row7[3:6],row7[6:10])
    print(row8[0:3],row8[3:6],row8[6:10])
    print(row9[0:3],row9[3:6],row9[6:10])



RE: use the print function in pygame - metulburr - Jul-07-2019

WE have button tutorials on this forum for pygame
https://python-forum.io/Thread-PyGame-User-Interface


RE: use the print function in pygame - rwahdan - Jul-07-2019

I found the below code to start with, problem is, when i try to click on any block it still prints the message. How to make the message appear on specific block?

for row in range(0,3,1):
    for column in range(3,6,1):
        color = GREEN
        mouse = pygame.mouse.get_pos()
        click = pygame.mouse.get_pressed()
        x = (MARGIN + WIDTH) * column + MARGIN
        y = (MARGIN + HEIGHT) * row + MARGIN
        pygame.draw.rect(screen,
                         color,
                         [x,
                          y,
                          WIDTH,
                          HEIGHT])
        if x+WIDTH > mouse[0] > x and y+HEIGHT > mouse[1] > y:
            if click[0] == 1:
                print("clicked")



RE: use the print function in pygame - metulburr - Jul-07-2019

What is your full current working code?


RE: use the print function in pygame - rwahdan - Jul-07-2019

Below is the full code that prints the grid:

# Draw the grid
for row in range(0,3,1):
    for column in range(0,3,1):
        color = WHITE
        x = (MARGIN + WIDTH) * column + MARGIN
        y = (MARGIN + HEIGHT) * row + MARGIN
        mouse = pygame.mouse.get_pos()
        click = pygame.mouse.get_pressed()
        pygame.draw.rect(screen,
                         color,
                         [x,
                          y,
                          WIDTH,
                          HEIGHT])

        if x+WIDTH > mouse[0] > x and y+HEIGHT > mouse[1] > y:
            if click[0] == 1:
                print("clicked")
for row in range(0,3,1):
    for column in range(3,6,1):
        color = GREEN
        mouse = pygame.mouse.get_pos()
        click = pygame.mouse.get_pressed()
        x = (MARGIN + WIDTH) * column + MARGIN
        y = (MARGIN + HEIGHT) * row + MARGIN

        pygame.draw.rect(screen,
                         color,
                         [x,
                          y,
                          WIDTH,
                          HEIGHT])

for row in range(0,3,1):
    for column in range(6,9,1):
        color = BLUE
        mouse = pygame.mouse.get_pos()
        click = pygame.mouse.get_pressed()
        x = (MARGIN + WIDTH) * column + MARGIN
        y = (MARGIN + HEIGHT) * row + MARGIN
        pygame.draw.rect(screen,
                         color,
                         [x,
                          y,
                          WIDTH,
                          HEIGHT])

for row in range(3,6,1):
    for column in range(0,3,1):
        color = GREEN
        mouse = pygame.mouse.get_pos()
        click = pygame.mouse.get_pressed()
        x = (MARGIN + WIDTH) * column + MARGIN
        y = (MARGIN + HEIGHT) * row + MARGIN
        pygame.draw.rect(screen,
                         color,
                         [x,
                          y,
                          WIDTH,
                          HEIGHT])

for row in range(3,6,1):
    for column in range(3,6,1):
        color = BLUE
        mouse = pygame.mouse.get_pos()
        click = pygame.mouse.get_pressed()
        x = (MARGIN + WIDTH) * column + MARGIN
        y = (MARGIN + HEIGHT) * row + MARGIN

        pygame.draw.rect(screen,
                         color,
                         [x,
                          y,
                          WIDTH,
                          HEIGHT])
for row in range(3,6,1):
    for column in range(6,9,1):
        color = WHITE
        mouse = pygame.mouse.get_pos()
        click = pygame.mouse.get_pressed()
        x = (MARGIN + WIDTH) * column + MARGIN
        y = (MARGIN + HEIGHT) * row + MARGIN
        pygame.draw.rect(screen,
                         color,
                         [x,
                          y,
                          WIDTH,
                          HEIGHT])


for row in range(6,9,1):
    for column in range(0,3,1):
        color = BLUE
        mouse = pygame.mouse.get_pos()
        click = pygame.mouse.get_pressed()
        x = (MARGIN + WIDTH) * column + MARGIN
        y = (MARGIN + HEIGHT) * row + MARGIN
        pygame.draw.rect(screen,
                         color,
                         [x,
                          y,
                          WIDTH,
                          HEIGHT])

for row in range(6,9,1):
    for column in range(3,6,1):
        color = WHITE
        mouse = pygame.mouse.get_pos()
        click = pygame.mouse.get_pressed()
        x = (MARGIN + WIDTH) * column + MARGIN
        y = (MARGIN + HEIGHT) * row + MARGIN
        pygame.draw.rect(screen,
                         color,
                         [x,
                          y,
                          WIDTH,
                          HEIGHT])


for row in range(6,9,1):
    for column in range(6,9,1):
        color = GREEN
        mouse = pygame.mouse.get_pos()
        click = pygame.mouse.get_pressed()
        x = (MARGIN + WIDTH) * column + MARGIN
        y = (MARGIN + HEIGHT) * row + MARGIN
        pygame.draw.rect(screen,
                         color,
                         [x,
                          y,
                          WIDTH,
                          HEIGHT])

# Limit to 60 frames per second
clock.tick(60)

# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
OUTPUT:
Grid Output


RE: use the print function in pygame - metulburr - Jul-07-2019

way too many repetitive for loops in that code. Im also not sure of why you have different colors for the sections?

Here is a tutorial for making a sodoku solver. It has some flaws such as is still using python2.x and the green chosen number is a little misaligned. But it has all the content of what you want there. And all those issues can be remedied.

https://trevorappleton.blogspot.com/2013/10/guide-to-creating-sudoku-solver-using.html