0

Every time I try to run I get a black screen. Could someone point the issue? I am completely unsure what might be the issue. Also, I checked out other solutions for the same issues on Stack My code:

import sys, os, random, pygame
sys.path.append(os.path.join("objects"))
import SudokuSquare
from GameResources import *

digits = '123456789'
rows = 'ABCDEFGHI'


def play(values_list):
    pygame.init()


    size = width, height = 700, 700
    screen = pygame.display.set_mode(size)

    background_image = pygame.image.load("./images/sudoku-board-bare.jpg").convert()

    clock = pygame.time.Clock()

    # The puzzleNumber sets a seed so either generate
    # a random number to fill in here or accept user
    # input for a duplicatable puzzle.

    for values in values_list:
        pygame.event.pump()
        theSquares = []
        initXLoc = 0
        initYLoc = 0
        startX, startY, editable, number = 0, 0, "N", 0
        for y in range(9):
            for x in range(9):
                if x in (0, 1, 2):  startX = (x * 57) + 38
                if x in (3, 4, 5):  startX = (x * 57) + 99
                if x in (6, 7, 8):  startX = (x * 57) + 159

                if y in (0, 1, 2):  startY = (y * 57) + 35
                if y in (3, 4, 5):  startY = (y * 57) + 100
                if y in (6, 7, 8):  startY = (y * 57) + 165
                col = digits[x]
                row = rows[y]
                string_number = values[row + col]
                if len(string_number) > 1 or string_number == '' or string_number == '.':
                    number = None
                else:
                    number = int(string_number)
                theSquares.append(SudokuSquare.SudokuSquare(number, startX, startY, editable, x, y))

        screen.blit(background_image, (0, 0))
        for num in theSquares:
            num.draw()

        pygame.display.flip()
        pygame.display.update()
        clock.tick(5)

    # leave game showing until closed by user
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

if __name__ == "__main__":
    main()
    sys.exit()

Here is the code calling that:

from PySudoku import play

def visualize_assignments(assignments):
    """ Visualizes the set of assignments created by the Sudoku AI"""
    last_assignment = None
    filtered_assignments = []

    for i in range(len(assignments)):
        if last_assignment:
            last_assignment_items = [item for item in last_assignment.items() if len(item[1]) == 1]
            current_assignment_items = [item for item in assignments[i].items() if len(item[1]) == 1]
            shared_items = set(last_assignment_items) & set(current_assignment_items)
            if len(shared_items) < len(current_assignment_items):
                filtered_assignments.append(assignments[i])
        last_assignment = assignments[i]

    play(filtered_assignments)

My complete code can be found here if that's required.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Chashmeet
  • 21
  • 4
  • 1
    I've never used pygame, so take this with a grain of salt. Where is the function `main` defined? That's never calling the `play` function, and it looks like you never update the display (pygame.display.flip()) after you set it up. My suggestion would be to run this in a Python debugger if you haven't already. IDLE is pretty bare bones, but it is generally installed with Python (not sure about 3.x) so you'll already have it most likely. You can also look into other IDEs, like JetBrains' PyCharm. – KymikoLoco May 25 '17 at 17:41
  • @KymikoLoco just updated the post and added code calling that. Also I have called `pygame.display.flip()` check just above `while True` – Chashmeet May 25 '17 at 17:50
  • That's my point, you never call flip INSIDE the while loop, as in, you're never updating the render target after you initially set it up (in a for loop as well?) I would suggest reading the documentation on pygame more, or trying to get a smaller example rendering. The code here describes what I'm talking about regarding `pygame.display.flip()`: https://www.pygame.org/docs/tut/tom_games2.html#makegames-2-1 Notice how the flip call is inside the main game while loop – KymikoLoco May 25 '17 at 18:05
  • Just tried that, didn't work. The example code on the link you gave does though. Any other suggestions? – Chashmeet May 25 '17 at 18:41
  • I tried various other example graphic related games they seem to show a black screen as well though static drawing show up perfectly. – Chashmeet May 25 '17 at 18:55
  • Sadly I have no other ideas as to what could be wrong. I would check if the pygame functions return any errors that might indicate what is not working. I would attach a debugger and go from there. – KymikoLoco May 25 '17 at 19:08

0 Answers0