Position the tank in X , Y?

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

Hotshot2005(Posted 2005) [#1]
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


WendellM(Posted 2005) [#2]
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." :)


klepto2(Posted 2005) [#3]
As a little help, I have added a bit mor functionality to your code.


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.