// check for snake collisions bool CheckSnakeCollisions(ContinuousSnake testSnake) { foreach (ContinuousSnake snake in snakes) { // run self collision code on the snake if the snakes are the same if (snake == testSnake) { if (snake.SelfCollision()) { return(true); // if snake collided with itself, the snake definetly collided } } else { foreach (BodySegment segment in snake.bodySegments) // check for collision with all body segments of other snakes { if ((testSnake.Collided(segment.picBox) & ContinuousSnake.HEAD_COLLISION) != 0) { return(true); // snake head collided with another snake so return true } } } } // if the function reached this point, not collision occured return(false); }
public override void gameConstruction() { LevelBanner = Properties.Resources.Level_3; // set right start banner for this level base.gameConstruction(); // add blocked apple spawns fixedSpawns = new AppleSpawnPad[] { new AppleSpawnPad(applespawnpad1, prob, engine), new AppleSpawnPad(applespawnpad2, prob, engine), new AppleSpawnPad(applespawnpad3, prob, engine) }; AddObstacles(); AddFoodSpawns(); // get coordinates for snake spawn location int x = spawnPoint.Location.X; int y = spawnPoint.Location.Y; snake = new ContinuousSnake(x, y, this, new Vector(2, 90), 3); engine.AddSnake(snake); // register it with the engine InitialMysterySpawn(); // spawn mysteryboxes in fixed areas engine.AddStationary(new EnemyStationary(guardian, threshold, this)); // register the guardian engine.gate_collider = gate_collider; // register gate collider }
public void ShouldRun(VoidFunction f, ContinuousSnake snake) // true if game is running and is used to make descisions whether or not to run something related to game play { if (snake != null && (snake.Alive && runTimer.Enabled)) // run only if gameplay is should happen { f(); } }
public override void gameConstruction() { // set the Level Banner to right one LevelBanner = Properties.Resources.Level_1; base.gameConstruction(); // run base class game construction AddObstacles(); AddSpawnPads(); engine.AddStationary(new EnemyStationary(guardian, threshold, this)); // register guardian (the integer is - // the size needed to kill the enemy guardian by ramming it // make snake at the "spawn point"(which is really a picturebox i made to be the spawn point) int spawnX = spawnPoint.Location.X; int spawnY = spawnPoint.Location.Y; snake = new ContinuousSnake(spawnX, spawnY, this, new Vector(1, 90), 3); engine.AddSnake(snake);// let the engine know about the snake (so it can run/handle it) // register gate collider wth engine engine.gate_collider = gate_collider; engine.SetMaxFoodItems(maxFoods); // maximum number of food items on screen spawnPoint.Visible = false; // make the spawn point invisible txtSizeNeeded.Text = threshold.ToString(); // show the threshold needed }
public _2Player() { InitializeComponent(); // set the banner LevelBanner = Properties.Resources.two_player; base.CoreBuild(); // add the obstacles engine.obstacles.Add(Obstacle.FromPicture(obstacle1, Color.Brown)); engine.obstacles.Add(Obstacle.FromPicture(obstacle2, Color.Brown)); engine.obstacles.Add(Obstacle.FromPicture(obstacle3, Color.Brown)); engine.obstacles.Add(Obstacle.FromPicture(obstacle4, Color.Brown)); engine.obstacles.Add(Obstacle.FromPicture(obstacle5, Color.Brown)); engine.obstacles.Add(Obstacle.FromPicture(obstacle6, Color.Brown)); engine.obstacles.Add(Obstacle.FromPicture(obstacle7, Color.Brown)); engine.obstacles.Add(Obstacle.FromPicture(obstacle8, Color.Brown)); engine.obstacles.Add(Obstacle.FromPicture(obstacle9, Color.Brown)); engine.obstacles.Add(Obstacle.FromPicture(obstacle10, Color.Brown)); engine.obstacles.Add(Obstacle.FromPicture(obstacle11, Color.Brown)); engine.obstacles.Add(Obstacle.FromPicture(obstacle12, Color.Brown)); engine.obstacles.Add(Obstacle.FromPicture(obstacle13, Color.Brown)); engine.obstacles.Add(Obstacle.FromPicture(obstacle14, Color.Brown)); engine.obstacles.Add(Obstacle.FromPicture(obstacle15, Color.Brown)); engine.obstacles.Add(Obstacle.FromPicture(obstacle16, Color.Brown)); engine.obstacles.Add(Obstacle.FromPicture(obstacle17, Color.Brown)); engine.obstacles.Add(Obstacle.FromPicture(obstacle18, Color.Brown)); engine.obstacles.Add(Obstacle.FromPicture(obstacle19, Color.Brown)); engine.obstacles.Add(Obstacle.FromPicture(obstacle20, Color.Brown)); engine.obstacles.Add(Obstacle.FromPicture(obstacle21, Color.Brown)); // add the snakes snake = new ContinuousSnake(spawnPoint.Location.X, spawnPoint.Location.Y, this, new Vector(3, 90), 3, false); engine.AddSnake(snake); // add the red snake snakeRed = new ContinuousSnake(spawnPointRed.Location.X, spawnPointRed.Location.Y, this, new Vector(3, 90), 3, true); engine.AddSnake(snakeRed); // make the spawnpoints invisible spawnPoint.Visible = false; spawnPointRed.Visible = false; // add the food spawners to a single spawn group FoodSpawnGroup spawnGroup = new FoodSpawnGroup(engine, 0.9); spawnGroup.Add(new AppleSpawnPad(applespawn1, 1, engine)); spawnGroup.Add(new AppleSpawnPad(applespawn2, 1, engine)); spawnGroup.Add(new AppleSpawnPad(applespawn3, 1, engine)); spawnGroup.Add(new AppleSpawnPad(applespawn4, 1, engine)); spawnGroup.Add(new AppleSpawnPad(applespawn5, 1, engine)); spawnGroup.Add(new AppleSpawnPad(applespawn6, 1, engine)); spawnGroup.Add(new AppleSpawnPad(applespawn7, 1, engine)); spawnGroup.Add(new AppleSpawnPad(applespawn8, 1, engine)); spawnGroup.Add(new AppleSpawnPad(applespawn9, 1, engine)); // add it to the engine engine.AddFoodGroupSpawner(spawnGroup); }
void sizeChange(ContinuousSnake snake) // random amount of size change to snake { // get random amount to add or remove int delta = mainForm.engine.GetRandom(-BodyPart.SIZE * 4, BodyPart.SIZE * 4); snake.ChangeSize(delta); finalize(); Finalized = true; // set flag up for deletion by game engine }
void CheckFollowerCollision(ContinuousSnake snake) // kill snake whos head collides with enemy { foreach (EnemyFollower enemy in followers) { if ((snake.Collided(enemy.picBox) & ContinuousSnake.HEAD_COLLISION) != 0) // kill the snake if head collision { Death(snake); } } }
// for when the snake dies public void Death(ContinuousSnake snake) { // make all the segments invisible foreach (BodySegment segment in snake.bodySegments) { segment.picBox.Visible = false; } snake.glorifiedDestroy(); // destroy the whole snake - 0 means the index which is the head // run the lose method on the main form mainForm.CoreLose(); }
// for when the snake attempts to eat the enemy (returns true if eat was successful) public bool AttemptEat(ContinuousSnake snake) { if (CanEat(snake)) // make sure the snake can kill the enemy { // run death animations/functions blahblahblah Death(); return(true); } else { // run attack animations/functions Attack(); return(false); } }
// check for projectile collision with snake void CheckProjectileCollision(ContinuousSnake snake) { foreach (Projectile projectile in projectiles) { if (projectile.finalized) { continue; // don't check the projectile if its finalized } // only kill the snake if it collides with the snake head if ((snake.Collided(projectile.picBox) & ContinuousSnake.HEAD_COLLISION) != 0) { Death(snake); } } }
void sizeScale(ContinuousSnake snake) // randomly scale snake size { // list of possible scales double[] validScales = new double[] { 0.8, 1.5, 2, 2.5 }; // 6 possible scales // get random scale factor int random = mainForm.engine.GetRandom(0, 5); double scale = validScales[random]; // get current length of snake and get size change int length = snake.Length; int newLength = (int)(length * scale + 0.5); // get new length (add 0.5 to round) int delta = newLength - length; snake.ChangeSize(delta); finalize(); Finalized = true; }
void foodCollision(ContinuousSnake snake) { // check for collision with food for (int i = 0; i < foodItems.Count; i++) { // retrieve current food item Food food = foodItems[i]; if ((snake.Collided(food.picBox) & ContinuousSnake.HEAD_COLLISION) != 0) // check for head collision { snake.ChangeSize(food.Change); // change snake size depending on food // delete the food item food.finalize(); foodItems.Remove(food); // subtract one from i to stop from skipping food items i--; } } }
// create objects in the form (coins, obstacles) void gameConstruction() { // make the paused banner pausedBanner = new PictureBox { Image = Properties.Resources.paused, // resize box to fit image SizeMode = PictureBoxSizeMode.AutoSize }; pausedBanner.Location = new Point((Width - pausedBanner.Width) / 2, (Height - pausedBanner.Height) / 2); // center the image // create the game engine engine = new GameEngine(this); // add existent coins engine.AddFood(new Apple(coin1, this)); engine.AddFood(new Apple(coin2, this)); engine.AddFood(new Apple(coin3, this)); engine.AddFood(new Apple(coin4, this)); engine.AddFood(new Apple(coin5, this)); engine.AddFood(new Apple(coin6, this)); engine.AddFood(new Apple(coin7, this)); // add obstacles existent on the map engine.AddObstacle(Obstacle.FromPicture(obstacle1, Color.Brown)); engine.AddObstacle(Obstacle.FromPicture(obstacle2, Color.Brown)); engine.AddObstacle(Obstacle.FromPicture(obstacle3, Color.Brown)); engine.AddObstacle(Obstacle.FromPicture(obstacle4, Color.Red)); // create and add food spawner group FoodSpawnGroup group = new FoodSpawnGroup(engine); group.Add(new AppleSpawnPad(spawnPadPic2, 0.00001, engine)); group.Add(new AppleSpawnPad(spawnPadPic, 0.00001, engine)); // add the second spawner engine.groupSpawners.Add(group); // Add enemies engine.AddStationary(new EnemyStationary(enemy1, 16, this)); // make a new snake snake = new ContinuousSnake(Size.Width / 2, Size.Height / 2, this, new Vector(2, 90), 300); // add it to the engine engine.AddSnake(snake); }
public void Run(List <ContinuousSnake> snakes, List <Obstacle> obstacles) { ContinuousSnake targetSnake = null; // target snake int targetDistance = 0; foreach (ContinuousSnake snake in snakes) { if (targetSnake == null) { targetSnake = snake; // replace target with shortest distance to it's head int thisX = picBox.Location.X + picBox.Width / 2; int thisY = picBox.Location.Y + picBox.Height / 2; int snakeX = snake.snakeHead.picBox.Location.X + snake.snakeHead.picBox.Width / 2; int snakeY = snake.snakeHead.picBox.Location.Y + snake.snakeHead.picBox.Height / 2; // get the distance int deltaX = snakeX - thisX; int deltaY = snakeY - thisY; int distance = (int)(0.5 + Math.Sqrt(Math.Pow(deltaX, 2) + Math.Pow(deltaY, 2))); targetDistance = distance; } else { /*// replace target with shortest distance to it's head * int thisX = picBox.Location.X + picBox.Width / 2; * int thisY = picBox.Location.Y + picBox.Height / 2; * int snakeX = snake.snakeHead.picBox.Location.X + snake.snakeHead.picBox.Width / 2; * int snakeY = snake.snakeHead.picBox.Location.Y + snake.snakeHead.picBox.Height / 2; * // get the distance * int deltaX = snakeX - thisX; * int deltaY = snakeY - thisY; * int distance = (int) (0.5 + Math.Sqrt(Math.Pow(deltaX, 2) + Math.Pow(deltaY, 2))); * // if the distance is less, replace the snake and the distance * if (distance < targetDistance) * { * targetSnake = snake; * targetDistance = distance; * }*/ } } ContinuedRun(targetSnake.snakeHead, snakes, obstacles); }
public void Invoke(ContinuousSnake snake) // run the mystery box on a snake { // get random number to choose an action int random = mainForm.engine.GetRandom(1, 100); if (random > 0 && (random <= 50)) // do speed change { Console.Write("Speed change!"); speedChange(snake); } /*else if (random > 33 && (random <= 67)) // snake size scaling * { * Console.Write("Size scaled!"); * sizeScale(snake); * }*/ else // add/subtract from snake { Console.Write("Size change!"); sizeChange(snake); } }
public override void gameConstruction() { // set the banner for this level LevelBanner = Properties.Resources.Level_4; base.gameConstruction(); AddObstacles(); snake = new ContinuousSnake(spawnPoint.Location.X, spawnPoint.Location.Y, this, new Vector(3, 90), 3); engine.AddSnake(snake); // register the snake engine.AddStationary(new EnemyStationary(guardian, threshold, this)); // create the guardian engine.gate_collider = gate_collider; // register the gate collider spawnPoint.Visible = false; // make the spawn point invisible // add followers follower = new EnemyFollower(follower1, 2, this); engine.followers.Add(follower); engine.followers.Add(new EnemyFollower(follower2, 2, this)); engine.followers.Add(new EnemyFollower(follower3, 2, this)); engine.followers.Add(new EnemyFollower(follower4, 2, this)); // show threshold needed to beat the game txtSizeNeeded.Text = threshold.ToString(); // add apple spawn pads to a single spawn group FoodSpawnGroup spawnGroup = new FoodSpawnGroup(engine, 0.6); spawnGroup.Add(new AppleSpawnPad(spawnpad1, 0.6, engine)); spawnGroup.Add(new AppleSpawnPad(spawnpad2, 0.6, engine)); spawnGroup.Add(new AppleSpawnPad(spawnpad3, 0.6, engine)); spawnGroup.Add(new AppleSpawnPad(spawnpad4, 0.6, engine)); spawnGroup.Add(new AppleSpawnPad(spawnpad5, 0.6, engine)); spawnGroup.Add(new AppleSpawnPad(spawnpad6, 0.6, engine)); spawnGroup.Add(new AppleSpawnPad(spawnpad7, 0.6, engine)); spawnGroup.Add(new AppleSpawnPad(spawnpad8, 0.6, engine)); spawnGroup.Add(new AppleSpawnPad(spawnpad9, 0.6, engine)); engine.AddFoodGroupSpawner(spawnGroup); }
public override void gameConstruction() { // set the level banner LevelBanner = Properties.Resources.Level_Boss; base.gameConstruction(); // rotate gate image and add it Image gateImg = Properties.Resources.Gate; gateImg.RotateFlip(RotateFlipType.Rotate270FlipXY); Gate.Image = gateImg; Gate.SizeMode = PictureBoxSizeMode.StretchImage; // add obstacles engine.AddObstacle(Obstacle.FromPicture(obstacle1, Color.Brown)); engine.AddObstacle(Obstacle.FromPicture(obstacle2, Color.Brown)); engine.AddObstacle(Obstacle.FromPicture(obstacle3, Color.Brown)); engine.AddObstacle(Obstacle.FromPicture(obstacle4, Color.Brown)); engine.AddObstacle(Obstacle.FromPicture(obstacle5, Color.Brown)); engine.AddObstacle(Obstacle.FromPicture(obstacle6, Color.Brown)); engine.AddObstacle(Obstacle.FromPicture(obstacle7, Color.Brown)); engine.AddObstacle(Obstacle.FromPicture(obstacle8, Color.Brown)); engine.AddObstacle(Obstacle.FromPicture(obstacle9, Color.Brown)); engine.AddObstacle(Obstacle.FromPicture(obstacle10, Color.Brown)); boss = new Boss(bossPic, 100, 8000, this); // create the boss // add the snake snake = new ContinuousSnake(spawnPoint.Location.X, spawnPoint.Location.Y, this, new Vector(3, 90), 3); engine.AddSnake(snake); // register the snake with the engine engine.gate_collider = gate_collider; // add the gate collider // add the apple spawn pads engine.AddFoodSpawner(new AppleSpawnPad(applespawnpad1, 0.5, engine)); engine.AddFoodSpawner(new AppleSpawnPad(applespawnpad2, 0.5, engine)); engine.AddFoodSpawner(new AppleSpawnPad(applespawnpad3, 0.5, engine)); // make the spawn point invisible spawnPoint.Visible = false; }
public override void gameConstruction() { // set correct level banner to be displayed at the beginning LevelBanner = Properties.Resources.Level_2; base.gameConstruction(); AddObstacles(); // get coord for spawn location int spawnX = spawnPoint.Location.X; int spawnY = spawnPoint.Location.Y; snake = new ContinuousSnake(spawnX, spawnY, this, new Vector(2, 90), 4); engine.AddSnake(snake); // register the snake with the engine // add the standalone spawnpad DespawnAppleSpawnPad standalone = new DespawnAppleSpawnPad(standalonespawnpad, 0.3, 6000, 6000, this); engine.AddFoodSpawner(standalone); engine.SetFoodInterval(50); engine.SetMaxFoodItems(maxFoods);// set max number of foods onsreen // register the guardian and gate collider engine.AddStationary(new EnemyStationary(guardian, threshold, this)); engine.gate_collider = gate_collider; // add the giant food spawn group CreateFoodSpawnGroup(); txtSizeNeeded.Text = threshold.ToString(); // tell the user of the size needed spawnPoint.Visible = false; // make the spawn point invisible // create mystery spawn pad engine.mysterySpawnPads.Add(new MysteryBoxSpawnPad(mysteryspawn, 0.4, this)); // test mystery spawn pad /*MysteryBoxSpawnPad spawnTest = new MysteryBoxSpawnPad(testmysteryspawn, 1, this); * engine.mysterySpawnPads.Add(spawnTest);*/ }
// run all enemies (movement, collision checking) void RunEnemies(ContinuousSnake snake) { foreach (EnemyStationary enemy in stationaryEnemies) // run all stationary enemies { // check if the snake head is in position to eat the enemy if (enemy.HeadCollision(snake)) { if (enemy.Alive) // dont attempt to eat if the enemy is dead! { // try eating the enemy if (enemy.AttemptEat(snake)) { } else { Death(snake); // kill the snake if the eating failed } } } enemy.Run(); } }
// run mysterbox handler void RunMysteryBoxes(ContinuousSnake snake) { List <MysteryBox> removeQueue = new List <MysteryBox>(); foreach (MysteryBox box in mysteryBoxes) // check all boxes for collision { if (snake.Collided(box.picBox) != 0) { box.Invoke(snake); // run the box's function if collided } if (box.Finalized) { removeQueue.Add(box); } } foreach (MysteryBox box in removeQueue) { mysteryBoxes.Remove(box); } RunMysterySpawners(); }
void speedChange(ContinuousSnake snake) { this.snake = snake; // store previous speed for reset prevSpeed = snake.Velocity.Magnitude; // get random speed int speed = mainForm.engine.GetRandom(1, 4); snake.Velocity.Magnitude = speed; // timer stuff here speedTimer = new System.Timers.Timer(); // get random interval to make the speed for (between 1 and 5 seconds) int speedDuration = mainForm.engine.GetRandom(1000, 5000); speedTimer.Interval = speedDuration; // run event at timer's first and only tick to reset snake speed and setup for deletion speedTimer.Elapsed += new System.Timers.ElapsedEventHandler(speedTimerTick); // start the timer speedTimer.Start(); Finalized = true; finalize(); }
// check for head collisions with snake public bool HeadCollision(ContinuousSnake snake) { return((snake.Collided(Bounds) & ContinuousSnake.HEAD_COLLISION) > 0); // true if head collision occurred }
// check for body collision with snake public bool BodyCollision(ContinuousSnake snake) { return((snake.Collided(Bounds) & ContinuousSnake.BODY_COLLISION) > 0); // true if body collision occurred }
public void AddSnake(ContinuousSnake snake) { snakes.Add(snake); }
// true if snake can eat the enemy public bool CanEat(ContinuousSnake snake) { double unitsLength = (double)snake.Length / BodyPart.SIZE;// get the snake length in body part size units return(unitsLength >= threshold); }