Advertisement
Guest User

PlaygroundDeprecated

a guest
Apr 7th, 2020
466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.41 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Windows.Forms;
  6. using App.View.Renderings;
  7.  
  8. namespace App.View
  9. {
  10.  
  11.     public partial class PlaygroundDeprecated : Form
  12.     {
  13.         private System.ComponentModel.IContainer components = null;
  14.         private bool gameOver = false;
  15.         private int startTime = 0;
  16.         private int currentTime = 0;
  17.         public Game game;
  18.         public Bitmap dragonImage;
  19.         public Sprite dragonSprite;
  20.         public Bitmap grass;
  21.         public int frameCount = 0;
  22.         public int frameTimer = 0;
  23.         public float frameRate = 0;
  24.         public PointF velocity;
  25.         public int direction = 2;
  26.            
  27.  
  28.         public PlaygroundDeprecated()
  29.         {
  30.             this.ClientSize = new System.Drawing.Size(1280, 720);
  31.             this.Load += new System.EventHandler(this.PlaygroundDeprecated_Load);
  32.             this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Playground_FormClosed);
  33.             this.KeyDown += new KeyEventHandler(this.PlaygroundDeprecated_KeyDown);
  34.             this.ResumeLayout(false);
  35.         }
  36.        
  37.         protected override void Dispose(bool disposing)
  38.         {
  39.             if (disposing && (components != null))
  40.             {
  41.                 components.Dispose();
  42.             }
  43.             base.Dispose(disposing);
  44.         }
  45.  
  46.         private void PlaygroundDeprecated_Load(object sender, EventArgs e)
  47.         {
  48.             Main();
  49.         }
  50.  
  51.         private void PlaygroundDeprecated_KeyDown(object sender, KeyEventArgs e)
  52.         {
  53.             Game_KeyPressed(e.KeyCode);
  54.         }
  55.  
  56.         public bool Game_Init()
  57.         {
  58.             this.Text = "Sprite Drawing Demo";
  59.             grass = game.LoadBitmap(@"img/grass.bmp");
  60.             dragonImage = game.LoadBitmap(@"img/dragon.png");
  61.             dragonSprite = new Sprite(ref game);
  62.             dragonSprite.Image = dragonImage;
  63.             dragonSprite.Width = 256;
  64.             dragonSprite.Height = 256;
  65.             dragonSprite.Columns = 8;
  66.             dragonSprite.TotalFrames = 64;
  67.             dragonSprite.AnimationRate = 20;
  68.             dragonSprite.X = 250;
  69.             dragonSprite.Y = 150;
  70.             return true;
  71.         }
  72.  
  73.         public void Game_Update(int time)
  74.         {
  75.            
  76.         }
  77.  
  78.         public void Game_Draw()
  79.         {
  80.             //draw background
  81.             game.DrawBitmap(ref grass, 0, 0, 800, 600);
  82.  
  83.             switch (direction)
  84.             {
  85.                 case 0: velocity = new Point(0, -1);
  86.                     break;
  87.                 case 2: velocity = new Point(1, 0);
  88.                     break;
  89.                 case 4: velocity = new Point(0, 1);
  90.                     break;
  91.                 case 6: velocity = new Point(-1, 0);
  92.                     break;
  93.             }
  94.  
  95.             dragonSprite.X += velocity.X;
  96.             dragonSprite.Y += velocity.Y;
  97.  
  98.             dragonSprite.Animate(direction * 8 + 1, direction * 8 + 7);
  99.             dragonSprite.Draw();
  100.            
  101.             game.Print(0, 0, "press any key to change direction");
  102.         }
  103.  
  104.         public void Game_End()
  105.         {
  106.             dragonImage = null;
  107.             dragonSprite = null;
  108.             grass = null;
  109.         }
  110.        
  111.         public void Game_KeyPressed(System.Windows.Forms.Keys key)
  112.         {
  113.             switch (key)
  114.             {
  115.                 case Keys.Escape: Shutdown(); break;
  116.                 case Keys.Up: direction = 0; break;
  117.                 case Keys.Right: direction = 2; break;
  118.                 case Keys.Down: direction = 4; break;
  119.                 case Keys.Left: direction = 6; break;
  120.             }
  121.         }
  122.  
  123.         public Bitmap LoadBitmap(string filename)
  124.         {
  125.             Bitmap bmp = null;
  126.             try
  127.             {
  128.                 bmp = new Bitmap(filename);
  129.             }
  130.             catch (Exception ex) { }
  131.             return bmp;
  132.         }
  133.        
  134.        
  135.         private void Playground_FormClosed(object sender, EventArgs e)
  136.         {
  137.             Shutdown();
  138.         }
  139.  
  140.         public void Shutdown()
  141.         {
  142.             gameOver = true;
  143.         }
  144.  
  145.         public void Main()
  146.         {
  147.             Form form = (Form) this;
  148.             game = new Game(ref form, width: 800, height: 600);
  149.             Game_Init();
  150.             while (!gameOver)
  151.             {
  152.                 //update time
  153.                 currentTime = Environment.TickCount;
  154.                
  155.                 //let gameplay code update
  156.                 Game_Update(time: currentTime - startTime);
  157.                
  158.                 //refresh at 60 fps
  159.                 if (currentTime > startTime + 16)
  160.                 {
  161.                     //update time
  162.                     startTime = currentTime;
  163.                    
  164.                     //let gameplay code draw
  165.                     Game_Draw();
  166.                    
  167.                     //give the form some cycles
  168.                     Application.DoEvents();
  169.                     game.Update();
  170.                 }
  171.  
  172.                 frameCount += 1;
  173.                 if (currentTime > frameTimer + 1000)
  174.                 {
  175.                     frameTimer = currentTime;
  176.                     frameRate = frameCount;
  177.                     frameCount = 0;
  178.                 }
  179.             }
  180.             Game_End();
  181.             Application.Exit();
  182.         }
  183.     }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement