Results 1 to 3 of 3

Thread: [2.0] I need a better way to do this

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Talking [2.0] I need a better way to do this

    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!!!!!!!!!!!!!!!!!!!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] I need a better way to do this

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2006
    Location
    Pennsylvania
    Posts
    1,069

    Re: [2.0] I need a better way to do this

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width