Advertisement
Alan468

GR_01_Duszki

Oct 16th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.00 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Audio;
  6. using Microsoft.Xna.Framework.Content;
  7. using Microsoft.Xna.Framework.GamerServices;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna.Framework.Media;
  11.  
  12. namespace GrafikaRuchoma_01a
  13. {
  14.     public class Game1 : Microsoft.Xna.Framework.Game
  15.     {
  16.         GraphicsDeviceManager graphics;
  17.         SpriteBatch spriteBatch;
  18.  
  19.         Tile Background;
  20.         Tile Ghost;
  21.         float ghostTime = 0;
  22.         float ghostSleep = 1f;
  23.         Random random;
  24.         int WhiteScore = 0, OtherScore = 0;
  25.         float Scale = 0.25f;
  26.  
  27.         List<Color> Colors = new List<Color>() { Color.White, Color.Red, Color.Green };
  28.  
  29.         public Game1()
  30.         {
  31.             graphics = new GraphicsDeviceManager(this);
  32.             Content.RootDirectory = "Content";
  33.  
  34.             graphics.PreferredBackBufferWidth = 800;
  35.             graphics.PreferredBackBufferHeight = 600;
  36.             IsMouseVisible = true;
  37.             Window.AllowUserResizing = true;
  38.         }
  39.  
  40.         protected override void Initialize()
  41.         {
  42.             random = new Random();
  43.             base.Initialize();
  44.         }
  45.         protected override void UnloadContent() { }
  46.  
  47.         protected override void LoadContent()
  48.         {
  49.             // Create a new SpriteBatch, which can be used to draw textures.
  50.             spriteBatch = new SpriteBatch(GraphicsDevice);
  51.  
  52.             Background = new Tile(Content.Load<Texture2D>("background"), new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height), 0);
  53.             Ghost = new Tile(Content.Load<Texture2D>("ghost"), new Rectangle(0, 0, 100, 100), 0);
  54.             Ghost.SetVisibility(false);
  55.             Ghost.SetScale(Scale);
  56.  
  57.             // TODO: use this.Content to load your game content here
  58.         }
  59.  
  60.  
  61.         protected override void Update(GameTime gameTime)
  62.         {
  63.             var mouseState = Mouse.GetState();
  64.             var keyboardState = Keyboard.GetState();
  65.             var mousePos = new Point(mouseState.X, mouseState.Y);
  66.  
  67.             if (GraphicsDevice.Viewport.Width < 200)
  68.             {
  69.                 graphics.PreferredBackBufferWidth = 300;
  70.             }
  71.  
  72.             if (GraphicsDevice.Viewport.Height < 200)
  73.             {
  74.                 graphics.PreferredBackBufferHeight = 300;
  75.             }
  76.  
  77.             ghostTime += (float)gameTime.ElapsedGameTime.TotalSeconds;
  78.  
  79.             // Allows the game to exit
  80.             if (keyboardState.IsKeyDown(Keys.Escape))
  81.                 this.Exit();
  82.  
  83.             Background.Resize(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
  84.  
  85.             {
  86.                 if ((ghostTime >= ghostSleep || !Ghost.IsVisible) && mouseState.LeftButton == ButtonState.Released )
  87.                 {
  88.                     var randX = random.Next(Ghost.Width / 2, GraphicsDevice.Viewport.Width - Ghost.Width / 2);
  89.                     var randY = random.Next(Ghost.Height / 2, GraphicsDevice.Viewport.Height - Ghost.Height / 2);
  90.                     var randColor = random.Next(0, Colors.Count);
  91.  
  92.                     Ghost.SetPosition(randX, randY);
  93.                     Ghost.SetVisibility(true);
  94.                     Ghost.SetColor(Colors[randColor]);
  95.                     ghostTime = 0;
  96.                 }
  97.  
  98.                 if (Ghost.IsOn(mousePos) && Ghost.IsVisible && mouseState.LeftButton == ButtonState.Pressed)
  99.                 {
  100.                     if (Ghost.TintColor == Color.White)
  101.                     {
  102.                         WhiteScore++;
  103.                     }
  104.                     else
  105.                     {
  106.                         OtherScore++;
  107.                     }
  108.                     Ghost.SetVisibility(false);
  109.                     Window.Title = "BiaΕ‚e = " + WhiteScore + " | Inne = " + OtherScore;
  110.                 }
  111.             }
  112.  
  113.             base.Update(gameTime);
  114.         }
  115.  
  116.         protected override void Draw(GameTime gameTime)
  117.         {
  118.             GraphicsDevice.Clear(Color.CornflowerBlue);
  119.  
  120.             spriteBatch.Begin();
  121.             {
  122.                 Background.Draw(spriteBatch, false);
  123.                 Ghost.Draw(spriteBatch);
  124.             }
  125.             spriteBatch.End();
  126.  
  127.             base.Draw(gameTime);
  128.         }
  129.     }
  130.  
  131.     public class Tile
  132.     {
  133.         public Texture2D Sprite { get; set; }
  134.         public Rectangle Position { get; set; }
  135.         public double Rotation { get; set; }
  136.         public Color TintColor { get; set; }
  137.         public Rectangle? Source { get; set; }
  138.         public float Scale { get; set; }
  139.  
  140.         public int Width { get { return Position.Width; } }
  141.         public int Height { get { return Position.Height; } }
  142.  
  143.         public bool IsVisible { get; set; }
  144.  
  145.         public Tile(Texture2D sprite, Rectangle position, int rotation, Rectangle? source = null)
  146.         {
  147.             Sprite = sprite;
  148.             Position = position;
  149.             Rotation = rotation;
  150.             Source = source;
  151.             TintColor = Color.White;
  152.             IsVisible = true;
  153.             Scale = 1;
  154.         }
  155.  
  156.         public void Rotate(int degrees)
  157.         {
  158.             Rotation += (Math.PI / 180) * degrees;
  159.  
  160.             if (Rotation >= 360)
  161.                 Rotation -= 360;
  162.             else if (Rotation < 0)
  163.                 Rotation += 360;
  164.         }
  165.  
  166.         public bool IsOn(Point mousePosition)
  167.         {
  168.             var position = Position;
  169.             position.X -= Position.Width / 2;
  170.             position.Y -= Position.Height / 2;
  171.  
  172.             return position.Contains(mousePosition);
  173.         }
  174.  
  175.         public void Draw(SpriteBatch spriteBatch, bool center = true)
  176.         {
  177.             if (IsVisible)
  178.             {
  179.                 var source = Source.HasValue ? Source : new Rectangle(0, 0, Sprite.Width, Sprite.Height);
  180.  
  181.                 Vector2 origin = new Vector2(0, 0);
  182.                 if (center)
  183.                     origin = Source.HasValue ? new Vector2(Source.Value.Width / 2, Source.Value.Height / 2) : new Vector2(Sprite.Width / 2, Sprite.Height / 2);
  184.  
  185.  
  186.                 var position = new Rectangle(Position.X, Position.Y, (int)(Position.Width * Scale), (int)(Position.Height * Scale));
  187.  
  188.                 spriteBatch.Draw(Sprite, position, source, TintColor, (float)Rotation, origin, SpriteEffects.None, 0);
  189.             }
  190.         }
  191.  
  192.         public void Resize(int width, int height)
  193.         {
  194.             Position = new Rectangle(Position.X, Position.Y, width, height);
  195.         }
  196.  
  197.         public void SetPosition(int x, int y)
  198.         {
  199.             Position = new Rectangle(x, y, Position.Width, Position.Height);
  200.         }
  201.  
  202.         public void SetScale(float scale)
  203.         {
  204.             Scale = scale;
  205.         }
  206.  
  207.         public void SetColor(Color color)
  208.         {
  209.             TintColor = color;
  210.         }
  211.  
  212.         public void SetVisibility(bool visible)
  213.         {
  214.             IsVisible = visible;
  215.         }
  216.  
  217.     }
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement