Skip to content

Commit 985b6be

Browse files
committed
Added the code for sounds in Pygame
1 parent 872b1c9 commit 985b6be

File tree

3 files changed

+65
-5
lines changed

3 files changed

+65
-5
lines changed

3. Playing Sounds in Pygame.py

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

README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,20 @@
88

99
- Fork the project repository
1010

11-
> Find the project's repository on GitHub, and then "fork" it by clicking the Fork button in the upper right corner:
11+
> Find the project's repository on GitHub, and then "fork" it by clicking the Fork button in the upper right corner:
1212
1313
- Clone your Fork
14-
15-
> Using Git on your local machine, clone your fork using the URL you just copied: git clone URL_OF_FORK.
14+
15+
> Using Git on your local machine, clone your fork using the URL you just copied: git clone URL_OF_FORK.
1616
1717
- Add the project repository as the "upstream" remote
18+
1819
> Add the project repository as the "upstream" remote using: git remote add upstream URL_OF_PROJECT.Before tryingto push first sync it with upstream repository.
1920
2021
- Upstream Repository - where you forked your project
2122
- Origin Repository - Your forked project
2223

23-
**Check Details workflow here before submitting - https://dataschool.io/how-to-contribute-on-github/
24+
\*\*Check Details workflow here before submitting - https://dataschool.io/how-to-contribute-on-github/
2425

2526
## Contributors
2627

@@ -31,4 +32,7 @@
3132
<td>
3233
<tr><a href="https://github.com/aayushi-droid"><img src="https://avatars1.githubusercontent.com/u/59429424?s=460&u=9fe1f1e20bff25ba6f044990ceab7a5d5bc4d1d4&v=4" alt="aayushi-droid" style="height:80px;width :80px;border-radius:50%"></a></tr>
3334
</td>
34-
</table>
35+
<td>
36+
<tr><a href="https://github.com/AmeyaJain-25"><img src="https://avatars0.githubusercontent.com/u/66305085?s=460&u=14abcae2ef5772d56e60111787a4a8fa80b3b077&v=4" alt="AmeyaJain-25" style="height:80px;width :80px;border-radius:50%"></a></tr>
37+
</td>
38+
</table>

Sounds/spaceInvadersWar.mp3

428 KB
Binary file not shown.

0 commit comments

Comments
 (0)