Discovery Gaming Community
A lil C# help - Printable Version

+- Discovery Gaming Community (https://discoverygc.com/forums)
+-- Forum: The Community (https://discoverygc.com/forums/forumdisplay.php?fid=4)
+--- Forum: Real Life Discussion (https://discoverygc.com/forums/forumdisplay.php?fid=16)
+--- Thread: A lil C# help (/showthread.php?tid=98000)

Pages: 1 2 3


A lil C# help - Huhuh - 05-02-2013

I'm making a space invaders game for uni in C#

Can someone please instruct me on how to use a timer to update the frames so the sprites move about (smoothly)?


RE: A lil C# help - Bitjump - 05-02-2013

If you can elaborate, I can probably help. You mean like a timer to control your average update/render cycle?


RE: A lil C# help - massdriver - 05-02-2013

should look like (not actual working code but a basic msg runloop section):

MSG msg;
PeekMessage( &msg, NULL, 0, 0, PM_REMOVE );

// Some timer object
_timer.Start();

while( msg.message != WM_QUIT )
{
// Process all win messages
if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
continue;
};

// Update time based actions
float time = _timer.GetElapsedTime();

if( time > 0.0f )
Update( time );

// Render frame
Render();
}


RE: A lil C# help - Bitjump - 05-02-2013

Massdriver is spot on.

Basically, store the current time (in ms). Handle input, then update positions, then render. When you loop, calculate the ms passed since last update and pass it to the update and render methods. It's usually referred to as "delta". Rinse and repeat, updating according to calculations done with delta.


RE: A lil C# help - utrack - 05-02-2013

@massdriver: C# is more high-level than C++. Smile

use Timer.Tick event - it called each time timer ticks (duh)
or you need something more specific?

You'll need to make interval ~40 msec or so for 25 fps. Possible invoking it from another thread so main window won't get locked for too long
Also it's good idea to use 2D SlimDX or Ogre so you'll get real DirectX and your invaders won't get locked at all - and you'll get real game. Smile


RE: A lil C# help - Huhuh - 05-02-2013

(05-02-2013, 12:48 PM)utrack Wrote: use Timer.Tick event - it called each time timer ticks (duh)
or you need something more specific?

That sounds spot on. Where can I access that?

Basically, I need my space invaders to move to the right at a constant rate for a period of time, then move down, and then left at a constant rate.

I will also need each shot fired to move upwards or downwards (depending on the firer) at a constant rate until it either leaves the game area or intercepts a valid hitbox (which technically don't exist, as I'll simply check if the shot is within a certain area from the point of origin of each entity).


RE: A lil C# help - massdriver - 05-02-2013

(05-02-2013, 12:48 PM)utrack Wrote: @massdriver: C# is more high-level than C++. Smile
[Image: kYRAwAg.jpg]

indeed its above any planes


RE: A lil C# help - massdriver - 05-02-2013

(05-02-2013, 01:18 PM)Crackpunch Wrote:
(05-02-2013, 12:48 PM)utrack Wrote: use Timer.Tick event - it called each time timer ticks (duh)
or you need something more specific?

That sounds spot on. Where can I access that?

Basically, I need my space invaders to move to the right at a constant rate for a period of time, then move down, and then left at a constant rate.

I will also need each shot fired to move upwards or downwards (depending on the firer) at a constant rate until it either leaves the game area or intercepts a valid hitbox (which technically don't exist, as I'll simply check if the shot is within a certain area from the point of origin of each entity).

also try appgamekit, must go for you ideally


RE: A lil C# help - Huhuh - 05-02-2013

It needs to be coded in C# (which is indeed higher level than C++).


RE: A lil C# help - utrack - 05-02-2013

@massdriver: rofl
i can't into english lately

@crackpunch: well, here's example
Code:
public partial class FormWithTimer : Form
    {
        Timer timer = new Timer();

        public FormWithTimer()
        {
            InitializeComponent();

            timer.Tick += new EventHandler(timer_Tick); // Everytime timer ticks, timer_Tick will be called
            timer.Interval = (1000) / (25);             // 25 FPS
            timer.Enabled = true;                       // Enable the timer
            timer.Start();                              // Start the timer
        }

        void timer_Tick(object sender, EventArgs e)
        {
            RenewImage();
        }
    }
goes kinda like that.

For the moving part - maybe you'll need UpwardShot and DownwardShot classes ('cause there could be more than one shot at once) with refresh methods and coords properties, and Refresh() will be called by RenewImage procedure which will just add some points when called.

Then for enemies: again classes for each type of enemy and some simple variable as step count, which will be ++'d each frame - and simple "select case" with this variable which will decide which coordinate to change on refresh. After some point it should be zeroed, cause you want them to move in loop.

Then you should get all the instances with their coordinates and place bitmaps using these coords. DOne - frame ready

P.S.: this could work only for uni project, for production better use 2D DX or OpenGL
P.P.S.: this algorithm was written under the influence of beer, and i don't know what performance it'll get. The code will look nicely tho