Jump to content

GDI plus help


Recommended Posts

hello.

well, i've just recently started taking part in GDI world :)

i have this script, however i'm not entirely sure why it flickers all the time. i'd greatly appreciate some input.

#NoTrayIcon
#include <GDIPlus.au3>
#include <WinAPI.au3>
#include <GuiConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <Misc.au3>



_GDIPlus_Startup ()
Global $bBUFFERING = True
Global $nBUFFER_SPEED = 100
Global $nGUIW = 800
Global $nGUIH = 600
Global $nPLAYER_X
Global $nPLAYER_Y
Global $bJUMPING = False
Global $aPLAYER_KEYS[4] = ['26','27','28','25']
;~ 25 LEFT ARROW key
;~ 26 UP ARROW key
;~ 27 RIGHT ARROW key
;~ 28 DOWN ARROW key

Global $nGRID_BLOCK_SIZE = 20
Global $nGRID_MAX_X = ($nGUIW/$nGRID_BLOCK_SIZE)-1
Global $nGRID_MAX_Y = ($nGUIH/$nGRID_BLOCK_SIZE)-1
Global $aGRID[$nGRID_MAX_Y+1][$nGRID_MAX_X+1]

Global $hHWND = GUICreate ('',$nGUIW,$nGUIH)
GUISetState (@SW_SHOW,$hHWND)


Global $hGUI_GRAPHIC = _GDIPlus_GraphicsCreateFromHWND ($hHWND)


Global $hGRID_BMP_BUFF = _GDIPlus_BitmapCreateFromGraphics($nGUIW, $nGUIH, $hGUI_GRAPHIC)
Global $hGRID_GRAPHIC = _GDIPlus_ImageGetGraphicsContext($hGRID_BMP_BUFF)
Global $hGRID_BRUSH = _GDIPlus_BrushCreateSolid ('0x20' & '000000')

Global $hENVIRO_BMP_BUFF = _GDIPlus_BitmapCreateFromGraphics($nGUIW, $nGUIH, $hGUI_GRAPHIC)
Global $hENVIRO_GRAPHIC = _GDIPlus_ImageGetGraphicsContext($hENVIRO_BMP_BUFF)
Global $hENVIRO_BACKROUND_BRUSH = _GDIPlus_BrushCreateSolid ('0xFF' & '0000FF')
Global $hENVIRO_BRUSH = _GDIPlus_BrushCreateSolid ('0xFF' & '008000')

Global $hPLAYER_BMP_BUFF = _GDIPlus_BitmapCreateFromGraphics($nGUIW, $nGUIH, $hGUI_GRAPHIC)
Global $hPLAYER_GRAPHIC = _GDIPlus_ImageGetGraphicsContext($hPLAYER_BMP_BUFF)
Global $hPLAYER_BRUSH = _GDIPlus_BrushCreateSolid ('0xFF' & '00FFFF')

reset_grid ()
draw_grid ()
draw_map ()
draw_player ()

$bBUFFERING = False
AdlibRegister ('BUFFER',$nBUFFER_SPEED)
AdlibRegister ('PHYSICS',$nBUFFER_SPEED)
AdlibRegister ('CHECK_MOVEMENT',$nBUFFER_SPEED)
GUIRegisterMsg(0xF, 'MY_PAINT')
GUIRegisterMsg(0x85,'MY_PAINT')


While GUIGetMsg () <> -3
WEnd


_GDIPlus_GraphicsDispose($hGUI_GRAPHIC)

_GDIPlus_BrushDispose($hGRID_BRUSH)
_GDIPlus_BitmapDispose ($hGRID_BMP_BUFF)
_GDIPlus_GraphicsDispose($hGRID_GRAPHIC)
_GDIPlus_Shutdown()



Func move_player ($nDIRECTION)
    Local $nX, $nY
    $nX = $nPLAYER_X
    $nY = $nPLAYER_Y
    Switch $nDIRECTION
        Case 0
            If $bJUMPING = False Then
                $bJUMPING = True
                $nY -= 5
            Else
                Return
            EndIf
        Case 1
            $nX += 1
        Case 2
            $nY += 1
        Case 3
            $nX -= 1
    EndSwitch
    If $nX <> $nPLAYER_X Or $nY <> $nPLAYER_Y Then
        If $nX <= $nGRID_MAX_X And $nX >= 0 And $nY <= $nGRID_MAX_Y And $nY >= 0 Then
            If $aGRID[$nY][$nX] <> 'e' And $aGRID[$nY+1][$nX] <> 'e' Then
                $aGRID[$nPLAYER_Y][$nPLAYER_X] = ''
                $aGRID[$nPLAYER_Y+1][$nPLAYER_X] = ''
                _GDIPlus_GraphicsFillRect ($hPLAYER_GRAPHIC,$nPLAYER_X*$nGRID_BLOCK_SIZE,$nPLAYER_Y*$nGRID_BLOCK_SIZE,$nGRID_BLOCK_SIZE,$nGRID_BLOCK_SIZE*2,$hENVIRO_BACKROUND_BRUSH)

                $nPLAYER_X = $nX
                $nPLAYER_Y = $nY

                $aGRID[$nPLAYER_Y][$nPLAYER_X] = 'p'
                $aGRID[$nPLAYER_Y+1][$nPLAYER_X] = 'p'
                _GDIPlus_GraphicsFillRect ($hPLAYER_GRAPHIC,$nPLAYER_X*$nGRID_BLOCK_SIZE,$nPLAYER_Y*$nGRID_BLOCK_SIZE,$nGRID_BLOCK_SIZE,$nGRID_BLOCK_SIZE*2,$hPLAYER_BRUSH)
            EndIf
        EndIf
    EndIf
    Return
EndFunc




Func reset_grid ()
    Local $nX, $nY
    For $nX = 0 To $nGRID_MAX_X
        For $nY = 0 To $nGRID_MAX_Y
            $aGRID[$nY][$nX] = ''
            _GDIPlus_GraphicsFillRect ($hENVIRO_GRAPHIC,$nX*$nGRID_BLOCK_SIZE,$nY*$nGRID_BLOCK_SIZE,$nGRID_BLOCK_SIZE,$nGRID_BLOCK_SIZE,$hENVIRO_BACKROUND_BRUSH)
        Next
    Next
EndFunc








Func draw_grid ()
    Local $nX, $nY
    _GDIPlus_GraphicsClear ($hGRID_GRAPHIC,'0x20' & '0000FF')
    For $nX = 0 To $nGUIW Step $nGRID_BLOCK_SIZE
        _GDIPlus_GraphicsFillRect ($hGRID_GRAPHIC,$nX,0,1,$nGUIH,$hGRID_BRUSH)
    Next
    For $nY = 0 To $nGUIH Step $nGRID_BLOCK_SIZE
        _GDIPlus_GraphicsFillRect ($hGRID_GRAPHIC,0,$nY,$nGUIW,1,$hGRID_BRUSH)
    Next
EndFunc
Func draw_map ()
    Local $nX, $nY, $nH
    For $nX = 0 To $nGRID_MAX_X
        $nH = 4
        For $nY = $nGRID_MAX_Y-$nH To $nGRID_MAX_Y
            _GDIPlus_GraphicsFillRect ($hENVIRO_GRAPHIC,$nX*$nGRID_BLOCK_SIZE,$nY*$nGRID_BLOCK_SIZE,$nGRID_BLOCK_SIZE,$nGRID_BLOCK_SIZE,$hENVIRO_BRUSH)
            $aGRID[$nY][$nX] = 'e'
        Next
    Next
EndFunc
Func draw_player ()
    Local $nX,$nY
    $nX = Floor ($nGRID_MAX_X/$nGRID_BLOCK_SIZE) * $nGRID_BLOCK_SIZE
    $nY = 1
    _GDIPlus_GraphicsFillRect ($hPLAYER_GRAPHIC,$nX*$nGRID_BLOCK_SIZE,$nY*$nGRID_BLOCK_SIZE,$nGRID_BLOCK_SIZE,$nGRID_BLOCK_SIZE*2,$hPLAYER_BRUSH)
    $aGRID[$nY][$nX] = 'p'
    $aGRID[$nY+1][$nX] = 'p'
    $nPLAYER_X = $nX
    $nPLAYER_Y = $nY
EndFunc











Func PHYSICS ()
    If $bBUFFERING = False Then
        $bBUFFERING = True
        If $aGRID[$nPLAYER_Y+2][$nPLAYER_X] == '' Then
            _GDIPlus_GraphicsFillRect ($hPLAYER_GRAPHIC,$nPLAYER_X*$nGRID_BLOCK_SIZE,$nPLAYER_Y*$nGRID_BLOCK_SIZE,$nGRID_BLOCK_SIZE,$nGRID_BLOCK_SIZE*2,$hENVIRO_BACKROUND_BRUSH)
            move_player (2)
            _GDIPlus_GraphicsFillRect ($hPLAYER_GRAPHIC,$nPLAYER_X*$nGRID_BLOCK_SIZE,$nPLAYER_Y*$nGRID_BLOCK_SIZE,$nGRID_BLOCK_SIZE,$nGRID_BLOCK_SIZE*2,$hPLAYER_BRUSH)
        Else
            $bJUMPING = False
        EndIf
        $bBUFFERING = False
    EndIf
EndFunc

Func CHECK_MOVEMENT ()
    Local $nINDEX
    For $nINDEX = 0 to 3
        If _IsPressed ($aPLAYER_KEYS[$nINDEX]) Then
            move_player($nINDEX)
            Return
        EndIf
    Next
EndFunc

Func BUFFER ()
    If $bBUFFERING = False Then
        $bBUFFERING = True
        _GDIPlus_GraphicsDrawImage($hGUI_GRAPHIC, $hENVIRO_BMP_BUFF, 0, 0)
        _GDIPlus_GraphicsDrawImage($hGUI_GRAPHIC, $hPLAYER_BMP_BUFF, 0, 0)
        _GDIPlus_GraphicsDrawImage($hGUI_GRAPHIC, $hGRID_BMP_BUFF, 0, 0)
        $bBUFFERING = False
    EndIf
EndFunc

Func MY_PAINT($hWnd, $msg, $wParam, $lParam)
    BUFFER ()
    _WinAPI_RedrawWindow($hWnd,Default,Default, BitOR($RDW_INVALIDATE, $RDW_UPDATENOW, $RDW_FRAME)); , $RDW_ALLCHILDREN
    Return $GUI_RUNDEFMSG
EndFunc
Link to comment
Share on other sites

Hi,

It may be better to draw all your buffers (3 I can see) onto one buffer then update the main gui in one step instead of using 3 buffers updating the gui every cycle.

So instead of drawing $hENVIRO_BMP_BUFF, $hPLAYER_BMP_BUFF and $hGRID_BMP_BUFF on the GUI directly you may want to draw them all to another single buffer off screen then paint the one off screen buffer to the GUI.

eg:

#NoTrayIcon
#include <GDIPlus.au3>
#include <WinAPI.au3>
#include <GuiConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <Misc.au3>

_GDIPlus_Startup()
Global $bBUFFERING = True
Global $nBUFFER_SPEED = 100
Global $nGUIW = 800
Global $nGUIH = 600
Global $nPLAYER_X
Global $nPLAYER_Y
Global $bJUMPING = False
Global $aPLAYER_KEYS[4] = ['26', '27', '28', '25']
;~ 25 LEFT ARROW key
;~ 26 UP ARROW key
;~ 27 RIGHT ARROW key
;~ 28 DOWN ARROW key

Global $nGRID_BLOCK_SIZE = 20
Global $nGRID_MAX_X = ($nGUIW / $nGRID_BLOCK_SIZE) - 1
Global $nGRID_MAX_Y = ($nGUIH / $nGRID_BLOCK_SIZE) - 1
Global $aGRID[$nGRID_MAX_Y + 1][$nGRID_MAX_X + 1]

