Position the tank in X , Y?

BlitzMax Forums/BlitzMax Beginners Area/Position the tank in X , Y?

Hiya all

I have try to move the red tank on the hill but it doesnt matter if I put 500,Y as it still on top left hand corner....it there a command to tell to move tank on where the position I want them to be?


[CODE]
Const ESCAPE=27
Graphics 640,480,0
Y=400

Cls
SetColor 255,0,0
DrawRect 0,0,20,10 ' Tank body

DrawRect 0,0,10,10 ' Tank Head

Local Tank_1=CreateImage(20,20,1,DYNAMICIMAGE|MASKEDIMAGE)

GrabImage Tank_1,100,50



While Not KeyDown(ESCAPE)

Hill()
DrawImage Tank_1,0,y

Flip

Wend
End

Function Hill()
Local sinetable[359]

For angle = 0 To 359
sinetable(angle) = 50 * Sin(angle)
Next
angle = 0

For x = 0 To 639
SetColor 0,255,0
DrawLine x, sinetable(angle) + 240,x,639
angle = angle + 1
If angle = 360 Then angle = 0
Next
End Function
[/CODE]
cheers

Hi!

"GrabImage Tank_1,100,50" isn't grabbing the tank (whose origin is 0,0). For that, use "GrabImage Tank_1,0,0". Instead, it's grabbing a black area with origin 100,50.

Because it's grabbing a black area and uses the MASKEDIMAGE flag, it's drawing on top of the hill but is invisible. The red rectangle that you see at 0,0 is the original tank body since there's no Cls after it's drawn. Adding one (at the top of the While loop seems the best place) fixes that.

Also, the drawing color is being left to the hill's green every time the Hill() function is called, which tints the tank green. Adding a "SetColor 255,255,255" before the DrawImage fixes that.

And there are a couple of other possible issues:

The Tank Head doesn't show up since it's being drawn in the same color as the body, in a subset of the same rectangle. Setting a different color (like a lighter red) before drawing it makes it stand out.

Lastly, "Local sinetable[359]" needs to be "Local sinetable[360]" since that's the total number of degrees in 0 to 359.

With those changes:
Const ESCAPE=27
Graphics 640,480,0
Y=400

Cls
SetColor 255,0,0
DrawRect 0,0,20,10 ' Tank body
SetColor 255,127,127
DrawRect 0,0,10,10 ' Tank Head

Local Tank_1=CreateImage(20,20,1,DYNAMICIMAGE|MASKEDIMAGE)

GrabImage Tank_1,0,0



While Not KeyDown(ESCAPE)
	Cls
	Hill()
	SetColor 255,255,255
	DrawImage Tank_1,0,y
	
	Flip

Wend
End

Function Hill()
	Local sinetable[360]
	
	For angle = 0 To 359
	sinetable(angle) = 50 * Sin(angle)
	Next
	angle = 0
	
	For x = 0 To 639
	SetColor 0,255,0
	DrawLine x, sinetable(angle) + 240,x,639
	angle = angle + 1
	If angle = 360 Then angle = 0
	Next
End Function
(your use of [CODE] and [/CODE] is fine except that they have to be lower-case)

It's a lot to take in all at once, but don't give up. Your sig is right: "keep learning and your reward will come as better programmer." :)

As a little help, I have added a bit mor functionality to your code.
Const ESCAPE=27
Graphics 640,480,0
Y=400
Global sinetable[360]
Global Height:Int[640]
Global XT:Int = 200

Cls
SetColor 255,0,0
DrawRect 0,0,20,10 ' Tank body
SetColor 0,100,100

DrawRect 0,0,10,10 ' Tank Head

Local Tank_1=CreateImage(20,20,1,DYNAMICIMAGE|MASKEDIMAGE)

GrabImage Tank_1,0,0



While Not KeyDown(ESCAPE)
Cls
SetColor 255,255,255
SetRotation GetAngle(xt,xt+20)
DrawImage Tank_1,xt,Height[xt] - 10
SetRotation 0
Hill()

If KeyDown(Key_Left) Then 
	If Xt > 0 Then Xt:-1
	Print Xt +" : "+ Height[xt]
	Print "Angle: " + GetAngle(xt,xt+20)

EndIf

If KeyDown(Key_Right)
 If Xt < 619 Then Xt:+1
	Print Xt + " : " + Height[xt]
	Print "Angle: " + GetAngle(xt,xt+20)
EndIf



Flip

Wend
End

Function Hill()

For angle = 0 To 359
sinetable(angle) = 50 * Sin(angle)
Next
angle = 0

For x = 0 To 639
SetColor 0,255,0
DrawLine x, sinetable(angle) + 240,x,639
Height[x] = sinetable(angle) + 240
angle = angle + 1
If angle = 360 Then angle = 0
Next
End Function

Function GetAngle:Int(x1,x2)
	Return Int(ATan2((Height[x2]-Height[x1]),(x2-x1)))
End Function


Now you can move the tank and the tank will go smooth on the hill with the correct angle. For getting the right Position of the tank, you have to store the y values in an extra Array. That makes life much easier. One thing I have also added was the CLS in the main loop.