-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3. Playing Sounds in Pygame.py
56 lines (41 loc) · 4.71 KB
/
3. Playing Sounds in Pygame.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import pygame # Importing all the packages of the pygame
import random # Importing random so that we can give random positions to our enemy
import math
from pygame import mixer
#--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
pygame.init() # Initializes the pygame
#--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Dimension of the screen
#--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
width = 800 # Width of the SCREEN or the FRAME or the WINDOW
height = 600 # Height of the SCREEN or the FRAME or the WINDOW
#--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Colours
#--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
blue = (0, 0, 255)
#--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Creating a Screen
#--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
screen = pygame.display.set_mode((width, height))
#--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Creating Background
#--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#backgroundSound
#--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
mixer.music.load("./Sounds/spaceInvadersWar.mp3") #play the music
mixer.music.play(-1) #-1 is for loop of sound
#--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Title
#--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
pygame.display.set_caption("PyGame Title") # Giving the title to the Output
running = True # running value i.e its true while the player is running
while running:
# --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
screen.fill(blue) # screen is filled with color
# --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
for event in pygame.event.get(): # getting all the events happening externally using loop
if event.type == pygame.QUIT: # if QUIT button or X button is pressed
running = False # Running value returns while loop as false and the loop breaks
pygame.display.update() # Updating the screen is imp because it changes the screen for each cycle or events or the loop