WASD movement

Blitz3D Forums/Blitz3D Programming/WASD movement

Tried Sin & Cos?

Hmm, dunno why I used 2D, but the principle is the same :)

Graphics 800,600,32,2

px#=400
py#=300
mv#=4.0
img=CreateImage(64,64)
dir#=0

HandleImage img,32,32
SetBuffer ImageBuffer(img)
Oval 0,0,64,64,True
Color 255,0,0
Line 32,0,35,12
Line 32,0,29,12
Line 32,12,35,12
Color 255,255,255
SetBuffer BackBuffer()

While Not KeyDown(1)
	dir = -(400.0 - MouseX()) * .5
	If KeyDown(17) And KeyDown(30) ;up left
		px=px + Sin(315+dir)*mv
		py=py - Cos(315+dir)*mv
	Else If KeyDown(17) And KeyDown(32) ;up right
		px=px + Sin(45+dir)*mv
		py=py - Cos(45+dir)*mv
	Else If KeyDown(31) And KeyDown(30) ;down left
		px=px + Sin(225+dir)*mv
		py=py - Cos(225+dir)*mv
	Else If KeyDown(31) And KeyDown(32) ;down right
		px=px + Sin(135+dir)*mv
		py=py - Cos(135+dir)*mv
	Else If KeyDown(30) ;left
		px=px + Sin(270+dir)*mv
		py=py - Cos(270+dir)*mv
	Else If KeyDown(32) ;right
		px=px + Sin(90+dir)*mv
		py=py - Cos(90+dir)*mv
	Else If KeyDown(17) ;up
		px=px + Sin(dir)*mv
		py=py - Cos(dir)*mv
	Else If KeyDown(31) ;down
		px=px + Sin(180+dir)*mv
		py=py - Cos(180+dir)*mv
	End If

	SetBuffer ImageBuffer(img)
	Oval 0,0,64,64,True
	Color 255,0,0
	Line 32,32,32+Sin(dir)*30,32-Cos(dir)*30
	
	Color 255,255,255
	SetBuffer BackBuffer()

	Cls
	DrawImage img,px,py
	Flip
Wend