Qbasicnews.com

Full Version: mouse input/movement by numpad PROBLEMS
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4 5
anyone ever played the helicopter game, u hold mouse button to go up and let go to let gravity pull u dwn, im tryin to mimic this effect using just a simple circle, ive tried quite a few things, but nothing seems to work properly, can anyone help me, and if mouse cant be used then space bar, or arrow keys (up = move up and dwn = move down)
Try: http://faq.qbasicnews.com/?blast=MouseCoordinatesInSvga

Remember, you must separate graphics functions from input functions, whether you use keyboard input (I$ = INKEY$, then do IF statements for I$..., for instance CHR$(13) is "enter".. look at the ASCII chart..) or mouse input..

The idea is you do a big process loop and then you decide the frequency with which you need to go to other areas of the game. IE:

DO
i% = i% + 1
if I% MOD 5 THEN GOSUB yoursub1.... 'every 5 loops go here..
If exitloop% = 1 THEN EXIT DO
LOOP
so CHR$(13) is also mouse input (button being pressed), im working on the actual loopig part a bit differently, all it does is creates random terrain, but thats not wat i want, i want the basic hold mouse button makes object go up and letting go makes it go dwn at same rate, just post some code if u can using a circle as object
No, INKEY$ only deals with keyboard input. I think that SVGA example works with all modes, but I'm not sure.. you can find the variables for the mouse there I think.

Anyway, if you're looking for same rates of movement, you pretty much need counters like I described, modified to use seconds or milliseconds. (use TIMER to get the time; watch out for midnight or mid-day changes)...

You'd set it up approximately like so:

Code:
'set up basic variables and screen, etc..
x1% = ....
x2% = .....
lastTime1% = TIMER
lastTime2% = TIMER

DO
curTime% = TIMER 'Remember, the idea is to only get input from any one object (at most) once per loop.

time1% = curTime% - lastTime1%
time2% = curTime% - lastTime2%

if time1% = X1% then lastTime1% = curTime%: gosub circlesub 'redraws the circle, screen.
if time2% = x2% then lastTime2% = curTime%: gosub keyinputsub 'polls the input, probably key input
if exit1% = 1 then exit do
LOOP
As for your code below, it is a good start but you are misunderstanding how to use WHILE and WEND. WHILE and WEND are loops.... so basically you are waiting for the user to press the first key, then second, and so on. What you want is to let him have an option to do it. I'll modify your code after I put the old one. And, try to remember to minimize duplicate code and duplicate keyboard polling is certainly a no-no.



OLD CODE:
Code:
SCREEN 7
'Try screen 7, so we can use PCOPY... not sure if it works in screen 13?

CLS
' Draw a filled circle for our sprite
CIRCLE (4, 3), 4, 4
PAINT (4, 3), 12, 4

  ' Set aside enough space to hold the sprite
  DIM Ball%(37)

  ' Clear the screen
  CLS

  ' Get the sprite into the Ball% array
  GET (0, 0)-(8, 7), Ball%

  ' Display the sprite elsewhere on the screen
  PUT (160, 100), Ball%

  ' Set default x and y coordiantes
  x = 160
  y = 100

  ' Allow user input
  WHILE INKEY$ = CHR$(71)
  CLS
  x = x - 1
  y = y + 1
  PUT (x, y), Ball%
  SLEEP .5
  WEND

  WHILE INKEY$ = CHR$(72)
  CLS
  y = y + 1
  PUT (x, y), Ball%
  SLEEP .5
  WEND

  WHILE INKEY$ = CHR$(73)
  CLS
  x = x + 1
  y = y + 1
  PUT (x, y), Ball%
  SLEEP .5
  WEND

  WHILE INKEY$ = CHR$(75)
  CLS
  x = x - 1
  PUT (x, y), Ball%
  SLEEP .5
  WEND

  WHILE INKEY$ = CHR$(76)
  CLS
  x = 160
  y = 100
  PUT (x, y), Ball%
  SLEEP .5
  WEND

  WHILE INKEY$ = CHR$(77)
  CLS
  x = x + 1
  PUT (x, y), Ball%
  SLEEP .5
  WEND

  WHILE INKEY$ = CHR$(79)
  CLS
  x = x - 1
  y = y - 1
  PUT (x, y), Ball%
  SLEEP .5
  WEND

  WHILE INKEY$ = CHR$(80)
  CLS
  y = y - 1
  PUT (x, y), Ball%
  SLEEP .5
  WEND

  WHILE INKEY$ = CHR$(82)
  CLS
  x = x + 1
  y = y - 1
  PUT (x, y), Ball%
  SLEEP .5
  WEND

  ' No button being pressed
  WHILE INKEY$ = ""
  PUT (x, y), Ball%
  WEND

  ' Allow quiting
  IF INKEY$ = CHR$(27) THEN END





