Qbasicnews.com

Full Version: Help.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey. Just for practice, I spent this afternoon working on a simple(yet hard) game. You try to collect as many hearts as you can, but you are being chased. If the enemy(the guy who is chasing you) is right next to you, you can press "b" to make him go to the edge of the screen. Here is the code for the "Bomb" section.

Code:
Bomb1:
LOCATE enemy.y, enemy.x
PRINT CHR$(0)
        IF enemy.y = player.y - 1 AND enemy.x = player.x THEN : enemy.y = 1
        IF enemy.y = player.y - 1 AND enemy.x = player.x - 1 THEN : enemy.x = 1
        IF enemy.y = player.y - 1 AND enemy.x = player.x + 1 THEN : enemy.y = 1
        IF enemy.y = player.y + 1 AND enemy.x = player.x THEN : enemy.x = 1
        IF enemy.y = player.y + 1 AND enemy.x = player.x + 1 THEN : enemy.y = 1
        IF enemy.y = player.y + 1 AND enemy.x = player.x - 1 THEN : enemy.x = 1
        IF enemy.x = player.x - 1 AND enemy.y = player.y THEN : enemy.y = 1
        IF enemy.x = player.x - 1 AND enemy.y = player.y - 1 THEN : enemy.x = 1
        IF enemy.x = player.x - 1 AND enemy.y = player.y + 1 THEN : enemy.y = 1
        IF enemy.x = player.x + 1 AND enemy.y = player.y + 1 THEN : enemy.x = 1
        IF enemy.x = player.x + 1 AND enemy.y = player.y - 1 THEN : enemy.y = 1
        IF enemy.x = player.x + 1 AND enemy.y = player.y THEN : enemy.x = 1

This is very long, but it works. My question is this. Is there an easier or shorter way to do this? Thanks.
Dunno if it works (haven't testes) so I won't take any responsability :wink: But here's my go at it [syntax="QBASIC"] if Enemy.X=(player.x - 1) or Enemy.X=(Player.X+1) then Xposit=1 'Check if Xpos maches
if Enemy.Y=(player.Y - 1) or Enemy.Y=(Player.Y+1) then Yposit=1 'Check if Ypos maches
if Xposit and yposit then 'if X and y bothe maches.
Xposit=0: Yposit=0
enemy.X = 1: enemy.Y = 1
elseif Xposit and (enemy.y = player.y) or Yposit and (enemy.X = player.X) then 'if only one of them
Xposit=0: Yposit=0
enemy.X = 1: enemy.Y = 1
end if[/syntax]It's not much sorter (three lines only), but I think it should be faster. Less comparison checks. And code is easier to understand. Ok hope it's some help.
I have been looking at this code for a few minutes, and I'm not sure exactly what you're trying to do with it. Several of the lines are redundant:

Code:
IF enemy.y = player.y - 1 AND enemy.x = player.x - 1 THEN : enemy.x = 1
        IF enemy.x = player.x - 1 AND enemy.y = player.y - 1 THEN : enemy.x = 1

Those two lines do the same thing.

If you want to check whether the enemy is next to you, try this:

Code:
IF ABS(enemy.x - player.x) <= 1 AND ABS(enemy.y - player.y) <= 1 THEN PRINT "The enemy is next to you!"

Note: This will also return true if the enemy is ON you.