Global $hHWND = GUICreate('', $nGUIW, $nGUIH)
GUISetState(@SW_SHOW, $hHWND)

Global $hGUI_GRAPHIC = _GDIPlus_GraphicsCreateFromHWND($hHWND)

; Use this as a back buffer to draw to off screen, then update the screen with this 1 buffer.
Global $hMAIN_BMP_BUFF = _GDIPlus_BitmapCreateFromGraphics($nGUIW, $nGUIH, $hGUI_GRAPHIC)
Global $hMAIN_GRAPHIC = _GDIPlus_ImageGetGraphicsContext($hMAIN_BMP_BUFF)

Global $hGRID_BMP_BUFF = _GDIPlus_BitmapCreateFromGraphics($nGUIW, $nGUIH, $hGUI_GRAPHIC)
Global $hGRID_GRAPHIC = _GDIPlus_ImageGetGraphicsContext($hGRID_BMP_BUFF)
Global $hGRID_BRUSH = _GDIPlus_BrushCreateSolid('0x20' & '000000')

Global $hENVIRO_BMP_BUFF = _GDIPlus_BitmapCreateFromGraphics($nGUIW, $nGUIH, $hGUI_GRAPHIC)
Global $hENVIRO_GRAPHIC = _GDIPlus_ImageGetGraphicsContext($hENVIRO_BMP_BUFF)
Global $hENVIRO_BACKROUND_BRUSH = _GDIPlus_BrushCreateSolid('0xFF' & '0000FF')
Global $hENVIRO_BRUSH = _GDIPlus_BrushCreateSolid('0xFF' & '008000')

Global $hPLAYER_BMP_BUFF = _GDIPlus_BitmapCreateFromGraphics($nGUIW, $nGUIH, $hGUI_GRAPHIC)
Global $hPLAYER_GRAPHIC = _GDIPlus_ImageGetGraphicsContext($hPLAYER_BMP_BUFF)
Global $hPLAYER_BRUSH = _GDIPlus_BrushCreateSolid('0xFF' & '00FFFF')

reset_grid()
draw_grid()
draw_map()
draw_player()

$bBUFFERING = False
AdlibRegister('BUFFER', $nBUFFER_SPEED)
AdlibRegister('PHYSICS', $nBUFFER_SPEED)
AdlibRegister('CHECK_MOVEMENT', $nBUFFER_SPEED)
GUIRegisterMsg(0xF, 'MY_PAINT')
GUIRegisterMsg(0x85, 'MY_PAINT')

While GUIGetMsg() <> -3
WEnd

_GDIPlus_GraphicsDispose($hGUI_GRAPHIC)
_GDIPlus_GraphicsDispose($hMAIN_GRAPHIC)
_GDIPlus_GraphicsDispose($hGRID_GRAPHIC)
_GDIPlus_GraphicsDispose($hENVIRO_GRAPHIC)
_GDIPlus_GraphicsDispose($hPLAYER_GRAPHIC)

_GDIPlus_BitmapDispose($hMAIN_BMP_BUFF)
_GDIPlus_BitmapDispose($hGRID_BMP_BUFF)
_GDIPlus_BitmapDispose($hENVIRO_BMP_BUFF)
_GDIPlus_BitmapDispose($hPLAYER_BMP_BUFF)

_GDIPlus_BrushDispose($hGRID_BRUSH)
_GDIPlus_BrushDispose($hENVIRO_BACKROUND_BRUSH)
_GDIPlus_BrushDispose($hENVIRO_BRUSH)
_GDIPlus_BrushDispose($hPLAYER_BRUSH)

_GDIPlus_Shutdown()


Func move_player($nDIRECTION)
    Local $nX, $nY
    $nX = $nPLAYER_X
    $nY = $nPLAYER_Y
    Switch $nDIRECTION
        Case 0
            If $bJUMPING = False Then
                $bJUMPING = True
                $nY -= 5
            Else
                Return
            EndIf
        Case 1
            $nX += 1
        Case 2
            $nY += 1
        Case 3
            $nX -= 1
    EndSwitch
    If $nX <> $nPLAYER_X Or $nY <> $nPLAYER_Y Then
        If $nX <= $nGRID_MAX_X And $nX >= 0 And $nY <= $nGRID_MAX_Y And $nY >= 0 Then
            If $aGRID[$nY][$nX] <> 'e' And $aGRID[$nY + 1][$nX] <> 'e' Then
                $aGRID[$nPLAYER_Y][$nPLAYER_X] = ''
                $aGRID[$nPLAYER_Y + 1][$nPLAYER_X] = ''
                _GDIPlus_GraphicsFillRect($hPLAYER_GRAPHIC, $nPLAYER_X * $nGRID_BLOCK_SIZE, $nPLAYER_Y * $nGRID_BLOCK_SIZE, $nGRID_BLOCK_SIZE, $nGRID_BLOCK_SIZE * 2, $hENVIRO_BACKROUND_BRUSH)

                $nPLAYER_X = $nX
                $nPLAYER_Y = $nY

                $aGRID[$nPLAYER_Y][$nPLAYER_X] = 'p'
                $aGRID[$nPLAYER_Y + 1][$nPLAYER_X] = 'p'
                _GDIPlus_GraphicsFillRect($hPLAYER_GRAPHIC, $nPLAYER_X * $nGRID_BLOCK_SIZE, $nPLAYER_Y * $nGRID_BLOCK_SIZE, $nGRID_BLOCK_SIZE, $nGRID_BLOCK_SIZE * 2, $hPLAYER_BRUSH)
            EndIf
        EndIf
    EndIf
    Return
EndFunc   ;==>move_player

Func reset_grid()
    Local $nX, $nY
    For $nX = 0 To $nGRID_MAX_X
        For $nY = 0 To $nGRID_MAX_Y
            $aGRID[$nY][$nX] = ''
            _GDIPlus_GraphicsFillRect($hENVIRO_GRAPHIC, $nX * $nGRID_BLOCK_SIZE, $nY * $nGRID_BLOCK_SIZE, $nGRID_BLOCK_SIZE, $nGRID_BLOCK_SIZE, $hENVIRO_BACKROUND_BRUSH)
        Next
    Next
EndFunc   ;==>reset_grid

Func draw_grid()
    Local $nX, $nY
    _GDIPlus_GraphicsClear($hGRID_GRAPHIC, '0x20' & '0000FF')
    For $nX = 0 To $nGUIW Step $nGRID_BLOCK_SIZE
        _GDIPlus_GraphicsFillRect($hGRID_GRAPHIC, $nX, 0, 1, $nGUIH, $hGRID_BRUSH)
    Next
    For $nY = 0 To $nGUIH Step $nGRID_BLOCK_SIZE
        _GDIPlus_GraphicsFillRect($hGRID_GRAPHIC, 0, $nY, $nGUIW, 1, $hGRID_BRUSH)
    Next
EndFunc   ;==>draw_grid
Func draw_map()
    Local $nX, $nY, $nH
    For $nX = 0 To $nGRID_MAX_X
        $nH = 4
        For $nY = $nGRID_MAX_Y - $nH To $nGRID_MAX_Y
            _GDIPlus_GraphicsFillRect($hENVIRO_GRAPHIC, $nX * $nGRID_BLOCK_SIZE, $nY * $nGRID_BLOCK_SIZE, $nGRID_BLOCK_SIZE, $nGRID_BLOCK_SIZE, $hENVIRO_BRUSH)
            $aGRID[$nY][$nX] = 'e'
        Next
    Next
EndFunc   ;==>draw_map

Func draw_player()
    Local $nX, $nY
    $nX = Floor($nGRID_MAX_X / $nGRID_BLOCK_SIZE) * $nGRID_BLOCK_SIZE
    $nY = 1
    _GDIPlus_GraphicsFillRect($hPLAYER_GRAPHIC, $nX * $nGRID_BLOCK_SIZE, $nY * $nGRID_BLOCK_SIZE, $nGRID_BLOCK_SIZE, $nGRID_BLOCK_SIZE * 2, $hPLAYER_BRUSH)
    $aGRID[$nY][$nX] = 'p'
    $aGRID[$nY + 1][$nX] = 'p'
    $nPLAYER_X = $nX
    $nPLAYER_Y = $nY
EndFunc   ;==>draw_player


Func PHYSICS()
    If $bBUFFERING = False Then
        $bBUFFERING = True
        If $aGRID[$nPLAYER_Y + 2][$nPLAYER_X] == '' Then
            _GDIPlus_GraphicsFillRect($hPLAYER_GRAPHIC, $nPLAYER_X * $nGRID_BLOCK_SIZE, $nPLAYER_Y * $nGRID_BLOCK_SIZE, $nGRID_BLOCK_SIZE, $nGRID_BLOCK_SIZE * 2, $hENVIRO_BACKROUND_BRUSH)
            move_player(2)
            _GDIPlus_GraphicsFillRect($hPLAYER_GRAPHIC, $nPLAYER_X * $nGRID_BLOCK_SIZE, $nPLAYER_Y * $nGRID_BLOCK_SIZE, $nGRID_BLOCK_SIZE, $nGRID_BLOCK_SIZE * 2, $hPLAYER_BRUSH)
        Else
            $bJUMPING = False
        EndIf
        $bBUFFERING = False
    EndIf
EndFunc   ;==>PHYSICS

Func CHECK_MOVEMENT()
    Local $nINDEX
    For $nINDEX = 0 To 3
        If _IsPressed($aPLAYER_KEYS[$nINDEX]) Then
            move_player($nINDEX)
            Return
        EndIf
    Next
EndFunc   ;==>CHECK_MOVEMENT

Func BUFFER()
    If $bBUFFERING = False Then
        $bBUFFERING = True
        ;draw all your buffers onto one buffer off screen.
        _GDIPlus_GraphicsDrawImage($hMAIN_GRAPHIC, $hENVIRO_BMP_BUFF, 0, 0)
        _GDIPlus_GraphicsDrawImage($hMAIN_GRAPHIC, $hPLAYER_BMP_BUFF, 0, 0)
        _GDIPlus_GraphicsDrawImage($hMAIN_GRAPHIC, $hGRID_BMP_BUFF, 0, 0)

        ;update the gui with the main buffer.
        _GDIPlus_GraphicsDrawImage($hGUI_GRAPHIC, $hMAIN_BMP_BUFF, 0, 0)
        $bBUFFERING = False
    EndIf
EndFunc   ;==>BUFFER

Func MY_PAINT($hWnd, $msg, $wParam, $lParam)
    BUFFER()
    _WinAPI_RedrawWindow($hWnd, Default, Default, BitOR($RDW_INVALIDATE, $RDW_UPDATENOW, $RDW_FRAME)); , $RDW_ALLCHILDREN
    Return $GUI_RUNDEFMSG
EndFunc   ;==>MY_PAINT

Trivial info.

If your after a bit more speed for painting a buffer to the gui then you may want to look into _WinAPI_BitBlt().

Also I notice in GDIPlus that _GDIPlus_GraphicsFillRect() is slower then _WinAPI_FillRect().

Here's a link to a small game I wrote last year using GDIPlus and GDI combined.

The codes a bit of a mess but it works ok'ish

Good luck and I look forward to seeing your game evolve.

Cheers

Edited by smashly
Link to comment
Share on other sites

WOW! thanks for that! :) works much better! May i ask WHY this is better? why is it faster and no flicker? O.o just for curiosities sake.

edit

new issue now, (probably a stupid one) i can't seem to make the graphics move anywhere.

(ie) main char [moveable two blocks], shoots something, i need it to follow a trajectory along the grid.

my math teacher didn't spend much time on trajectory, neither did my physics teacher. so i'm kinda plum-dumb about this.

i've separated the shooting functions, now i can create the ammunition but not make it move.

#NoTrayIcon
#include <GDIPlus.au3>
#include <WinAPI.au3>
#include <GuiConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <Misc.au3>



_GDIPlus_Startup ()
Global $bBUFFERING = True
Global $nBUFFER_SPEED = 50
Global $nGUIW = 800
Global $nGUIH = 600
Global $nPLAYER_X
Global $nPLAYER_Y
Global $bJUMPING = False
Global $aPLAYER_KEYS[4] = ['26','27','28','25']
;~ 25 LEFT ARROW key
;~ 26 UP ARROW key
;~ 27 RIGHT ARROW key
;~ 28 DOWN ARROW key

Global $nGRID_BLOCK_SIZE = 20
Global $nGRID_MAX_X = ($nGUIW/$nGRID_BLOCK_SIZE)-1
Global $nGRID_MAX_Y = ($nGUIH/$nGRID_BLOCK_SIZE)-1
Global $aGRID[$nGRID_MAX_Y+1][$nGRID_MAX_X+1]
Global $nAMMUNITION_ARRAY_SIZE = 1
Global $aAMMUNITION_BMP_BUFF[$nAMMUNITION_ARRAY_SIZE] = [0]
Global $aAMMUNITION_GRAPHIC[$nAMMUNITION_ARRAY_SIZE] = [-1]
Global $aAMMUNITION_X[$nAMMUNITION_ARRAY_SIZE]
Global $aAMMUNITION_Y[$nAMMUNITION_ARRAY_SIZE]
Global $aAMMUNITION_TRAJECTORY_X[$nAMMUNITION_ARRAY_SIZE]
Global $aAMMUNITION_TRAJECTORY_Y[$nAMMUNITION_ARRAY_SIZE]

Global $hHWND = GUICreate ('',$nGUIW,$nGUIH)
GUISetState (@SW_SHOW,$hHWND)


Global $hGUI_GRAPHIC = _GDIPlus_GraphicsCreateFromHWND ($hHWND)

Global $hBACK_BUFFER_BMP_BUFF =  _GDIPlus_BitmapCreateFromGraphics($nGUIW, $nGUIH, $hGUI_GRAPHIC)
Global $hBACK_BUFFER_GRAPHIC = _GDIPlus_ImageGetGraphicsContext($hBACK_BUFFER_BMP_BUFF)




Global $hGRID_BMP_BUFF = _GDIPlus_BitmapCreateFromGraphics($nGUIW, $nGUIH, $hBACK_BUFFER_GRAPHIC)
Global $hGRID_GRAPHIC = _GDIPlus_ImageGetGraphicsContext($hGRID_BMP_BUFF)
Global $hGRID_BRUSH = _GDIPlus_BrushCreateSolid ('0x20' & '000000')

Global $hENVIRO_BMP_BUFF = _GDIPlus_BitmapCreateFromGraphics($nGUIW, $nGUIH, $hBACK_BUFFER_GRAPHIC)
Global $hENVIRO_GRAPHIC = _GDIPlus_ImageGetGraphicsContext($hENVIRO_BMP_BUFF)
Global $hENVIRO_BACKROUND_BRUSH = _GDIPlus_BrushCreateSolid ('0xFF' & '000080')
Global $hENVIRO_BRUSH = _GDIPlus_BrushCreateSolid ('0xFF' & '008000')

Global $hPLAYER_BMP_BUFF = _GDIPlus_BitmapCreateFromGraphics($nGUIW, $nGUIH, $hBACK_BUFFER_GRAPHIC)
Global $hPLAYER_GRAPHIC = _GDIPlus_ImageGetGraphicsContext($hPLAYER_BMP_BUFF)
Global $hPLAYER_BRUSH = _GDIPlus_BrushCreateSolid ('0xFF' & '00FFFF')


reset_grid ()
draw_grid ()
draw_map ()
draw_player ()

$bBUFFERING = False

AdlibRegister ('BUFFER',$nBUFFER_SPEED)
AdlibRegister ('PHYSICS',$nBUFFER_SPEED)
AdlibRegister ('CHECK_MOVEMENT',$nBUFFER_SPEED)
GUIRegisterMsg(0x0201,'CLICK')
GUIRegisterMsg(0xF, 'MY_PAINT')
GUIRegisterMsg(0x85,'MY_PAINT')


While GUIGetMsg () <> -3
WEnd

For $nINDEX = 0 To $nAMMUNITION_ARRAY_SIZE-1
    If $aAMMUNITION_BMP_BUFF[$nINDEX] <> 0 Then
        _GDIPlus_BitmapDispose($aAMMUNITION_BMP_BUFF[$nINDEX])
    EndIf
    If $aAMMUNITION_GRAPHIC[$nINDEX] <> -1 Then
        _GDIPlus_GraphicsDispose($aAMMUNITION_GRAPHIC[$nINDEX])
    EndIf
Next


_GDIPlus_GraphicsDispose($hGUI_GRAPHIC)

_GDIPlus_BitmapDispose($hBACK_BUFFER_BMP_BUFF)
_GDIPlus_GraphicsDispose($hBACK_BUFFER_GRAPHIC)

_GDIPlus_BitmapDispose($hGRID_BMP_BUFF)
_GDIPlus_GraphicsDispose($hGRID_GRAPHIC)
_GDIPlus_BrushDispose($hGRID_BRUSH)

_GDIPlus_BitmapDispose($hENVIRO_BMP_BUFF)
_GDIPlus_GraphicsDispose($hENVIRO_GRAPHIC)
_GDIPlus_BrushDispose($hENVIRO_BACKROUND_BRUSH)
_GDIPlus_BrushDispose($hENVIRO_BRUSH)

_GDIPlus_BitmapDispose($hPLAYER_BMP_BUFF)
_GDIPlus_GraphicsDispose($hPLAYER_GRAPHIC)
_GDIPlus_BrushDispose($hPLAYER_BRUSH)

_GDIPlus_Shutdown()







Func SHOOT ($nX,$nY)
    Local $nINDEX
    For $nINDEX = 0 To $nAMMUNITION_ARRAY_SIZE-1
        If $aAMMUNITION_BMP_BUFF[$nINDEX] = 0 Or $aAMMUNITION_GRAPHIC[$nINDEX] = -1 Then
            add_ammunition ($nINDEX,$nX,$nY)
            Return
        EndIf
    Next
    $nAMMUNITION_ARRAY_SIZE += 1
    ReDim $aAMMUNITION_BMP_BUFF[$nAMMUNITION_ARRAY_SIZE]
    ReDim $aAMMUNITION_GRAPHIC[$nAMMUNITION_ARRAY_SIZE]
    ReDim $aAMMUNITION_X[$nAMMUNITION_ARRAY_SIZE]
    ReDim $aAMMUNITION_Y[$nAMMUNITION_ARRAY_SIZE]
    ReDim $aAMMUNITION_TRAJECTORY_X[$nAMMUNITION_ARRAY_SIZE]
    ReDim $aAMMUNITION_TRAJECTORY_Y[$nAMMUNITION_ARRAY_SIZE]
    add_ammunition ($nAMMUNITION_ARRAY_SIZE-1,$nX,$nY)
EndFunc
Func add_ammunition ($nINDEX,$nX,$nY)
    $aAMMUNITION_BMP_BUFF[$nINDEX] = _GDIPlus_BitmapCreateFromGraphics($nGUIW, $nGUIH, $hBACK_BUFFER_GRAPHIC)
    $aAMMUNITION_GRAPHIC[$nINDEX] = _GDIPlus_ImageGetGraphicsContext($aAMMUNITION_BMP_BUFF[$nINDEX])
    $aAMMUNITION_X[$nINDEX] = $nPLAYER_X*$nGRID_BLOCK_SIZE
    $aAMMUNITION_Y[$nINDEX] = $nPLAYER_Y*$nGRID_BLOCK_SIZE
    $aAMMUNITION_TRAJECTORY_X[$nINDEX] = $nX-($nPLAYER_X*$nGRID_BLOCK_SIZE)
    $aAMMUNITION_TRAJECTORY_Y[$nINDEX] = $nY-($nPLAYER_Y*$nGRID_BLOCK_SIZE)
    draw_ammunition ($nINDEX)
EndFunc
Func draw_ammunition ($nINDEX)
    _GDIPlus_GraphicsFILLRect($aAMMUNITION_GRAPHIC[$nINDEX],$aAMMUNITION_X[$nINDEX],$aAMMUNITION_Y[$nINDEX],$nGRID_BLOCK_SIZE/4,$nGRID_BLOCK_SIZE/4)
EndFunc
Func move_ammunition ()
    If $bBUFFERING = False Then
        $bBUFFERING = True
        Local $nINDEX
        For $nINDEX = 0 To $nAMMUNITION_ARRAY_SIZE-1
            If $aAMMUNITION_BMP_BUFF[$nINDEX] <> 0 And $aAMMUNITION_GRAPHIC[$nINDEX] <> -1 Then
                _GDIPlus_GraphicsClear ($aAMMUNITION_GRAPHIC[$nINDEX])
                $aAMMUNITION_X[$nINDEX] += $aAMMUNITION_TRAJECTORY_X[$nINDEX]
                $aAMMUNITION_Y[$nINDEX] += $aAMMUNITION_TRAJECTORY_Y[$nINDEX]
                draw_ammunition ($nINDEX)
            EndIf
        Next
        $bBUFFERING = False
    EndIf
EndFunc








Func move_player ($nDIRECTION)
    Local $nX, $nY
    $nX = $nPLAYER_X
    $nY = $nPLAYER_Y
    Switch $nDIRECTION
        Case 0
            If $bJUMPING = False Then
                $bJUMPING = True
                $nY -= 5
            Else
                Return
            EndIf
        Case 1
            $nX += 1
        Case 2
            $nY += 1
        Case 3
            $nX -= 1
    EndSwitch
    If $nX <> $nPLAYER_X Or $nY <> $nPLAYER_Y Then
        If $nX <= $nGRID_MAX_X And $nX >= 0 And $nY <= $nGRID_MAX_Y And $nY >= 0 Then
            If $aGRID[$nY][$nX] <> 'e' And $aGRID[$nY+1][$nX] <> 'e' Then
                $aGRID[$nPLAYER_Y][$nPLAYER_X] = ''
                $aGRID[$nPLAYER_Y+1][$nPLAYER_X] = ''
                _GDIPlus_GraphicsFillRect ($hPLAYER_GRAPHIC,$nPLAYER_X*$nGRID_BLOCK_SIZE,$nPLAYER_Y*$nGRID_BLOCK_SIZE,$nGRID_BLOCK_SIZE,$nGRID_BLOCK_SIZE*2,$hENVIRO_BACKROUND_BRUSH)

                $nPLAYER_X = $nX
                $nPLAYER_Y = $nY

                $aGRID[$nPLAYER_Y][$nPLAYER_X] = 'p'
                $aGRID[$nPLAYER_Y+1][$nPLAYER_X] = 'p'
                _GDIPlus_GraphicsFillRect ($hPLAYER_GRAPHIC,$nPLAYER_X*$nGRID_BLOCK_SIZE,$nPLAYER_Y*$nGRID_BLOCK_SIZE,$nGRID_BLOCK_SIZE,$nGRID_BLOCK_SIZE*2,$hPLAYER_BRUSH)
            EndIf
        EndIf
    EndIf
    Return
EndFunc




Func reset_grid ()
    Local $nX, $nY
    For $nX = 0 To $nGRID_MAX_X
        For $nY = 0 To $nGRID_MAX_Y
            $aGRID[$nY][$nX] = ''
            _GDIPlus_GraphicsFillRect ($hENVIRO_GRAPHIC,$nX*$nGRID_BLOCK_SIZE,$nY*$nGRID_BLOCK_SIZE,$nGRID_BLOCK_SIZE,$nGRID_BLOCK_SIZE,$hENVIRO_BACKROUND_BRUSH)
        Next
    Next
EndFunc








Func draw_grid ()
    Local $nX, $nY
    _GDIPlus_GraphicsClear ($hGRID_GRAPHIC,'0x20' & '0000FF')
    For $nX = 0 To $nGUIW Step $nGRID_BLOCK_SIZE
        _GDIPlus_GraphicsFillRect ($hGRID_GRAPHIC,$nX,0,1,$nGUIH,$hGRID_BRUSH)
    Next
    For $nY = 0 To $nGUIH Step $nGRID_BLOCK_SIZE
        _GDIPlus_GraphicsFillRect ($hGRID_GRAPHIC,0,$nY,$nGUIW,1,$hGRID_BRUSH)
    Next
EndFunc
Func draw_map ()
    Local $nX, $nY, $nH
    For $nX = 0 To $nGRID_MAX_X
        $nH = 5
;~      $nH = Random (5,9,1)
        For $nY = $nGRID_MAX_Y-$nH To $nGRID_MAX_Y
            _GDIPlus_GraphicsFillRect ($hENVIRO_GRAPHIC,$nX*$nGRID_BLOCK_SIZE,$nY*$nGRID_BLOCK_SIZE,$nGRID_BLOCK_SIZE,$nGRID_BLOCK_SIZE,$hENVIRO_BRUSH)
            $aGRID[$nY][$nX] = 'e'
        Next
    Next
EndFunc
Func draw_player ()
    Local $nX,$nY
    $nX = Floor ($nGRID_MAX_X/$nGRID_BLOCK_SIZE) * $nGRID_BLOCK_SIZE
    $nY = 1
    _GDIPlus_GraphicsFillRect ($hPLAYER_GRAPHIC,$nX*$nGRID_BLOCK_SIZE,$nY*$nGRID_BLOCK_SIZE,$nGRID_BLOCK_SIZE,$nGRID_BLOCK_SIZE*2,$hPLAYER_BRUSH)
    $aGRID[$nY][$nX] = 'p'
    $aGRID[$nY+1][$nX] = 'p'
    $nPLAYER_X = $nX
    $nPLAYER_Y = $nY
EndFunc











Func PHYSICS ()
    If $bBUFFERING = False Then
        $bBUFFERING = True
        move_ammunition ()
        If $aGRID[$nPLAYER_Y+2][$nPLAYER_X] == '' Then
            move_player (2)
        Else
            $bJUMPING = False
        EndIf
        $bBUFFERING = False
    EndIf
EndFunc

Func CHECK_MOVEMENT ()
    Local $nINDEX
    For $nINDEX = 0 to 3
        If _IsPressed ($aPLAYER_KEYS[$nINDEX]) Then
            move_player($nINDEX)
            Return
        EndIf
    Next
EndFunc

Func CLICK ()
    Local $nX = _WinAPI_GetMousePosX(True,$hHWND)
    Local $nY = _WinAPI_GetMousePosY(True,$hHWND)
    SHOOT ($nX,$nY)
EndFunc


Func BUFFER ()
    If $bBUFFERING = False Then
        $bBUFFERING = True
        _GDIPlus_GraphicsDrawImage($hBACK_BUFFER_GRAPHIC, $hGRID_BMP_BUFF, 0, 0)
        _GDIPlus_GraphicsDrawImage($hBACK_BUFFER_GRAPHIC, $hENVIRO_BMP_BUFF, 0, 0)
        _GDIPlus_GraphicsDrawImage($hBACK_BUFFER_GRAPHIC, $hPLAYER_BMP_BUFF, 0, 0)

        Local $nINDEX
        For $nINDEX = 0 To $nAMMUNITION_ARRAY_SIZE-1
            If $aAMMUNITION_BMP_BUFF[$nINDEX] <> 0 Then
                _GDIPlus_GraphicsDrawImage($hBACK_BUFFER_GRAPHIC, $aAMMUNITION_BMP_BUFF[$nINDEX], 0, 0)
            EndIf
        Next

        _GDIPlus_GraphicsDrawImage($hGUI_GRAPHIC, $hBACK_BUFFER_BMP_BUFF, 0, 0)
        $bBUFFERING = False
    EndIf
EndFunc

Func MY_PAINT($hWnd, $msg, $wParam, $lParam)
    BUFFER ()
    _WinAPI_RedrawWindow($hWnd,Default,Default, BitOR($RDW_INVALIDATE, $RDW_UPDATENOW, $RDW_FRAME)); , $RDW_ALLCHILDREN
    Return $GUI_RUNDEFMSG
EndFunc
Edited by CodyBarrett
Link to comment
Share on other sites

I made a simple trajectory example here:

Maybe it is usefull for you.

Welcome to the GDI+ world :)

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

THANK YOU!

:) however, i still have an issue with the MATH part of trajectory, i've got it to move thanks to you! but i'm not understanding the acurate calculations.

#NoTrayIcon
#include <GDIPlus.au3>
#include <WinAPI.au3>
#include <GuiConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <Misc.au3>



_GDIPlus_Startup ()
Global $bBUFFERING = True
Global $nBUFFER_SPEED = 50
Global $nGUIW = 800
Global $nGUIH = 600
Global $nPLAYER_X
Global $nPLAYER_Y
Global $bJUMPING = False
Global $aPLAYER_KEYS[4] = ['26','27','28','25']
;~ 25 LEFT ARROW key
;~ 26 UP ARROW key
;~ 27 RIGHT ARROW key
;~ 28 DOWN ARROW key

Global $nGRID_BLOCK_SIZE = 10
Global $nGRID_MAX_X = ($nGUIW/$nGRID_BLOCK_SIZE)-1
Global $nGRID_MAX_Y = ($nGUIH/$nGRID_BLOCK_SIZE)-1
Global $aGRID[$nGRID_MAX_Y+1][$nGRID_MAX_X+1]
Global $nAMMUNITION_ARRAY_SIZE = 1
Global $aAMMUNITION_BMP_BUFF[$nAMMUNITION_ARRAY_SIZE] = [0]
Global $aAMMUNITION_GRAPHIC[$nAMMUNITION_ARRAY_SIZE] = [-1]
Global $aAMMUNITION_X[$nAMMUNITION_ARRAY_SIZE]
Global $aAMMUNITION_Y[$nAMMUNITION_ARRAY_SIZE]
Global $aAMMUNITION_GRID_X[$nAMMUNITION_ARRAY_SIZE]
Global $aAMMUNITION_GRID_Y[$nAMMUNITION_ARRAY_SIZE]
Global $aAMMUNITION_TRAJECTORY_X[$nAMMUNITION_ARRAY_SIZE]
Global $aAMMUNITION_TRAJECTORY_Y[$nAMMUNITION_ARRAY_SIZE]

Global $hHWND = GUICreate ('',$nGUIW,$nGUIH)
GUISetState (@SW_SHOW,$hHWND)


Global $hGUI_GRAPHIC = _GDIPlus_GraphicsCreateFromHWND ($hHWND)

Global $hBACK_BUFFER_BMP_BUFF =  _GDIPlus_BitmapCreateFromGraphics($nGUIW, $nGUIH, $hGUI_GRAPHIC)
Global $hBACK_BUFFER_GRAPHIC = _GDIPlus_ImageGetGraphicsContext($hBACK_BUFFER_BMP_BUFF)


Global $hGRID_BMP_BUFF = _GDIPlus_BitmapCreateFromGraphics($nGUIW, $nGUIH, $hBACK_BUFFER_GRAPHIC)
Global $hGRID_GRAPHIC = _GDIPlus_ImageGetGraphicsContext($hGRID_BMP_BUFF)
Global $hGRID_BRUSH = _GDIPlus_BrushCreateSolid ('0x20' & '000000')

Global $hENVIRO_BMP_BUFF = _GDIPlus_BitmapCreateFromGraphics($nGUIW, $nGUIH, $hBACK_BUFFER_GRAPHIC)
Global $hENVIRO_GRAPHIC = _GDIPlus_ImageGetGraphicsContext($hENVIRO_BMP_BUFF)
Global $hENVIRO_BACKROUND_BRUSH = _GDIPlus_BrushCreateSolid ('0xFF' & '000080')
Global $hENVIRO_BRUSH = _GDIPlus_BrushCreateSolid ('0xFF' & '008000')

Global $hPLAYER_BMP_BUFF = _GDIPlus_BitmapCreateFromGraphics($nGUIW, $nGUIH, $hBACK_BUFFER_GRAPHIC)
Global $hPLAYER_GRAPHIC = _GDIPlus_ImageGetGraphicsContext($hPLAYER_BMP_BUFF)
Global $hPLAYER_BRUSH = _GDIPlus_BrushCreateSolid ('0xFF' & '00FFFF')


reset_grid ()
draw_grid ()
draw_map ()
draw_player ()

$bBUFFERING = False

AdlibRegister ('BUFFER',$nBUFFER_SPEED)
AdlibRegister ('PHYSICS',$nBUFFER_SPEED)
AdlibRegister ('CHECK_MOVEMENT',$nBUFFER_SPEED)
GUIRegisterMsg(0x0201,'CLICK')
GUIRegisterMsg(0xF, 'MY_PAINT')
GUIRegisterMsg(0x85,'MY_PAINT')


While GUIGetMsg () <> -3
WEnd

For $nINDEX = 0 To $nAMMUNITION_ARRAY_SIZE-1
    If $aAMMUNITION_BMP_BUFF[$nINDEX] <> 0 Then
        _GDIPlus_BitmapDispose($aAMMUNITION_BMP_BUFF[$nINDEX])
    EndIf
    If $aAMMUNITION_GRAPHIC[$nINDEX] <> -1 Then
        _GDIPlus_GraphicsDispose($aAMMUNITION_GRAPHIC[$nINDEX])
    EndIf
Next


_GDIPlus_GraphicsDispose($hGUI_GRAPHIC)

_GDIPlus_BitmapDispose($hBACK_BUFFER_BMP_BUFF)
_GDIPlus_GraphicsDispose($hBACK_BUFFER_GRAPHIC)

_GDIPlus_BitmapDispose($hGRID_BMP_BUFF)
_GDIPlus_GraphicsDispose($hGRID_GRAPHIC)
_GDIPlus_BrushDispose($hGRID_BRUSH)

_GDIPlus_BitmapDispose($hENVIRO_BMP_BUFF)
_GDIPlus_GraphicsDispose($hENVIRO_GRAPHIC)
_GDIPlus_BrushDispose($hENVIRO_BACKROUND_BRUSH)
_GDIPlus_BrushDispose($hENVIRO_BRUSH)

_GDIPlus_BitmapDispose($hPLAYER_BMP_BUFF)
_GDIPlus_GraphicsDispose($hPLAYER_GRAPHIC)
_GDIPlus_BrushDispose($hPLAYER_BRUSH)

_GDIPlus_Shutdown()







Func SHOOT ($nX,$nY)
    Local $nINDEX
    For $nINDEX = 0 To $nAMMUNITION_ARRAY_SIZE-1
        If $aAMMUNITION_BMP_BUFF[$nINDEX] = 0 Or $aAMMUNITION_GRAPHIC[$nINDEX] = -1 Then
            add_ammunition ($nINDEX,$nX,$nY)
            Return
        EndIf
    Next
    $nAMMUNITION_ARRAY_SIZE += 1
    ReDim $aAMMUNITION_BMP_BUFF[$nAMMUNITION_ARRAY_SIZE]
    ReDim $aAMMUNITION_GRAPHIC[$nAMMUNITION_ARRAY_SIZE]
    ReDim $aAMMUNITION_X[$nAMMUNITION_ARRAY_SIZE]
    ReDim $aAMMUNITION_Y[$nAMMUNITION_ARRAY_SIZE]
    ReDim $aAMMUNITION_GRID_X[$nAMMUNITION_ARRAY_SIZE]
    ReDim $aAMMUNITION_GRID_Y[$nAMMUNITION_ARRAY_SIZE]
    ReDim $aAMMUNITION_TRAJECTORY_X[$nAMMUNITION_ARRAY_SIZE]
    ReDim $aAMMUNITION_TRAJECTORY_Y[$nAMMUNITION_ARRAY_SIZE]
    add_ammunition ($nAMMUNITION_ARRAY_SIZE-1,$nX,$nY)
EndFunc
Func add_ammunition ($nINDEX,$nX,$nY)
    $aAMMUNITION_BMP_BUFF[$nINDEX] = _GDIPlus_BitmapCreateFromGraphics($nGUIW, $nGUIH, $hBACK_BUFFER_GRAPHIC)
    $aAMMUNITION_GRAPHIC[$nINDEX] = _GDIPlus_ImageGetGraphicsContext($aAMMUNITION_BMP_BUFF[$nINDEX])

    $aAMMUNITION_X[$nINDEX] = $nPLAYER_X*$nGRID_BLOCK_SIZE
    $aAMMUNITION_Y[$nINDEX] = $nPLAYER_Y*$nGRID_BLOCK_SIZE
    $aAMMUNITION_GRID_X[$nINDEX] = $nPLAYER_X
    $aAMMUNITION_GRID_Y[$nINDEX] = $nPLAYER_Y











If $nX > $aAMMUNITION_X[$nINDEX] Then
    $aAMMUNITION_TRAJECTORY_X[$nINDEX] = $nGRID_BLOCK_SIZE*1
Else
    $aAMMUNITION_TRAJECTORY_X[$nINDEX] = $nGRID_BLOCK_SIZE*-1
EndIf
If $nY > $aAMMUNITION_Y[$nINDEX] Then
    $aAMMUNITION_TRAJECTORY_Y[$nINDEX] = $nGRID_BLOCK_SIZE*1
Else
    $aAMMUNITION_TRAJECTORY_Y[$nINDEX] = $nGRID_BLOCK_SIZE*-1
EndIf


;~ $aAMMUNITION_TRAJECTORY_X[$nINDEX] = ;need help here, need more accurate X point
;~ $aAMMUNITION_TRAJECTORY_Y[$nINDEX] = ;need help here, need more accurate Y point











    draw_ammunition ($nINDEX,$aAMMUNITION_X[$nINDEX],$aAMMUNITION_Y[$nINDEX])
EndFunc
Func draw_ammunition ($nINDEX,$nX,$nY)
    _GDIPlus_GraphicsFILLRect($aAMMUNITION_GRAPHIC[$nINDEX],$nX,$nY,$nGRID_BLOCK_SIZE,$nGRID_BLOCK_SIZE)
EndFunc
Func move_ammunition ()
    Local $nINDEX
    For $nINDEX = 0 To $nAMMUNITION_ARRAY_SIZE-1
        If $aAMMUNITION_BMP_BUFF[$nINDEX] <> 0 And $aAMMUNITION_GRAPHIC[$nINDEX] <> -1 Then
            _GDIPlus_GraphicsClear ($aAMMUNITION_GRAPHIC[$nINDEX],0x00000000)
            If $aAMMUNITION_GRID_X[$nINDEX] >= $nGRID_MAX_X Or $aAMMUNITION_GRID_Y[$nINDEX] >= $nGRID_MAX_Y Or $aAMMUNITION_GRID_X[$nINDEX] <= 0 Or $aAMMUNITION_GRID_Y[$nINDEX] <= 0 Then
                _GDIPlus_BitmapDispose($aAMMUNITION_BMP_BUFF[$nINDEX])
                _GDIPlus_GraphicsDispose($aAMMUNITION_GRAPHIC[$nINDEX])
                $aAMMUNITION_BMP_BUFF[$nINDEX] = 0
                $aAMMUNITION_GRAPHIC[$nINDEX] = -1
            Else
                $aAMMUNITION_X[$nINDEX] += $aAMMUNITION_TRAJECTORY_X[$nINDEX]
                $aAMMUNITION_Y[$nINDEX] += $aAMMUNITION_TRAJECTORY_Y[$nINDEX]
                $aAMMUNITION_GRID_X[$nINDEX] += $aAMMUNITION_TRAJECTORY_X[$nINDEX]/$nGRID_BLOCK_SIZE
                $aAMMUNITION_GRID_Y[$nINDEX] += $aAMMUNITION_TRAJECTORY_Y[$nINDEX]/$nGRID_BLOCK_SIZE
                Switch $aGRID[$aAMMUNITION_GRID_Y[$nINDEX]][$aAMMUNITION_GRID_X[$nINDEX]]
                    Case 'e'
                        _GDIPlus_BitmapDispose($aAMMUNITION_BMP_BUFF[$nINDEX])
                        _GDIPlus_GraphicsDispose($aAMMUNITION_GRAPHIC[$nINDEX])
                        $aAMMUNITION_BMP_BUFF[$nINDEX] = 0
                        $aAMMUNITION_GRAPHIC[$nINDEX] = -1
                        $aGRID[$aAMMUNITION_GRID_Y[$nINDEX]][$aAMMUNITION_GRID_X[$nINDEX]] = ''
                        _GDIPlus_GraphicsFillRect ($hENVIRO_GRAPHIC,$aAMMUNITION_X[$nINDEX],$aAMMUNITION_Y[$nINDEX],$nGRID_BLOCK_SIZE,$nGRID_BLOCK_SIZE,$hENVIRO_BACKROUND_BRUSH)
                    Case 'p'
                    Case ''
                        draw_ammunition ($nINDEX,$aAMMUNITION_X[$nINDEX],$aAMMUNITION_Y[$nINDEX])
                EndSwitch
            EndIf
        EndIf
    Next
EndFunc








Func move_player ($nDIRECTION)
    Local $nX, $nY
    $nX = $nPLAYER_X
    $nY = $nPLAYER_Y
    Switch $nDIRECTION
        Case 0
            If $bJUMPING = False Then
                $bJUMPING = True
                $nY -= 5
            Else
                Return
            EndIf
        Case 1
            $nX += 1
        Case 2
            $nY += 1
        Case 3
            $nX -= 1
    EndSwitch
    If $nX <> $nPLAYER_X Or $nY <> $nPLAYER_Y Then
        If $nX <= $nGRID_MAX_X And $nX >= 0 And $nY <= $nGRID_MAX_Y And $nY >= 0 Then
            If $aGRID[$nY][$nX] <> 'e' And $aGRID[$nY+1][$nX] <> 'e' Then
                $aGRID[$nPLAYER_Y][$nPLAYER_X] = ''
                $aGRID[$nPLAYER_Y+1][$nPLAYER_X] = ''
                _GDIPlus_GraphicsFillRect ($hPLAYER_GRAPHIC,$nPLAYER_X*$nGRID_BLOCK_SIZE,$nPLAYER_Y*$nGRID_BLOCK_SIZE,$nGRID_BLOCK_SIZE,$nGRID_BLOCK_SIZE*2,$hENVIRO_BACKROUND_BRUSH)

                $nPLAYER_X = $nX
                $nPLAYER_Y = $nY

                $aGRID[$nPLAYER_Y][$nPLAYER_X] = 'p'
                $aGRID[$nPLAYER_Y+1][$nPLAYER_X] = 'p'
                _GDIPlus_GraphicsFillRect ($hPLAYER_GRAPHIC,$nPLAYER_X*$nGRID_BLOCK_SIZE,$nPLAYER_Y*$nGRID_BLOCK_SIZE,$nGRID_BLOCK_SIZE,$nGRID_BLOCK_SIZE*2,$hPLAYER_BRUSH)
            EndIf
        EndIf
    EndIf
    Return
EndFunc




Func reset_grid ()
    Local $nX, $nY
    For $nX = 0 To $nGRID_MAX_X
        For $nY = 0 To $nGRID_MAX_Y
            $aGRID[$nY][$nX] = ''
            _GDIPlus_GraphicsFillRect ($hENVIRO_GRAPHIC,$nX*$nGRID_BLOCK_SIZE,$nY*$nGRID_BLOCK_SIZE,$nGRID_BLOCK_SIZE,$nGRID_BLOCK_SIZE,$hENVIRO_BACKROUND_BRUSH)
        Next
    Next
EndFunc








Func draw_grid ()
    Local $nX, $nY
    _GDIPlus_GraphicsClear ($hGRID_GRAPHIC,'0x20' & '0000FF')
    For $nX = 0 To $nGUIW Step $nGRID_BLOCK_SIZE
        _GDIPlus_GraphicsFillRect ($hGRID_GRAPHIC,$nX,0,1,$nGUIH,$hGRID_BRUSH)
    Next
    For $nY = 0 To $nGUIH Step $nGRID_BLOCK_SIZE
        _GDIPlus_GraphicsFillRect ($hGRID_GRAPHIC,0,$nY,$nGUIW,1,$hGRID_BRUSH)
    Next
EndFunc
Func draw_map ()
    Local $nX, $nY, $nH
    For $nX = 0 To $nGRID_MAX_X
        $nH = 5
;~      $nH = Random (5,9,1)
        For $nY = $nGRID_MAX_Y-$nH To $nGRID_MAX_Y
            _GDIPlus_GraphicsFillRect ($hENVIRO_GRAPHIC,$nX*$nGRID_BLOCK_SIZE,$nY*$nGRID_BLOCK_SIZE,$nGRID_BLOCK_SIZE,$nGRID_BLOCK_SIZE,$hENVIRO_BRUSH)
            $aGRID[$nY][$nX] = 'e'
        Next
    Next
EndFunc
Func draw_player ()
    Local $nX,$nY
    $nX = Floor ($nGRID_MAX_X/$nGRID_BLOCK_SIZE) * $nGRID_BLOCK_SIZE
    $nY = 1
    _GDIPlus_GraphicsFillRect ($hPLAYER_GRAPHIC,$nX*$nGRID_BLOCK_SIZE,$nY*$nGRID_BLOCK_SIZE,$nGRID_BLOCK_SIZE,$nGRID_BLOCK_SIZE*2,$hPLAYER_BRUSH)
    $aGRID[$nY][$nX] = 'p'
    $aGRID[$nY+1][$nX] = 'p'
    $nPLAYER_X = $nX
    $nPLAYER_Y = $nY
EndFunc











Func PHYSICS ()
    If $bBUFFERING = False Then
        $bBUFFERING = True
        move_ammunition ()
        If $aGRID[$nPLAYER_Y+2][$nPLAYER_X] == '' Then
            move_player (2)
        Else
            $bJUMPING = False
        EndIf
        $bBUFFERING = False
    EndIf
EndFunc

Func CHECK_MOVEMENT ()
    Local $nINDEX
    For $nINDEX = 0 to 3
        If _IsPressed ($aPLAYER_KEYS[$nINDEX]) Then
            move_player($nINDEX)
            Return
        EndIf
    Next
EndFunc

Func CLICK ()
    If $bBUFFERING = False Then
        $bBUFFERING = True
        Local $nX = _WinAPI_GetMousePosX(True,$hHWND)
        Local $nY = _WinAPI_GetMousePosY(True,$hHWND)
        ConsoleWrite ($nAMMUNITION_ARRAY_SIZE & @CRLF)
        SHOOT ($nX,$nY)
        $bBUFFERING = False
    EndIf
EndFunc


Func BUFFER ()
    If $bBUFFERING = False Then
        $bBUFFERING = True
        _GDIPlus_GraphicsDrawImage($hBACK_BUFFER_GRAPHIC, $hGRID_BMP_BUFF, 0, 0)
        _GDIPlus_GraphicsDrawImage($hBACK_BUFFER_GRAPHIC, $hENVIRO_BMP_BUFF, 0, 0)
        _GDIPlus_GraphicsDrawImage($hBACK_BUFFER_GRAPHIC, $hPLAYER_BMP_BUFF, 0, 0)

        Local $nINDEX
        For $nINDEX = 0 To $nAMMUNITION_ARRAY_SIZE-1
            If $aAMMUNITION_BMP_BUFF[$nINDEX] <> 0 Then
                _GDIPlus_GraphicsDrawImage($hBACK_BUFFER_GRAPHIC, $aAMMUNITION_BMP_BUFF[$nINDEX], 0, 0)
            EndIf
        Next

        _GDIPlus_GraphicsDrawImage($hGUI_GRAPHIC, $hBACK_BUFFER_BMP_BUFF, 0, 0)
        $bBUFFERING = False
    EndIf
