Jump to content

_GDIplus game


Phillip
 Share

Recommended Posts

Hello all,

Ive been dicking around with GDIplus functions lately, and right now im just testing some things out with it. Ive got images being generated on the screen, and a "player" whos also being generated on the screen, right in the middle. Ive coded most of it, but it feels very choppy. Can someone have a look at my code to see if im missing anything/ doing anything wrong:

#include <ColorConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Misc.au3>
#include <Array.au3>

#include <GDIPlus.au3>

Opt("MouseCoordMode", 2)
Opt("PixelCoordMode", 2)

HotKeySet("{esc}", "esc")
;HotKeySet("{1}", "test")


Func esc()
    Exit
    _GDIPlus_Shutdown()
EndFunc   ;==>esc



$gui = GUICreate("map", 1600, 1000)


GUISetState()


_GDIPlus_Startup()


$gfx = _GDIPlus_GraphicsCreateFromHWND($gui)
$Bitmap = _GDIPlus_BitmapCreateFromGraphics(600, 400, $gfx) ;create bitmap
$buffer = _GDIPlus_ImageGetGraphicsContext($Bitmap) ;create buffer



$grass = _GDIPlus_ImageLoadFromFile("C:\users\phill\desktop\survivor\pics\terrain\grass.jpg")
$player = _GDIPlus_ImageLoadFromFile("C:\users\phill\desktop\survivor\pics\terrain\player.jpg")
$water = _GDIPlus_ImageLoadFromFile("C:\users\phill\desktop\survivor\pics\terrain\water.jpg")

;Next
;$y += 1
;wend

$count = 0

$p = 1

$p1 = 2
$p2 = 10

$p3 = 5
$p4 = 8

draw()
func draw()
   _GDIPlus_GraphicsClear($buffer, 0x7F000000) ;clear buffer


$y = 0
while $y <> 40
for $i = 0 to 60


_GDIPlus_GraphicsDrawImage($buffer, $grass,  $i * 10, $y * 10) ;draws grass


    if $i >= $p1 + 15 and $i <= $p2 + 15 and $y >= $p3 + 15 and $y <= $p4 + 15 then
        _GDIPlus_GraphicsDrawImage($buffer, $water,  $i * 10, $y * 10) ;draws water
    endif



    if $i = 30 and $y = 20 Then ; player

        _GDIPlus_GraphicsDrawImage($buffer, $player,  $i * 10, $y * 10) ;draws the player

        endif




Next
$y += 1
wend


_GDIPlus_GraphicsDrawImageRect($gfx, $Bitmap, 500, 300, 600, 400) ;copy to bitmap

endfunc








While 1

Select

        case _IsPressed(41) and _IsPressed(57) ;left and up
        $p1 += 1
        $p2 += 1
        $p3 += 1
        $p4 += 1
        draw()

        case _IsPressed(44) and _IsPressed(57) ;right and up
        $p1 -= 1
        $p2 -= 1
        $p3 += 1
        $p4 += 1
        draw()

        case _IsPressed(41) and _IsPressed(53) ;left and down
        $p1 += 1
        $p2 += 1
        $p3 -= 1
        $p4 -= 1
        draw()

            case _IsPressed(44) and _IsPressed(53) ;right and down
        $p1 -= 1
        $p2 -= 1
        $p3 -= 1
        $p4 -= 1
        draw()



    case _IsPressed(41) ;left
        $p1 += 1
        $p2 += 1
        $p3 += 0
        $p4 += 0
        draw()

    case _IsPressed(44) ;right
        $p1 -= 1
        $p2 -= 1
        $p3 -= 0
        $p4 -= 0
        draw()

        case _IsPressed(53) ; down
        $p1 += 0
        $p2 += 0
        $p3 -= 1
        $p4 -= 1
        draw()

    case _IsPressed(57) ;up
        $p1 -= 0
        $p2 -= 0
        $p3 += 1
        $p4 += 1
        draw()




EndSelect
;draw()

WEnd

As you can see, you can use the WASD keys to "move around". Ive also uploaded the images for you to use.

I feel like it can be a lot smoother, i just have no idea how to do it.

Thanks for any help at all!

grass.jpg

player.jpg

water.jpg

Link to comment
Share on other sites

The WM_KEYDOWN message would help and make things a bit easier. Also, you're exiting before shutting down GDIPlus and you're not disposing of your GDI variables before exiting, your math on the player coordinates is inverted, and WASD actually moves the water, not the player (:P). This might give you an idea on where to go

<snip>

When you start adding collision to your map you could check the coordinate first, if the space is valid then do the math, otherwise not.

If you wanted to get really detailed on your map, instead of clearing the graphic every time you could check the tile the player is standing on before moving, draw the player in the new tile, then overwrite the old player spot with the correct tile. (I.e., if the player is standing at 10, 10 and moves to the right, at x0, y0, width10, height10 is supposed to be a grass tile, draw the player at 20, 10 then draw a grass at 0, 0). Just keep an array of the map and what each tile is supposed to be and add the row/column of the tile standing on and moving to. This way you're not redrawing every single time and your draw function just got cut down to 1/3 the size it was before

Func draw(Const $iOldRow, Const $iOldColumn, Const $iNewRow, Const $iNewColumn)
    ; Get the tile to draw at the $iOldRow and $iOldColumn
    _GDIPlus_GraphicsDrawImage($buffer, $player, $iNewRow * 10, $iNewColumn * 10)
    _GDIPlus_GraphicsDrawImage($buffer, $aMap[$iOldRow][$iOldColumn], $iOldRow * 10, $iOldColumn * 10)
    _GDIPlus_GraphicsDrawImageRect($gfx, $Bitmap, 500, 300, 600, 400) ;copy to bitmap
EndFunc

 

Edited by Melba23
Removed code
Link to comment
Share on other sites

Hey man,

Firstly, thanks for the response. The player not moving is intended, he is suppose to be in the center of the map, with the map moving around him as you hit the movement keys.

Im planning on generating the map through an array, but with the player being static in the middle, i dont know if i can do it another way other than redrawing the entire area everytime you move.

Also, i tried implementing your WM_KEYDOWN message function, but that just made it feel more clunky and slower.

Furthermore: Thanks for pointing out my error with disposing the images and the GDIshutdown, completely missed that haha.

Edited by Phillip
Link to comment
Share on other sites

Ah I see, if that's your plan then you would probably need to redraw it every time (Unless you wanted to get really detailed, create a complete graphics image of the whole map first, store it in memory and don't touch it! Draw the map, adjusting the x/y coordinates of where to start drawing, and then draw the player on top. Sounds complicated and there's probably an easier and more efficient way of doing it, just have to get the GDIPlus gurus to give a better answer lol)

Link to comment
Share on other sites

  • Moderators

InunoTaishou,

What you posted was essentially a keylogger and has been removed - please read this announcement to understand the limits on what is permitted here.

As I fully realise that you were not intending to post a keylogger I will take no further action. Have a good Xmas.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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...