Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.56 KB | None | 0 0
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace Digger
  12. {
  13.     public class DiggerWindow : Form
  14.     {
  15.         Dictionary<string, Bitmap> bitmaps = new Dictionary<string, Bitmap>();
  16.         GameState gameState;
  17.  
  18.         public Game game;
  19.  
  20.         public DiggerWindow()
  21.         {
  22.             game = new Game(20, 20);
  23.             gameState = new GameState();
  24.             ClientSize = new Size(GameState.ElementSize * game.MapWidth, GameState.ElementSize * game.MapHeight + GameState.ElementSize);
  25.             FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
  26.             Text = "Digger";
  27.             DoubleBuffered = true;
  28.  
  29.             var imagesDirectory = new DirectoryInfo("Images");
  30.             foreach(var e in imagesDirectory.GetFiles("*.png"))
  31.                 bitmaps[e.Name]=(Bitmap)Bitmap.FromFile(e.FullName);
  32.             var timer = new Timer();
  33.             timer.Interval = 1;
  34.             timer.Tick += TimerTick;
  35.             timer.Start();
  36.         }
  37.  
  38.         protected override void OnKeyDown(KeyEventArgs e)
  39.         {
  40.             game.KeyPressed = e.KeyCode;
  41.         }
  42.  
  43.         protected override void OnKeyUp(KeyEventArgs e)
  44.         {
  45.             game.KeyPressed = Keys.None;
  46.         }
  47.  
  48.  
  49.         protected override void OnPaint(PaintEventArgs e)
  50.         {
  51.             e.Graphics.TranslateTransform(0, GameState.ElementSize);
  52.             e.Graphics.FillRectangle(Brushes.Black,0,0, GameState.ElementSize *game.MapWidth, GameState.ElementSize *game.MapHeight);
  53.             foreach (var a in gameState.animations)
  54.             {
  55.                 var fileName = a.Creature.ImageFileName;
  56.                 var loc = a.Location;
  57.                 e.Graphics.DrawImage(bitmaps[a.Creature.ImageFileName], a.Location);
  58.             }
  59.             e.Graphics.ResetTransform();
  60.             e.Graphics.DrawString(game.Scores.ToString(), new Font("Arial", 16), Brushes.Green, 0, 0);
  61.         }
  62.  
  63.         int tickCount = 0;
  64.  
  65.         void TimerTick(object sender, EventArgs args)
  66.         {
  67.             if (tickCount == 0) gameState.BeginAct();
  68.             foreach (var e in gameState.animations)
  69.                 e.Location = new Point(e.Location.X + 4*e.Command.DeltaX, e.Location.Y + 4*e.Command.DeltaY);
  70.             if (tickCount == 7)
  71.                 gameState.EndAct();
  72.  
  73.             tickCount++;
  74.             if (tickCount == 8) tickCount = 0;
  75.             Invalidate();
  76.         }
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement