PDA

Click to See Complete Forum and Search --> : [2.0] I need a better way to do this


Fromethius
Nov 10th, 2006, 04:01 PM
Alright.. I definaltly do not like this code. I like making games, and I often can finish a game exactly to my liking without asking anyone how to do something. Although I can do it, I don't like HOW I do it.

Basically, let's use Pong as an example. I use a Timer to make the ball move, and that is ok, but then I need an if statement to run every 5 milliseconds about whether or not the ball hit player one's bar. Then another for player two's bar, then another about whether or not the ball went through to bottom, another one for if it went through the top, another for changing direction. Hell, the program is running like 10 if statements every 5 milliseconds.

Let's use Snake as an example, I run an if statement every 5 milliseconds about whether or not the snake hit a piece of food, another for if it went outside the borderline, and so many others... So I am doing another 5 if statements every 5 milliseconds

I mean.. is there another way to do this? I hate the way I am coding this.. It just feels.. so.. wrong..





Also, while I'm at it, I might as well ask this, how do you make it so something cannot go through something? Like.. if I want pacman to not go through the borders of the game, the walls, etc. I prefer not to use if statements as well.. Just make it so.. it cannot go through the walls and border. i dunno if it is possible with like a property or something.. but that would be GREAT.

THANKS!!!!!!!!!!!!!!!!!!!

jmcilhinney
Nov 10th, 2006, 06:55 PM
You need some sort of conditional construct like If or Select Case but you don't need to execute them any more than is necessary. A collision can only occur when something moves so you need to handle the appropriate event. If you're moving a PictureBox then you'd handle its Move or LocationChanged event and test for collisions there. If you're using GDI+ to draw then you'd execute your collision detection code either on the Paint event or from the Tick event of the Timer that instigated the move. The latter would probably prove to be the better option.

Fromethius
Nov 12th, 2006, 01:47 AM
Hmm.. If I use Move or LocationChanged, then it will only execute when it moves. It is moving on a Timer that has an interval of 5 milliseconds. So if I put it on LocationChanged, since it moves every 5 milliseconds, it will activate every 5 milliseconds. Also, if I put it on the Timer event handler, then, since the interval is 5 milliseconds, then it will ALSO run every 5 milliseconds. I don't see how this way is better or quicker. Each one activates every 5 milliseconds. Also, I AM using a PictureBox.