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 GrafikaRuchoma_01a
- {
- public class Game1 : Microsoft.Xna.Framework.Game
- {
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
- Tile Background;
- Tile Ghost;
- float ghostTime = 0;
- float ghostSleep = 1f;
- Random random;
- int WhiteScore = 0, OtherScore = 0;
- float Scale = 0.25f;
- List<Color> Colors = new List<Color>() { Color.White, Color.Red, Color.Green };
- public Game1()
- {
- graphics = new GraphicsDeviceManager(this);
- Content.RootDirectory = "Content";
- graphics.PreferredBackBufferWidth = 800;
- graphics.PreferredBackBufferHeight = 600;
- IsMouseVisible = true;
- Window.AllowUserResizing = true;
- }
- protected override void Initialize()
- {
- random = new Random();
- base.Initialize();
- }
- protected override void UnloadContent() { }
- protected override void LoadContent()
- {
- // Create a new SpriteBatch, which can be used to draw textures.
- spriteBatch = new SpriteBatch(GraphicsDevice);
- Background = new Tile(Content.Load<Texture2D>("background"), new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height), 0);
- Ghost = new Tile(Content.Load<Texture2D>("ghost"), new Rectangle(0, 0, 100, 100), 0);
- Ghost.SetVisibility(false);
- Ghost.SetScale(Scale);
- // TODO: use this.Content to load your game content here
- }
- protected override void Update(GameTime gameTime)
- {
- var mouseState = Mouse.GetState();
- var keyboardState = Keyboard.GetState();
- var mousePos = new Point(mouseState.X, mouseState.Y);
- if (GraphicsDevice.Viewport.Width < 200)
- {
- graphics.PreferredBackBufferWidth = 300;
- }
- if (GraphicsDevice.Viewport.Height < 200)
- {
- graphics.PreferredBackBufferHeight = 300;
- }
- ghostTime += (float)gameTime.ElapsedGameTime.TotalSeconds;
- // Allows the game to exit
- if (keyboardState.IsKeyDown(Keys.Escape))
- this.Exit();
- Background.Resize(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
- {
- if ((ghostTime >= ghostSleep || !Ghost.IsVisible) && mouseState.LeftButton == ButtonState.Released )
- {
- var randX = random.Next(Ghost.Width / 2, GraphicsDevice.Viewport.Width - Ghost.Width / 2);
- var randY = random.Next(Ghost.Height / 2, GraphicsDevice.Viewport.Height - Ghost.Height / 2);
- var randColor = random.Next(0, Colors.Count);
- Ghost.SetPosition(randX, randY);
- Ghost.SetVisibility(true);
- Ghost.SetColor(Colors[randColor]);
- ghostTime = 0;
- }
- if (Ghost.IsOn(mousePos) && Ghost.IsVisible && mouseState.LeftButton == ButtonState.Pressed)
- {
- if (Ghost.TintColor == Color.White)
- {
- WhiteScore++;
- }
- else
- {
- OtherScore++;
- }
- Ghost.SetVisibility(false);
- Window.Title = "Białe = " + WhiteScore + " | Inne = " + OtherScore;
- }
- }
- base.Update(gameTime);
- }
- protected override void Draw(GameTime gameTime)
- {
- GraphicsDevice.Clear(Color.CornflowerBlue);
- spriteBatch.Begin();
- {
- Background.Draw(spriteBatch, false);
- Ghost.Draw(spriteBatch);
- }
- spriteBatch.End();
- base.Draw(gameTime);
- }
- }
- public class Tile
- {
- public Texture2D Sprite { get; set; }
- public Rectangle Position { get; set; }
- public double Rotation { get; set; }
- public Color TintColor { get; set; }
- public Rectangle? Source { get; set; }
- public float Scale { get; set; }
- public int Width { get { return Position.Width; } }
- public int Height { get { return Position.Height; } }
- public bool IsVisible { get; set; }
- public Tile(Texture2D sprite, Rectangle position, int rotation, Rectangle? source = null)
- {
- Sprite = sprite;
- Position = position;
- Rotation = rotation;
- Source = source;
- TintColor = Color.White;
- IsVisible = true;
- Scale = 1;
- }
- public void Rotate(int degrees)
- {
- Rotation += (Math.PI / 180) * degrees;
- if (Rotation >= 360)
- Rotation -= 360;
- else if (Rotation < 0)
- Rotation += 360;
- }
- public bool IsOn(Point mousePosition)
- {
- var position = Position;
- position.X -= Position.Width / 2;
- position.Y -= Position.Height / 2;
- return position.Contains(mousePosition);
- }
- public void Draw(SpriteBatch spriteBatch, bool center = true)
- {
- if (IsVisible)
- {
- var source = Source.HasValue ? Source : new Rectangle(0, 0, Sprite.Width, Sprite.Height);
- Vector2 origin = new Vector2(0, 0);
- if (center)
- origin = Source.HasValue ? new Vector2(Source.Value.Width / 2, Source.Value.Height / 2) : new Vector2(Sprite.Width / 2, Sprite.Height / 2);
- var position = new Rectangle(Position.X, Position.Y, (int)(Position.Width * Scale), (int)(Position.Height * Scale));
- spriteBatch.Draw(Sprite, position, source, TintColor, (float)Rotation, origin, SpriteEffects.None, 0);
- }
- }
- public void Resize(int width, int height)
- {
- Position = new Rectangle(Position.X, Position.Y, width, height);
- }
- public void SetPosition(int x, int y)
- {
- Position = new Rectangle(x, y, Position.Width, Position.Height);
- }
- public void SetScale(float scale)
- {
- Scale = scale;
- }
- public void SetColor(Color color)
- {
- TintColor = color;
- }
- public void SetVisibility(bool visible)
- {
- IsVisible = visible;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement