Sign in to follow this  
Followers 0
newbiescripter

Racing Game in AutoIt

3 posts in this topic

#1 ·  Posted (edited)

Hello

I was trying to make a little Race game in AutoIt. It's very basic and doesn't works so well, so now I post it in hope that some of you might have some keywords to help me muttley

; AutoIt Race Game
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <Misc.au3>

Opt("GUIOnEventMode", 1)

$width = 600
$height = 400
$title = "AutoIt Racing Game"
GUICreate($title,$width,$height)
$startButton = GUICtrlCreateButton("Start/Stop",0,0)
$track = GUICtrlCreateGraphic(0,0,$width,$height)

$outsideTrackColor = 0x00FF00
$insideTrackColor = 0x848284
GUICtrlSetGraphic($track,$GUI_GR_COLOR,$outsideTrackColor,$insideTrackColor) ; color of the field is defined as the draw color
GUICtrlSetBkColor($track,$insideTrackColor) ; color of the track is defined as the BK color

; --------- The code below draw the track: -------------
$x0 = $width / 3
$y0 = $height / 2

; draws outer cirkel
$radius1 = 150

For $x = 0 To $x0 Step +1
    For $y = 0 To $height step +1
        $r = Sqrt((($x-$x0)^2 + ($y-$y0)^2))
        If $r >= $radius1 Then
            GUICtrlSetGraphic($track,$GUI_GR_PIXEL,$x,$y)
        EndIf
    Next
Next

For $x = $x0 To $x0*2 Step +1
    For $y = 0 To $height Step +1
        If ($y <= ($y0-$radius1) Or $y >= ($y0+$radius1)) Then
            GUICtrlSetGraphic($track,$GUI_GR_PIXEL,$x,$y)
        EndIf
    Next
Next

For $x = $x0*2 To $x0*3 Step +1
    For $y = 0 To $height step +1
        $r = Sqrt((($x-($x0*2))^2 + ($y-$y0)^2))
        If $r >= $radius1 Then
            GUICtrlSetGraphic($track,$GUI_GR_PIXEL,$x,$y)
        EndIf
    Next
Next

; draws inner cirkel:
$radius2 = 70

For $x = 0 To $x0 Step +1
    For $y = 0 To $height step +1
        $r = Sqrt((($x-$x0)^2 + ($y-$y0)^2))
        If $r <= $radius2 Then
            GUICtrlSetGraphic($track,$GUI_GR_PIXEL,$x,$y)
        EndIf
    Next
Next

For $x = $x0 To $x0*2 Step +1
    For $y = 0 To $height Step +1
        If ($y >= ($y0-$radius2) And $y <= ($y0+$radius2)) Then
            GUICtrlSetGraphic($track,$GUI_GR_PIXEL,$x,$y)
        EndIf
    Next
Next

For $x = $x0*2 To $x0*3 Step +1
    For $y = 0 To $height step +1
        $r = Sqrt((($x-($x0*2))^2 + ($y-$y0)^2))
        If $r <= $radius2 Then
            GUICtrlSetGraphic($track,$GUI_GR_PIXEL,$x,$y)
        EndIf
    Next
Next

Global $carX = ($width/2), $carY = ($height/2-$radius2-($radius1-$radius2)/2), $carSize = 10, $carSpeed = 10, $running

$car = GUICtrlCreateGraphic(0,0,$width,$height)
GUICtrlSetGraphic($car, $GUI_GR_COLOR, 0x000000, 0x000000)
GUICtrlSetGraphic($car, $GUI_GR_ELLIPSE, $carX, $carY, $carSize, $carSize)

; --------------- Drawing finally ends here :) ------------------


GUISetOnEvent($GUI_EVENT_CLOSE,"terminate")
GUICtrlSetOnEvent($startButton,"start")

GUISetState()

$dll = DllOpen("user32.dll")
Do
    Sleep(25)
    
    While $running
        If _IsPressed("25",$dll) Then
            $carX = $carX - $carSpeed
            GUICtrlSetGraphic($car, $GUI_GR_ELLIPSE, $carX,$carY, $carSize, $carSize)
            GUICtrlSetGraphic($car,$GUI_GR_REFRESH)
        EndIf
        If _IsPressed("27",$dll) Then
            $carX = $carX + $carSpeed
            GUICtrlSetGraphic($car, $GUI_GR_ELLIPSE, $carX,$carY, $carSize, $carSize)
            GUICtrlSetGraphic($car,$GUI_GR_REFRESH)
        EndIf
        If _IsPressed("26",$dll) Then
            $carY = $carY - $carSpeed
            GUICtrlSetGraphic($car, $GUI_GR_ELLIPSE, $carX,$carY, $carSize, $carSize)
            GUICtrlSetGraphic($car,$GUI_GR_REFRESH)
        EndIf
        If _IsPressed("28",$dll) Then
            $carY = $carY + $carSpeed
            GUICtrlSetGraphic($car, $GUI_GR_ELLIPSE, $carX,$carY, $carSize, $carSize)
            GUICtrlSetGraphic($car,$GUI_GR_REFRESH)
        EndIf
        Sleep(25)
    WEnd
    
