Skip to main content
GameDev.net gamedev.net
🔒 Locked

game loop

Started by phil67rpg Mar 22, 2012 at 12:53 AM 8 replies 3k views
Original Post
phil67rpg
phil67rpg
well I am still working on breakout using c#.I have done alot of research on game loops and have found the following .

private void Form1_Load(object sender, EventArgs e)
{
Application.Idle += new EventHandler(Application_Idle);
}
private void Application_Idle(Object sender, EventArgs e)
{
Graphics g=this.CreateGraphics();

Image ball_image = Image.FromFile(@"c:\Users\phil\Desktop\ball.bmp");
g.DrawImage(ball_image, x + vx, y + vy);

x += vx;
y += vy;

if (y >= bottom_wall || y<=top_wall)
{
vy = -vy;
}
if (x >= right_wall || x <= left_wall)
{
vx = -vx;
}
if ((x >= paddle_left+paddle_x && x <= paddle_right+paddle_x) && (y >= paddle_top ))
{
vx = -vx;
vy = -vy;
}
if ((x <= brick_left && x >= brick_right + paddle_x) && (y <= brick_bottom))
{
vx = -vx;
vy = -vy;
}
}
}

thanks for all the help


}
Cornstalks
Cornstalks
Phil, what do we need to do to help you to learn how to post effectively and effectively ask questions? You do this every time you post. Look over your post and pretend you're someone else reading it. Can you see what your question is? Is it clear what you're asking and what you're trying to do? It isn't for me. Not at all.

I'm not saying this to be rude, but I don't know if anyone's pointed it out yet, and I think helping you understand this is important to helping you progress in the long run.
phil67rpg
phil67rpg
sorry corn nice to hear from you again,I will try to be more specific in my posts.as for this one I am trying to code a game loop.so that the ball will move around the screen by itself and not need any input from the keyboard.
Postie
Postie
What is the behaviour of the code you posted? And is that not what you expected?

On another note, you should move that line that loads the image from the file outside the game loop and put it somewhere where it will be initialised only once.
[size="2"]Currently working on an open world survival RPG - For info check out my Development blog:[size="2"] ByteWrangler
LePhantom
LePhantom
Is that actually in a loop?
phil67rpg
phil67rpg
well postie the ball does not move at all
dragonalumni
dragonalumni
add a timer object

make a function called something like "update_game"

use the timer to call "update_game" at whatever speed you like.

done.
Nypyren
Nypyren
This might be a silly question, but have you set vx and vy to nonzero values?


This part...

[color="#666600"]([color="#000000"]x [color="#666600"]<=[color="#000000"] brick_left [color="#666600"]&&[color="#000000"] x [color="#666600"]>=[color="#000000"] brick_right [color="#666600"]+[color="#000000"] paddle_x[color="#666600"])

...seems incorrect to me.

I think it should be:

(x >= brick_left && x <= brick_right)


The parts where you reverse BOTH vx and vy, I think you only want to reverse vy, not vx.
kunos
kunos
As I have already explained you here http://www.gamedev.net/topic/622066-game-run-loop/page__p__4923192#entry4923192 The "OnIdle" event doesn't get called continuously but only when the application BECOMES idle, only one time, so it is your responsability to check it is still Idle using Win32 API PeekMessage.

Do not fall into the syndrome of cut and paste random code that you don't understand from the internet. If you don't understand the code, take a break, relax, and step back. Go back to your basics, check the documentation of every single function you are using and make sure they do what you want them to do. That's programming.

To make a game based on WinForm you need to understand you are "bending" the system to do something it is not designed to do.. so you do not have a trivial problem there...and that can't be solved by staying in WinForm land.
Postie
Postie

well postie the ball does not move at all

Sorry, what I was alluding to in my post but apparently left out, was that loading the ball bitmap from disk every frame is going to make your game run very slow.

SlimDX is a .net wrapper for DirectX, and appears to have had the same issues with establishing a proper game loop under WinForms. Their solution seems to work well, though. The last few sections of the following tutorial might have some ideas that you can make use of: http://slimdx.org/tutorials/BasicWindow.php
[size="2"]Currently working on an open world survival RPG - For info check out my Development blog:[size="2"] ByteWrangler

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.