[C#][2D game programming]The game loop.

Started by
11 comments, last by Portishead 15 years, 4 months ago
Like I said in the other thread, I'm trying to do my first project - Block Breakout. So far I've put the key events that I need for the game. I decided that for now instead of a ball I'll do a regular shooting line with the blocks(when I understand how to do this, I'll try and make a ball later). What I dont unserstand from the code is where does the game loop take place. Here's my code:

        private void Form1_Load(object sender, EventArgs e)
        {
            this.StartPosition = FormStartPosition.CenterScreen;
            this.FormBorderstyle = FormBorderstyle.None;
            this.Cursor.Dispose();
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (e.KeyCode == Keys.A)
            {
                Point myPoint = new Point();
                myPoint.X = pictureBox1.Location.X - 1;
                myPoint.Y = pictureBox1.Location.Y;
                pictureBox1.Location = myPoint;
            }
            if (e.KeyCode == Keys.D)
            {
                Point myPoint = new Point();
                myPoint.X = pictureBox1.Location.X + 1;
                myPoint.Y = pictureBox1.Location.Y;
                pictureBox1.Location = myPoint;
            }
            if (e.KeyCode == Keys.S)
            {
                //It will be the shooting.
            }
            if (e.KeyCode == Keys.Escape)
            {
                this.Close();
            }
        }
What do I do next in the code?
Advertisement
you should use state management
do something like:

bool gameRunning = false;

then you'd make that variable = true, maybe when user press a key or something?
then you'd loop thought it

while(gameRunning)
{
// all your rendering, logic goes here
}

hope it helps
blog: www.brasilokau.com/games/blog
I still don't get it.. :The key events I did should not be there? Where should they be?
Where do I write the loop? What do I do to render?
I wouldn't use a GUI application for a real-time game. Those programs are event-driven by design, and while it's possible to cram a game-loop into them, it's usually not pretty. Leave those forms, dialogs and controls for tools and other apps. ;)

For games, you'll want to build 'normal' applications instead, one where you control the program flow. For graphics and input, take a look at Tao.SDL or SDL.NET. There are some examples and tutorials on their sites which should help you getting started.
Create-ivity - a game development blog Mouseover for more information.
in form load, just throw in a while (true) {//method calls here} statement
Jimi: I tried doing it, but I don't know what functions I need to do and what to include in them and the while loop got my program stuck.

Captain P - In some articles I read they always changed the window properties, but wrote the loop in static void Main(). How do I turn from console to Window Form?
On the other thread you said you have done other projects, what have you done? Based on you're questions it sounds like you may be getting ahead of yourself.
This is the preferred game loop in C#, as having been determined by experimenting with all the various options. Read more about it here.

using System;using System.Drawing;using System.Runtime.InteropServices;using System.Windows.Forms;namespace MyProject{    [StructLayout(LayoutKind.Sequential)]    struct Message    {        public IntPtr hWnd;        public uint msg;        public IntPtr wParam;        public IntPtr lParam;        public uint time;        public Point p;    }    static class Program    {        [DllImport("User32.dll", CharSet = CharSet.Auto)]        [return: MarshalAs(UnmanagedType.Bool)]        static extern bool PeekMessage(out Message msg, IntPtr hWnd, uint messageFilterMin, uint messageFilterMax, uint flags);        static bool AppStillIdle        {            get            {                Message msg;                return !PeekMessage(out msg, IntPtr.Zero, 0, 0, 0);            }        }        [STAThread]        static void Main()        {            Application.EnableVisualStyles();            Application.SetCompatibleTextRenderingDefault(false);            var myForm = new MyForm();            Application.Idle += (s, e) => { while (AppStillIdle) game.Render(); };            Application.Run(myForm);        }    }}


It's still quite possible to use a form in the normal way, but you do your rendering separately like shown.

[Edited by - Mike.Popoloski on November 28, 2008 9:04:34 AM]
Mike Popoloski | Journal | SlimDX
All the projects I've done before were programs in Windows Application, never done games before..

Edit: Mike - thanks, I'm trying to understand the code now..

Edit2: When I tried typing the code in console app, it said it didn't recognize system.Drawing and System.Windows.Forms.

[Edited by - Portishead on November 28, 2008 8:04:10 AM]
Quote:Original post by Portishead
Edit: Mike - thanks, I'm trying to understand the code now..

Something went wrong with the link, but Mike probably wanted to refer you to his journal.

This topic is closed to new replies.

Advertisement