News:

PlayBASIC2DLL V0.99 Revision I Commercial Edition released! - Convert PlayBASIC programs to super fast Machine Code. 

Main Menu

quick question

Started by blackmeadow, November 22, 2007, 01:59:13 PM

Previous topic - Next topic

blackmeadow

so i'm just starting to learn playbasic so i've been working on a little programming example for myself for player input:

PlayBASIC Code: [Select]
x# = 400
y# = 300
setfps 60

do

if x# >= 600 then x# = 600
if x# <= 200 then x# = 200
if y# >= 500 then y# = 500
if y# <= 100 then y# = 100
if leftkey() then x# = x# - 1
if rightkey() then x# = x# + 1
if upkey() then y# = y# - 1
if downkey() then y# = y# + 1

cls 0

dot x#, y#
sync

loop



so, whenever i run it i wanted the dot to be bound to a 200x200 square but see, whenever you reach the limit it jumps to 1 over before sliding back to the position it is supposed to be in.  is there a better way to execute what i am trying?

tide

Try putting your navigation commands before your conditionals that set the positions.

Big C.

Hi blackmeadow,

i think its a logical thing...

You have to know that your program is starting at the first line of code and would end at your last line if there wouldn't be the loop which is
repeating the lines between the command do and the command loop.

So if you read your code then its happend the following - starts at the beginning of the loop:

- the program checks first if the variables have reached the bound. If so the variables will set to absolute value

    if x# >= 600 then x# = 600
    if x# <= 200 then x# = 200
    if y# >= 500 then y# = 500
    if y# <= 100 then y# = 100


- then the program check the Key-Input. if pressed the varibales will increase/decrease around +/-1

if leftkey() then x# = x# - 1
    if rightkey() then x# = x# + 1
    if upkey() then y# = y# - 1
    if downkey() then y# = y# + 1


Summary: If X has the value 600, the event affects "X is supposed to be in 600". The arrow key is pressed now to the right X gets the value 601. After that the dot is set at position 601. In the next loop cycle the if query fixed, the value of X is greater than 600 and drafts X again back to poaition 600. Thus have at the edges a "hopping".

So if I understand your question right one solution for you coud be...  swap the logic


x# = 400
y# = 300
setfps 60

do

    if leftkey() then x# = x# - 1
    if rightkey() then x# = x# + 1
    if upkey() then y# = y# - 1
    if downkey() then y# = y# + 1

    if x# >= 600 then x# = 600
    if x# <= 200 then x# = 200
    if y# >= 500 then y# = 500
    if y# <= 100 then y# = 100

    cls 0

    dot x#, y#           
    sync

loop


cheers Big C.  :)





blackmeadow

o  :P i get it, thanks big c

Big C.

no problem  :D

but here is another solution maybe optimized...


x# = 400
y# = 300
SetFPS 60

OpenScreen 800,600,32,1

Do

Cls 0

Print "X-Value: " + Str$(x#)
Print "Y-Value: " + Str$(y#)

Ink RGB(200,0,220)

Box 200,100,600,500,0

Ink RGB(255,255,255)

   If LeftKey() And x# > 200 Then x# = x# - 1
   If RightKey() And x# < 600 Then x# = x# + 1
   If UpKey() And y# > 100 Then y# = y# - 1
   If DownKey() And y# < 500 Then y# = y# + 1

   Dot x#, y#           

   Sync

Loop

End


Big C.

blackmeadow

i think i like that better  :)