d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Want To Make A Easy 1k Fg? > Yes
Add Reply New Topic New Poll
Member
Posts: 3,392
Joined: Oct 9 2009
Gold: 30.00
Oct 3 2012 11:12am
I had to an assignment and I completely forgot to do it.

Its due in an hour,even less.

this is what I have to do:

You will notice the ship is drawn on top of the score; the ship should be drawn under the score. (Game1.cs – Draw())

Change the color of the score and health to red. (Game1.cs – Draw())

When an enemy is created, give it a random speed between 2 and 10. (Enemy.cs – Initialize())

Faster enemies should do more damage. The damage of an enemy should be twice its speed. (Enemy.cs – Initialize())

Faster enemies should be worth more. The value of an enemy should be ten times its speed. (Enemy.cs – Initialize())

When the player’s health hits 0, end the game. (Game1.cs – UpdatePlayer())

Do not allow the player to move more than 1/3 of the way to the right. (Game1.cs – UpdatePlayer())

Currently the game auto-fires the projectiles. Only fire a projectile if the “Space Bar” key on the keyboard or the “A” button on the controller is pressed. (Game1.cs – UpdatePlayer())

Currently an enemy is created every 1 second. If the player’s score hits 500, create enemies every ½ second. If the player’s score hits 1000, create enemies every ¼ second. (Game1.cs – UpdateEnemies())

Add something of your own choosing! Be creative!



Member
Posts: 3,392
Joined: Oct 9 2009
Gold: 30.00
Oct 3 2012 11:13am
Here is the Code:

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;

namespace Shooter
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;

// Represents the player
Player player;

// Keyboard states used to determine key presses
KeyboardState currentKeyboardState;
KeyboardState previousKeyboardState;

// Gamepad states used to determine button presses
GamePadState currentGamePadState;
GamePadState previousGamePadState;

// A movement speed for the player
float playerMoveSpeed;

Texture2D mainBackground;

// Parallaxing Layers
ParallaxingBackground bgLayer1;
ParallaxingBackground bgLayer2;


// Enemies
Texture2D enemyTexture;
List<Enemy> enemies;

// The rate at which the enemies appear
TimeSpan enemySpawnTime;
TimeSpan previousSpawnTime;

// A random number generator
Random random;

Texture2D projectileTexture;
List<Projectile> projectiles;

// The rate of fire of the player laser
TimeSpan fireTime;
TimeSpan previousFireTime;

Texture2D explosionTexture;
List<Animation> explosions;

//Number that holds the player score
float score;
// The font used to display UI elements
SpriteFont font;

public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}

/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
// Initialize the player class
player = new Player();

// Set a constant player move speed
playerMoveSpeed = 8.0f;

bgLayer1 = new ParallaxingBackground();
bgLayer2 = new ParallaxingBackground();

// Initialize the enemies list
enemies = new List<Enemy>();

// Set the time keepers to zero
previousSpawnTime = TimeSpan.Zero;

// Used to determine how fast enemy respawns
enemySpawnTime = TimeSpan.FromSeconds(1.0f);

// Initialize our random number generator
random = new Random();

projectiles = new List<Projectile>();

// Set the laser to fire every quarter second
fireTime = TimeSpan.FromSeconds(.15f);

explosions = new List<Animation>();

//Set player's score to zero
score = 0;

base.Initialize();
}

/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
// Load the player resources
// Load the player resources
Animation playerAnimation = new Animation();
Texture2D playerTexture = Content.Load<Texture2D>("shipAnimation");
playerAnimation.Initialize(playerTexture, Vector2.Zero, 115, 69, 8, 30, Color.White, 1f, true);

Vector2 playerPosition = new Vector2(GraphicsDevice.Viewport.TitleSafeArea.X, GraphicsDevice.Viewport.TitleSafeArea.Y + GraphicsDevice.Viewport.TitleSafeArea.Height / 2);
player.Initialize(playerAnimation, playerPosition);


// Load the parallaxing background
bgLayer1.Initialize(Content, "bgLayer1", GraphicsDevice.Viewport.Width, -1);
bgLayer2.Initialize(Content, "bgLayer2", GraphicsDevice.Viewport.Width, -2);

mainBackground = Content.Load<Texture2D>("mainbackground");

enemyTexture = Content.Load<Texture2D>("mineAnimation");

projectileTexture = Content.Load<Texture2D>("laser");

explosionTexture = Content.Load<Texture2D>("explosion");

font = Content.Load<SpriteFont>("gameFont");

// TODO: use this.Content to load your game content here
}

/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}

