Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////////////////////////////////////////////////////////////////
- // Copyright 2013, CompuScholar, Inc.
- //
- // This source code is for use by the students and teachers who
- // have purchased the corresponding TeenCoder or KidCoder product.
- // It may not be transmitted to other parties for any reason
- // without the written consent of CompuScholar, Inc.
- // This source is provided as-is for educational purposes only.
- // CompuScholar, Inc. makes no warranty and assumes
- // no liability regarding the functionality of this program.
- //
- ////////////////////////////////////////////////////////////////
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Audio;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.GamerServices;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using Microsoft.Xna.Framework.Media;
- using Microsoft.Xna.Framework.Net;
- using Microsoft.Xna.Framework.Storage;
- using SpriteLibrary;
- namespace BumperCars
- {
- public class BumperCars : Microsoft.Xna.Framework.Game
- {
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
- Texture2D car1Texture;
- Texture2D car2Texture;
- Texture2D oilSlickTexture;
- Texture2D spinningCoinTexture;
- KeyboardState oldKeyState;
- GamePadState oldGamePadState1;
- GamePadState oldGamePadState2;
- bool aiPlayerActive = false;
- const int NUM_OIL_SLICKS = 3;
- const int SCORE_TO_WIN = 10;
- const double ACCELERATION_FACTOR = 0.02;
- const double MAX_SPEED = 3.0;
- const double STEER_FACTOR = 5;
- Car car1;
- Car car2;
- SoundEffect bounceSound;
- SoundEffect coinSound;
- SoundEffect oilSlickSound;
- SoundEffect backgroundMusic;
- SoundEffectInstance backMusicInstance;
- SoundEffectInstance oilSlickInstance;
- SpriteFont gameFont;
- Sprite coin;
- LinkedList<Sprite> oilSlicks = new LinkedList<Sprite>();
- Random random = new System.Random();
- double lastOilSlick = 0;
- enum GameScreen
- {
- MENU = 0,
- PLAYING = 1,
- GAMEOVER = 2
- }
- GameScreen currentScreen;
- string displayMessage;
- public BumperCars()
- {
- graphics = new GraphicsDeviceManager(this);
- Content.RootDirectory = "Content";
- }
- protected override void Initialize()
- {
- currentScreen = GameScreen.MENU;
- base.Initialize();
- }
- protected override void LoadContent()
- {
- spriteBatch = new SpriteBatch(GraphicsDevice);
- gameFont = this.Content.Load<SpriteFont>("GameFont");
- car1Texture = this.Content.Load<Texture2D>("Images\\bumper_car1");
- car2Texture = this.Content.Load<Texture2D>("Images\\bumper_car2");
- oilSlickTexture = this.Content.Load<Texture2D>("Images\\oil_slick");
- spinningCoinTexture =
- this.Content.Load<Texture2D>("Images\\spinning_coin");
- bounceSound =
- this.Content.Load<SoundEffect>("Sound\\bumpercar_bounce");
- coinSound = this.Content.Load<SoundEffect>("Sound\\bumpercar_coin");
- oilSlickSound =
- this.Content.Load<SoundEffect>("Sound\\bumpercar_oilSlick");
- backgroundMusic =
- this.Content.Load<SoundEffect>("Sound\\background_music");
- backMusicInstance = backgroundMusic.CreateInstance();
- backMusicInstance.IsLooped = true;
- backMusicInstance.Volume = .5f;
- oilSlickInstance = oilSlickSound.CreateInstance();
- }
- protected override void UnloadContent()
- {
- }
- private void startGame()
- {
- currentScreen = GameScreen.PLAYING;
- car1 = new Car();
- car1.SetTexture(car1Texture);
- car1.Scale = new Vector2(1.5f, 1.5f);
- car1.UpperLeft = new Vector2(10,
- this.GraphicsDevice.Viewport.Height / 2);
- car1.SetVelocity(1.0, 0.0);
- car1.RotationAngle = 0;
- car1.Origin = car1.GetCenter();
- car1.MaxSpeed = MAX_SPEED;
- car1.Score = 0;
- car2 = new Car();
- car2.SetTexture(car2Texture);
- car2.Scale = new Vector2(1.5f, 1.5f);
- car2.UpperLeft = new Vector2(
- this.GraphicsDevice.Viewport.Width - 10 - car2.GetWidth(),
- this.GraphicsDevice.Viewport.Height / 2);
- car2.SetVelocity(1.0, 0.0);
- car2.RotationAngle = 180;
- car2.RotationAngle = 0;
- car2.Origin = car2.GetCenter();
- car2.MaxSpeed = MAX_SPEED;
- car2.Score = 0;
- coin = new Sprite();
- coin.SetTexture(spinningCoinTexture, 5);
- coin.AnimationInterval = 200;
- coin.Scale = new Vector2(1.5f, 1.5f);
- placeCoin();
- oilSlicks.Clear();
- for (int i = 0; i < NUM_OIL_SLICKS; i++)
- {
- newOilSlick();
- }
- backMusicInstance.Play();
- }
- private void stopGame(String message)
- {
- currentScreen = GameScreen.GAMEOVER;
- displayMessage = message;
- backMusicInstance.Stop();
- }
- protected override void Update(GameTime gameTime)
- {
- KeyboardState keyState = Keyboard.GetState();
- if (oldKeyState == null)
- oldKeyState = keyState;
- GamePadState currentGamePad1 = GamePad.GetState(PlayerIndex.One);
- GamePadState currentGamePad2 = GamePad.GetState(PlayerIndex.Two);
- if (oldGamePadState1 == null)
- oldGamePadState1 = currentGamePad1;
- if (oldGamePadState2 == null)
- oldGamePadState2 = currentGamePad2;
- switch (currentScreen)
- {
- case GameScreen.MENU:
- updateMenu(gameTime, keyState);
- break;
- case GameScreen.PLAYING:
- updatePlaying(gameTime, keyState);
- break;
- case GameScreen.GAMEOVER:
- updateGameover(gameTime, keyState);
- break;
- }
- oldKeyState = keyState;
- oldGamePadState1 = currentGamePad1;
- oldGamePadState2 = currentGamePad2;
- base.Update(gameTime);
- }
- private void updateMenu(GameTime gameTime, KeyboardState keyState)
- {
- if (keyState.IsKeyDown(Keys.Escape) &&
- oldKeyState.IsKeyUp(Keys.Escape))
- {
- Exit();
- }
- if (keyState.IsKeyDown(Keys.D1) && oldKeyState.IsKeyUp(Keys.D1))
- {
- aiPlayerActive = true;
- startGame();
- }
- else if (keyState.IsKeyDown(Keys.D2) &&
- oldKeyState.IsKeyUp(Keys.D2))
- {
- aiPlayerActive = false;
- startGame();
- }
- }
- private void updateMenuXbox(GamePadState player1GamePad,
- GamePadState player2Gamepad)
- {
- if ((player1GamePad.Buttons.Back == ButtonState.Pressed &&
- oldGamePadState1.Buttons.Back == ButtonState.Released) ||
- (player2Gamepad.Buttons.Back == ButtonState.Pressed &&
- oldGamePadState2.Buttons.Back == ButtonState.Released))
- {
- Exit();
- }
- if ((player1GamePad.Buttons.A == ButtonState.Pressed &&
- oldGamePadState1.Buttons.A == ButtonState.Released) ||
- (player2Gamepad.Buttons.A == ButtonState.Pressed &&
- oldGamePadState2.Buttons.A == ButtonState.Released))
- {
- aiPlayerActive = true;
- startGame();
- }
- // if 'B' pressed
- if ((player1GamePad.Buttons.B == ButtonState.Pressed &&
- oldGamePadState1.Buttons.B == ButtonState.Released) ||
- (player2Gamepad.Buttons.B == ButtonState.Pressed &&
- oldGamePadState2.Buttons.B == ButtonState.Released))
- {
- aiPlayerActive = false;
- startGame();
- }
- }
- private void updatePlaying(GameTime gameTime, KeyboardState keyState)
- {
- if (keyState.IsKeyDown(Keys.A))
- {
- car1.Steer(STEER_FACTOR);
- }
- if (keyState.IsKeyDown(Keys.D))
- {
- car1.Steer(-STEER_FACTOR);
- }
- if (keyState.IsKeyDown(Keys.W))
- {
- car1.Accelerate(ACCELERATION_FACTOR);
- }
- if (keyState.IsKeyDown(Keys.X))
- {
- car1.Accelerate(-ACCELERATION_FACTOR);
- if (!aiPlayerActive)
- {
- if (keyState.IsKeyDown(Keys.Left))
- {
- car2.Steer(STEER_FACTOR);
- }
- if (keyState.IsKeyDown(Keys.Right))
- {
- car2.Steer(-STEER_FACTOR);
- }
- if (keyState.IsKeyDown(Keys.Up))
- {
- car2.Accelerate(ACCELERATION_FACTOR);
- }
- if (keyState.IsKeyDown(Keys.Down))
- {
- car2.Accelerate(-ACCELERATION_FACTOR);
- }
- }
- else
- {
- DoAI();
- }
- moveCars();
- checkTimedEvents(gameTime);
- coin.Animate(gameTime);
- }
- }
- private void updatePlayingXbox(GameTime gameTime,
- GamePadState currentGamePad1,
- GamePadState currentGamePad2)
- {
- if (currentGamePad1.ThumbSticks.Left.X < 0)
- {
- car1.Steer(STEER_FACTOR);
- }
- if (currentGamePad1.ThumbSticks.Left.X > 0)
- {
- car1.Steer(-STEER_FACTOR);
- }
- if (currentGamePad1.ThumbSticks.Left.Y > 0)
- {
- car1.Accelerate(ACCELERATION_FACTOR);
- }
- if (currentGamePad1.ThumbSticks.Left.Y < 0)
- {
- car1.Accelerate(-ACCELERATION_FACTOR);
- }
- if (!aiPlayerActive)
- {
- if (currentGamePad2.ThumbSticks.Left.X < 0)
- {
- car2.Steer(STEER_FACTOR);
- }
- if (currentGamePad2.ThumbSticks.Left.X > 0)
- {
- car2.Steer(-STEER_FACTOR);
- }
- if (currentGamePad2.ThumbSticks.Left.Y > 0)
- {
- car2.Accelerate(ACCELERATION_FACTOR);
- }
- if (currentGamePad2.ThumbSticks.Left.Y < 0)
- {
- car2.Accelerate(-ACCELERATION_FACTOR);
- }
- }
- else
- {
- DoAI();
- }
- moveCars();
- checkTimedEvents(gameTime);
- coin.Animate(gameTime);
- }
- private int findQuadrant(double angle)
- {
- if ((angle >= 0) && (angle < 90))
- return 1;
- if ((angle >= 90) && (angle < 180))
- return 2;
- if ((angle >= 180) && (angle < 270))
- return 3;
- return 4;
- }
- private int findLeftQuadrant(int quadrant)
- {
- if (quadrant < 3)
- return quadrant + 1;
- else
- return 0;
- }
- private int findRightQuadrant(int quadrant)
- {
- if (quadrant > 1)
- return quadrant - 1;
- else
- return 4;
- }
- private void DoAI()
- {
- double dx = coin.UpperLeft.X - car2.UpperLeft.X;
- double dy = coin.UpperLeft.Y - car2.UpperLeft.Y;
- double angleToCoin = Sprite.CalculateDirectionAngle(
- new Vector2((float)dx, (float)dy));
- double carDirectionAngle = car2.GetDirectionAngle();
- int coinAngleQuadrant = findQuadrant(angleToCoin);
- int carAngleQuadrant = findQuadrant(carDirectionAngle);
- if (coinAngleQuadrant == carAngleQuadrant)
- {
- double angleDifference = carDirectionAngle - angleToCoin;
- if (Math.Abs(angleDifference) > STEER_FACTOR)
- {
- if (angleDifference < 0)
- car2.Steer(STEER_FACTOR);
- else
- car2.Steer(-STEER_FACTOR);
- }
- }
- else if (findLeftQuadrant(carAngleQuadrant) == coinAngleQuadrant)
- {
- car2.Steer(STEER_FACTOR);
- }
- else if (findRightQuadrant(carAngleQuadrant) == coinAngleQuadrant)
- {
- car2.Steer(-STEER_FACTOR);
- }
- else
- {
- car2.Steer(STEER_FACTOR);
- }
- car2.Accelerate(ACCELERATION_FACTOR);
- }
- private void updateGameover(GameTime gameTime, KeyboardState keyState)
- {
- if (keyState.IsKeyDown(Keys.Space) &&
- oldKeyState.IsKeyUp(Keys.Space))
- {
- currentScreen = GameScreen.MENU;
- }
- }
- private void updateGameoverXbox(GamePadState player1GamePad,
- GamePadState player2Gamepad)
- {
- if ((player1GamePad.Buttons.Back == ButtonState.Pressed &&
- oldGamePadState1.Buttons.Back == ButtonState.Released) ||
- (player2Gamepad.Buttons.Back == ButtonState.Pressed &&
- oldGamePadState2.Buttons.Back == ButtonState.Released))
- {
- currentScreen = GameScreen.MENU;
- }
- }
- private void checkTimedEvents(GameTime gameTime)
- {
- if ((gameTime.TotalGameTime.TotalMilliseconds - lastOilSlick) > 5000)
- {
- newOilSlick();
- lastOilSlick = gameTime.TotalGameTime.TotalMilliseconds;
- }
- }
- private void placeCoin()
- {
- coin.UpperLeft.X = random.Next(0,
- this.GraphicsDevice.Viewport.Width - coin.GetWidth());
- coin.UpperLeft.Y = random.Next(0,
- this.GraphicsDevice.Viewport.Height - coin.GetHeight());
- }
- private void moveCars()
- {
- if (car1.MoveAndReflect(this.GraphicsDevice.Viewport.Width,
- this.GraphicsDevice.Viewport.Height))
- bounceSound.Play();
- if (car2.MoveAndReflect(this.GraphicsDevice.Viewport.Width,
- this.GraphicsDevice.Viewport.Height))
- bounceSound.Play();
- if (car1.CheckBump(car2, this.GraphicsDevice.Viewport.Width,
- this.GraphicsDevice.Viewport.Height))
- bounceSound.Play();
- if (car1.IsCollided(coin))
- {
- car1.Score++;
- placeCoin();
- coinSound.Play();
- }
- if (car2.IsCollided(coin))
- {
- car2.Score++;
- placeCoin();
- coinSound.Play();
- }
- foreach (Sprite slick in oilSlicks)
- {
- if (car1.IsCollided(slick))
- {
- car1.SetSpeedAndDirection(0.5, car1.GetDirectionAngle());
- oilSlickInstance.Play();
- }
- if (car2.IsCollided(slick))
- {
- car2.SetSpeedAndDirection(0.5, car2.GetDirectionAngle());
- oilSlickInstance.Play();
- }
- }
- if (car1.Score >= SCORE_TO_WIN)
- {
- stopGame("Player 1 wins!");
- }
- else if (car2.Score >= SCORE_TO_WIN)
- {
- stopGame("Player 2 wins!");
- }
- }
- private void newOilSlick()
- {
- Sprite slick = new Sprite();
- slick.SetTexture(oilSlickTexture);
- slick.Scale = new Vector2(2.0f, 2.0f);
- slick.UpperLeft.X = random.Next(0,
- this.GraphicsDevice.Viewport.Width - slick.GetWidth());
- slick.UpperLeft.Y = random.Next(0,
- this.GraphicsDevice.Viewport.Height - slick.GetHeight());
- oilSlicks.AddLast(slick);
- if (oilSlicks.Count > NUM_OIL_SLICKS)
- {
- oilSlicks.RemoveFirst();
- }
- }
- /// <summary>
- /// This is called when the game should draw itself.
- /// </summary>
- /// <param name="gameTime">Provides a snapshot of timing values.</param>
- protected override void Draw(GameTime gameTime)
- {
- GraphicsDevice.Clear(Color.CornflowerBlue);
- spriteBatch.Begin();
- switch (currentScreen)
- {
- case GameScreen.MENU:
- drawMenu(gameTime);
- break;
- case GameScreen.PLAYING:
- drawPlaying(gameTime);
- break;
- case GameScreen.GAMEOVER:
- drawGameOver(gameTime);
- break;
- }
- spriteBatch.End();
- base.Draw(gameTime);
- }
- private void drawMenu(GameTime gameTime)
- {
- spriteBatch.DrawString(gameFont,
- "Press 1 for single-player game",
- new Vector2(10, 10), Color.Black);
- spriteBatch.DrawString(gameFont,
- "Press 2 for two-player local game",
- new Vector2(10, 30), Color.Black);
- spriteBatch.DrawString(gameFont,
- "Press ESC (or Back) to exit",
- new Vector2(10, 50), Color.Black);
- }
- private void drawPlaying(GameTime gameTime)
- {
- foreach (Sprite s in oilSlicks)
- {
- s.Draw(spriteBatch);
- }
- coin.Draw(spriteBatch);
- spriteBatch.DrawString(gameFont, "Player 1 Score: " + car1.Score,
- new Vector2(10, 10), Color.Red);
- spriteBatch.DrawString(gameFont, "Player 2 Score: " + car2.Score,
- new Vector2(10, 30), Color.Yellow);
- car1.Draw(spriteBatch);
- car2.Draw(spriteBatch);
- }
- private void drawGameOver(GameTime gameTime)
- {
- spriteBatch.DrawString(gameFont, displayMessage,
- new Vector2(this.GraphicsDevice.Viewport.Width / 2 - 100,
- this.GraphicsDevice.Viewport.Height / 2 - 10), Color.Black);
- spriteBatch.DrawString(gameFont,
- "Press <Space> to return to menu",
- new Vector2(this.GraphicsDevice.Viewport.Width / 2 - 100,
- this.GraphicsDevice.Viewport.Height / 2 + 10), Color.Black);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement