How to slow this down?

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

User avatar
Stoves
Veteran
Posts: 101
Joined: Fri Feb 10, 2006 12:24 am
Location: Nashville, TN

Character collision detection and fixing flashing problem.

Post by Stoves »

Fixing the flashing asteroids

This won't be too tough since we know that the flashing problem occurs from clearing a character and then immediately re-drawing the same character in the same spot. If there's a way to prevent re-drawing the same character over itself, then we can remove the annoying flashing.

Here's the code where the flashing occurs: (I'm assuming you changed your code to clear the asteroid by using 2 spaces.)

Code: Select all

LOCATE meteory, meteorx 
PRINT "  " 
Hmm, same trouble-making line of code as before. Lol. Well, the problem is that the asteroid only moves one space at a time, but we clear 2 spaces. If you change "LOCATE meteory, meteorx" to "LOCATE meteory, meteorx + 2", that should solve the flashing problem, because it will only clear a space once the asteroid has moved on. Also at this point, you can change the PRINT statement to clear only one space instead of two if you want to, since the 2nd space doesn't really clear anything anymore. So the new code would look something like:

Code: Select all

LOCATE meteory, meteorx + 2
PRINT SPACE$(1)
OR

Code: Select all

LOCATE meteory, meteorx + 2
PRINT " "
You'll notice now we have another problem. Once the meteor reaches the end of the screen, it doesn't disappear completely, so we'll need to do something about clearing the asteroid once it hits the edge of the screen. I'll leave that for you to work out for now. I'm still glad to help, but I don't want to take away all the fun.

Character Collision Detection

There's lots of ways to approach this, but I think there's a nice easy way that won't involve a major coding change. Here's the key code you use to check for a collision:

Code: Select all

IF SCREEN(checky, checkx) <> 48 THEN 
  y = checky 
  x = checkx 
END IF
If I understand what you intend to do in your program, you just want to allow the ship to travel into a blank part of the screen. So, if you simply check that the cursor position your spaceship is trying to move into is either a space (ASC(" ") = 32) or nothing (ASC("") = 0), then you can allow the ship to move. That way anything other than a blank space will prevent the ship from moving there, and you can make your asteroids any characters you want. So try replacing the code above with something like:

Code: Select all

IF SCREEN(checky, checkx) = 32 OR SCREEN(checky, checkx) = 0 THEN
  y = checky 
  x = checkx 
END IF
You can probably leave out the check for nothing: "SCREEN(checky, checkx) = 0". Anyway, I think that should work pretty well. All that's left now really is to prevent the ship from flashing and start detecting when the asteroid hits the ship. Have fun!
Post Reply