move my ship with the mouse
BlitzMax Forums/BlitzMax Beginners Area/move my ship with the mouse
| ||
| Hi ! in my game i used a poor code to move my ship with the mouse !!! Could you help me to transform this to an interesting very cool super pro movement code ! ;-) Thanks ! see also : http://www.blitzbasic.com/Community/posts.php?topic=57242 Graphics 800,600 HideMouse() MoveMouse GraphicsWidth()/2, GraphicsHeight() / 2 OldMouseX = MouseX() OldMouseY = MouseY() PosX = MouseX() PosY = MouseY() While Not KeyDown (KEy_ESCAPE) DeltaX = MouseX() - OldMouseX DeltaY = MouseY() - OldMouseY If DeltaX <> 0 Then If (DeltaX > 0 And PosX < GraphicsWidth() - 20) Then PosX = PosX + DeltaX If (DeltaX < 0 And PosX > 20) Then PosX = PosX + DeltaX End If If DeltaY <> 0 Then If (DeltaY > 0 And PosY < GraphicsHeight() - 20) Then PosY = PosY + DeltaY If (DeltaY < 0 And PosY > 20) Then PosY = PosY + DeltaY End If OldMouseX = MouseX() OldMouseY = MouseY() Cls DrawOval PosX, PosY, 50,50 Flip Wend ShowMouse() |
| ||
| Have a look to the 'Rockout' code in the Bmax examples. The ship is mouse controlled, and has a smoothed movement. Perhaps it suits your needs. Sergio. |
| ||
| Thanks semar ! Could have some reactions about this method ? Updated code to test :
Graphics 800,600
Const cte_div = 5
HideMouse()
MoveMouse GraphicsWidth()/2, GraphicsHeight() / 2
PosX = MouseX()
PosY = MouseY()
While Not KeyDown (KEy_ESCAPE)
xdist# = MouseX() - Posx
ydist# = MouseY() - Posy
xs = xdist / cte_div
ys = ydist / cte_div
Posx = Posx + xs
Posy = Posy + ys
Cls
SetColor 255,255,255
DrawOval PosX-25, PosY-25, 50,50
SetColor 255,0,0
DrawOval MouseX()-3, MouseY()-3,6,6
Flip
Wend
ShowMouse()
|
| ||
| i've udapted my game. is this code is adaptable for a keyboard or joypad or should i search another method to have the same effect ? |
| ||
| answer seems to be yes : keyboard
Graphics 800,600
Const cte_div = 5
destx = GraphicsWidth()/2
desty = GraphicsHeight() / 2
PosX = destx
PosY = desty
While Not KeyDown (KEy_ESCAPE)
xdist# = destx - Posx
ydist# = desty - Posy
xs = xdist / cte_div
ys = ydist / cte_div
Posx = Posx + xs
Posy = Posy + ys
If KeyDown (KEy_UP) Then desty = desty - 3
If KeyDown (KEy_DOWN) Then desty = desty + 3
If KeyDown (KEy_LEFT) Then destx = destx - 3
If KeyDown (KEy_RIGHT) Then destx = destx + 3
Cls
SetColor 255,255,255
DrawOval PosX-25, PosY-25, 50,50
SetColor 255,0,0
DrawOval destx-3, desty-3,6,6
Flip
Wend
|