Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- namespace Game2
- {
- /// <summary>
- /// This is the main type for your game.
- /// </summary>
- public class Game1 : Game
- {
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
- Texture2D rightWalk, leftWalk, bullet;
- Rectangle destRect, bulletdestRect;
- Rectangle sourceRect;
- KeyboardState ks;
- Vector2 position = new Vector2(20,20) ;
- Vector2 bullet_pos = new Vector2(0, 0);
- int kierunek =0;
- int proffesorMaxFrame;
- private float Elapsed;
- private float delay = 1000/6f;
- int frames = 0;
- int bullet_frames = 0;
- bool locker;
- Rectangle BulletsSourceRect;
- public Game1()
- {
- graphics = new GraphicsDeviceManager(this);
- Content.RootDirectory = "Content";
- graphics.PreferredBackBufferHeight = 500;
- graphics.PreferredBackBufferWidth = 500;
- }
- /// <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
- destRect = new Rectangle(100, 100, 64, 64);
- proffesorMaxFrame = 9;
- locker = false;
- 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);
- rightWalk = Content.Load<Texture2D>("proffessor");
- bullet = Content.Load<Texture2D>("bullet");
- // TODO: use this.Content to load your game content here
- }
- /// <summary>
- /// UnloadContent will be called once per game and is the place to unload
- /// game-specific 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>
- ///
- private void Animated (GameTime gameTime, int way)
- {
- Elapsed += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
- switch (way)
- {
- case 0: // stanie
- {
- sourceRect = new Rectangle(64, 192, 64, 64);
- break;
- }
- case 1: // wprawo
- {
- if (Elapsed >= delay)
- {
- if (frames >= proffesorMaxFrame-1)
- {
- frames = 0;
- }
- else
- {
- frames++;
- }
- Elapsed = 0;
- }
- sourceRect = new Rectangle(64 * frames, 192, 64, 64);
- break;
- }
- case 2: // w lewo
- {
- if (Elapsed >= delay)
- {
- if (frames >= proffesorMaxFrame-1)
- {
- frames = 0;
- }
- else
- {
- frames++;
- }
- Elapsed = 0;
- }
- sourceRect = new Rectangle(64 * frames, 64, 64, 64);
- break;
- }
- case 3: // w up
- {
- if (Elapsed >= delay)
- {
- if (frames >= proffesorMaxFrame - 1)
- {
- frames = 0;
- }
- else
- {
- frames++;
- }
- Elapsed = 0;
- }
- sourceRect = new Rectangle(64 * frames, 0, 64, 64);
- break;
- }
- case 4: // w down
- {
- if (Elapsed >= delay)
- {
- if (frames >= proffesorMaxFrame - 1)
- {
- frames = 0;
- }
- else
- {
- frames++;
- }
- Elapsed = 0;
- }
- sourceRect = new Rectangle(64 * frames, 128, 64, 64);
- break;
- }
- }
- }
- void Shoot( GameTime gameTime, int way)
- {
- switch(way)
- {
- case 1:
- {
- if (Elapsed >= delay)
- {
- if (bullet_frames >= proffesorMaxFrame - 1)
- {
- bullet_frames = 0;
- locker = false;
- }
- else
- {
- bullet_pos.X += 10;
- bullet_frames++;
- }
- Elapsed = 0;
- }
- BulletsSourceRect = new Rectangle(64 * frames, 192, 64, 64);
- break;
- }
- default:
- {
- break;
- }
- }
- }
- protected override void Update(GameTime gameTime)
- {
- if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
- Exit();
- ks = Keyboard.GetState();
- if(ks.IsKeyDown(Keys.Right) && position.X < (graphics.PreferredBackBufferHeight-48))
- {
- position.X += 2f;
- kierunek = 1;
- }
- else if (ks.IsKeyDown(Keys.Left))
- {
- position.X -= 2f;
- kierunek = 2;
- }
- else if( ks.IsKeyDown(Keys.Up))
- {
- position.Y -= 2f;
- kierunek = 3;
- }
- else if(ks.IsKeyDown(Keys.Down))
- {
- position.Y += 2f;
- kierunek = 4;
- }
- else
- {
- kierunek = 0;
- }
- if(ks.IsKeyDown(Keys.Space) && locker == false)
- {
- locker = true;
- Shoot(gameTime, kierunek);
- }
- Animated(gameTime, kierunek);
- // TODO: Add your update logic here
- destRect = new Rectangle((int)position.X, (int)position.Y, 64, 64);
- bulletdestRect = new Rectangle((int)bullet_pos.X, (int)bullet_pos.Y, 25, 25);
- 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();
- spriteBatch.Draw(rightWalk, destRect, sourceRect, Color.White);
- // spriteBatch.Draw(rightWalk, new Rectangle(200,200,64,64), new Rectangle(0,0,64,64), Color.White);
- if(locker == true)
- {
- spriteBatch.Draw(bullet, bulletdestRect, BulletsSourceRect, Color.White);
- }
- spriteBatch.End();
- // TODO: Add your drawing code here
- base.Draw(gameTime);
- }
- }
- }
Add Comment
Please, Sign In to add comment