1

Im trying to create a simple window that will record a mouse's position on click, but I need to set the background display to an image.

So far I have this:

import pygame
LEFT = 1

pygame.display.init()
screen = pygame.display.set_mode((320,200))
pygame.mouse.set_visible(False)
image = pygame.image.load("picture.png")
screen.blit(image, (100,100))

running = 1

while running:
     event = pygame.event.poll()
     if event.type == pygame.QUIT:
          running = 0
     elif event.type == pygame.MOUSEBUTTONDOWN and event.button == LEFT:
          print("Left mouse was pressed at: %d, %d" %event.pos)
     pygame.display.flip()

The mouse clicks are working correctly, even when trying to load the image, just the window is remaining black.

Jkind9
  • 742
  • 7
  • 24
  • 1
    I hope nlit is spelling error. Try changing flip to update. – MegaIng Oct 30 '17 at 12:32
  • `pygame.display.flip()` should work fine. If it's just a typo (`nlit` should be `blit`), then the question is off-topic and should be closed. But if the typo is the only issue, the game shouldn't run at all. You need to provide more information. Do you get any error messages? – skrx Oct 30 '17 at 12:36
  • It was indeed a typo, and no I don't get any errors. – Jkind9 Oct 30 '17 at 12:42
  • Try to blit your image inside of the while loop. Is that your exact code or do you maybe fill the `screen` every frame? – skrx Oct 30 '17 at 12:46
  • That is my exact code, I'm new to pygame and relatively new to python too. Also, moving the blit into the loop didn't change anything, still get a black window where the clicks still register – Jkind9 Oct 30 '17 at 12:57
  • Does it work with a different image? I've tested your code and it works correctly for me. Also, call `pygame.init()` to initialize all pygame modules. – skrx Oct 30 '17 at 13:03
  • Different image has worked, no idea why but there we go! – Jkind9 Oct 30 '17 at 13:09
  • That's odd, but at least it works. :) Perhaps the image file was corrupted. BTW, call the `convert` method (or `convert_alpha` for images with transparency) to improve the performance: `image = pygame.image.load("picture.png").convert()`. – skrx Oct 30 '17 at 13:20

0 Answers0