Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public partial class GameForm : Form
- {
- const String VERSION = "v0.2";
- const String TITLE = "PacMaze " + VERSION;
- // Timer variables.
- int lastTick;
- int lastFrameRate;
- int frameRate;
- int delta;
- // Graphics variables.
- Graphics graphics;
- Graphics backBufferObject;
- Bitmap backBuffer;
- public GameForm()
- {
- InitializeComponent();
- this.Show();
- this.Focus();
- // Initialize graphics objects.
- graphics = this.CreateGraphics();
- backBuffer = new Bitmap(this.Width, this.Height);
- LoopGame();
- }
- /// <summary>
- /// Start permanent game logic loop, run game completely.
- /// </summary>
- private void LoopGame()
- {
- while (true) // Permanent loop, will run as long as game is NOT being exited.
- {
- Application.DoEvents(); // Test for running.
- // Handle user-input (Keyboard).
- // Draw things to the screen.
- DrawScreen();
- // Handle timing issues.
- DoTimer();
- }
- }
- /// <summary>
- /// Draw main graphics to the screen.
- /// </summary>
- private void DrawScreen()
- {
- // Copy the BackBuffer to Graphics Object.
- using (graphics = Graphics.FromImage(backBuffer))
- using (backBufferObject = this.CreateGraphics())
- {
- // Draw BackBuffer to screen (buffer switching).
- backBufferObject.DrawImage(backBuffer, 0, 0, this.Width, this.Height);
- graphics.DrawImage(Properties.Resources.scary_pacman, new Rectangle(
- this.Width / 2 , this.Height / 2, 32, 32));
- graphics.Clear(Color.Thistle); // Clear BackBuffer for efficient rendering.
- }
- }
- /// <summary>
- /// Control timing with ticks per second.
- /// </summary>
- private void DoTimer()
- {
- delta = System.Environment.TickCount - lastTick;
- if (delta >= 1000)
- {
- this.Text = TITLE + " FPS: " + lastFrameRate + " Delta: " + delta; // Display timing stats every second.
- lastFrameRate = frameRate;
- frameRate = 0;
- lastTick = System.Environment.TickCount;
- }
- frameRate++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement