"nyashit" By Mellen (https://pastebin.com/u/Mellen) URL: https://pastebin.com/sUBj4Tv6 Created on: Sunday 9th of September 2012 12:33:13 AM CDT Retrieved on: Saturday 31 of October 2020 06:28:47 PM UTC 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 { /// /// This is the main type for your game /// 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"; } /// /// 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. /// 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(); } /// /// LoadContent will be called once per game and is the place to load /// all of your content. /// 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("nyancat"); chenTex = Content.Load("chen"); font = Content.Load("mainfont"); // TODO: use this.Content to load your game content here } /// /// UnloadContent will be called once per game and is the place to unload /// all content. /// protected override void UnloadContent() { // TODO: Unload any non ContentManager content here } /// /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// /// Provides a snapshot of timing values. 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); } /// /// This is called when the game should draw itself. /// /// Provides a snapshot of timing values. 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); } } }