img-sounds.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import pygame, sys
  2. from keyboard import is_pressed
  3. #Initialize pygame and set up window
  4. pygame.init()
  5. pygame.mixer.init()
  6. winSize = (800, 600)
  7. screen = pygame.display.set_mode(winSize)
  8. pygame.display.set_caption('Control where text is on screen by using mouse')
  9. #Load image
  10. helloWorld = pygame.image.load('PS circle.png')
  11. helloWorldSize = helloWorld.get_size()
  12. sound = pygame.mixer.Sound('helloworld.wav')
  13. #Set mouse visibility to false
  14. pygame.mouse.set_visible(0)
  15. x, y = 0, 0
  16. directionX, directionY = 1, 1
  17. #Add clock so that the next frame is displayed at an exact time
  18. clock = pygame.time.Clock()
  19. while True:
  20. #Use is_pressed() from keyboard module to let user quit application by clicking 'q' on their keyboard
  21. if is_pressed('q'):
  22. sys.exit()
  23. clock.tick(40)
  24. #Keep screen open and let user exit window using for loop
  25. for ev in pygame.event.get():
  26. if ev.type == pygame.QUIT:
  27. sys.exit()
  28. screen.fill((0,0,0))
  29. screen.blit(helloWorld, (x, y))
  30. mousePos = pygame.mouse.get_pos()
  31. x, y = mousePos
  32. if x + helloWorldSize[0] > 800:
  33. x = 800 - helloWorldSize[0]
  34. sound.stop()
  35. sound.play()
  36. if y + helloWorldSize[1] > 600:
  37. y = 600 - helloWorldSize[1]
  38. sound.stop()
  39. sound.play()
  40. if x == 0 or y == 0:
  41. sound.stop()
  42. sound.play()
  43. #Update window
  44. pygame.display.update()