Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Storage;
- using Microsoft.Xna.Framework.Input;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.Graphics;
- namespace CatTetris
- {
- class Block
- {
- public Sprite sprite1;
- public Sprite sprite2;
- public Sprite sprite3;
- public Sprite sprite4;
- public Vector2 mDirection = Vector2.Zero;
- public Vector2 mSpeed = Vector2.Zero;
- //Amount of Incremental movement
- public int increment = 50;
- //Collision booleans
- public bool pressAgain = true;
- public bool leftWallCollide = false;
- public bool rightWallCollide = false;
- public bool dead = false;
- //So I don't have to call every single sprite at once
- public Sprite[] SpriteList;
- //Matrix for rotation on the sprite batch
- public Matrix rotationMatrix = Matrix.CreateRotationZ(0);
- //radians for the rotation
- public float radians = 0f;
- public double elapsedTime = 0;
- //a tetrimo is made up of 4 sprites, here we load them individually
- public void LoadSprites(Sprite sprite1, Sprite sprite2, Sprite sprite3, Sprite sprite4,
- Vector2 pos1, Vector2 pos2, Vector2 pos3, Vector2 pos4)
- {
- this.sprite1 = sprite1;
- this.sprite2 = sprite2;
- this.sprite3 = sprite3;
- this.sprite4 = sprite4;
- SpriteList = new Sprite[4] { sprite1, sprite2, sprite3, sprite4 };
- sprite1.Position = pos1;
- sprite2.Position = pos2;
- sprite3.Position = pos3;
- sprite4.Position = pos4;
- }
- public void Draw(SpriteBatch theSpriteBatch)
- {
- for (int i = 0; i < SpriteList.Length; i++ )
- {
- SpriteList[i].Draw(theSpriteBatch);
- }
- }
- //This updates the rotation matrix
- private Matrix UpdateMatrix(Vector2 origin, float deltaRadians)
- {
- radians += deltaRadians;
- Vector3 matrixOrigin = new Vector3(origin, 0);
- rotationMatrix = Matrix.CreateTranslation(-matrixOrigin)
- * Matrix.CreateRotationZ(radians)
- * Matrix.CreateTranslation(matrixOrigin);
- return rotationMatrix;
- }
- //Method to update all of the sprites
- public void updateSprites(GameTime theGameTime)
- {
- KeyboardState theCurrentKeyboardState = Keyboard.GetState();
- if (!dead)
- Move(theCurrentKeyboardState, theGameTime);
- testCollision();
- for (int i = 0; i < SpriteList.Length; i++)
- {
- SpriteList[i].Update(theGameTime, mSpeed, mDirection);
- }
- waitToPress(theGameTime, theCurrentKeyboardState);
- }
- //Method for moving the sprites around
- public void Move(KeyboardState aCurrentKeyboardState, GameTime theGameTime)
- {
- elapsedTime += theGameTime.ElapsedGameTime.TotalMilliseconds;
- if (aCurrentKeyboardState.IsKeyDown(Keys.Down) && elapsedTime >= 200)
- {
- MoveBlockY(increment);
- elapsedTime -= 100;
- }
- else if(elapsedTime >= 1000)
- {
- MoveBlockY(increment);
- elapsedTime -= 1000;
- }
- if (aCurrentKeyboardState.IsKeyDown(Keys.Left) && pressAgain && !leftWallCollide)
- {
- MoveBlockX(-increment);
- pressAgain = false;
- }
- else if (aCurrentKeyboardState.IsKeyDown(Keys.Right) && pressAgain && !rightWallCollide)
- {
- MoveBlockX(increment);
- pressAgain = false;
- }
- if (aCurrentKeyboardState.IsKeyDown(Keys.Up) && pressAgain)
- {
- UpdateMatrix(sprite1.origin, (float)Math.PI / 2);
- pressAgain = false;
- }
- }
- //Methods to move all sprites in X or Y directions
- private void MoveBlockY(int Y)
- {
- for (int i = 0; i < SpriteList.Length; i++)
- {
- SpriteList[i].Position.Y += Y;
- }
- }
- private void MoveBlockX(int X)
- {
- for (int i = 0; i < SpriteList.Length; i++)
- {
- SpriteList[i].Position.X += X;
- }
- }
- private void waitToPress(GameTime theGameTime, KeyboardState theCurrentKeyboardState) {
- if (pressAgain == false)
- {
- if (theCurrentKeyboardState.IsKeyUp(Keys.Left)
- && theCurrentKeyboardState.IsKeyUp(Keys.Right)
- && theCurrentKeyboardState.IsKeyUp(Keys.Up))
- {
- pressAgain = true;
- }
- }
- }// end waitToPress()
- private void testCollision()
- {
- for (int i = 0; i < SpriteList.Length; i++)
- {
- if (SpriteList[i].BoundingBox().Intersects(GCat.wallLeft.BoundingBox()))
- {
- leftWallCollide = true;
- break;
- }
- else
- leftWallCollide = false;
- if (SpriteList[i].BoundingBox().Intersects(GCat.wallRight.BoundingBox()))
- {
- rightWallCollide = true;
- break;
- }
- else
- rightWallCollide = false;
- if (SpriteList[i].Position.Y + 50 >= 600)
- {
- dead = true;
- break;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment