0

I'm very new to coding. I can't figure out why the image won't display. The image is in the correct folder, and I'm using pygame.display.update(), so I don't understand why it won't show up. When I run the program, the screen turns green, but nothing else happens.

Here's the area I'm having the problem with:

run = True
while run:
  display.fill(green)

  message_display('yoyo')
  pygame.display.update()
  time.sleep(2)

  display.blit(image, (width, height))
  pygame.display.update()
  time.sleep(2)

Here's the full code, if needed:

import pygame
import time
pygame.init()

black = (255, 255, 255)
white = (0, 0, 0)
blue = (108, 201, 245)
red = (235, 38, 38)
green = (52, 163, 84)

height = 800
width = 600

display = pygame.display.set_mode((width, height), 0, 32)

image = pygame.image.load('alvin.jpg')

def text_objects(text, font):
  textSurface = font.render(text, True, black)
  return textSurface, textSurface.get_rect()

def message_display(text):
  largeText = pygame.font.Font('freesansbold.ttf',115)
  TextSurf, TextRect = text_objects(text, largeText)
  TextRect.center = ((width/2),(height/2))
  display.blit(TextSurf, TextRect)

  pygame.display.update()

  time.sleep(2)

run = True
while run:
  display.fill(green)

  message_display('yoyo')
  pygame.display.update()
  time.sleep(2)

  display.blit(image, (width, height))
  pygame.display.update()
  time.sleep(2)

Thanks!

g_coll
  • 1

1 Answers1

0

The second argument to blit must be the position of the upper left corner of the image, not the size:

display.blit(image, (width, height))

display.blit(image, (0, 0))

You have to handle the events in the application loop. See pygame.event.get() respectively pygame.event.pump():

For each frame of your game, you will need to make some sort of call to the event queue. This ensures your program can internally interact with the rest of the operating system. e.g.:

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False 

    display.fill(green)
    display.blit(image, (0, 0))
    message_display('yoyo')
    pygame.display.update()
Rabbid76
  • 177,135
  • 25
  • 101
  • 146