using System; /** * A game of Pick up the Peanuts. Pick up the Peanuts has a player * and an elephant who race to pick up peanuts. * * @author Jim Glenn * @version 2K9.02 translated to C# (still needs XML comments * @version 2K9 translated to Java * @version 1.0 1982 or so in Atari BASIC */ namespace Peanuts { public class PeanutsModel : GameModel { /** * The objects in this game. Distinguished objects (OK, all of them) * are kept separately from the list for easy access. */ private Player player; private Elephant elephant; private Peanut peanut; private const long ticksPerSecond = 10000000; /** * The amount of time this game has been going, in seconds. */ private long startTime; /** * The length of a game, in milliseconds. */ private const long gameLength = 180 * ticksPerSecond; /** * The player's score in this game. */ private int score; private Random rand; /** * Creates a new Pick up the Peanut game with player and elephant * at their starting positions (left and right respectively) * and a randomly placed peanut. */ public PeanutsModel() { // create a random number generator rand = new Random(); // create player near middle of left edge player = new Player(0.1, 0.5); AddSprite(player); // create elephant near middle of right edge elephant = new Elephant(0.9, 0.5); AddSprite(elephant); // throw a peanut at a random location MakePeanut(); // initialize player's score score = 0; // record start time startTime = DateTime.Now.Ticks; } /** * Returns the player's score in this game. * * @return the player's score */ public int GetPlayerScore() { return score; } /** * Returns the current peanut in this game. * * @return the peanut */ public Peanut GetPeanut() { return peanut; } /** * Forwards move events to the player. The direction is given * as an x- and a y-component. * * @param dx -1, 0, or 1 * @param dy -1, 0, or 1 */ public void MovePlayer(int dx, int dy) { player.Move(dx, dy); } /** * Climbs the player out of a hole. */ public void ClimbPlayer() { player.Climb(); } /** * Places a new peanut randomly in this game. */ private void MakePeanut() { if (peanut != null) { RemoveSprite(peanut); } peanut = new Peanut(rand.NextDouble(), rand.NextDouble()); AddSprite(peanut); } /** * Updates this game. */ public override void Update() { if (DateTime.Now.Ticks - startTime < gameLength) { base.Update(); } } /** * Returns the amount of time left in this game, in seconds. * * @return the time left */ public int GetTimeLeft() { return (int)((gameLength - (DateTime.Now.Ticks - startTime)) / ticksPerSecond); } /** * Handles collisions between the given objects. * * @param s1 a sprite * @param s2 a sprite that has collided with s1 */ protected override void HandleCollision(Sprite s1, Sprite s2) { // do nothing when something collides with a score if (s1 is ValueDisplay || s2 is ValueDisplay) { return; } if (s1 is Peanut || s2 is Peanut) { // swap to make s1 the one that ate the peanut if (s1 is Peanut) { Sprite temp = s1; s1 = s2; s2 = temp; } if (s1 is Player) { // player ate the peanut Peanut p = s2 as Peanut; int value = p.GetValue(); score += value; AddSprite(new ValueDisplay(p.GetX(), p.GetY(), value)); } else if (s1 is Elephant) { // elephant ate the peanut (s1 as Elephant).EatPeanut(); } else { // eww! } // make a new peanut MakePeanut(); } else if (s1 is Elephant && s2 is Player || s1 is Player && s2 is Elephant) { // elephant tramples player player.Trample(); } else if (s1 is Player && s2 is Hole) { // player steps in hole player.StepInHole(); RemoveSprite(s2); } else if (s1 is Hole && s2 is Player) { // hole steps on player player.StepInHole(); RemoveSprite(s1); } } } }