Sign in to follow this  

qbasic hit collison

This topic is 4815 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i am a new programer i know how to use qbasic very well and also visual basic is got intrsted in making games and im starting from low to high low as qbasic i ave a qbasic class in school i started to make a game sort of like snake but u just see how long u can sirvive with out hiting the wall or ur self also there is 2 player i can do most of the stuff my self but i cant seem to be able to do the hit colision i tries putting IF x or y <> 0 THEN END 0 is black the background is black and if it hits something not black u die. my code is this but i just started it its not big yet can someone tell me wat im doing wrong or how to do this CLS SCREEN 9 LINE (20, 1)-(630, 330), 3, B REM this is a box a boundry x = 50 y = 50 DO c = 14 press$ = INKEY$ PSET (x, y), c IF y > 1 THEN IF press$ = "w" THEN y = y - 1 IF y < 330 THEN IF press$ = "s" THEN y = y + 1 IF x > 20 THEN IF press$ = "a" THEN x = x - 1 IF x < 630 THEN IF press$ = "d" THEN x = x + 1 IF x <> 0 THEN END IF y <> 0 THEN END LOOP UNTIL press$ = CHR$(27) IVE ALSO TRIED CLS SCREEN 9 LINE (20, 1)-(630, 330), 3, B x = 50 y = 50 xl = 50 yl = 50 DO c = 14 press$ = INKEY$ PSET (x, y), c IF y > 1 THEN IF press$ = "w" THEN y = y - 1 IF y < 330 THEN IF press$ = "s" THEN y = y + 1 IF x > 20 THEN IF press$ = "a" THEN x = x - 1 IF x < 630 THEN IF press$ = "d" THEN x = x + 1 IF C <> 0 THEN END LOOP UNTIL press$ = CHR$(27)

Share this post


Link to post
Share on other sites
i also have 1 more question how do i get it so when i press w once or s once it will keep moving that direction till another directional key is pressed ???

Share this post


Link to post
Share on other sites
Quote:
Original post by pyrokid
i also have 1 more question how do i get it so when i press w once or s once it will keep moving that direction till another directional key is pressed ???

It's been forever since I've touched any PC Basic. Anyway, generally you'll keep an array of keys or flags, updating these at each keydown/up event. So when 'w' is pushed, set the array at 'w' (or variable w, or moveLeft or somesuch) to 1 and the opposing flag ('s') to 0. Then, in your main loop, check the status of these and apply physics/motion as necessary.

Share this post


Link to post
Share on other sites
QBASIC is absolutely obsolete, if you are interested in using an up-to-date basic that is similar to Q-Basic try out one of the following basics:

PureBasic
http://www.purebasic.com/

IBasic
http://www.pyxia.com/

BlitzBasic
http://www.blitzbasic.com/

If you want a free basic language to use do a search on google you will find lots.

Share this post


Link to post
Share on other sites
Quote:
Original post by pyrokid
i am a new programer i know how to use qbasic very well and also visual basic is got intrsted in making games and im starting from low to high low as qbasic i ave a qbasic class in school i started to make a game sort of like snake but u just see how long u can sirvive with out hiting the wall or ur self also there is 2 player i can do most of the stuff my self but i cant seem to be able to do the hit colision i tries putting
IF x or y <> 0 THEN END
0 is black the background is black and if it hits something not black u die.
my code is this but i just started it its not big yet

can someone tell me wat im doing wrong or how to do this


[code snipped]

Hi. I love snake games too - Tron is one of my favorites.

In the first program, you're using x and y as variables for where the head of your snake is, but you're also checking them for color. The reason your program ends right away is that x and y will *always* be <> 0 the first time into your main loop, because you just set them both to 50.

