Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help with some stuff
#1
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...
Reply
#2
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.
Reply
#3
To move an ASCII character, you also need to check out LOCATE
/post]
Reply
#4
ok thanks
Reply
#5
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.
Reply
#6
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. Big Grin
I just added this as an option. :bounce:
I hope I didn't just confuse you.
adsherm
Reply
#7
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.
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)
Reply
#8
Quote: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!
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply
#9
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.
f only life let you press CTRL-Z.
--------------------------------------
Freebasic is like QB, except it doesn't suck.
Reply
#10
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
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)