NEW CODE:
Code:
DEFINT A-Z

SCREEN 13
CLS
'Draw a filled circle for our sprite
CIRCLE (4, 3), 4, 4
PAINT (4, 3), 12, 4

'Set aside enough space to hold the sprite
DIM Ball%(37)
DIM back1%(37)

'Get the sprite into the Ball% array .. before clearing the screen, doofus!
GET (0, 0)-(8, 7), Ball%

CLS
GET (0, 0)-(8, 7), back1%

' Set default x and y coordinates 'coordinates ....
x = 160
y = 100

DO

gosub getInput
if nothinghappened% = 0 thEN gosub drawCircle

IF exitloop1% = 1 THEN EXIT DO

LOOP


CLS
PRINT "Done."
SLEEP
SYSTEM

getInput:
I$ = UCASE$(INKEY$) 'your characters are all uppercase.
IF I$ = "" THEN nothinghappened% = 1: RETURN   ' No button being pressed? DO NOTHING! (so do nothing!)
oldx = x
oldy = y
IF I$ = CHR$(71) THEN  x = x - 1 :  y = y + 1
IF I$ = CHR$(72) THEN  y = y + 1
IF I$ = CHR$(73) THEN  x = x + 1 :  y = y + 1
IF I$ = CHR$(75) THEN  x = x - 1
IF I$ = CHR$(76) THEN  x = 160 :  y = 100
IF I$ = CHR$(77) THEN  x = x + 1
IF I$ = CHR$(79) THEN  x = x - 1 :  y = y - 1
IF I$ = CHR$(80) THEN  y = y - 1
IF I$ = CHR$(82) THEN  x = x + 1 :  y = y - 1
nothingHappened% = 0
'Allow quitting .... "quitting" not "quiting"...
IF I$ = CHR$(27) THEN exitloop1% = 1: system
RETURN

drawCircle:

PUT (oldx, oldy), back1%
GET (x+4, y+3)-(x+8, y+7), back1%
PUT (x, y), Ball%
'What you want to do here is copy the background image (before you start),
'then paste your sprite. Then copy the new background image, etc...
RETURN

This code has bugs but i gtg.

LET'S REFRAIN FROM SUGGESTING ALTERNATE PROGRAMMING STYLES LIKE SELECT CASE AND TABBING TO OUR FRIEND HERE; HE HAS ENOUGH TO THINK ABOUT.
Hahaha Aga, back to your style Big Grin
ok, well im using qb4.5 too and it doenst work, the keyboard scna codes different, so when i chnaged them to the new ones which use hex, it sais "Expected: )" whats going wrong, since im using qb4.5 to compile, i hav to get it working with qb4.5, how come it takes codes with number but not with letters??? :???:
Those aren't the keyboard scan codes, they're the ASCII codes. Read the HELP info on INKEY$!
i did, and the values in 4.5 are hex for the numpad and it doesnt run with the hex values, and normal values from 4.0 dont work, they are the codes for letters
Can you post your code? There's are no hexadecimal numbers involved I think...
Pages: 1 2 3 4 5