Jump to content
  • Advertisement
Sign in to follow this  
phil67rpg

game loop

This topic is 2254 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

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


}

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites

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

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!