qbasic hit collison

Started by
34 comments, last by Xerxes 19 years, 6 months ago
Quote:Original post by pyrokid
it moves really slow and if i can get itto move with onke key press it would be more easier then i can set a delay to make it faster or slower and xerxes ur example is sorto of complicated could u redo it so it looks more like mine??
u dont have to
the main reason it needs to move wit one key press is cause in 2 player both players cant move at once only one key can be pressed at a time so if one layer holds the key down when the other player preses a key player 1 would stop


Then I would say get a library, or try reading your keyboard port with INP() (can't remember which port) first, if you want a home-made solution. This will get you over that 1-key at a time barrier.

Edit: Alternatively you could try trapping the keys with KEY(), although you can only assign a limited number of traps, which may or may not be enough for your game. Plus, I'm not quite sure of the speed issues, and it's not very expandable.
Advertisement
were would i put inp() and how would i set up and use a library like sdl wit qbasic?
Quote:Original post by pyrokid
were would i put inp() and how would i set up and use a library like sdl wit qbasic?

You'd put INP() where you put INKEY$, and if you want to use a library like SDL, you don't want to use INP() for keyboard polling - you can do that with SDL. I'd take a look at some of Ravyne's library links.
My code? Complicated? Heh... it's not.

DX and DY hold the direction of movement using coordinate changes (deltas), so if you do X = X + DX and Y = Y + DY you get the new position. For up, DX = 0 (X coordinate doesn't change) and DY = -1 (Y coordinate decreases). The direction changes only when the player presses a key. If not, it remains constant and the loop continues to go in the current direction.

The initial direction is (0, 0), that is, the player is not moving. If you want him to move from beginning, simply assing DX and DY some direction during initialization, (1, 0) (right) for example.

If you don't want screen wrapping, forget about X = (X + DX + 320) MOD 320 and Y = (Y + DY + 200) MOD 200 and simply do X = X + DX and Y = Y + DY.

The PLAY command is just my quick hack to do a small delay, you can use whatever you want, timer loop for example.
Society's never gonna make any progress until we all learn to pretend to like each other.
can someone tell what i have to add to my code and were so that it keeps going one directio till another directional buton is presses
OK, I copy & pasted your code to QBasic, redone it, and copy & pasted it back here. But be warned - there's nothing much left.

Please, copy & paste it to your QBasic, play it to see that it works just fine, and then try to study how. It is really very easy to understand and it solves collision as well as going in one direction until another key is pressed, both for both players.

SCREEN 13CLSX1 = 50Y1 = 50X2 = 50Y2 = 150DX1 = 1DY1 = 0DX2 = 1DY2 = 0PRINT "Press any key to begin..."SLEEPCLSLINE (0, 0)-(319, 199), 3, BDO Press$ = UCASE$(INKEY$) SELECT CASE Press$ CASE "A"  DX1 = -1  DY1 = 0 CASE "D"  DX1 = 1  DY1 = 0 CASE "W"  DX1 = 0  DY1 = -1 CASE "S"  DX1 = 0  DY1 = 1 CASE "L"  DX2 = -1  DY2 = 0 CASE "'"  DX2 = 1  DY2 = 0 CASE "P"  DX2 = 0  DY2 = -1 CASE ";"  DX2 = 0  DY2 = 1 END SELECT X1 = X1 + DX1 Y1 = Y1 + DY1 X2 = X2 + DX2 Y2 = Y2 + DY2 IF POINT(X1, Y1) <> 0 THEN  CLS  PRINT "Player 1 loses."  END ELSEIF POINT(X2, Y2) <> 0 THEN  CLS  PRINT "Player 2 loses."  END END IF PSET (X1, Y1), 13 PSET (X2, Y2), 9 PLAY "MFT255P32"LOOP UNTIL Press$ = CHR$(27)
There.
Society's never gonna make any progress until we all learn to pretend to like each other.
u cant paste stuff to qbasic if u didnt copy it from qbasic
i i dont feel like writing it all
Oh yes you can, even in two ways. Copy it to Notepad, save it as something.bas and open it with QBasic. Or, if you have Windows XP as I do, run QBasic in a dos-window, right-click the upper banner, and select edit->paste.
Society's never gonna make any progress until we all learn to pretend to like each other.
lol i didnt know that i typed it all out tho
ur lines start moving by themselfs and i have no control over them
Quote:Original post by pyrokid
... i i dont feel like writing it all

Go for you.

EDIT: sorry, caught me offgaurd.

If you don't want to use deltas - (or select case) - you can try the code below. You can, and probably should, TYPEDEF a snake type that contains the movement data, whether it be delta based or otherwise.

const LEFT% = 1const RIGHT% = 2const DOWN% = 3const UP% = 4int snake_direction% = RIGHT   '** snake is initially moving rightdo   (poll your keyboard however you  decide)   '** depending on how you get keyboard info,           **''** none or all of the following execute each "frame" **'   if (w key pressed) then snake_direction% = UP   if (a key pressed) then snake_direction% = LEFT   ... etc.'** exactly one of the following statements executes every "frame" **'   if snake_direction% = UP then y% = y% - 1   if snake_direction% = LEFT then x% = x% - 1   ... etc.'** check for collision here **''** draw snake head here **'loop until done

In this case, I would encourage SELECT/CASE, though, because it can be alot cleaner than IF/THEN statements - (unsure of the speed difference though, if any)

[Edited by - MdLeadG on October 8, 2004 4:47:15 PM]

This topic is closed to new replies.

Advertisement