Browse Source

Add files via upload

PythonCoder8 4 years ago
parent
commit
b37849b821
3 changed files with 64 additions and 0 deletions
  1. BIN
      PS circle.png
  2. BIN
      helloworld.wav
  3. 64 0
      img-sounds.py

BIN
PS circle.png


BIN
helloworld.wav


+ 64 - 0
img-sounds.py

@@ -0,0 +1,64 @@
+import pygame, sys
+from keyboard import is_pressed
+
+#Initialize pygame and set up window
+pygame.init()
+pygame.mixer.init()
+winSize = (800, 600)
+screen = pygame.display.set_mode(winSize)
+pygame.display.set_caption('Control where text is on screen by using mouse')
+
+#Load image
+helloWorld = pygame.image.load('PS circle.png')
+
+helloWorldSize = helloWorld.get_size()
+
+sound = pygame.mixer.Sound('helloworld.wav')
+
+#Set mouse visibility to false
+pygame.mouse.set_visible(0)
+
+x, y = 0, 0
+
+directionX, directionY = 1, 1
+
+#Add clock so that the next frame is displayed at an exact time
+clock = pygame.time.Clock()
+
+while True:
+    
+    #Use is_pressed() from keyboard module to let user quit application by clicking 'q' on their keyboard
+    if is_pressed('q'):
+        sys.exit()
+    
+    clock.tick(40)
+
+    #Keep screen open and let user exit window using for loop
+    for ev in pygame.event.get():
+        if ev.type == pygame.QUIT:
+            sys.exit()
+    
+    screen.fill((0,0,0))
+
+    screen.blit(helloWorld, (x, y))
+    
+    mousePos = pygame.mouse.get_pos()
+    
+    x, y = mousePos
+    
+    if x + helloWorldSize[0] > 800:
+        x = 800 - helloWorldSize[0]
+        sound.stop()
+        sound.play()
+    
+    if y + helloWorldSize[1] > 600:
+        y = 600 - helloWorldSize[1]
+        sound.stop()
+        sound.play()
+    
+    if x == 0 or y == 0:
+        sound.stop()
+        sound.play()
+        
+    #Update window    
+    pygame.display.update()