EndFunc

Func MY_PAINT($hWnd, $msg, $wParam, $lParam)
    BUFFER ()
    _WinAPI_RedrawWindow($hWnd,Default,Default, BitOR($RDW_INVALIDATE, $RDW_UPDATENOW, $RDW_FRAME)); , $RDW_ALLCHILDREN
    Return $GUI_RUNDEFMSG
EndFunc
Link to comment
Share on other sites

WOW! thanks for that! :) works much better! May i ask WHY this is better? why is it faster and no flicker? O.o just for curiosities sake.

It doesn't flicker because there's only one thing drawing to screen, so now we can't see the all the layers as they are drawn to the screen one by one.

Also some other points you may want to look at if you have a "need for speed" :P:)

Point 1:

Use OnEvent instead of MessageLoop (delete GUIGetMsg()!!). Why? Well it doesn't matter here since you don't use the mainloop for timing, but run your snake* game and it gets noticeably faster when you move you mouse over the window. Why again? See quote:

This function automatically idles the CPU when required so that it can be safely used in tight loops without hogging all the CPU.

So it basically does a Sleep(10) before doing "Return 0" (no message).

See example:

GUICreate(@ScriptName, @DesktopWidth /2, @DesktopHeight /2, 0, 0)
GUISetState()

While 1
    $iTimer = TimerInit()
    If GUIGetMsg() = -3 Then Exit
    ConsoleWrite(TimerDiff($iTimer) & @CRLF)
WEnd

Around 10 ms when nothing happens, and something like 0.0018 ms when you move the mouse over.

Point 2:

Look at the SDL UDF instead of GDI+. Easier to use, and slightly faster. May take a little time getting used to, but totally worth it IMHO.

Edit*: Arrg, you updated your scripts, may have been tron, one of the older ones anyway. GUIGetMsg() isn't good for realtime stuff....

Edited by AdmiralAlkex
Link to comment
Share on other sites

i HAD used ONEVENT for dynamic timing, but i had problems when my adlibs were being called when my GUI funcs werent done messing up the code. yea i've tried conditionals. GetMsg works for now....

TRON will need speed. :) but snake and this new one doesn't need speed.

still having trouble with the ammunition trajectory accuracy. :)

Link to comment
Share on other sites

Hi,

What are you trying to accomplish with your trajectory?

eg:

Click anywhere on the gui and a bullet is fired to the point of where the mouse was clicked at?

or is there restrictions to where a bullet can be fired, are physics needed for trajectory of the bullet (bullet looses speed height over distance)?

Link to comment
Share on other sites

Hi,

What are you trying to accomplish with your trajectory?

eg:

Click anywhere on the gui and a bullet is fired to the point of where the mouse was clicked at?

or is there restrictions to where a bullet can be fired, are physics needed for trajectory of the bullet (bullet looses speed height over distance)?

bullet fired in straight line. where the mouse is pointed and past the clicked x/y pos.

EDIT

UEZ, your example was perfect, i changed gravity to 0 and it does exactly what i want, however i don't know how it does it, or how to incorporate that into my script. May i ask how you did it mathematically? :)

Edited by CodyBarrett
Link to comment
Share on other sites

BUMP

i've edited the code so as to not include a GRID array. and limited the number of shots. they move similar to my first issue... only diagonally. can someone help with the math part? so i can make it move relative to the mouse pos when clicked?

#NoTrayIcon
#include <GDIPlus.au3>
#include <WinAPI.au3>
#include <GuiConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <Misc.au3>


Opt('PixelCoordMode', 2)
_GDIPlus_Startup ()
Global $bBUFFERING = True
Global $bSHOOTING = False
Global $bJUMPING = False

Global $nBUFFER_SPEED = 35
Global $nPHYSICS_SPEED = 50
Global $nMOVEMENT_SPEED = 35


Global $nGUIW = 800
Global $nGUIH = 600
Global $nPLAYER_X
Global $nPLAYER_Y
Global $nBLOCK_SIZE = 20
Global $bJUMPING = False
Global $aPLAYER_KEYS[4] = ['26','27','28','25']
;~ 25 LEFT ARROW key
;~ 26 UP ARROW key
;~ 27 RIGHT ARROW key
;~ 28 DOWN ARROW key
Global $nAMMUNITION_MAX = 2
Global $nAMMUNITION_MIN = $nAMMUNITION_MAX-1
Global $aAMMUNTION_STATUS[$nAMMUNITION_MAX]
Global $aAMMUNTION_BMP_BUFF[$nAMMUNITION_MAX]
Global $aAMMUNTION_GRAPHIC[$nAMMUNITION_MAX]
Global $aAMMUNTION_C_X[$nAMMUNITION_MAX]
Global $aAMMUNTION_C_Y[$nAMMUNITION_MAX]
Global $aAMMUNTION_D_X[$nAMMUNITION_MAX]
Global $aAMMUNTION_D_Y[$nAMMUNITION_MAX]
Global $aAMMUNTION_S_X[$nAMMUNITION_MAX]
Global $aAMMUNTION_S_Y[$nAMMUNITION_MAX]

Global $hHWND = GUICreate ('',$nGUIW,$nGUIH)
GUISetState (@SW_SHOW,$hHWND)

Global $hGUI_GRAPHIC = _GDIPlus_GraphicsCreateFromHWND ($hHWND)

Global $hBACK_BUFFER_BMP_BUFF =  _GDIPlus_BitmapCreateFromGraphics($nGUIW, $nGUIH, $hGUI_GRAPHIC)
Global $hBACK_BUFFER_GRAPHIC = _GDIPlus_ImageGetGraphicsContext($hBACK_BUFFER_BMP_BUFF)

    Global $hGRID_BMP_BUFF = _GDIPlus_BitmapCreateFromGraphics($nGUIW, $nGUIH, $hBACK_BUFFER_GRAPHIC)
    Global $hGRID_GRAPHIC = _GDIPlus_ImageGetGraphicsContext($hGRID_BMP_BUFF)
    Global $sGRID_COLOR = '000000'
    Global $hGRID_BRUSH = _GDIPlus_BrushCreateSolid ('0xFF' & $sGRID_COLOR)

    Global $hENVIRO_BMP_BUFF = _GDIPlus_BitmapCreateFromGraphics($nGUIW, $nGUIH, $hBACK_BUFFER_GRAPHIC)
    Global $hENVIRO_GRAPHIC = _GDIPlus_ImageGetGraphicsContext($hENVIRO_BMP_BUFF)
    Global $sENVIRO_BACKGROUND_COLOR = '0000FF'
    Global $hENVIRO_BACKROUND_BRUSH = _GDIPlus_BrushCreateSolid ('0xFF' & $sENVIRO_BACKGROUND_COLOR)
    Global $sENVIRO_COLOR = '00FF00'
    Global $hENVIRO_BRUSH = _GDIPlus_BrushCreateSolid ('0xFF' & $sENVIRO_COLOR)

    Global $hPLAYER_BMP_BUFF = _GDIPlus_BitmapCreateFromGraphics($nGUIW, $nGUIH, $hBACK_BUFFER_GRAPHIC)
    Global $hPLAYER_GRAPHIC = _GDIPlus_ImageGetGraphicsContext($hPLAYER_BMP_BUFF)
    Global $sPLAYER_COLOR = '00FFFF'
    Global $hPLAYER_BRUSH = _GDIPlus_BrushCreateSolid ('0xFF' & $sPLAYER_COLOR)

For $nINDEX = 0 To $nAMMUNITION_MIN
    delete_ammunition ($nINDEX)
Next
draw_grid ()
draw_map ()
draw_player_init ()



$bBUFFERING = False
AdlibRegister ('BUFFER',$nBUFFER_SPEED)
AdlibRegister ('PHYSICS',$nPHYSICS_SPEED)
AdlibRegister ('CHECK_MOVEMENT',$nMOVEMENT_SPEED)
GUIRegisterMsg(0x0201,'CLICK')
GUIRegisterMsg(0xF, 'MY_PAINT')
GUIRegisterMsg(0x85,'MY_PAINT')


While GUIGetMsg () <> -3
WEnd


For $nINDEX = 0 To $nAMMUNITION_MIN
    _GDIPlus_BitmapDispose($aAMMUNTION_BMP_BUFF[$nINDEX])
    _GDIPlus_GraphicsDispose($aAMMUNTION_GRAPHIC[$nINDEX])
Next

_GDIPlus_BitmapDispose($hBACK_BUFFER_BMP_BUFF)
_GDIPlus_GraphicsDispose($hBACK_BUFFER_GRAPHIC)

_GDIPlus_BitmapDispose($hGRID_BMP_BUFF)
_GDIPlus_GraphicsDispose($hGRID_GRAPHIC)
_GDIPlus_BrushDispose($hGRID_BRUSH)

_GDIPlus_BitmapDispose($hENVIRO_BMP_BUFF)
_GDIPlus_GraphicsDispose($hENVIRO_GRAPHIC)
_GDIPlus_BrushDispose($hENVIRO_BACKROUND_BRUSH)
_GDIPlus_BrushDispose($hENVIRO_BRUSH)

_GDIPlus_BitmapDispose($hPLAYER_BMP_BUFF)
_GDIPlus_GraphicsDispose($hPLAYER_GRAPHIC)
_GDIPlus_BrushDispose($hPLAYER_BRUSH)

_GDIPlus_GraphicsDispose($hGUI_GRAPHIC)
_GDIPlus_Shutdown()


Func delete_ammunition ($nINDEX)
    _GDIPlus_BitmapDispose($aAMMUNTION_BMP_BUFF[$nINDEX])
    _GDIPlus_GraphicsDispose($aAMMUNTION_GRAPHIC[$nINDEX])
    $aAMMUNTION_STATUS[$nINDEX] = False
    $aAMMUNTION_BMP_BUFF[$nINDEX] = 0
    $aAMMUNTION_GRAPHIC[$nINDEX] = -1
    $aAMMUNTION_C_X[$nINDEX] = ''
    $aAMMUNTION_C_Y[$nINDEX] = ''
    $aAMMUNTION_D_X[$nINDEX] = ''
    $aAMMUNTION_D_Y[$nINDEX] = ''
    $aAMMUNTION_S_X[$nINDEX] = ''
    $aAMMUNTION_S_Y[$nINDEX] = ''
EndFunc


Func move_ammunition ()
    Local $nINDEX, $nX,$nY,$nM,$nB
    For $nINDEX = 0 To $nAMMUNITION_MIN
        If $aAMMUNTION_STATUS[$nINDEX] = True Then
            $nX = $aAMMUNTION_C_X[$nINDEX]
            $nY = $aAMMUNTION_C_Y[$nINDEX]

; FAILED attempt at         y=MX+B


;~          $nM=($aAMMUNTION_S_Y[$nINDEX]-$aAMMUNTION_D_Y[$nINDEX]) / _
;~              ($aAMMUNTION_S_X[$nINDEX]-$aAMMUNTION_D_X[$nINDEX])


;~          $nB=$aAMMUNTION_S_Y[$nINDEX]-$nM*$aAMMUNTION_S_X[$nINDEX]