/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();

// TODO: Add your update logic here
// Save the previous state of the keyboard and game pad so we can determinesingle key/button presses
previousGamePadState = currentGamePadState;
previousKeyboardState = currentKeyboardState;

// Read the current state of the keyboard and gamepad and store it
currentKeyboardState = Keyboard.GetState();
currentGamePadState = GamePad.GetState(PlayerIndex.One);

bgLayer1.Update();
bgLayer2.Update();

//Update the player
UpdatePlayer(gameTime);

// Update the enemies
UpdateEnemies(gameTime);

UpdateCollision();

UpdateProjectiles();

UpdateExplosions(gameTime);

base.Update(gameTime);
}

private void AddEnemy()
{
// Create the animation object
Animation enemyAnimation = new Animation();

// Initialize the animation with the correct animation information
enemyAnimation.Initialize(enemyTexture, Vector2.Zero, 47, 61, 8, 30, Color.White, 1f, true);

// Randomly generate the position of the enemy
Vector2 position = new Vector2(GraphicsDevice.Viewport.Width + enemyTexture.Width / 2, random.Next(100, GraphicsDevice.Viewport.Height - 100));

// Create an enemy
Enemy enemy = new Enemy();

// Initialize the enemy
enemy.Initialize(enemyAnimation, position);

// Add the enemy to the active enemies list
enemies.Add(enemy);
}

private void UpdateEnemies(GameTime gameTime)
{
// Spawn a new enemy enemy every 1 second
if (gameTime.TotalGameTime - previousSpawnTime > enemySpawnTime)
{
previousSpawnTime = gameTime.TotalGameTime;

// Add an Enemy
AddEnemy();
}

// Update the Enemies
for (int i = enemies.Count - 1; i >= 0; i--)
{
enemies[i].Update(gameTime);

if (enemies[i].Active == false)
{
// If not active and health <= 0
if (enemies[i].Health <= 0)
{
// Add an explosion
AddExplosion(enemies[i].Position);
score += enemies[i].Value;
}
enemies.RemoveAt(i);
}
}
}

private void UpdateExplosions(GameTime gameTime)
{
for (int i = explosions.Count - 1; i >= 0; i--)
{
explosions[i].Update(gameTime);
if (explosions[i].Active == false)
{
explosions.RemoveAt(i);
}
}
}

private void AddExplosion(Vector2 position)
{
Animation explosion = new Animation();
explosion.Initialize(explosionTexture, position, 134, 134, 12, 45, Color.White, 1f, false);
explosions.Add(explosion);
}

private void UpdatePlayer(GameTime gameTime)
{
player.Update(gameTime);

// Get Thumbstick Controls
player.Position.X += currentGamePadState.ThumbSticks.Left.X * playerMoveSpeed;
player.Position.Y -= currentGamePadState.ThumbSticks.Left.Y * playerMoveSpeed;

// Use the Keyboard / Dpad
if (currentKeyboardState.IsKeyDown(Keys.Left) ||
currentGamePadState.DPad.Left == ButtonState.Pressed)
{
player.Position.X -= playerMoveSpeed;
}
if (currentKeyboardState.IsKeyDown(Keys.Right) ||
currentGamePadState.DPad.Right == ButtonState.Pressed)
{
player.Position.X += playerMoveSpeed;
}
if (currentKeyboardState.IsKeyDown(Keys.Up) ||
currentGamePadState.DPad.Up == ButtonState.Pressed)
{
player.Position.Y -= playerMoveSpeed;
}
if (currentKeyboardState.IsKeyDown(Keys.Down) ||
currentGamePadState.DPad.Down == ButtonState.Pressed)
{
player.Position.Y += playerMoveSpeed;
}

// Make sure that the player does not go out of bounds
player.Position.X = MathHelper.Clamp(player.Position.X, 0, GraphicsDevice.Viewport.Width - player.Width);
player.Position.Y = MathHelper.Clamp(player.Position.Y, 0, GraphicsDevice.Viewport.Height - player.Height);

// Fire only every interval we set as the fireTime
if (gameTime.TotalGameTime - previousFireTime > fireTime)
{
// Reset our current time
previousFireTime = gameTime.TotalGameTime;

// Add the projectile, but add it to the front and center of the player
AddProjectile(player.Position + new Vector2(player.Width / 2, 0));
}

if (player.Health <= 0)
{
player.Health = 100;
score = 0;
}

}

private void UpdateProjectiles()
{
// Update the Projectiles
for (int i = projectiles.Count - 1; i >= 0; i--)
{
projectiles[i].Update();

if (projectiles[i].Active == false)
{
projectiles.RemoveAt(i);
}
}
}

