Skip to content

Commit

Permalink
Added sound
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSofox committed Apr 13, 2020
1 parent c8c24d6 commit 7156c83
Show file tree
Hide file tree
Showing 13 changed files with 32 additions and 9 deletions.
5 changes: 4 additions & 1 deletion ball.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ struct Ball initBall(int serve){
ball.vel.y = 0;
ball.offside = 0;
ball.pauseTime = 120;
ball.justHit = FALSE;
return ball;
}
struct Vector2f hitBall(struct Ball ball, struct Player player){
ball.vel.x *= -1.05;
ball.vel.y += player.vel.y/2;

ball.vel.y += ((ball.pos.y + 4.0f) - (player.pos.y + 16.0f))/20;

return ball.vel;
}

Expand All @@ -34,11 +34,14 @@ struct Ball updateBall(struct Ball ball, struct Player player1, struct Player pl
ball.ppos.y = ball.pos.y;
return ball;
}
ball.justHit = FALSE;
if (ball.vel.x<0 && check_ball_hit(ball, player1)){
ball.vel = hitBall(ball, player1);
ball.justHit = TRUE;
}
if (ball.vel.x>0 && check_ball_hit(ball, player2)){
ball.vel = hitBall(ball, player2);
ball.justHit = TRUE;
}
if (ball.pos.y <= 0){
ball.vel.y *= -1;
Expand Down
2 changes: 1 addition & 1 deletion ball.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
extern bool check_ball_hit(struct Ball ball, struct Player player);
extern struct Ball initBall(int serve);
extern struct Vector2f hitBall(struct Ball ball, struct Player player);
extern struct Ball updateBall(struct Ball ball, struct Player player1, struct Player player2);
extern struct Ball updateBall(struct Ball ball, struct Player player1, struct Player player2);
2 changes: 1 addition & 1 deletion constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#define SWING_TIME 10


#define SCORE_LIMIT 2
#define SCORE_LIMIT 5

/*
Notes regarding SGDK API changes:
Expand Down
20 changes: 16 additions & 4 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ int main(){
u16 joyInput1;
u16 joyInput2;

SND_startPlay_4PCM_ENV(soundTheme, sizeof(soundTheme), SOUND_PCM_CH2, FALSE);
while(1)
{
if(gameState.screen == 1){
Expand Down Expand Up @@ -124,27 +125,38 @@ int main(){
player1 = updatePlayer(player1, joyInput1);
player2 = updatePlayer(player2, joyInput2);

//VDP_drawText(3, 1, 2);

ball = updateBall(ball, player1, player2);

if(player1.hitFrame || player2.hitFrame){
if(ball.justHit)
SND_startPlay_4PCM_ENV(soundSwipe, sizeof(soundSwipe), SOUND_PCM_CH2, FALSE);
else
SND_startPlay_4PCM_ENV(soundSwoosh, sizeof(soundSwoosh), SOUND_PCM_CH2, FALSE);

}

if(ball.offside!=0){

SND_startPlay_4PCM_ENV(soundMiss, sizeof(soundMiss), SOUND_PCM_CH2, FALSE);
if(ball.offside>0){
player1 = player_add_score(player1);
gameState.serve = 1;
if(player1.score>=2){
if(player1.score>=SCORE_LIMIT){
player1 = player_set_winner(player1);
player2 = player_set_loser(player2);
gameState.winScreen = TRUE;
SND_startPlay_4PCM_ENV(soundVictory, sizeof(soundVictory), SOUND_PCM_CH2, FALSE);
}
}
if(ball.offside<0){
player2 = player_add_score(player2);
gameState.serve = -1;
if(player2.score>=2){
if(player2.score>=SCORE_LIMIT){
player2 = player_set_winner(player2);
player1 = player_set_loser(player1);
gameState.winScreen = TRUE;
SND_startPlay_4PCM_ENV(soundVictory, sizeof(soundVictory), SOUND_PCM_CH2, FALSE);
}
}
//
Expand All @@ -162,7 +174,7 @@ int main(){
drawInt(player1.score,18,2,1);
drawInt(player2.score,22,2,1);


SPR_setHFlip (ballSprite, ball.vel.x>0);
SPR_setPosition(ballSprite, ball.ppos.x,ball.ppos.y);

Expand Down
1 change: 1 addition & 0 deletions player.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ struct Player updatePlayer(struct Player player, u16 input){
player.vel.y = 0;
player.pos.y += player.vel.y;



if(player.pos.y<0){
player.pos.y = 0;
Expand Down
2 changes: 1 addition & 1 deletion player.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ extern struct Player initPlayer(int pno);
extern struct Player updatePlayer(struct Player player, u16 input);
extern struct Player player_add_score(struct Player player);
extern struct Player player_set_winner(struct Player player);
extern struct Player player_set_loser(struct Player player);
extern struct Player player_set_loser(struct Player player);
Binary file added res/audio/miss.wav
Binary file not shown.
Binary file added res/audio/swipe.wav
Binary file not shown.
Binary file added res/audio/swoosh.wav
Binary file not shown.
Binary file added res/audio/theme.wav
Binary file not shown.
Binary file added res/audio/victory.wav
Binary file not shown.
8 changes: 7 additions & 1 deletion res/resources.res
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
SPRITE imgball "sprites/ball.png" 1 1 NONE
SPRITE imgplayer "sprites/player.png" 4 4 0
IMAGE imgcave "tiles/cave.bmp" 0
IMAGE imglogo "logo.png" 0
IMAGE imglogo "logo.png" 0

WAV soundMiss "audio/miss.wav" 2
WAV soundSwipe "audio/swipe.wav" 2
WAV soundTheme "audio/theme.wav" 2
WAV soundVictory "audio/victory.wav" 2
WAV soundSwoosh "audio/swoosh.wav" 2
1 change: 1 addition & 0 deletions structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ struct Ball{
struct Vector2f vel;
int pauseTime;
int offside;
bool justHit;
};

struct GameState{
Expand Down

0 comments on commit 7156c83

Please sign in to comment.