-
Notifications
You must be signed in to change notification settings - Fork 4
Added background music #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nitic04
wants to merge
10
commits into
gdgutm:8-add-music-to-game
Choose a base branch
from
nitic04:main
base: 8-add-music-to-game
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
94662e4
Added jump sound. Fixed a few pep-8 style guideline errors. Changed F…
daniyalb 651fe74
Added assets folder with jump sound
daniyalb 99f2932
Added sound.py and Sound class
daniyalb 1f50e58
Merge pull request #7 from utmgdsc/3-add-sound-when-player-jumps
daniyalb 9d36919
added background music and credits
nitic04 186b77a
fixed readme formatting
nitic04 b3f62cd
changed implementation of background music
nitic04 52a9a03
Removed comment
nitic04 a749ec7
Update constants.py
nitic04 83794e3
Added game over sound and FileNotFound handling
nitic04 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,30 +1,37 @@ | ||
| import pygame as pg | ||
| from constants import * | ||
| from sound import Sound | ||
|
|
||
|
|
||
| class Player: | ||
|
|
||
| def __init__(self, surface: pg.Surface): | ||
| self.width = PLAYER_WIDTH | ||
| self.height = PLAYER_HEIGHT | ||
| self.initial_pos = X_OFFSET, surface.get_height() - self.height | ||
| self.rect = pg.rect.Rect(X_OFFSET, surface.get_height() - self.height, self.width, | ||
| self.rect = pg.rect.Rect(X_OFFSET, surface.get_height() - self.height, | ||
| self.width, | ||
| self.height) | ||
| self.jumping = False | ||
| self.velocity = 0 | ||
| self.sound = Sound() | ||
|
|
||
| def Show(self, surface: pg.Surface): | ||
| def show(self, surface: pg.Surface): | ||
| pg.draw.rect(surface, PLAYER_COLOR, self.rect) | ||
|
|
||
| def Jump(self): | ||
| def jump(self): | ||
| if self.jumping: | ||
| return | ||
| self.jumping = True | ||
| if not self.sound.no_music: | ||
| self.sound.play('jump') | ||
| self.velocity = PLAYER_JUMP_FORCE | ||
|
|
||
| def UpdateCoords(self, dt): | ||
| def update_coords(self, dt): | ||
| if self.jumping: | ||
| self.rect.move_ip(0,-dt * self.velocity*PLAYER_JUMP_COEFFICIENT) | ||
| self.rect.move_ip(0, -dt * self.velocity * PLAYER_JUMP_COEFFICIENT) | ||
| self.velocity = self.velocity + dt * GRAVITY | ||
| if self.rect.y > self.initial_pos[1]: | ||
| self.rect.update(self.initial_pos[0], self.initial_pos[1], PLAYER_WIDTH, PLAYER_HEIGHT) | ||
| self.rect.update(self.initial_pos[0], self.initial_pos[1], | ||
| PLAYER_WIDTH, PLAYER_HEIGHT) | ||
| self.jumping = False | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,8 @@ | ||
| # dino-game | ||
| This project is a replicated dino-game from Chrome, running on Python. This is a work in progress game made by the UTM GDSC. | ||
| To start contributing please check the "issues" tab. | ||
|
|
||
| Background Music Credit: | ||
| "Pixelland" | ||
| Kevin MacLeod (incompetech.com) | ||
| Licensed under Creative Commons: By Attribution 3.0 http://creativecommons.org/licenses/by/3.0/ |
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,16 +6,19 @@ | |
|
|
||
| from Obstacle import Obstacle | ||
| from Player import Player | ||
| from sound import Sound | ||
| from constants import * | ||
|
|
||
| game_active = True | ||
|
|
||
|
|
||
| def game_over(): | ||
| # TODO | ||
| global game_active | ||
| game_active = False | ||
| print('You lost!') | ||
|
|
||
|
|
||
| def main(): | ||
| # Initialize pygame | ||
| pg.init() | ||
|
|
@@ -24,6 +27,12 @@ def main(): | |
| clock = pg.time.Clock() | ||
| pg.mouse.set_visible(True) | ||
|
|
||
| sound = Sound() | ||
| if not sound.no_music: | ||
| sound.play('background_music') | ||
| else: | ||
| print("Warning: A Music file could not be found. The game will run without music.") | ||
|
|
||
|
Comment on lines
+30
to
+35
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe put this logic in the sound class (play method) |
||
| player = Player(screen) | ||
| obstacles = [] | ||
| next_spawn = random.randint(SPAWN_MIN, SPAWN_MAX) | ||
|
|
@@ -37,21 +46,27 @@ def main(): | |
| return | ||
| if event.type == KEYDOWN: | ||
| if event.key == constants.K_SPACE: | ||
| player.Jump() | ||
| player.jump() | ||
| next_spawn -= dt | ||
| if next_spawn <= 0: | ||
| obstacles.append(Obstacle(screen)) | ||
| next_spawn = random.randint(SPAWN_MIN, SPAWN_MAX) | ||
| screen.fill(BG_RGB) | ||
| player.Show(screen) | ||
| player.UpdateCoords(dt) | ||
| player.show(screen) | ||
| player.update_coords(dt) | ||
| for obstacle in obstacles: | ||
| if obstacle.rect.right <= 0: | ||
| obstacles.remove(obstacle) | ||
| obstacle.UpdateCoords(dt) | ||
| obstacle.Show(screen) | ||
| obstacle.update_coords(dt) | ||
| obstacle.show(screen) | ||
|
|
||
| if player.rect.collidelist([obstacle.rect for obstacle in obstacles]) != -1: | ||
| pg.display.update() | ||
| if not sound.no_music: | ||
| sound.stop('background_music') | ||
| sound.play('game_over') | ||
|
|
||
| pg.time.wait(1200) | ||
| game_over() | ||
|
|
||
| pg.display.update() | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import pygame.mixer | ||
|
|
||
|
|
||
| class Sound: | ||
| sounds: dict | ||
| no_music: bool | ||
|
|
||
| def __init__(self): | ||
| self.sounds = {} | ||
| self.no_music = False | ||
| try: | ||
| self.sounds['jump'] = pygame.mixer.Sound('assets/jump_sound.wav') | ||
|
|
||
| background_music = pygame.mixer.Sound('assets/background_music.mp3') | ||
| background_music.set_volume(0.4) | ||
| self.sounds['background_music'] = background_music | ||
|
|
||
| game_over_sound = pygame.mixer.Sound('assets/game_over_sound.wav') | ||
| game_over_sound.set_volume(0.4) | ||
| self.sounds['game_over'] = game_over_sound | ||
| except FileNotFoundError: | ||
| self.no_music = True | ||
|
|
||
| def play(self, sound: str): | ||
| if sound == "background_music": | ||
| self.sounds[sound].play(-1) | ||
| else: | ||
| self.sounds[sound].play() | ||
|
|
||
| def stop(self, sound: str): | ||
| self.sounds[sound].stop() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This kind of logic should probably be in the sound class