The second program is closer, but fails because C never changes (it's the color of your snake). You need a way to figure out the color of the pixel that the snake head is currently at, but do that *before* you actually draw the snake head, otherwise you'll overwrite your background before you know what color you hit. So, you'll need yet another variable.

The QBasic Help file is pretty much *all* you need for something like this, it's one of the best QBasic programmers' resources. Look up POINT(), and I think that will get you going. There's an on-line version of the help at http://www.qbasicnews.com/qboho/qck-9996.shtml.

Is this a project for class ?

Share this post


Link to post
Share on other sites
Aww, QBasic, the language I was programming in since I was six... I hope I can still remember after the years...

Try this, it's a simple line that you control with ASDW and that wraps around the screen, but it should give you the idea.


SCREEN 13
CLS

X = 160
Y = 100
DX = 0
DY = 0

DO

PLAY "MFT255P63" 'Wait a while

X = (X + DX + 320) MOD 320
Y = (Y + DY + 200) MOD 200

PSET (X, Y), 15

Key$ = INKEY$

SELECT CASE UCASE$(Key$)
CASE "A"
DX = -1: DY = 0
CASE "D"
DX = 1: DY = 0
CASE "W"
DX = 0: DY = -1
CASE "S"
DX = 0: DY = 1
CASE CHR$(27) 'ESC key
EXIT DO
END SELECT

LOOP
EDIT: Corrected to work with Copy&Paste

[Edited by - Xerxes on October 8, 2004 2:44:12 PM]

Share this post


Link to post
Share on other sites
I just want to point out something about libraries in QB. First, QB 4.5 is the way to go for game/graphics libraries, which there are alot of because QB has a very strong game community. QB 7.1 isn't compatible with most libs that I've tried. QB 1.0 & 1.1, which is what came free with Dos/windows can not access libraries, and furthermore, only interprets: it doesn't produce a stand-alone EXE.
Theres also a QB 2 & 3 I believe, but I haven't used either.

If you have access to 4.5 I recomend the following libs:

DirectQB - A really nice, easy mode 13(320x200 256 color) games lib with additional support for sound effects and input.

Cosmox - An extended version of DirectQB, Its supposed to be faster in many instances, although I've never tried it.

FutureLib - A high-res high-color game lib with sound and input support.

DS4QB - The best option for sound in QB. It uses a 'slave' windows application to provide extensive sound support. It can play and mix music and sound effects including wave, mod, mp3 and others.

You should be able to find any of them on nearly any QB-site with downloads.

Share this post


Link to post
Share on other sites
Quote:
Original post by PnP Bios
use a 3rd party support library. inkey$ is slow.


I'm sure it's fast enough for a snake program ... Sometimes we have to settle for "good enough" and get to the things that are truely important. Nobody cares if their snake program is using the most efficient method of polling the keyboard.

Share this post


Link to post
Share on other sites
Quote:
Original post by smr
Quote:
Original post by PnP Bios
use a 3rd party support library. inkey$ is slow.


I'm sure it's fast enough for a snake program ... Sometimes we have to settle for "good enough" and get to the things that are truely important. Nobody cares if their snake program is using the most efficient method of polling the keyboard.

Only the anal-retentive, like me - but it seems like it would be fast enough for the OP's game, at least in it's current state. It would not be hard to convert it later if need be.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
this is my code as of right now

CLS
SCREEN 9
INPUT " press s to start"; s$
IF s$ = "s" THEN
END IF
CLS
LINE (20, 1)-(630, 330), 3, B
x = 50
y = 50
xl = 220
yl = 220
DO
c = 14
press$ = INKEY$
PSET (x, y), c
IF y > 1 THEN IF press$ = "w" THEN y = y - 1
IF y < 330 THEN IF press$ = "s" THEN y = y + 1
IF x > 20 THEN IF press$ = "a" THEN x = x - 1
IF x < 630 THEN IF press$ = "d" THEN x = x + 1
PSET (xl, yl), 9
IF yl > 1 THEN IF press$ = "p" THEN yl = yl - 1
IF yl < 330 THEN IF press$ = ";" THEN yl = yl + 1
IF xl > 20 THEN IF press$ = "l" THEN xl = xl - 1
IF xl < 630 THEN IF press$ = "'" THEN xl = xl + 1

COLOR 9
IF POINT(x, y) = 3 THEN : LOCATE 12, 32: PRINT " player 1 looses"
IF POINT(x, y) = 3 THEN : LOCATE 13, 32: INPUT "enter q to quit"; q$
IF POINT(x, y) = 9 THEN : LOCATE 12, 32: PRINT "player 1 looses"
IF POINT(x, y) = 9 THEN : LOCATE 13, 32: INPUT "enter q to quit"; q$
IF q$ = "q" THEN END
IF POINT(xl, yl) = 3 THEN : LOCATE 12, 32: PRINT "player 2 looses"
IF POINT(xl, yl) = 3 THEN : LOCATE 13, 32: INPUT "enter q to quit"; s$
IF POINT(xl, yl) = 14 THEN : LOCATE 12, 32: PRINT "player 2 looses"
IF POINT(xl, yl) = 14 THEN : LOCATE 13, 32: INPUT "enter q to quit"; s$

IF s$ = "q" THEN END
LOOP UNTIL press$ = CHR$(27)






Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 4815 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this