Advertisement
Guest User

Untitled

a guest
Dec 9th, 2013
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.69 KB | None | 0 0
  1.     public partial class GameForm : Form
  2.     {        
  3.         const String VERSION = "v0.2";
  4.         const String TITLE = "PacMaze " + VERSION;
  5.  
  6.         // Timer variables.
  7.         int lastTick;
  8.         int lastFrameRate;
  9.         int frameRate;
  10.         int delta;
  11.  
  12.         // Graphics variables.
  13.         Graphics graphics;
  14.         Graphics backBufferObject;
  15.         Bitmap backBuffer;
  16.  
  17.         public GameForm()
  18.         {          
  19.             InitializeComponent();
  20.             this.Show();
  21.             this.Focus();
  22.  
  23.             // Initialize graphics objects.
  24.             graphics = this.CreateGraphics();
  25.             backBuffer = new Bitmap(this.Width, this.Height);            
  26.  
  27.             LoopGame();            
  28.         }
  29.  
  30.         /// <summary>
  31.         /// Start permanent game logic loop, run game completely.
  32.         /// </summary>
  33.         private void LoopGame()
  34.         {
  35.             while (true) // Permanent loop, will run as long as game is NOT being exited.
  36.             {
  37.                 Application.DoEvents(); // Test for running.
  38.  
  39.                 // Handle user-input (Keyboard).
  40.  
  41.                 // Draw things to the screen.
  42.                 DrawScreen();
  43.  
  44.                 // Handle timing issues.
  45.                 DoTimer();
  46.             }
  47.         }
  48.  
  49.  
  50.         /// <summary>
  51.         /// Draw main graphics to the screen.
  52.         /// </summary>
  53.         private void DrawScreen()
  54.         {
  55.             // Copy the BackBuffer to Graphics Object.
  56.             using (graphics = Graphics.FromImage(backBuffer))
  57.             using (backBufferObject = this.CreateGraphics())
  58.             {
  59.                 // Draw BackBuffer to screen (buffer switching).                  
  60.                 backBufferObject.DrawImage(backBuffer, 0, 0, this.Width, this.Height);
  61.            
  62.                 graphics.DrawImage(Properties.Resources.scary_pacman, new Rectangle(
  63.                     this.Width / 2 , this.Height / 2, 32, 32));
  64.  
  65.                 graphics.Clear(Color.Thistle); // Clear BackBuffer for efficient rendering.          
  66.             }                      
  67.         }
  68.  
  69.         /// <summary>
  70.         /// Control timing with ticks per second.
  71.         /// </summary>
  72.         private void DoTimer()
  73.         {
  74.             delta = System.Environment.TickCount - lastTick;
  75.             if (delta >= 1000)
  76.             {
  77.                 this.Text = TITLE + "  FPS: " + lastFrameRate + " Delta: " + delta; // Display timing stats every second.
  78.                 lastFrameRate = frameRate;
  79.                 frameRate = 0;
  80.                 lastTick = System.Environment.TickCount;                
  81.             }        
  82.  
  83.             frameRate++;            
  84.         }
  85.  
  86.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement