Qbasicnews.com
February 25, 2020, 11:53:45 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1] 2 3 4
  Print  
Author Topic: Need help with some stuff  (Read 9698 times)
Weasel88
Member
*
Posts: 41


« on: February 10, 2004, 07:31:35 AM »

Hi...

Need help with some stuff...
Im working on an game, but i need some help.

1. I uses PRINT CHR$(1) for charakter but how do i make so i can
    move it with the arrow keys...

2. How do i make so the charakter cant move on W for an example...
Logged
KiZ
__/--\__
*****
Posts: 2879


WWW
« Reply #1 on: February 10, 2004, 08:51:18 AM »

Ok, well to make the character move, you must be able to move him along Coordinates.

Example:

When you press the up key, character moves 1 up the y axis.
so you would subract one from the Y axis value

when you press the right key, he moves 1 right along the X axis.
so you add 1 to the X axis value

So to dectect the keys being pressed, you need the INKEY$ command. Let a variable equal INKEY$, so you can read the value of the key being pressed:

Code:
key$ = INKEY$


Then you need to dectect the arrow keys. they are detected as

Up =  CHR$(0) + "H"
down = CHR$(0) + "P"
left = CHR$(0) + "K"
right = CHR$(0) + "P"


so you would detect if the up key was being pressed with something like this:

Code:
IF key$ = CHR$(0) + "H" then y = y - 1


When you learn more qbasic, you will learn about Keyboard handlers, using ports. they are much better than this, but i would think this is good enough to get you started.
Logged
red_Marvin
Na_th_an
*****
Posts: 1509



WWW
« Reply #2 on: February 10, 2004, 08:59:28 AM »

To move an ASCII character, you also need to check out LOCATE
Logged

/post]
Weasel88
Member
*
Posts: 41


« Reply #3 on: February 10, 2004, 09:20:56 AM »

ok thanks
Logged
Meg
Ancient QBer
****
Posts: 483


« Reply #4 on: February 10, 2004, 12:24:11 PM »

Here's the pseudo code I'd use:

Code:
set initial coordinates X and Y
start a loop
     draw character at coordinates X and Y ("Locate" command)
     get user input ("Inkey$" command)
     erase character at coordinates X and Y ("Locate" again)
     change coordinates X and Y depending on user input (just math)
end loop if user hit the exit key or if character died, or whatever


As to the second part of your question, you've got two options from what I can think of:

option 1.. use a 2-dimensional array.
----------------------------------------------

In this method, you have a 2D array-- Maze(1 to 80, 1 to 24) as STRING * 1, for example.  This array holds all the letters that are on the screen, so when the user enters a direction you can check to make sure it's a valid move before you change the coordinates of the character.  If they enter an invalid move, then don't update the coordinates.  That way the character will be redrawn exactly where it already was.

option 2.. use the SCREEN command.
-----------------------------------------------

You can retrieve the ASCII code of a character on the screen by using the SCREEN function.  Note that this is different than the SCREEN statement!  For example:

Code:
if CHR$(SCREEN(RowToCheck%, ColumnToCheck%)) = "W" then...


You could use this to see whether the square the character is trying to move to contains a W or not.  If it does, don't update the character's coordinates.

Hope this helps.

*peace*

Meg.
Logged
dadsherm
Member
*
Posts: 75


« Reply #5 on: February 10, 2004, 01:15:11 PM »

Another option to use is the KEY statement, such as:
KEY(11) ON   'KEY 11 = UP ARROW KEY
KEY(12) ON   'KEY 12 = LEFT ARROW KEY
KEY(13) ON   'KEY 13 = RIGHT ARROW KEY
KEY(14) ON   'KEY 14 = DOWN ARROW KEY
These KEY(n) ON statements are used to trap certain keys and send the program to a Sub routine,  via a pre-determined GOSUB statement.(i;e, ON KEY(11) GOSUB UPARROW)
If you wish to completely turn off these keys then just add a KEY(n) OFF statement.(Where (n) stands for the key of choice to turn off.)
And if you wish to put the keystroke "on hold" sort of speak, you can use the KEY(n) STOP. This enables the SUB routine to be activated as soon as the next KEY(N) ON is encountered.
Although this seems simple, I think what was already suggested earlier, the key$ = inkey$...etc. works better at understanding program flow. Cheesy
I just added this as an option.  :bounce:
I hope I didn't just confuse you.
Logged

adsherm
SCM
Wandering Guru
***
Posts: 311



« Reply #6 on: February 10, 2004, 10:14:32 PM »

I believe using ON KEY, like ON ERROR,  also has the disadvantage of slowing the program down by checking the key status after each statement.
Logged

hrist Jesus came into the world to save sinners, of whom I am first.(I Timothy 1:15)

For God so loved the world, that He gave His only begotten Son,
that whoever believes in Him should not perish, but have eternal life.(John 3:16)
na_th_an
*/-\*
*****
Posts: 8244



WWW
« Reply #7 on: February 10, 2004, 11:01:28 PM »

Quote from: "SCM"
I believe using ON KEY, like ON ERROR,  also has the disadvantage of slowing the program down by checking the key status after each statement.


You are correct. Avoid them!
Logged

SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Zack
*/-\*
*****
Posts: 3974



WWW
« Reply #8 on: February 10, 2004, 11:05:07 PM »

Actually, SCM, I don't think so.
Try this code:
Code:

TIMER ON
ON TIMER(1) GOSUB theLabel
t!=TIMER
FOR i=1 to 100000
NEXT
PRINT TIMER - t!
theLabel:

It takes (on my 2.4 GHZ P4 with Windows XP) approximately .5 seconds more to run through the FOR loop, than if we comment out the ON TIMER(1) line. And we are doing 100000 iterations. So each iteration, it's taking .5/100000, or 0.000005 seconds. I doubt that would make much of a difference, unless you're doing extremely high-res complex animation.
Logged

f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
na_th_an
*/-\*
*****
Posts: 8244



WWW
« Reply #9 on: February 10, 2004, 11:13:55 PM »

2.4 Ghz is really fast, you won't notice it. But it does add checking functions between each two translated lines of Basic (which make several ASM instructions each) so your EXE ends being larger and slower.

Your CPU has a jumps predictor, so you won't notice it Tongue
Logged

SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Zack
*/-\*
*****
Posts: 3974



WWW
« Reply #10 on: February 10, 2004, 11:16:44 PM »

Ah, well. 2.4 GHZ is fast? I guess so, compared...but I'm thinking of getting a Windows 95 333 MHZ or something. For development. Tongue
Logged

f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
KiZ
__/--\__
*****
Posts: 2879


WWW
« Reply #11 on: February 11, 2004, 09:57:06 AM »

Heh... now that my friend, I would recommend =D
Logged
Zack
*/-\*
*****
Posts: 3974



WWW
« Reply #12 on: February 11, 2004, 11:09:18 AM »

Yeah, I'm sick of XP's superfast, really easy VM. :roll:
Logged

f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
dadsherm
Member
*
Posts: 75


« Reply #13 on: February 11, 2004, 11:13:49 AM »

Slow you down because of comparisons?
Well, it is true that each key stroke is compared with the ON KEY statment, but then, each and every key stroke is compared to it's pre-determined feature. How else would the computer know to print the letter "a" when "a" is pressed as opposed to something else? Not to mention the "timer" statement like zack mentioned or the ON GOSUB statements.
 I agree that it can "slow down" the program to a certain degree, but for all practical purposes, it really doesn't makes a difference. Unless of course you're running a "graphics" program, as in a game.
I just mentioned it as an option was all.
I personally don't use it any more, but it's nice to know it's there, and it was fun to learn when I was just starting in progrmming. Cheesy
Logged

adsherm
Zack
*/-\*
*****
Posts: 3974



WWW
« Reply #14 on: February 11, 2004, 07:32:40 PM »

Actually, that got me thinking, dadsherm...
If one could disable keystroke-interrupt-handling, and other sensitivety interrupt features, would we get a signifigant speedup?
Logged

f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
Pages: [1] 2 3 4
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!