Advertisement
Spa_m

gr

Oct 16th, 2018
410
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.55 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Microsoft.Xna.Framework;
  7.  
  8. namespace LAB_01_Duszek
  9. {
  10.     class Ghost
  11.     {
  12.         public Rectangle Position { get; set; }
  13.         public bool IsHitted { get; set; }
  14.         public int AppearanceTime { get; set; }
  15.        
  16.     }
  17. }
  18.  
  19. /////////////////////////////////////////////////////////////////////////////////////
  20. using Microsoft.Xna.Framework;
  21. using Microsoft.Xna.Framework.Graphics;
  22. using Microsoft.Xna.Framework.Input;
  23. using System;
  24. using System.Collections.Generic;
  25.  
  26. namespace LAB_01_Duszek
  27. {
  28.     /// <summary>
  29.     /// This is the main type for your game.
  30.     /// </summary>
  31.     public class Game1 : Game
  32.     {
  33.         GraphicsDeviceManager graphics;
  34.         SpriteBatch spriteBatch;
  35.  
  36.         Texture2D ghost;
  37.         Texture2D ghostFoot;
  38.         Texture2D street;
  39.  
  40.         Rectangle backgroundRec;
  41.         List<Ghost> ghosts;
  42.  
  43.         int points, newGhostTimer;
  44.         Random rng;
  45.         MouseState mouseState;
  46.         private Point mousePosition;
  47.  
  48.         private const int GhostSize = 50;
  49.  
  50.  
  51.  
  52.         int RandomGhostX()
  53.         {
  54.             return rng.Next(0, GraphicsDevice.Viewport.Width - GhostSize);
  55.         }
  56.         int RandomGhostY()
  57.         {
  58.             return rng.Next(0, GraphicsDevice.Viewport.Height - GhostSize);
  59.         }
  60.  
  61.         void AddNewGhost()
  62.         {
  63.             Ghost ghost = new Ghost();
  64.             ghost.AppearanceTime = rng.Next(0, 2000);
  65.             ghost.IsHitted = false;
  66.             Rectangle ghostRectangle = new Rectangle(RandomGhostX(), RandomGhostY(), GhostSize, GhostSize);
  67.             ghost.Position = ghostRectangle;
  68.  
  69.             ghosts.Add(ghost);
  70.         }
  71.  
  72.         void UpdateGhostsAppearanceTime(int deltaTime)
  73.         {
  74.             for (int i = ghosts.Count - 1; i >= 0; i--)
  75.             {
  76.                 ghosts[i].AppearanceTime -= deltaTime;
  77.  
  78.                 if (ghosts[i].AppearanceTime <= 0 && ghosts[i].IsHitted == false)
  79.                     ghosts.Remove(ghosts[i]);
  80.             }
  81.         }
  82.  
  83.  
  84.         void CheckGhostClick()
  85.         {
  86.             mouseState = Mouse.GetState();
  87.             mousePosition = new Point(mouseState.X, mouseState.Y);
  88.  
  89.             for (int i = ghosts.Count - 1; i >= 0; i--)
  90.             {
  91.                 if (mouseState.LeftButton == ButtonState.Pressed &&
  92.                     ghosts[i].Position.Contains(mousePosition) &&
  93.                     ghosts[i].IsHitted == false)
  94.                 {
  95.                     ghosts[i].IsHitted = true;
  96.                     points++;
  97.                 }
  98.             }
  99.         }
  100.  
  101.  
  102.  
  103.         string TitleBuilder()
  104.         {
  105.             return "Score: " + Convert.ToString(points);
  106.         }
  107.  
  108.         public Game1()
  109.         {
  110.             graphics = new GraphicsDeviceManager(this);
  111.             Content.RootDirectory = "Content";
  112.  
  113.  
  114.             Window.AllowUserResizing = true;
  115.  
  116.         }
  117.  
  118.         /// <summary>
  119.         /// Allows the game to perform any initialization it needs to before starting to run.
  120.         /// This is where it can query for any required services and load any non-graphic
  121.         /// related content.  Calling base.Initialize will enumerate through any components
  122.         /// and initialize them as well.
  123.         /// </summary>
  124.         protected override void Initialize()
  125.         {
  126.             // TODO: Add your initialization logic here
  127.             IsMouseVisible = true;
  128.             base.Initialize();
  129.             points = 0;
  130.             Window.Title = TitleBuilder();
  131.             rng = new Random();
  132.  
  133.             ghosts = new List<Ghost>();
  134.             newGhostTimer = 0;
  135.  
  136.         }
  137.  
  138.         /// <summary>
  139.         /// LoadContent will be called once per game and is the place to load
  140.         /// all of your content.
  141.         /// </summary>
  142.         protected override void LoadContent()
  143.         {
  144.             // Create a new SpriteBatch, which can be used to draw textures.
  145.             spriteBatch = new SpriteBatch(GraphicsDevice);
  146.  
  147.             // TODO: use this.Content to load your game content here
  148.             ghost = Content.Load<Texture2D>("Graphics/ghost");
  149.             ghostFoot = Content.Load<Texture2D>("Graphics/ghost-foot");
  150.             street = Content.Load<Texture2D>("Graphics/street");
  151.             backgroundRec = new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
  152.         }
  153.  
  154.         /// <summary>
  155.         /// UnloadContent will be called once per game and is the place to unload
  156.         /// game-specific content.
  157.         /// </summary>
  158.         protected override void UnloadContent()
  159.         {
  160.             // TODO: Unload any non ContentManager content here
  161.         }
  162.  
  163.         /// <summary>
  164.         /// Allows the game to run logic such as updating the world,
  165.         /// checking for collisions, gathering input, and playing audio.
  166.         /// </summary>
  167.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  168.         protected override void Update(GameTime gameTime)
  169.         {
  170.             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
  171.                 Exit();
  172.  
  173.  
  174.             // TODO: Add your update logic here
  175.             backgroundRec.Width = GraphicsDevice.Viewport.Width;
  176.             backgroundRec.Height = GraphicsDevice.Viewport.Height;
  177.  
  178.             if (newGhostTimer <= 0)
  179.             {
  180.                 AddNewGhost();
  181.                 newGhostTimer = 500;
  182.             }
  183.  
  184.             newGhostTimer -= gameTime.ElapsedGameTime.Milliseconds;
  185.  
  186.  
  187.  
  188.             Window.Title = TitleBuilder();
  189.  
  190.             CheckGhostClick();
  191.             UpdateGhostsAppearanceTime(gameTime.ElapsedGameTime.Milliseconds);
  192.  
  193.  
  194.             base.Update(gameTime);
  195.         }
  196.  
  197.         /// <summary>
  198.         /// This is called when the game should draw itself.
  199.         /// </summary>
  200.         /// <param name="gameTime">Provides a snapshot of timing values.</param>
  201.         protected override void Draw(GameTime gameTime)
  202.         {
  203.             GraphicsDevice.Clear(Color.CornflowerBlue);
  204.  
  205.             // TODO: Add your drawing code here
  206.             spriteBatch.Begin();
  207.  
  208.             spriteBatch.Draw(street, backgroundRec, Color.White);
  209.  
  210.             foreach (var ghost_ in ghosts)
  211.             {
  212.                 if (ghost_.IsHitted)
  213.                 {
  214.                     spriteBatch.Draw(ghostFoot, ghost_.Position, Color.White);
  215.                 }
  216.                 else
  217.                 {
  218.                     spriteBatch.Draw(ghost, ghost_.Position, Color.White);
  219.                 }
  220.  
  221.             }
  222.  
  223.  
  224.  
  225.             spriteBatch.End();
  226.  
  227.             base.Draw(gameTime);
  228.         }
  229.     }
  230. }
  231.  
  232. ///////////////////////////////////////////////////
  233.  
  234.             foreach (var tile_ in tiles)
  235.             {
  236.  
  237.                 /*spriteBatch.Draw(tile_.texture, tile_.position, null, Color.White,
  238.                     (float)Math.PI / 2 * tile_.rotation, new Vector2(36, 36), SpriteEffects.None, 0f);*/
  239.                 spriteBatch.Draw(
  240.                         tile_.texture,
  241.                         new Vector2(tile_.position.X + (tileSize / 2), tile_.position.Y + (tileSize / 2)),
  242.                         null,
  243.                         Color.White,
  244.                         (float)(Math.PI / 2) * tile_.rotation,
  245.                         new Vector2(tile_.position.Width,tile_.position.Height),
  246.                         0.5f,
  247.                         SpriteEffects.None,
  248.                         0f);
  249.             }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement