Jump to content
  • Advertisement
Sign in to follow this  
Portishead

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

This topic is 3612 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!