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;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
namespace WindowsGame8
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Sprite mBackground1;
Texture2D character;
Point frameSize = new Point(48, 48);
Point currentFrame = new Point(0, 0);
Point sheetSize = new Point(3, 11);
Vector2 pos1 = new Vector2(140, 150);
float speed1 = 4f;
int hits = 0;
Texture2D enemy;
Point frameSize2 = new Point(48, 48);
Point currentFrame2 = new Point(0, 0);
Point sheetSize2 = new Point(3, 11);
Vector2 pos2 = new Vector2(300, 150);
float speed2 = 4f;
Texture2D dragon;
Vector2 pos3 = new Vector2(200, 0);
Texture2D hand1;
Vector2 pos4 = new Vector2(260,0);
Texture2D hand2;
Vector2 pos5 = new Vector2(170, 0);
SoundEffect soundEffect;
SoundEffect soundEffect2;
string soundName = "song";
string soundName2 = "punch2";
bool dead = false;
int timeSinceLastFrame = 0;
int millisecondsPerFrame = 20;
int characterOffset = 10;
int enemyOffset = 15;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
TargetElapsedTime = new TimeSpan(0, 0, 0, 0, 100);
}
private bool Collide()
{
Rectangle ringsRect = new Rectangle(
(int)pos1.X + characterOffset,
(int)pos1.Y + characterOffset,
frameSize.X - (characterOffset * 2),
frameSize.Y - (characterOffset * 2));
Rectangle skullRect = new Rectangle(
(int)pos2.X + enemyOffset,
(int)pos2.Y + enemyOffset,
frameSize2.X - (enemyOffset * 2),
frameSize2.Y - (enemyOffset * 2));
return ringsRect.Intersects(skullRect);
}
/// <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
graphics.PreferredBackBufferWidth = 420;
graphics.PreferredBackBufferHeight = 216;
graphics.IsFullScreen = false;
graphics.ApplyChanges();
Window.Title = "Break the shit out of them";
mBackground1 = new Sprite();
character = Content.Load<Texture2D>("a2");
enemy = Content.Load<Texture2D>("enemy");
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);
mBackground1.LoadContent(this.Content, "assss");
dragon = Content.Load<Texture2D>("dragonh");
hand1 = Content.Load<Texture2D>("hand1");
hand2 = Content.Load<Texture2D>("hand2");
mBackground1.Position = new Vector2(0, 0);
mBackground1.Position2 = new Vector2(-1320, 0);
ContentManager contentManager = new ContentManager(this.Services, @"Content\");
soundEffect = contentManager.Load<SoundEffect>(soundName);
soundEffect2 = contentManager.Load<SoundEffect>(soundName2);
SoundEffectInstance instance = soundEffect.CreateInstance();
//instance.IsLooped = true;
//instance.Play();
// 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();
Vector2 aDirection = new Vector2(-1, 0);
Vector2 aSpeed = new Vector2(100, 0);
currentFrame.Y = 1;
++currentFrame.X;
if (currentFrame.X >= sheetSize.X)
{
currentFrame.X = 0;
}
KeyboardState keyboardState = Keyboard.GetState();
if (keyboardState.IsKeyDown(Keys.A))
{
pos1.X += speed1;
currentFrame.Y = 9;
timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
if (timeSinceLastFrame > millisecondsPerFrame)
{
timeSinceLastFrame -= millisecondsPerFrame;
if (currentFrame.Y >= sheetSize.Y)
currentFrame.Y = 0;
{
++currentFrame.X;
if (currentFrame.X >= sheetSize.X)
{
currentFrame.X = 0;
}
}
}
if (Collide())
{
hits += 1;
soundEffect2.Play();
pos1.X -= speed1;
speed2 = 0;
currentFrame2.Y = 2;
++currentFrame2.X;
if (currentFrame2.X >= sheetSize2.X)
{
currentFrame2.X = 0;
}
}
}
else if (keyboardState.IsKeyDown(Keys.S))
{
pos1.X += speed1;
currentFrame.Y = 8;
timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
if (timeSinceLastFrame > millisecondsPerFrame)
{
timeSinceLastFrame -= millisecondsPerFrame;
if (currentFrame.Y >= sheetSize.Y)
currentFrame.Y = 0;
{
++currentFrame.X;
if (currentFrame.X >= sheetSize.X)
{
currentFrame.X = 0;
}
}
}
if (dead == false)
{
if (Collide())
{
hits += 1;
soundEffect2.Play();
pos1.X -= speed1;
speed2 = 0;
currentFrame2.Y = 2;
++currentFrame2.X;
if (currentFrame2.X >= sheetSize2.X)
{
currentFrame2.X = 0;
}
}
}
}
else if (keyboardState.IsKeyDown(Keys.Right))
{
pos1.X += speed1;
currentFrame.Y = 6;
timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
if (timeSinceLastFrame > millisecondsPerFrame)
{
timeSinceLastFrame -= millisecondsPerFrame;
if (currentFrame.Y >= sheetSize.Y)
currentFrame.Y = 0;
{
++currentFrame.X;
if (currentFrame.X >= sheetSize.X)
{
currentFrame.X = 0;
}
}
}
if (pos1.X >= Window.ClientBounds.X /2)
{
mBackground1.Position += aDirection * aSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;;
if (mBackground1.Position.X < mBackground1.Position2.X)
{
mBackground1.Position -= aDirection * aSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;;
}
}
}
else if (keyboardState.IsKeyDown(Keys.Left))
{
pos1.X -= speed1;
currentFrame.Y = 7;
timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
if (timeSinceLastFrame > millisecondsPerFrame)
{
timeSinceLastFrame -= millisecondsPerFrame;
if (currentFrame.Y >= sheetSize.Y)
currentFrame.Y = 0;
{
++currentFrame.X;
if (currentFrame.X >= sheetSize.X)
{
currentFrame.X = 0;
}
}
}
if (pos1.X <= Window.ClientBounds.X / 2)
{
mBackground1.Position -= aDirection * aSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
if (mBackground1.Position.X > mBackground1.Position3.X)
{
mBackground1.Position += aDirection * aSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
}
}
}
if (pos1.X < 0)
pos1.X = 0;
if (pos1.X > Window.ClientBounds.Width - frameSize.X)
pos1.X = Window.ClientBounds.Width - frameSize.X;
currentFrame2.Y = 6;
pos2.X -= speed2;
++currentFrame2.X;
if (currentFrame2.X >= sheetSize2.X)
{
currentFrame2.X = 0;
}
if (pos2.X <= pos1.X +25)
{
//speed2 =0;
currentFrame2.Y = 8;
++currentFrame2.X;
if (currentFrame2.X >= sheetSize2.X)
{
currentFrame2.X = 0;
}
if (Collide())
{
speed2 = 0;
soundEffect2.Play();
currentFrame.Y = 2;
++currentFrame.X;
if (currentFrame.X >= sheetSize.X)
{
currentFrame.X = 0;
}
}
}
if (hits >= 2)
{
currentFrame2 = new Point(2, 3);
pos2.Y = 160;
dead = true;
}
base.Update(gameTime);
}
/// <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();
mBackground1.Draw(this.spriteBatch);
spriteBatch.Draw(enemy, pos2,
new Rectangle(currentFrame2.X * frameSize2.X,
currentFrame2.Y * frameSize2.Y,
frameSize2.X,
frameSize2.Y),
Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0);
spriteBatch.Draw(character, pos1,
new Rectangle(currentFrame.X * frameSize.X,
currentFrame.Y * frameSize.Y,
frameSize.X,
frameSize.Y),
Color.White
, 0, Vector2.Zero, 1, SpriteEffects.FlipHorizontally, 0);
spriteBatch.Draw(dragon, pos3, Color.White);
spriteBatch.Draw(hand1, pos5, Color.White);
spriteBatch.Draw(hand2, pos4, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
There is a sprite class also which i use it for the background image but you don't need that.
What i want to do is when the enemy get's two hits i change the frame so it look's like he is dead.
I check for collision only when you press key A or key S which is when the character is punching.
So when the enemy has get two hits or more i consider he is dead so i want then to make the collision false so if
my character is above and punching it won't collide and play the sound of a puch.
Sorry if it's a bit crappy i just try first to learn things by creating simply things and when i am able to do everything i will start creating classes and move to more object orient programming.