;~          $nY = $nM*$nX+$nB




            If $aAMMUNTION_D_X[$nINDEX] > $aAMMUNTION_S_X[$nINDEX] Then
                $nX += $nBLOCK_SIZE
            ElseIf $aAMMUNTION_D_X[$nINDEX] < $aAMMUNTION_S_X[$nINDEX] Then
                $nX -= $nBLOCK_SIZE
            Else
                $nX = $aAMMUNTION_C_X[$nINDEX]
            EndIf
            If $aAMMUNTION_D_Y[$nINDEX] > $aAMMUNTION_S_Y[$nINDEX] Then
                $nY += $nBLOCK_SIZE
            ElseIf $aAMMUNTION_D_Y[$nINDEX] < $aAMMUNTION_S_Y[$nINDEX] Then
                $nY -= $nBLOCK_SIZE
            Else
                $nY = $aAMMUNTION_C_Y[$nINDEX]
            EndIf


            Switch check_for_ammunition_blocking ($nINDEX,$nX,$nY)
                Case -1
                    _GDIPlus_GraphicsFillRect ($hENVIRO_GRAPHIC,$nX,$nY,$nBLOCK_SIZE,$nBLOCK_SIZE,$hENVIRO_BACKROUND_BRUSH)
                    delete_ammunition ($nINDEX)
                Case 0
                    $aAMMUNTION_C_X[$nINDEX] = $nX
                    $aAMMUNTION_C_Y[$nINDEX] = $nY
                Case 1
                    _GDIPlus_GraphicsFillRect ($hENVIRO_GRAPHIC,$nX,$nY,$nBLOCK_SIZE,$nBLOCK_SIZE,$hENVIRO_BACKROUND_BRUSH)
                    delete_ammunition ($nINDEX)
            EndSwitch
        EndIf
    Next
EndFunc


Func draw_ammunition ($nINDEX)
    _GDIPlus_GraphicsClear ($aAMMUNTION_GRAPHIC[$nINDEX],'0x00000000')
    _GDIPlus_GraphicsFillRect ($aAMMUNTION_GRAPHIC[$nINDEX],$aAMMUNTION_C_X[$nINDEX],$aAMMUNTION_C_Y[$nINDEX],$nBLOCK_SIZE,$nBLOCK_SIZE)
EndFunc
Func FIRE ($nX,$nY)
    Local $nINDEX
    For $nINDEX = 0 To $nAMMUNITION_MIN
        If $aAMMUNTION_STATUS[$nINDEX] = False Then
            $aAMMUNTION_STATUS[$nINDEX] = True
            $aAMMUNTION_BMP_BUFF[$nINDEX] =_GDIPlus_BitmapCreateFromGraphics($nGUIW, $nGUIH, $hGUI_GRAPHIC)
            $aAMMUNTION_GRAPHIC[$nINDEX] =  _GDIPlus_ImageGetGraphicsContext($aAMMUNTION_BMP_BUFF[$nINDEX])
            $aAMMUNTION_C_X[$nINDEX] = $nPLAYER_X
            $aAMMUNTION_C_Y[$nINDEX] = $nPLAYER_Y
            $aAMMUNTION_D_X[$nINDEX] = $nX
            $aAMMUNTION_D_Y[$nINDEX] = $nY
            $aAMMUNTION_S_X[$nINDEX] = $nPLAYER_X
            $aAMMUNTION_S_Y[$nINDEX] = $nPLAYER_Y
            Return
        EndIf
    Next
EndFunc





Func draw_grid ()
    Local $nX, $nY
    For $nX = 0 To $nGUIW Step $nBLOCK_SIZE
        For $nY = 0 To $nGUIH Step $nBLOCK_SIZE
            _GDIPlus_GraphicsFillRect ($hENVIRO_GRAPHIC,$nX,$nY,$nBLOCK_SIZE,$nBLOCK_SIZE,$hENVIRO_BACKROUND_BRUSH)
        Next
    Next
EndFunc
Func draw_map ()
    Local $nX, $nY, $nH
    For $nX = 0 To $nGUIW Step $nBLOCK_SIZE
        $nH = 5*$nBLOCK_SIZE
        $nH = Random (5,6,1)*$nBLOCK_SIZE
        For $nY = $nGUIH-$nH To $nGUIH Step $nBLOCK_SIZE
            _GDIPlus_GraphicsFillRect ($hENVIRO_GRAPHIC,$nX,$nY,$nBLOCK_SIZE,$nBLOCK_SIZE,$hENVIRO_BRUSH)
        Next
    Next
EndFunc
Func draw_player_init ()
    Local $nX,$nY
    $nX = $nBLOCK_SIZE*2
    $nY = $nBLOCK_SIZE*2
    _GDIPlus_GraphicsFillRect ($hPLAYER_GRAPHIC,$nX,$nY,$nBLOCK_SIZE,$nBLOCK_SIZE*2,$hPLAYER_BRUSH)
    $nPLAYER_X = $nX
    $nPLAYER_Y = $nY
EndFunc
Func draw_player ()
    _GDIPlus_GraphicsClear ($hPLAYER_GRAPHIC,'0x00000000')
    _GDIPlus_GraphicsFillRect ($hPLAYER_GRAPHIC,$nPLAYER_X,$nPLAYER_Y,$nBLOCK_SIZE,$nBLOCK_SIZE*2,$hPLAYER_BRUSH)
EndFunc



Func check_for_ammunition_blocking ($nINDEX,$nX,$nY)
    If $nX > $nGUIW Then
        Return -1
    EndIf
    If $nY > $nGUIH Then
        Return -1
    EndIf
    If $nX < 0 Then
        Return -1
    EndIf
    If $nY < 0 Then
        Return -1
    EndIf
    Local $sCOLOR =  Hex (PixelGetColor ($nX,$nY,$hHWND),6)
    If $sCOLOR = $sENVIRO_COLOR Then
        Return 1
    EndIf
    Return 0
EndFunc


Func check_for_player_blocking ($nINDEX,$nX,$nY)
    If $nX >= $nGUIW-$nBLOCK_SIZE Then
        Return True
    EndIf
    If $nY >= $nGUIH-$nBLOCK_SIZE Then
        $bJUMPING = False
        Return True
    EndIf
    If $nX <= 0 Then
        Return True
    EndIf
    If $nY <= 0 Then
        Return True
    EndIf
    Local $sCOLOR =  Hex (PixelGetColor ($nX,$nY+($nBLOCK_SIZE),$hHWND),6)
    If $sCOLOR = $sENVIRO_COLOR Then
        $bJUMPING = False
        Return True
    EndIf
    Return False
EndFunc



Func move_player ($nINDEX)
    Local $nX = $nPLAYER_X
    Local $nY = $nPLAYER_Y
    Switch $nINDEX
        Case 0
            If $bJUMPING = False Then
                $bJUMPING = True
                $nY -= $nBLOCK_SIZE*3
            EndIf
        Case 1
            $nX += $nBLOCK_SIZE
        Case 2
            $nY += $nBLOCK_SIZE
        Case 3
            $nX -= $nBLOCK_SIZE
    EndSwitch
    If check_for_player_blocking ($nINDEX,$nX,$nY) = True Then
        Return
    EndIf
    $nPLAYER_X = $nX
    $nPLAYER_Y = $nY
EndFunc









Func PHYSICS ()
    move_player (2)
    move_ammunition ()
    If $bBUFFERING = False Then
        $bBUFFERING = True
        draw_player ()
        For $nINDEX = 0 To $nAMMUNITION_MIN
            If $aAMMUNTION_STATUS[$nINDEX] = True Then
                draw_ammunition ($nINDEX)
            EndIf
        Next
        $bBUFFERING = False
    EndIf
EndFunc

Func CHECK_MOVEMENT ()
    Local $nINDEX
    For $nINDEX = 0 To 3
        If _IsPressed ($aPLAYER_KEYS[$nINDEX]) Then
            move_player($nINDEX)
            Return
        EndIf
    Next
EndFunc

Func CLICK ()
    If $bSHOOTING = False Then
        $bSHOOTING = True
        Local $nX = _WinAPI_GetMousePosX(True,$hHWND)
        Local $nY = _WinAPI_GetMousePosY(True,$hHWND)
        FIRE ($nX,$nY)
        $bSHOOTING = False
    EndIf
EndFunc


Func BUFFER ()
    If $bBUFFERING = False Then
        $bBUFFERING = True
        _GDIPlus_GraphicsDrawImage($hBACK_BUFFER_GRAPHIC, $hENVIRO_BMP_BUFF, 0, 0)
        _GDIPlus_GraphicsDrawImage($hBACK_BUFFER_GRAPHIC, $hPLAYER_BMP_BUFF, 0, 0)
        _GDIPlus_GraphicsDrawImage($hBACK_BUFFER_GRAPHIC, $hGRID_BMP_BUFF, 0, 0)

        For $nINDEX = 0 To $nAMMUNITION_MIN
            If $aAMMUNTION_STATUS[$nINDEX] = True Then
                _GDIPlus_GraphicsDrawImage($hBACK_BUFFER_GRAPHIC, $aAMMUNTION_BMP_BUFF[$nINDEX], 0, 0)
            EndIf
        Next

        _GDIPlus_GraphicsDrawImage($hGUI_GRAPHIC, $hBACK_BUFFER_BMP_BUFF, 0, 0)
        $bBUFFERING = False
    EndIf
EndFunc

Func MY_PAINT($hWnd, $msg, $wParam, $lParam)
    _WinAPI_RedrawWindow($hWnd,Default,Default, BitOR($RDW_INVALIDATE, $RDW_UPDATENOW, $RDW_FRAME)); , $RDW_ALLCHILDREN
    Return $GUI_RUNDEFMSG
EndFunc

any help would be greatly appreciated :)

Link to comment
Share on other sites

bullet fired in straight line. where the mouse is pointed and past the clicked x/y pos.

EDIT

UEZ, your example was perfect, i changed gravity to 0 and it does exactly what i want, however i don't know how it does it, or how to incorporate that into my script. May i ask how you did it mathematically? :)

Well, it is not easy to explain it but I will try it in a short way.

For the movement of the ball I used vectors (dx and dy) and some trigonometric functions for calculating the angles.

I would advise to have a closer look to these mathematical areas to get more detailed knowledge.

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

bullet fired in straight line. where the mouse is pointed and past the clicked x/y pos.

EDIT

UEZ, your example was perfect, i changed gravity to 0 and it does exactly what i want, however i don't know how it does it, or how to incorporate that into my script. May i ask how you did it mathematically? :)

Well, it is not easy to explain it but I will try it in a short way.

For the movement of the ball I used vectors (dx and dy) and some trigonometric functions for calculating the angles.

To calculate the path from the object to the coordinate of the mouse you can use Pythagoras' theorem -> c²=a²+b² whereas c is the path and you need to calculate the slope of c.

I used these theorem in e.g. in Pixel Text Effect to calcuate the path for morphing the pixels from one text to another.

I would advise to have a closer look to these mathematical areas to get more detailed knowledge.

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Hi,

Here's a crude example of just firing 1 bullet from the player through the mouse point to the edge of the gui.

Sorry I didn't try modifying or fit it into your code as I needed to wrap my head around the basic math (which I'm not good at..lol)

#include <GDIPlus.au3>
#include <WinAPI.au3>
#include <GuiConstants.au3>
#include <WindowsConstants.au3>
#include <Misc.au3>

Opt("MustDeclareVars", 1)
Opt("GUIOnEventMode", 1)

;Pi for working angles out
Global Const $nPI = 4 * ATan(1)
Global Const $nPI_180 = $nPI / 180

;Gui variables
Global $hGui, $nGuiW = 800, $nGuiH = 600

;GDIP variables
Global $hGui_Gr, $hBuffer, $hBuffer_Gr, $hPlayer_Br, $hBullet_Br

;Bullet variables
Global $aClick, $aBullet, $iBulletSize = 10, $iBulletSpeed = 10

;Player variables
Global $aPlayer[2], $iPlayerW = 10, $iPlayerH = 20, $iPlayerSpeed = 5, $aPlayer_Keys[4] = ['26', '27', '28', '25']

; start point for my player
$aPlayer[0] = $nGuiW / 2 - $iPlayerW / 2
$aPlayer[1] = $nGuiH / 2 - $iPlayerH / 2


