Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 TileMap
- {
- public class Game1 : Microsoft.Xna.Framework.Game
- {
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
- #region Variables
- #region Texture2D
- Texture2D playerRight;
- Texture2D playerLeft;
- Texture2D player;
- Texture2D zombie;
- Texture2D bullet;
- Texture2D turrentBullet;
- Texture2D turrent;
- #endregion Texture2D
- #region Rectangles
- Rectangle shopButton1;
- Rectangle shopButton2;
- Rectangle shopButton3;
- Rectangle shopButton4;
- Rectangle shopButton5;
- Rectangle shopButton6;
- Rectangle turrentLoc;
- #endregion Rectangles
- #region Booleans
- bool shopButtonLock = false;
- bool mouseButtonLock = false;
- bool gameOver = false;
- bool shoot = true;
- bool mapDraw = true;
- bool shopOpen = false;
- bool gotTurrent=false;
- #endregion Booleans
- #region SpriteFonts
- SpriteFont font;
- SpriteFont mFont;
- SpriteFont lFont;
- #endregion SpriteFonts
- #region strings
- string t1 = "Null";
- #endregion
- #region floats
- float playerLocBufferY = 0;
- float elapsed;
- float timer1=3;
- float timer2 = 2;
- float timer3 = 1;
- float zSpawnTime = 8;
- float waveZSpawn = 8;
- #endregion
- #region ints
- int zombiesSpawned = 0;
- int wave = 20;
- int money = 20;
- int playerHP;
- int playerMaxHP;
- int playerView = 1;
- int playerDamage = 25;
- int turrentDamage = 15;
- #endregion
- #region Vector2
- Vector2 playerLoc;
- Vector2 screenSize;
- Vector2 screenSizeRounded;
- #endregion
- #region Lists
- List<Texture2D> textureMap=new List<Texture2D>();
- List<Rectangle> collisonBoxes = new List<Rectangle>();
- List<Rectangle> bullets = new List<Rectangle>();
- List<Rectangle> zombieLoc = new List<Rectangle>();
- List<Rectangle> turrentBullets = new List<Rectangle>();
- List<int> zombieHP = new List<int>();
- #endregion
- #region map
- int[,] map=new int[,]{
- {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
- {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
- };
- #endregion map
- #endregion
- public Game1()
- {
- graphics = new GraphicsDeviceManager(this);
- graphics.IsFullScreen = false;
- graphics.PreferredBackBufferHeight = 480;
- graphics.PreferredBackBufferWidth = 1568;
- Content.RootDirectory = "Content";
- }
- protected override void Initialize()
- {
- this.IsMouseVisible = true;
- screenSize = new Vector2(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
- screenSizeRounded = new Vector2((int)Math.Round(screenSize.X), (int)Math.Round(screenSize.Y));
- shopButton1 = new Rectangle(0, 0, 14*3, 20);
- shopButton3 = new Rectangle((int)screenSizeRounded.X / 2 - 500, (int)screenSizeRounded.Y / 2 - 180, 14 * (18/4*3), 20);
- shopButton2 = new Rectangle((int)screenSizeRounded.X/2-500, (int)screenSizeRounded.Y/2-150, 14*(15/4*3), 20);
- shopButton4 = new Rectangle((int)screenSizeRounded.X / 2 - 500, (int)screenSizeRounded.Y / 2 - 120, 14 * (12/4*3), 20);
- playerMaxHP = 100;
- playerHP = playerMaxHP;
- base.Initialize();
- }
- protected override void LoadContent()
- {
- spriteBatch = new SpriteBatch(GraphicsDevice);
- textureMap.Add(Content.Load<Texture2D>("tile1"));
- textureMap.Add(Content.Load<Texture2D>("tile2"));
- player = Content.Load<Texture2D>("player");
- playerRight = Content.Load<Texture2D>("playerRight");
- playerLeft = Content.Load<Texture2D>("playerLeft");
- zombie = Content.Load<Texture2D>("enemy");
- bullet = Content.Load<Texture2D>("playerBullet");
- turrent = Content.Load<Texture2D>("turrent");
- turrentBullet = Content.Load<Texture2D>("turrentBullet");
- font = Content.Load<SpriteFont>("RegularFont");
- mFont = Content.Load<SpriteFont>("MediumFont");
- lFont = Content.Load<SpriteFont>("LargeFont");
- playerLoc = new Vector2(20, screenSize.Y - player.Height-32);
- turrentLoc = new Rectangle(200, (int)screenSizeRounded.Y-32 - turrent.Height, turrent.Width, turrent.Height);
- }
- protected override void UnloadContent()
- {
- }
- public void spawnZombie()
- {
- zombieLoc.Add(new Rectangle((int)Math.Round(screenSize.X) - zombie.Width, (int)Math.Round(screenSize.Y) - zombie.Height-32, zombie.Width, zombie.Height));
- zombieHP.Add(100);
- if (zSpawnTime < 28 && wave==2)
- {
- zSpawnTime = 8;
- }
- else if (zSpawnTime > 2 || wave > 15)
- {
- if(zSpawnTime>0.4)
- zSpawnTime -= 0.02f;
- }
- t1 = zSpawnTime.ToString() ;
- zombiesSpawned++;
- }
- public void killZombie(int z)
- {
- zombieLoc.Remove(zombieLoc[z]);
- zombieHP.Remove(zombieHP[z]);
- money += wave * 2;
- }
- public void shootTurrent()
- {
- turrentBullets.Add(new Rectangle(turrentLoc.X + turrent.Width / 2, turrentLoc.Y + turrent.Height / 2-6, turrentBullet.Width, turrentBullet.Height));
- }
- protected override void Update(GameTime gameTime)
- {
- if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
- this.Exit();
- #region KeyChecker
- #region Alts
- if (Keyboard.GetState().IsKeyDown(Keys.LeftAlt))
- {
- if (Keyboard.GetState().IsKeyDown(Keys.L))
- {
- mapDraw = false;
- }
- else
- {
- mapDraw = true;
- }
- }
- #endregion Alts
- #region Ctrls
- if (Keyboard.GetState().IsKeyDown(Keys.LeftControl))
- {
- if (Keyboard.GetState().IsKeyDown(Keys.S))
- {
- shopButtonLock = true;
- }
- }
- if (Keyboard.GetState().IsKeyUp(Keys.S) && shopButtonLock)
- {
- shopButtonLock = false;
- shopOpen = !shopOpen;
- mapDraw = !mapDraw;
- }
- #endregion Ctrls
- #region MouseLeft
- if (Mouse.GetState().LeftButton == ButtonState.Pressed)
- {
- mouseButtonLock = true;
- } if (Mouse.GetState().LeftButton == ButtonState.Released && mouseButtonLock)
- {
- mouseButtonLock = false;
- if (shopButton1.Contains(Mouse.GetState().X, Mouse.GetState().Y))
- {
- //Back Button Clicked
- shopOpen = !shopOpen;
- mapDraw = !mapDraw;
- }
- if (shopButton2.Contains(Mouse.GetState().X, Mouse.GetState().Y) && money>=50)
- {
- playerMaxHP += 25;
- playerHP += 25;
- money -= 50;
- }
- if (shopButton3.Contains(Mouse.GetState().X, Mouse.GetState().Y) && money >= 25 && playerDamage<45)
- {
- playerDamage += 15;
- money -= 25;
- }
- if (shopButton4.Contains(Mouse.GetState().X, Mouse.GetState().Y) && money >= 150 && !gotTurrent)
- {
- gotTurrent = true;
- money -= 150;
- shopButton4 = new Rectangle((int)screenSizeRounded.X / 2 - 500, (int)screenSizeRounded.Y / 2 - 120, 14 * (28 / 4 * 3), 20);
- }
- else if (shopButton4.Contains(Mouse.GetState().X, Mouse.GetState().Y) && money >= 400 && gotTurrent && turrentDamage<60)
- {
- turrentDamage += 10;
- money -= 400;
- }
- }
- #endregion mouseleft
- #endregion KeyChecker
- #region timers
- elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
- if(mapDraw)
- timer1 -= elapsed;
- if (timer1 < 0)
- {
- spawnZombie();
- timer1 = zSpawnTime;
- }
- if(timer2>0 && mapDraw)
- timer2 -= elapsed;
- if (timer2 <= 0)
- {
- timer2 = 2f;
- shoot = true;
- }
- if(gotTurrent)
- timer3 -= elapsed;
- if (timer3 <= 0)
- {
- timer3 = 1f;
- shootTurrent();
- }
- #endregion timers
- #region playerMovement
- if (playerHP <= 0)
- {
- gameOver = true;
- mapDraw = false;
- playerHP = 0;
- }
- if (Keyboard.GetState().IsKeyDown(Keys.Left))
- {
- playerView = 0;
- }
- else if (Keyboard.GetState().IsKeyDown(Keys.Right))
- {
- playerView = 2;
- }
- else
- {
- //playerView = 1; Enables Front View While Not Moveing
- }
- if (Keyboard.GetState().IsKeyDown(Keys.Left) && playerLoc.X>0+5)
- {
- playerLoc.X -= gameTime.ElapsedGameTime.Milliseconds * 0.5f;
- } if (Keyboard.GetState().IsKeyDown(Keys.Right) && playerLoc.X < (GraphicsDevice.Viewport.Width- player.Width-10))
- {
- playerLoc.X += gameTime.ElapsedGameTime.Milliseconds * 0.5f;
- }
- if (Keyboard.GetState().IsKeyDown(Keys.Space) && shoot)
- {
- bullets.Add(new Rectangle((int)Math.Round(playerLoc.X)+player.Width, (int)Math.Round(playerLoc.Y)+player.Height/2, bullet.Width, bullet.Height));
- shoot = false;
- }
- playerLoc.Y -= playerLocBufferY;
- #endregion playerMovement
- #region collison
- for(int i=0; i<collisonBoxes.Count; i++){
- playerLocBufferY = 0f;
- //Checking bottom
- //if((playerLoc.Y+player.Height)>collisonBoxes[i].Y && playerLoc.X > collisonBoxes[i].X && playerLoc.X+player.Width>collisonBoxes[i].X-32) Complex collision
- if(collisonBoxes[i].Intersects(new Rectangle((int)Math.Round(playerLoc.X),(int)Math.Round(playerLoc.Y), player.Width, player.Height))) //Simple collision
- {
- playerLoc.Y = collisonBoxes[i].Y - (player.Height);
- }
- if ((playerLoc.Y + player.Height < collisonBoxes[i].Y))
- {
- playerLocBufferY -=3.5f;
- }
- }
- #endregion collison
- #region zombies
- if (!gameOver)
- {
- for (int i = 0; i < zombieLoc.Count; i++)
- {
- Rectangle rect = zombieLoc[i];
- if (mapDraw)
- rect.X -= gameTime.ElapsedGameTime.Milliseconds/8;
- ;
- zombieLoc[i] = rect;
- for (int b = 0; b < bullets.Count; b++)
- {
- if (zombieLoc[i].Contains(bullets[b].X + bullet.Width, bullets[b].Y))
- {
- zombieHP[i] -= playerDamage;
- bullets.Remove(bullets[b]);
- }
- }
- for (int b = 0; b < turrentBullets.Count; b++)
- {
- if (zombieLoc[i].Contains(turrentBullets[b].X + turrentBullet.Width, turrentBullets[b].Y))
- {
- zombieHP[i] -= turrentDamage;
- turrentBullets.Remove(turrentBullets[b]);
- }
- }
- if (zombieHP[i] <= 0)
- {
- killZombie(i);
- }
- else if (zombieLoc[i].X < playerLoc.X + player.Width)
- {
- Rectangle rect2 = zombieLoc[i];
- if (mapDraw)
- rect2.X += 50;
- zombieLoc[i] = rect2;
- playerHP -= 25;
- }
- }
- for (int b = 0; b < bullets.Count; b++)
- {
- try
- {
- Rectangle rect = bullets[b];
- if (mapDraw)
- rect.X += gameTime.ElapsedGameTime.Milliseconds * 1;
- bullets[b] = rect;
- }
- catch
- {
- }
- }for(int b=0;b < turrentBullets.Count;b++){
- Rectangle rect=turrentBullets[b];
- try{
- rect.X+=gameTime.ElapsedGameTime.Milliseconds * 2;
- }catch{}
- turrentBullets[b] = rect;
- }
- }
- #endregion zombies
- #region gameInfo
- if (zombiesSpawned==(wave*3) && zombieLoc.Count>=0)
- {
- wave++;
- zombieLoc.Clear();
- zombieHP.Clear();
- bullets.Clear();
- turrentBullets.Clear();
- zSpawnTime = waveZSpawn;
- timer1 = 30f;
- timer2 = 0f;
- zombiesSpawned = 0;
- if (waveZSpawn > 1)
- waveZSpawn -= 0.1f;
- }
- #endregion
- base.Update(gameTime);
- }
- protected override void Draw(GameTime gameTime)
- {
- GraphicsDevice.Clear(Color.CornflowerBlue);
- spriteBatch.Begin();
- #region mapDraw
- if (mapDraw)
- {
- #region tileMap
- for (int y = 0; y < map.GetLength(0); y++)
- {
- for (int x = 0; x < map.GetLength(1); x++)
- {
- int index = map[y, x];
- if (index != -1)
- {
- Texture2D texture = textureMap[index];
- spriteBatch.Draw(texture, new Rectangle(x * 32, y * 32, 32, 32), Color.White);
- if (index == 1)
- {
- collisonBoxes.Add(new Rectangle(x * 32, y * 32, 32, 32));
- }
- }
- }
- }
- #endregion tileMap
- for (int i = 0; i < turrentBullets.Count; i++)
- {
- spriteBatch.Draw(turrentBullet, turrentBullets[i], Color.White);
- }
- if(gotTurrent)
- spriteBatch.Draw(turrent, turrentLoc, Color.White);
- spriteBatch.Draw(playerRight, playerLoc, Color.White);
- for (int i = 0; i < zombieLoc.Count; i++)
- {
- spriteBatch.Draw(zombie, zombieLoc[i], Color.White);
- }
- for (int i = 0; i < bullets.Count; i++)
- {
- spriteBatch.Draw(bullet, bullets[i], Color.White);
- }
- spriteBatch.DrawString(font, "Wave " + wave, new Vector2(screenSize.X/2, 0), Color.White);
- }
- #endregion mapDraw
- #region shop
- if (shopOpen)
- {
- spriteBatch.DrawString(font, "Back", new Vector2(shopButton1.X, shopButton1.Y), Color.White);
- spriteBatch.DrawString(font, "Health Upgrade: $50", new Vector2(shopButton2.X, shopButton2.Y), Color.White);
- if(playerDamage<45)
- spriteBatch.DrawString(font, "Damage Upgrade: $25", new Vector2(shopButton3.X, shopButton3.Y), Color.White);
- else if(playerDamage>=45)
- spriteBatch.DrawString(font, "Damage Upgrade: MAXED", new Vector2(shopButton3.X, shopButton3.Y), Color.White);
- if(!gotTurrent)
- spriteBatch.DrawString(font, "Turrent: $150", new Vector2(shopButton4.X, shopButton4.Y), Color.White);
- else if(turrentDamage<60)
- spriteBatch.DrawString(font, "Upgrade Turrents Damage: $400", new Vector2(shopButton4.X, shopButton4.Y), Color.White);
- else if(turrentDamage>= 60)
- spriteBatch.DrawString(font, "Upgrade Turrents Damage: MAXED", new Vector2(shopButton4.X, shopButton4.Y), Color.White);
- }
- #endregion shop
- #region gameOver
- if (gameOver)
- {
- spriteBatch.DrawString(lFont, "Game Over.", new Vector2(screenSize.X / 2-40, screenSize.Y / 2), Color.White);
- //spriteBatch.DrawString(mFont, "Press F4 To retry!", new Vector2(screenSize.X / 2-40, screenSize.Y / 2-40), Color.White);
- }
- #endregion
- spriteBatch.DrawString(font, ("Money: "+money.ToString()), new Vector2(0,screenSize.Y-20),Color.White);
- spriteBatch.DrawString(font, ("Health: " + playerHP.ToString() + " / " + playerMaxHP), new Vector2(screenSize.X-130, screenSize.Y - 20), Color.White);
- spriteBatch.End();
- base.Draw(gameTime);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement