Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 use the print function in pygame
#1
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])

Quote
#2
WE have button tutorials on this forum for pygame
https://python-forum.io/Thread-PyGame-User-Interface
Quote
#3
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")
Quote
#4
What is your full current working code?
Quote
#5
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
Quote
#6
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...using.html
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [PyGame] pygame.draw.rect function stretches across the screen instead of moving BubblesTheGiraffe 2 343 Jun-11-2019, 08:32 PM
Last Post: metulburr
  [PyGame] Problem importing pygame / installing pygame Klar 4 2,656 Dec-16-2017, 05:48 PM
Last Post: Klar
  [Pygame] Avoid SentDex pygame tutorials georgecoopers 1 1,805 Apr-09-2017, 12:40 PM
Last Post: metulburr

Forum Jump:


Users browsing this thread: 1 Guest(s)