360 degree movement


Tweet blitz3d code-archives algorithms tutorials
(Posted 3 months ago) RonTek

360 degree movement with bullet firing by andy_mc

Image

;360 degree movement
;Blitz coding
;http://www.youtube.com/amcadam26

;Assets
;https://i.imgur.com/5TYe7vt.png
;https://i.imgur.com/t6dkAyo.png

;Set graphics mode
Graphics 800,600,32,2

;create timer
mytimer = CreateTimer(30)
AutoMidHandle True
;Load tank image
img_tank = LoadAnimImage("gfx\tankbase.png",38,38,0,2)
;Load target pointer image
img_target = LoadImage("gfx\target.bmp")
;set tank speed
speed = 4
;set tank start position
tankX# = 200
tankY# = 200
;Hide mouse pointer
HidePointer

;Define shell type
Type shell
    Field mx#
    Field my#
    Field x#
    Field y#
    Field speed#
    Field life
End Type

;Set graphics drawing buffer to back buffer
SetBuffer BackBuffer()

;repeat main loop until Esc key pressed
While not keyhit(1)

    ; Gamelogic ---------------------------------------
    dir = 0
    ;move tank when keys pressed
    If KeyDown(17) Then 
        tankY = tankY - speed
        tankf = 0
        dir = 1
    ElseIf KeyDown(31) Then 
        tankY = tankY + speed
        tankf = 0
        dir = 2
    ElseIf KeyDown(30) Then 
        tankX = tankX - speed
        tankf = 1
        dir = 3
    ElseIf KeyDown(32) Then 
        tankX = tankX + speed
        tankf = 1
        dir = 4
    EndIf   

    ;Calculate angle from tank to mouse pointer     
    ;angle# = ATan((MouseY()-tankY)/(MouseX()-tankX))
    angle# = ATan2(MouseY()-tankY, MouseX()-tankX)

    ;Fire shell when mouse clicked
    If MouseHit(1) Then
        ;create new shell
        sh.shell = New shell
        ;set shells mx value
        sh\mx = Cos(angle)
        ;set shells my value
        sh\my = Sin(angle)
        ;set shells speed
        sh\speed = 10
        ;set shells starting x coord
        sh\x = tankX+sh\mx*10
        ;set shells starting y coord
        sh\y = tankY+sh\my*10
        ;set shell life to 50
        sh\life = 50
    EndIf

    ;update shells
    For sh.shell = Each shell
        ;alter shells x position by mx
        sh\x = sh\x + sh\mx*sh\speed
        ;alter shells y position by my
        sh\y = sh\y + sh\my*sh\speed
        ;decrement shells life by one
        sh\life = sh\life - 1
        ;delete shell when it's ife is ended
        If sh\life = 0 Then Delete sh
    Next

    ; draw graphics --------------------------------------

    ;Clear screen   
    Cls
    ;Draw target mouse pointer
    DrawImage img_target,MouseX(),MouseY()
    ;Draw tank
    DrawImage img_tank,tankX,tankY,tankf
    ;Draw line for tank turret
    Line tankX,tankY,tankX+20*Cos(angle),tankY+20*Sin(angle)
    ;Draw shells
    For sh.shell = Each shell
        Rect sh\x-2,sh\y-2,5,5
    Next
    ;Wait timer
    WaitTimer(mytimer)
    ;Flip buffers
    Flip
;End main loop
Wend

Reply To Topic (minimum 10 characters)

Please log in to reply