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 Lesson01
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
//GLOBAL VARIABLES
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
//screen size
public int screenHeight;
public int screenWidth;
public int gameState = 0;
//make a texture
Texture2D nyanTex;
Texture2D chenTex;
SpriteFont font;
//position things
Vector2 nyanPos = Vector2.Zero;
//make a chen army, an array if you will
static int chenPopulation = 25;
Vector2[] chen = new Vector2[chenPopulation];
Random rand = new Random();
int score = 0;
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
//screen size
screenWidth = 1024;
screenHeight = 768;
graphics.PreferredBackBufferWidth = screenWidth;
graphics.PreferredBackBufferHeight = screenHeight;
graphics.IsFullScreen = false;
graphics.ApplyChanges();
//nyan start position
nyanPos.Y = screenHeight-75;
//start position for chen soldiers
for (int i = 0; i < chenPopulation; i++)
{
//position within screen width minus nyan width
chen[i].X = rand.Next(screenWidth - 100);
chen[i].Y = 0 - ((i * 150) + 100);
//float y = chen[i].Y;
}
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 up dem grafix
nyanTex = Content.Load<Texture2D>("nyancat");
chenTex = Content.Load<Texture2D>("chen");
font = Content.Load<SpriteFont>("mainfont");
// 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)
{
//gameState menu
switch (gameState)
{
case 0:
UpdateMenu(gameTime); //intro screen
break;
case 1:
UpdateGame(gameTime); //main game
break;
default:
/*error*/
break;
}
}
public void UpdateMenu(GameTime gameTime)
{
KeyboardState key = Keyboard.GetState();
if (key.IsKeyDown(Keys.Space))
{
gameState = 1;
}
}
public void UpdateGame(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
// TODO: Add your update logic here
//Control our nyan
KeyboardState key = Keyboard.GetState();
if (key.IsKeyDown(Keys.Left) && nyanPos.X > 0)
{
nyanPos.X -= 5;
}
if (key.IsKeyDown(Keys.Right) && nyanPos.X < screenWidth - nyanTex.Width)
{
nyanPos.X += 5;
}
if (key.IsKeyDown(Keys.Up) && nyanPos.Y > 0)
{
nyanPos.Y -= 5;
}
if (key.IsKeyDown(Keys.Down) && nyanPos.Y < screenHeight - nyanTex.Height)
{
nyanPos.Y += 5;
}
//Drop some chens!
for (int i = 0; i < chenPopulation; i++)
{
chen[i].Y += 5;
if (chen[i].Y > screenHeight)
{
Random rand = new Random();
chen[i].X = rand.Next(screenWidth - chenTex.Width); //randomise spawn position of next chen
chen[i].Y = 0 - (chenPopulation * (150 - score / 10)) - 100; //score increments by 10 when chen leaves bottom of screen
score += 10;
}
}
//get centre of nyan
int x = (int)nyanPos.X + (nyanTex.Width / 2);
int y = (int)nyanPos.Y + (nyanTex.Height / 2);
//check for collision
for (int i = 0; i < chenPopulation; i++)
{
if (x > chen[i].X && x < chen[i].X + chenTex.Width)
{
if (y > chen[i].Y && y < chen[i].Y + chenTex.Height)
{
score = 0; //score resets
nyanPos.X = 0;
nyanPos.Y = 0; //reset your position too
}
}
}
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.Black);
// TODO: Add your drawing code here
switch (gameState)
{
//draw menu
case 0:
spriteBatch.Begin();
spriteBatch.DrawString(font, "CHEEEEEN!\nArrows to move\nPress Space to Play ",
new Vector2(100, 10), Color.White);
spriteBatch.End();
break;
//draw main game
case 1:
spriteBatch.Begin();
spriteBatch.Draw(nyanTex, nyanPos, Color.White);
//draw the chen army
for (int i = 0; i < chenPopulation; i++)
{
spriteBatch.Draw(chenTex, chen[i], Color.White);
}
spriteBatch.DrawString(font, "Score: " + score, new Vector2(20, 10), Color.White);
spriteBatch.End();
break;
}
base.Draw(gameTime);
}
}
}