/// <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)
{
Member
Posts: 3,392
Joined: Oct 9 2009
Gold: 30.00
Oct 3 2012 11:14am
GraphicsDevice.Clear(Color.CornflowerBlue);

// TODO: Add your drawing code here
// Start drawing
spriteBatch.Begin();


// Draw the moving background
bgLayer1.Draw(spriteBatch);
bgLayer2.Draw(spriteBatch);

// Draw the Enemies
for (int i = 0; i < enemies.Count; i++)
{
enemies[i].Draw(spriteBatch);
}

// Draw the Projectiles
for (int i = 0; i < projectiles.Count; i++)
{
projectiles[i].Draw(spriteBatch);
}

for (int i = 0; i < explosions.Count; i++)
{
explosions[i].Draw(spriteBatch);
}

// Draw the score
spriteBatch.DrawString(font, "Score: " + score, new Vector2(GraphicsDevice.Viewport.TitleSafeArea.X, GraphicsDevice.Viewport.TitleSafeArea.Y), Color.White);
// Draw the player health
spriteBatch.DrawString(font, "Health: " + player.Health, new Vector2(GraphicsDevice.Viewport.TitleSafeArea.X, GraphicsDevice.Viewport.TitleSafeArea.Y + 30), Color.White);

// Draw the Player
player.Draw(spriteBatch);

// Stop drawing
spriteBatch.End();

base.Draw(gameTime);
}

private void UpdateCollision()
{
// Use the Rectangle's built-in intersect function to
// determine if two objects are overlapping
Rectangle rectangle1;
Rectangle rectangle2;

// Only create the rectangle once for the player
rectangle1 = new Rectangle((int)player.Position.X,
(int)player.Position.Y,
player.Width,
player.Height);

// Do the collision between the player and the enemies
for (int i = 0; i < enemies.Count; i++)
{
rectangle2 = new Rectangle((int)enemies[i].Position.X,
(int)enemies[i].Position.Y,
enemies[i].Width,
enemies[i].Height);

// Determine if the two objects collided with each
// other
if (rectangle1.Intersects(rectangle2))
{
// Subtract the health from the player based on
// the enemy damage
player.Health -= enemies[i].Damage;

// Since the enemy collided with the player
// destroy it
enemies[i].Health = 0;

// If the player health is less than zero we died
if (player.Health <= 0)
player.Active = false;
}
}

// Projectile vs Enemy Collision
for (int i = 0; i < projectiles.Count; i++)
{
for (int j = 0; j < enemies.Count; j++)
{
// Create the rectangles we need to determine if we collided with each other
rectangle1 = new Rectangle((int)projectiles[i].Position.X -
projectiles[i].Width / 2, (int)projectiles[i].Position.Y -
projectiles[i].Height / 2, projectiles[i].Width, projectiles[i].Height);

rectangle2 = new Rectangle((int)enemies[j].Position.X - enemies[j].Width / 2,
(int)enemies[j].Position.Y - enemies[j].Height / 2,
enemies[j].Width, enemies[j].Height);

// Determine if the two objects collided with each other
if (rectangle1.Intersects(rectangle2))
{
enemies[j].Health -= projectiles[i].Damage;
projectiles[i].Active = false;
}
}
}
}

private void AddProjectile(Vector2 position)
{
Projectile projectile = new Projectile();
projectile.Initialize(GraphicsDevice.Viewport, projectileTexture, position);
projectiles.Add(projectile);
}
}
}
Member
Posts: 3,392
Joined: Oct 9 2009
Gold: 30.00
Oct 3 2012 12:08pm
Already done,thx.Did it my self.
Member
Posts: 827
Joined: Jan 16 2012
Gold: 0.00
Warn: 10%
Oct 4 2012 07:32am
Quote (Sergeant_Popo @ Oct 3 2012 04:08pm)
Already done,thx.Did it my self.


Congratulations. In less than an hour you've done it yourself. Might as well think for an extra hour before asking something in the future ;)
Member
Posts: 7,969
Joined: Jul 7 2008
Gold: 286.54
Oct 4 2012 09:38pm
Quote (StormHasHe @ Oct 4 2012 05:32am)
Congratulations. In less than an hour you've done it yourself. Might as well think for an extra hour before asking something in the future ;)


this...

Do your damn homework.
Member
Posts: 7,427
Joined: Aug 22 2008
Gold: 2,334.50
Oct 14 2012 06:23pm
Or post it 1 week earlier xD
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll