FieryFangs
FieryFangs

Reputation: 25

Pygame Window not Loading Image

Today is my first day of pygame and I cannot understand why this code doesn't work, the pygame windows is black doesn't respond and no image is displayed

import pygame
pygame.init()


screen_width=800
screen_height=800
screen=pygame.display.set_mode([screen_width,screen_height])
screen.fill((255,255,255))

Quit=input("Press'Y' is you want to quit")

if Quit == "Y":
    pygame.display.quit()






Board = pygame.image.load("TicTacToeBoard.jpg")

screen.blit(Board,(0,0))

pygame.display.flip()

Upvotes: 1

Views: 158

Answers (1)

Kingsley
Kingsley

Reputation: 14906

All PyGame programs have an Event Loop. This is a continual loop that accepts events from the window manager / operating environment. Events are things like mouse movements, button clicks and key presses. If you program does not accept events, eventually the launching program will consider it to have stopped responding and perhaps prompt the user to terminate it.

Your existing code grabs input from the console. This can be done in PyGame if you use a thread, and then post an event back to the main loop. But generally it's easier to just handle exiting as an event. In the code below I've handled exiting with the QUIT event, and pressing Q.

import pygame

pygame.init()
screen_width=800
screen_height=800
screen=pygame.display.set_mode([screen_width,screen_height])

Board = pygame.image.load("TicTacToeBoard.jpg")
clock = pygame.time.Clock()

# Main Event Loop
exiting = False
while not exiting:

    # Handle events
    for event in pygame.event.get():
        if ( event.type == pygame.QUIT ):
            exiting = True
        elif ( event.type == pygame.MOUSEBUTTONUP ):
            # On mouse-click
            mouse_pos = pygame.mouse.get_pos()
            print( "Mouse Click at "+str( mouse_pos ) )
        elif ( event.type == pygame.KEYUP ):
            if ( event.key == pygame.K_q ):
                # Q is quit too
                exiting = True    

    # Paint the screen
    screen.fill((255,255,255))
    screen.blit(Board,(0,0))
    pygame.display.flip()

    # Limit frame-rate to 60 FPS
    clock.tick_busy_loop(60)

Additionally, this code also limits the frame-rate to 60 FPS.

Upvotes: 1

Related Questions