$hGui = GUICreate('', $nGuiW, $nGuiH)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
GUISetState(@SW_SHOW, $hGui)

GUIRegisterMsg($WM_LBUTTONDOWN, '_CLICK')

_StartUp()

While 1
    Sleep(10)
WEnd

Func _StartUp()
    _GDIPlus_Startup()
    $hGui_Gr = _GDIPlus_GraphicsCreateFromHWND($hGui)
    $hBuffer = _GDIPlus_BitmapCreateFromGraphics($nGuiW, $nGuiH, $hGui_Gr)
    $hBuffer_Gr = _GDIPlus_ImageGetGraphicsContext($hBuffer)
    $hPlayer_Br = _GDIPlus_BrushCreateSolid(0xFF0000FF)
    $hBullet_Br = _GDIPlus_BrushCreateSolid(0xFFFF0000)
    AdlibRegister("_Draw", 10)
EndFunc   ;==>_StartUp


Func _Draw()
    ; Clear the back buffer and redraw everything
    _GDIPlus_GraphicsClear($hBuffer_Gr)

    ;If there's been a click then set up a bullet.
    If IsArray($aClick) Then

        ;Dim an array for our single bullet
        Dim $aBullet[2][2]

        ;get the angle of the click
        Local $iAngle = _GetAngleDegree($aPlayer[0], $aPlayer[1], $aClick[0] - ($iBulletSize / 2), $aClick[1] - ($iBulletSize / 2))

        ;set an X and Y step that will follow our angle
        $aBullet[0][0] = Cos(($iAngle) * $nPI_180) * $iBulletSpeed ;X bullet angle/speed
        $aBullet[0][1] = Sin(($iAngle) * $nPI_180) * $iBulletSpeed ;Y bullet angle/speed

        ;save the start X and Y point of our bullet
        $aBullet[1][0] = $aPlayer[0] ;X Bullet start
        $aBullet[1][1] = $aPlayer[1] ;Y Bullet start

        ;Clear the click array as we don't want it calleed again until another click msg
        $aClick = 0
    EndIf

    ;if there's a bullet array then procees it
    If IsArray($aBullet) Then

        ;Draw a bullet at the start X and Y on the back buffer
        _GDIPlus_GraphicsFillEllipse($hBuffer_Gr, $aBullet[1][0], $aBullet[1][1], $iBulletSize, $iBulletSize, $hBullet_Br)

        ;Step the start X and Y along our angle
        $aBullet[1][0] += $aBullet[0][0]
        $aBullet[1][1] -= $aBullet[0][1]

        ;Crude check to see if the bullet has hit the edge of the gui, if so then no need to draw it any further, kill the bullet array
        If ($aBullet[1][0] > $nGuiW) Or ($aBullet[1][0] < -$iBulletSize) Or ($aBullet[1][1] > $nGuiH) Or ($aBullet[1][1] < -$iBulletSize) Then $aBullet = 0

    EndIf

    ;Just checking for directional press to move player
    For $i = 0 To UBound($aPlayer_Keys) - 1
        If _IsPressed($aPlayer_Keys[$i]) Then
            Switch $i
                Case 0 ;Up
                    If $aPlayer[1] > 0 Then $aPlayer[1] -= $iPlayerSpeed
                Case 1 ;Right
                    If $aPlayer[0] < ($nGuiW - $iPlayerW) Then $aPlayer[0] += $iPlayerSpeed
                Case 2 ;Down
                    If $aPlayer[1] < ($nGuiH - $iPlayerH) Then $aPlayer[1] += $iPlayerSpeed
                Case 3 ;Left
                    If $aPlayer[0] > 0 Then $aPlayer[0] -= $iPlayerSpeed
            EndSwitch
        EndIf
    Next

    ;Draw the player on the back buffer.
    _GDIPlus_GraphicsFillRect($hBuffer_Gr, $aPlayer[0], $aPlayer[1], $iPlayerW, $iPlayerH, $hPlayer_Br)

    ;Drawthe back buffer to the screen
    _GDIPlus_GraphicsDrawImage($hGui_Gr, $hBuffer, 0, 0)

EndFunc   ;==>_Draw

; crude function to get the angle in degrees of a line based on X1, Y1, X2, Y2
; there's probably some nice and simple way for this but I'm hopeless at math..lol
Func _GetAngleDegree($X1, $Y1, $X2, $Y2)
    Local Const $nPI = 4 * ATan(1)
    Local $XDIFF, $YDIFF, $TempAngle
    $YDIFF = Abs($Y2 - $Y1)
    If $X1 = $X2 And $Y1 = $Y2 Then Return 0
    If $YDIFF = 0 And $X1 < $X2 Then
        Return 0
    ElseIf $YDIFF = 0 And $X1 > $X2 Then
        Return $nPI
    EndIf
    $XDIFF = Abs($X2 - $X1)
    $TempAngle = ATan($XDIFF / $YDIFF)
    If $Y2 > $Y1 Then $TempAngle = $nPI - $TempAngle
    If $X2 < $X1 Then $TempAngle = -$TempAngle
    $TempAngle = ($nPI / 2) - $TempAngle
    If $TempAngle < 0 Then $TempAngle = ($nPI * 2) + $TempAngle
    Return $TempAngle * 180 / $nPI
EndFunc   ;==>_GetAngleDegree

Func _CLICK()
    Dim $aClick[2]
    $aClick[0] = _WinAPI_GetMousePosX(True, $hGui)
    $aClick[1] = _WinAPI_GetMousePosY(True, $hGui)
EndFunc   ;==>_CLICK

Func _Exit()
    AdlibUnRegister("_Draw")
    _GDIPlus_BrushDispose($hBullet_Br)
    _GDIPlus_BrushDispose($hPlayer_Br)
    _GDIPlus_BitmapDispose($hBuffer)
    _GDIPlus_GraphicsDispose($hBuffer_Gr)
    _GDIPlus_GraphicsDispose($hGui_Gr)
    _GDIPlus_Shutdown()
    Exit
EndFunc   ;==>_Exit

Cheers

Edited by smashly
Link to comment
Share on other sites

@smashly: nice example :)

Here another example I found to calculate the angle:

;Coded by UEZ Build 2010-06-21
#include <GDIPlus.au3>
Opt("GUIOnEventMode", 1)
Opt("MouseCoordMode", 2)
$width = 600
$height = $width
$w05 = $width / 2
$h05 = $height / 2
$180_div_pi = 180 / ACos(-1)
Local $mx = $w05, $my = $h05
$r = 100
$r2 = $r / 2
$angle = 0
$hGUI = GUICreate("GDI+ Trigonometrie by UEZ 2010", $width, $height)
GUISetState()

_GDIPlus_Startup()
$hGraphics = _GDIPlus_GraphicsCreateFromHWND($hGUI)
$hBitmap = _GDIPlus_BitmapCreateFromGraphics($width, $height, $hGraphics)
$hBuffer = _GDIPlus_ImageGetGraphicsContext($hBitmap)
_GDIPlus_GraphicsSetSmoothingMode($hBuffer, 2)
_GDIPlus_GraphicsClear($hBuffer, 0xFFF0F0F0)
$hPen1 = _GDIPlus_PenCreate(0xFF0000A0, 2)
$hPen2 = _GDIPlus_PenCreate(0xA0000000, 1)
$hBrush1 = _GDIPlus_BrushCreateSolid(0x8000A000)
$hBrush2 = _GDIPlus_BrushCreateSolid(0x80A00000)

GUISetOnEvent(-3, "_Exit")

While Sleep(30)
    _GDIPlus_GraphicsClear($hBuffer, 0xA0F0F0F0)
    _GDIPlus_GraphicsDrawLine($hBuffer, 0, $h05, $width, $h05, $hPen2)
    _GDIPlus_GraphicsDrawLine($hBuffer, $w05, 0, $w05, $height, $hPen2)

    _GDIPlus_GraphicsDrawString($hBuffer, "X", $width - 15, $h05)
    _GDIPlus_GraphicsDrawString($hBuffer, "Y", $w05 - 15, 0)
    _GDIPlus_GraphicsDrawString($hBuffer, "-X", 2, $h05)
    _GDIPlus_GraphicsDrawString($hBuffer, "-Y", $w05 - 20, $height - 17)

    $mpos = MouseGetPos()
    $mwx = $mpos[0] - $w05
    $mwy = $h05 - $mpos[1]
    ConsoleWrite($mwx & "," & $mwy & @CRLF)
    $pd = Pixel_Distance($w05, $h05, $mpos[0], $mpos[1])
    If $pd > 50 And $pd < $w05 - 10 Then
        $angle = -ATan($mwy / $mwx) * $180_div_pi
        If $mwx < 0 Then
            $angle = -180 + $angle
        ElseIf $mwx >= 0 And $mwy < 0 Then
            $angle = -360 + $angle
        EndIf
        _GDIPlus_GraphicsDrawString($hBuffer, StringFormat("%.2f", Abs($angle)) & "°", $mpos[0], $h05)
        _GDIPlus_GraphicsFillPie($hBuffer, $w05 - $r2, $h05 - $r2, $r, $r, 0, $angle, $hBrush1)
;~         _GDIPlus_GraphicsFillPie($hBuffer, $w05 - $r2, $h05 - $r2, $r, $r, -180, $angle, $hBrush2)
        _GDIPlus_GraphicsDrawLine($hBuffer, $width - $mpos[0], $height - $mpos[1], $mpos[0], $mpos[1], $hPen1)
        $mx = $mpos[0]
        $my = $mpos[1]
    Else
        _GDIPlus_GraphicsDrawString($hBuffer, StringFormat("%.2f", Abs($angle)) & "°", $mx, $h05)
        _GDIPlus_GraphicsFillPie($hBuffer, $w05 - $r2, $h05 - $r2, $r, $r, 0, $angle, $hBrush1)
;~         _GDIPlus_GraphicsFillPie($hBuffer, $w05 - $r2, $h05 - $r2, $r, $r, -180, $angle, $hBrush2)
        _GDIPlus_GraphicsDrawLine($hBuffer, $width - $mx, $height - $my, $mx, $my, $hPen1)
    EndIf
    _GDIPlus_GraphicsDrawImage($hGraphics, $hBitmap, 0, 0)
WEnd

Func Pixel_Distance($x1, $y1, $x2, $y2) ;Pythagoras theorem
    Local $a, $b, $c
    If $x2 = $x1 And $y2 = $y1 Then
        Return 0
    Else
        $a = $y2 - $y1
        $b = $x2 - $x1
        $c = Sqrt($a * $a + $b * $b)
        Return $c
    EndIf
EndFunc   ;==>Pixel_Distance

Func _Exit()
    _GDIPlus_BrushDispose($hBrush1)
    _GDIPlus_BrushDispose($hBrush2)
    _GDIPlus_PenDispose($hPen1)
    _GDIPlus_PenDispose($hPen2)
    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_GraphicsDispose($hBuffer)
    _GDIPlus_Shutdown()
    GUIDelete($hGUI)
    Exit
EndFunc

Might be usefull for somebody...

Further you can have a look to AUTOITEROIDS where the starship shoots also in any direction.

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Hi UEZ,

I wondered how to use your Pixel_Distance() function in this instance, now i know.. Thank You :)

Even looking through your AUTOITEROIDS (which I keep handy all the time, many useful math examples in your work of art:) ), I still had problems trying to work it all out.

I can't even work out how to use anything in maths much more then the basic +, -, *, / ...lol.

Add Acos, Atan, Tan etc into any math equations and I start scratching my head.

Cheers

Link to comment
Share on other sites

NICE smashly! thanks for the comments!

UEZ, heh... simple though your examples may be, the math is needed to be explained first before your examples are understood.

MOAR RESEARCH! i might actually have to stay for math tutoring one day to ask my old teacher this question.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...