Why doesn't it go left or right?

Blitz3D Forums/Blitz3D Beginners Area/Why doesn't it go left or right?

Apollonius(Posted 2004) [#1]
Could someone explain to me why this ain't working, it doesn't go to right nor left, I dont get why?

Const ScreenX = 800
Const ScreenY = 600

Graphics ScreenX,ScreenY,32

Global rec_x = 0
Global rec_y = 0

;Include "includes/types.bb"

While Not KeyHit(1)
Cls

Rect rec_x, rec_y, 100, 100, 0

; Press UP
If KeyDown(200) Then
rec_y = rec_y - 5
EndIf
; Press Down
If KeyDown(208) Then
rec_y = rec_y + 5
EndIf
; Press LEFT
If KeyDown(203) Then
rec_y = rec_x - 5
EndIf
; Press Right
If KeyDown(205) Then
rec_y = rec_x + 5
EndIf

Flip

Wend



EOF(Posted 2004) [#2]
If KeyDown(203) Then
 rec_y = rec_x - 5
EndIf
; Press Right
If KeyDown(205) Then
 rec_y = rec_x + 5
EndIf

should be:
If KeyDown(203) Then
 rec_x = rec_x - 5
EndIf
; Press Right
If KeyDown(205) Then
 rec_x = rec_x + 5
EndIf



EOF(Posted 2004) [#3]
You could also do:
Const ScreenX = 800 , ScreenY = 600

Graphics ScreenX,ScreenY,32

Global rec_x , rec_y

;Include "includes/types.bb"

While Not KeyHit(1)
 Cls
 rec_y=rec_y+(KeyDown(208)-KeyDown(200))*5
 rec_x=rec_x+(KeyDown(205)-KeyDown(203))*5
 Rect rec_x, rec_y, 100, 100, 0
 Flip
Wend

End



Apollonius(Posted 2004) [#4]
That looks complicated :o

rec_y=rec_y+(KeyDown(208)-KeyDown(200))*5
^^^

I dont get the math logic in this

rec_y PLUS (Down MINUS Up) Multi. by 5

Not sure I get why down minus up thing..

Could anyone try to explain to me the formula?


EOF(Posted 2004) [#5]
I'll try and explain whats going on in:

rec_y=rec_y+(KeyDown(208)-KeyDown(200))*5


When you use KeyDown() to test the status of a key Blitz will return a value of 1 if the key being pressed. Otherwise it returns a value of 0

Ok, lets make the above code a little clearer by replacing the 208 and 200 with CURSORDOWN and CURSORUP:
rec_y=rec_y+(KeyDown(CURSORDOWN)-KeyDown(CURSORUP))*5


Here is what happens when you hold the 'cursor down' key:
rec_y=rec_y+(KeyDown(CURSORDOWN)-KeyDown(CURSORUP))*5

becomes

rec_y=rec_y+(1-0)*5

This results in a value of 5


Here is what happens when you hold the 'cursor up' key:
rec_y=rec_y+(KeyDown(CURSORDOWN)-KeyDown(CURSORUP))*5

becomes

rec_y=rec_y+(0-1)*5

This results in a value of -5

So, holding cursor DOWN gives 5 and holding cursor UP gives -5.
Hope that helps.


Apollonius(Posted 2004) [#6]
Yes it helps but the other point I don't get is

1-0=1
0-1=1

rec_y+1*5... rec_y + 5 it should always return +5 i dont get it :o


EOF(Posted 2004) [#7]
The (0-1)*5 bit is:

(ZERO take away ONE which is MINUS ONE)
Multiply this by FIVE and you get MINUS FIVE

I don't know if you have heard of BODMAS but ..
Computers use BODMAS (Brackets Of Division, Multiplication, Addition, and Subtraction) when it comes to mathematical formulas.

Taking (0-1)*5 this is what happens:

Anything in brackets is worked out first so

(0-1)*5 becomes (-1)*5

Therefore, (-1)*5 equals -5


Apollonius(Posted 2004) [#8]
I think I get it now,

-3 -2 -1 0 1 2 3

So we start with 0 because its first in the 0-1
and if we are at the 0 and we remove 1 we get to -1, which with the *5 would equal -5


bit if we start with 1 and we remove 0 we get to 1, which with the *5 would be +5

Hehe thanks