Skip to content
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

add game timer logic to Snap and have the game started when the space… #95

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Resources/fonts/Chunkfive.otf
Binary file not shown.
Binary file modified Resources/images/Cards.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Resources/images/cardsBoard.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Resources/sounds/slap.wav
Binary file not shown.
20 changes: 17 additions & 3 deletions src/GameLogic/Snap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class Snap
public Snap ()
{
_deck = new Deck ();
_gameTimer = SwinGame.CreateTimer ();
}

/// <summary>
Expand Down Expand Up @@ -92,6 +93,7 @@ public void Start()
_deck.Shuffle (); // Return the cards and shuffle

FlipNextCard (); // Flip the first card...
_gameTimer.Start();
}
}

Expand All @@ -112,6 +114,10 @@ public void FlipNextCard()
public void Update()
{
//TODO: implement update to automatically slip cards!
if (_gameTimer.Ticks > _flipTime)
{
_gameTimer.Reset (); FlipNextCard ();
}
}

/// <summary>
Expand All @@ -133,16 +139,24 @@ public int Score(int idx)
public void PlayerHit (int player)
{
//TODO: consider deducting score for miss hits???
if ( player >= 0 && player < _score.Length && // its a valid player
IsStarted && // and the game is started
_topCards [0] != null && _topCards [0].Rank == _topCards [1].Rank) // and its a match
if (player >= 0 && player < _score.Length && // its a valid player
IsStarted && // and the game is started
_topCards[0] != null && _topCards[0].Rank == _topCards[1].Rank) // and its a match
{
_score[player]++;
SwinGame.LoadSoundEffectNamed("Slap", "slap.wav");
SwinGame.PlaySoundEffect("Slap");
//TODO: consider playing a sound here...
}
else if (player >= 0 && player < _score.Length)
{
_score[player]--;
}


// stop the game...
_started = false;
_gameTimer.Stop ();
}

#region Snap Game Unit Tests
Expand Down
34 changes: 27 additions & 7 deletions src/SnapGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public static void LoadResources()
{
Bitmap cards;
cards = SwinGame.LoadBitmapNamed ("Cards", "Cards.png");
SwinGame.BitmapSetCellDetails (cards, 82, 110, 13, 5, 53); // set the cells in the bitmap to match the cards
SwinGame.BitmapSetCellDetails (cards, 167, 250, 13, 5, 53); // set the cells in the bitmap to match the cards
SwinGame.LoadFontNamed("GameFont", "Chunkfive.otf", 24);
}

/// <summary>
Expand All @@ -24,8 +25,26 @@ private static void HandleUserInput(Snap myGame)

if (SwinGame.KeyTyped (KeyCode.vk_SPACE))
{
myGame.FlipNextCard ();
myGame.Start();
}

if(myGame.IsStarted)
{
if (SwinGame.KeyTyped (KeyCode.vk_LSHIFT) && SwinGame.KeyTyped (KeyCode.vk_RSHIFT))
{
//TODO: add sound effects;
SwinGame.LoadSoundEffectNamed("SwinGameStrat", "SwinGameStart.wav");
SwinGame.PlaySoundEffect("SwinGameStart");
}
else if(SwinGame.KeyTyped (KeyCode.vk_LSHIFT))
{
myGame.PlayerHit(0);
}
else if(SwinGame.KeyTyped (KeyCode.vk_RSHIFT))
{
myGame.PlayerHit(0);
}
}
}

/// <summary>
Expand All @@ -34,7 +53,7 @@ private static void HandleUserInput(Snap myGame)
/// <param name="myGame">The details of the game -- mostly top card and scores.</param>
private static void DrawGame(Snap myGame)
{
SwinGame.ClearScreen(Color.White);
SwinGame.DrawBitmap("cardsBoard.png",0,0);

// Draw the top card
Card top = myGame.TopCard;
Expand All @@ -43,15 +62,15 @@ private static void DrawGame(Snap myGame)
SwinGame.DrawText ("Top Card is " + top.ToString (), Color.RoyalBlue, 0, 20);
SwinGame.DrawText ("Player 1 score: " + myGame.Score(0), Color.RoyalBlue, 0, 30);
SwinGame.DrawText ("Player 2 score: " + myGame.Score(1), Color.RoyalBlue, 0, 40);
SwinGame.DrawCell (SwinGame.BitmapNamed ("Cards"), top.CardIndex, 350, 50);
SwinGame.DrawCell (SwinGame.BitmapNamed ("Cards"), top.CardIndex, 521, 153);
}
else
{
SwinGame.DrawText ("No card played yet...", Color.RoyalBlue, 0, 20);
SwinGame.DrawText ("No card played yet..."+myGame.Score(0),Color.White,"GameFont", 0, 30);
}

// Draw the back of the cards... to represent the deck
SwinGame.DrawCell (SwinGame.BitmapNamed ("Cards"), 52, 160, 50);
SwinGame.DrawCell (SwinGame.BitmapNamed ("Cards"), 52, 155, 153);

//Draw onto the screen
SwinGame.RefreshScreen(60);
Expand Down Expand Up @@ -85,5 +104,6 @@ public static void Main()
UpdateGame (myGame);
}
}

}
}
}