Until False
        
Func start()
    $running = Not $running
EndFunc

Func terminate()
    Exit
EndFunc

As you can see when you run it. it doesn't work so well because it's also refreshing the track which is very big..

hmm.. I've tried to use GUICtrlSetPos, but it seems that it doesn't work with graphic since I still need to refresh before seeing that it has been moved.

then I tried to create another cirkel object (the car) with GDI+ ellipse, but it didn't work either..

I know that I could load an image as the background and use an icon or something as the car, but I was trying to make it all in AutoIt. :)

Maybe some of you have some nice ideas that I can work on with..

Regards

Edited by newbiescripter

Share this post


Link to post
Share on other sites



Hi newbiescripter,

Have you had a chance to look into prospeed for autoit? It allows you to create sprites and move/refresh them quickly. Do a search in the forums to find it.

Share this post


Link to post
Share on other sites

Hi!

Drawing sprites in GDI+ that moves around on a background is fairly simple.

I made some sample code for you to study.

#include <GDIPlus.au3>
#include <WinAPI.au3>
#include <misc.au3>
; Create GUI..blabla
Opt("GUIOnEventMode",1)
$hwnd=GUICreate("Sample",300,300)
GUISetState()
GUISetOnEvent(-3,"close")

_GDIPlus_Startup()
$graphics=_GDIPlus_GraphicsCreateFromHWND($hwnd); This graphic object gives access to the drawing area of the GUI
$backgroundbitmap=_GDIPlus_BitmapCreateFromGraphics(300,300,$graphics); Create a bitmap in memory, this will be our background
$backgroundgraphics=_GDIPlus_ImageGetGraphicsContext($backgroundbitmap); We can access the background bitmap with this
$backbufferbitmap=_GDIPlus_BitmapCreateFromGraphics(300,300,$graphics); Create a bitmap in memory, this will be our temporary buffer
$backbuffergraphics=_GDIPlus_ImageGetGraphicsContext($backbufferbitmap); We can access the backbuffer with this

$whitebrush=_GDIPlus_BrushCreateSolid(0xFFFFFFFF)
$greenbrush=_GDIPlus_BrushCreateSolid(0xFF00FF00)
; Let's draw our background
_GDIPlus_GraphicsFillRect($backgroundgraphics,25,25,250,250,$whitebrush)

Dim $x,$y
Do
; Clear the backbuffer and draw the background on it
    _GDIPlus_GraphicsClear($backbuffergraphics,0xFF000000)
    _GDIPlus_GraphicsDrawImageRect($backbuffergraphics,$backgroundbitmap,0,0,300,300)
    
; move the dot
    If _IsPressed("25") Then
        $x-=3
    EndIf
    If _IsPressed("26") Then
        $y-=3
    EndIf
    If _IsPressed("27") Then
        $x+=3
    EndIf
    If _IsPressed("28") Then
        $y+=3
    EndIf
    
    
; Draw the action;)
    _GDIPlus_GraphicsFillEllipse($backbuffergraphics,$x,$y,10,10,$greenbrush)
    
    
; We're done drawing, let's show the user the result!
    _GDIPlus_GraphicsDrawImageRect($graphics,$backbufferbitmap,0,0,300,300)
    Sleep(20)
Until False






Func close()
    _GDIPlus_BrushDispose($whitebrush)
    _GDIPlus_BrushDispose($greenbrush)
    _GDIPlus_GraphicsDispose($graphics)
    _GDIPlus_GraphicsDispose($backbuffergraphics)
    _GDIPlus_GraphicsDispose($backgroundgraphics)
    _WinAPI_DeleteObject($backbufferbitmap)
    _WinAPI_DeleteObject($backgroundbitmap)
    _GDIPlus_Shutdown()
    Exit
EndFunc

Just ask if there is some part you don't understand.

muttley


Broken link? PM me and I'll send you the file!

Share this post


Link to post
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
Sign in to follow this  
Followers 0