Sign in to follow this  
Followers 0
idusy

Car Game

29 posts in this topic

#1 ·  Posted (edited)

I've been making a few games in AutoIt lately, figured I'd post one of my few fully working (Hopefully) ones.

Cooked this up yesterday, I'm fairly proud of myself. >.>

Update: Added up and down movement, and fixed a bug that would send you back to crash location if hot key was held down.

Update: Made your cars movement a little faster.

Update: Downloads sprites if they don't exist :lmao:

Update: Fixed score bug. Now shows 3 digit numbers.

Update: Additional speed ups at 50, 100, 150, and 200 points.

Update: Added pretty grass.

HotKeySet("a","Left")
HotKeySet("d","Right")
HotKeySet("w","Up")
HotKeySet("s","Down")
HotKeySet("{LEFT}","Left")
HotKeySet("{RIGHT}","Right")
HotKeySet("{UP}","Up")
HotKeySet("{DOWN}","Down")

Dim $Speed = 5
Dim $Rate = 100
Dim $Score = "0  "
Dim $Life = 3

If (Not FileExists("Car1.bmp")) Or (Not FileExists("Car2.bmp")) Then
    MsgBox(0,"Error","Car1.bmp and Car2.bmp were not found. Will attempt to download")
    InetGet("http://idusy0.googlepages.com/Car1.bmp",@ScriptDir & "\Car1.bmp")
    InetGet("http://idusy0.googlepages.com/Car2.bmp",@ScriptDir & "\Car2.bmp")
EndIf

$GUI=GUICreate("Speeding Very Fast With No Set Destination",625,450)
GUISetState()
GUISetBkColor(0x808080)
$Car1=GUICtrlCreatePic(@ScriptDir & "\Car1.bmp",300,300,50,102)
$Car2=GUICtrlCreatePic(@ScriptDir & "\Car2.bmp",300,-110,50,102)
$LableScore=GUICtrlCreateLabel("Score: " & $Score,475,25)
GUICtrlSetBkColor($LableScore,0x008800)
$LableLife=GUICtrlCreateLabel("Lives: " & $Life,475,60)
GUICtrlSetBkColor($LableLife,0x008800)
$Line1=GUICtrlCreateGraphic(315,0,20,60)
GUICtrlSetBkColor($Line1,16776960)
$Line2=GUICtrlCreateGraphic(315,100,20,60)
GUICtrlSetBkColor($Line2,16776960)
$Line3=GUICtrlCreateGraphic(315,200,20,60)
GUICtrlSetBkColor($Line3,16776960)
$Line4=GUICtrlCreateGraphic(315,300,20,60)
GUICtrlSetBkColor($Line4,16776960)
$Line5=GUICtrlCreateGraphic(315,400,20,60)
GUICtrlSetBkColor($Line5,16776960)
$LineLeft=GUICtrlCreateGraphic(185,0,40,500)
GUICtrlSetBkColor($LineLeft,0)
$LineRight=GUICtrlCreateGraphic(420,0,40,500)
GUICtrlSetBkColor($LineRight,0)
$grassleft=GUICtrlCreateGraphic(440,0,200,500)
GUICtrlSetBkColor($grassleft,0x008800)
$grassRight=GUICtrlCreateGraphic(0,0,205,500)
GUICtrlSetBkColor($grassRight,0x008800)

While 1

If GUIGetMsg() = -3 Then Exit

$Car1Pos=ControlGetPos($GUI,"",$Car1) ; [0] = X, [1] = Y, [2] = Width, [3] = Height
$Car2Pos=ControlGetPos($GUI,"",$Car2)
$Line1Pos=ControlGetPos($GUI,"",$Line1)
$Line2Pos=ControlGetPos($GUI,"",$Line2)
$Line3Pos=ControlGetPos($GUI,"",$Line3)
$Line4Pos=ControlGetPos($GUI,"",$Line4)
$Line5Pos=ControlGetPos($GUI,"",$Line5)
$LineLeftPos=ControlGetPos($GUI,"",$LineLeft)
$LineRightPos=ControlGetPos($GUI,"",$LineRight)

GUICtrlSetPos($Line1,$Line1Pos[0],$Line1Pos[1]+8)
GUICtrlSetPos($Line2,$Line2Pos[0],$Line2Pos[1]+8)
GUICtrlSetPos($Line3,$Line3Pos[0],$Line3Pos[1]+8)
GUICtrlSetPos($Line4,$Line4Pos[0],$Line4Pos[1]+8)
GUICtrlSetPos($Line5,$Line5Pos[0],$Line5Pos[1]+8)

If $Line1Pos[1] > 450 Then GUICtrlSetPos($Line1,$Line1Pos[0],-50)
If $Line2Pos[1] > 450 Then GUICtrlSetPos($Line2,$Line2Pos[0],-50)
If $Line3Pos[1] > 450 Then GUICtrlSetPos($Line3,$Line3Pos[0],-50)
If $Line4Pos[1] > 450 Then GUICtrlSetPos($Line4,$Line4Pos[0],-50)
If $Line5Pos[1] > 450 Then GUICtrlSetPos($Line5,$Line5Pos[0],-50)
    
GUICtrlSetPos($Car2,$Car2Pos[0],$Car2Pos[1]+$Speed)
Sleep($Rate)

If _IsCollide($Car1Pos[0],$Car1Pos[1],$Car1Pos[2],$Car1Pos[3],$Car2Pos[0],$Car2Pos[1],$Car2Pos[2],$Car2Pos[3])=1 Then
MsgBox(0,"","You Crashed!" & @CRLF & "Lost One Life",3)
GUICtrlSetPos($Car1,300,300)
GUICtrlSetPos($Car2,$Car2Pos[0],-110)
$Life-=1
GUICtrlSetData($LableLife,"Lives: " & $Life)
EndIf

If $Car2Pos[1] > 500 Then
$Score += 1
If $Rate >30 Then
$Rate -=15
Else
$Rate -=5
EndIf
If $Rate < 6 Then $Rate -= 1
If $Rate < 3 Then $Rate = 2
$RandPos=Random(210,390,1)
GUICtrlSetData($LableScore,"Score: " & $Score)
GUICtrlSetPos($Car2,$RandPos,-110)

If $Score = 50 Then $Speed=7
If $Score = 100 Then $Speed=9
If $Score = 150 Then $Speed=11
If $Score = 200 Then $Speed=13
EndIf

If $Life < 1 Then
MsgBox(0,"","Game Over!" & @CRLF & "You Scored " & $Score & " Points.")
Exit
EndIf
WEnd

Func Left()
If _IsCollide($Car1Pos[0],$Car1Pos[1],$Car1Pos[2],$Car1Pos[3],$LineLeftPos[0],$LineLeftPos[1],$LineLeftPos[2],$LineLeftPos[3])<>1 Then GUICtrlSetPos($Car1,$Car1Pos[0]-7,$Car1Pos[1])
EndFunc

Func Right()
If _IsCollide($Car1Pos[0],$Car1Pos[1],$Car1Pos[2],$Car1Pos[3],$LineRightPos[0],$LineRightPos[1],$LineRightPos[2],$LineRightPos[3])<>1 Then GUICtrlSetPos($Car1,$Car1Pos[0]+7,$Car1Pos[1])
EndFunc

Func Up()
If $Car1Pos[1]>0 Then GUICtrlSetPos($Car1,$Car1Pos[0],$Car1Pos[1]-4)
EndFunc

Func Down()
If $Car1Pos[1]<350 Then GUICtrlSetPos($Car1,$Car1Pos[0],$Car1Pos[1]+4)
EndFunc

Func _IsCollide ($left1, $top1, $width1, $height1, $left2, $top2, $width2, $height2)
    $lewi1 = $left1 + $width1
    $tohi1 = $height1 + $top1
    $lewi2 = $left2 + $width2
    $tohi2 = $height2 + $top2
    If BitAND($left1 < $lewi2, $lewi1 > $left2, $tohi1 > $top2, $top1 < $tohi2) Then
        Return 1
    Else
        Return 0
    EndIf
EndFunc

I got the collision UDF from another game script, but for the life of me, I can't remember what it was :whistle:

Edit: Got the collision function from BananaFredSoft's Pong Clone script.

So, enjoy my first script posted here, let me know of any issues or improvements to add.

Oh, and there's the issue of flickering stuff, because it moves all the time...

Car sprites:

http://idusy0.googlepages.com/Car1.bmp

http://idusy0.googlepages.com/Car2.bmp

(Google Images random car + Hue shift)

Edited by idusy

Share this post


Link to post
Share on other sites



Fairly cool.

Reminds me of one of the example games from Game Maker :whistle:

You may also need to fix the Yellow Car problem. The white is still around it. What about making the car move up and down as well?

Share this post


Link to post
Share on other sites

I've been making a few games in AutoIt lately, figured I'd post one of my few fully working (Hopefully) ones.

Cooked this up yesterday, I'm fairly proud of myself. >.>

HotKeySet("a","Left")
HotKeySet("d","Right")
HotKeySet("{LEFT}","Left")
HotKeySet("{RIGHT}","Right")

Dim $Speed = 5
Dim $Rate = 100
Dim $Score = "0"
Dim $Life = 3

$GUI=GUICreate("Speeding Very Fast With No Set Destination",625,450)
GUISetState()
GUISetBkColor(0x808080)
$Car1=GUICtrlCreatePic(@ScriptDir & "\Car1.bmp",300,300,50,102)
$Car2=GUICtrlCreatePic(@ScriptDir & "\Car2.bmp",300,-110,50,102)
$LableScore=GUICtrlCreateLabel("Score: " & $Score,475,25)
$LableLife=GUICtrlCreateLabel("Lives: " & $Life,475,60)
$Line1=GUICtrlCreateGraphic(315,0,20,60)
GUICtrlSetBkColor($Line1,16776960)
$Line2=GUICtrlCreateGraphic(315,100,20,60)
GUICtrlSetBkColor($Line2,16776960)
$Line3=GUICtrlCreateGraphic(315,200,20,60)
GUICtrlSetBkColor($Line3,16776960)
$Line4=GUICtrlCreateGraphic(315,300,20,60)
GUICtrlSetBkColor($Line4,16776960)
$Line5=GUICtrlCreateGraphic(315,400,20,60)
GUICtrlSetBkColor($Line5,16776960)
$LineLeft=GUICtrlCreateGraphic(185,0,40,500)
GUICtrlSetBkColor($LineLeft,0)
$LineRight=GUICtrlCreateGraphic(420,0,40,500)
GUICtrlSetBkColor($LineRight,0)

While 1

If GUIGetMsg() = -3 Then Exit

$Car1Pos=ControlGetPos($GUI,"",$Car1) ; [0] = X, [1] = Y, [2] = Width, [3] = Height
$Car2Pos=ControlGetPos($GUI,"",$Car2)
$Line1Pos=ControlGetPos($GUI,"",$Line1)
$Line2Pos=ControlGetPos($GUI,"",$Line2)
$Line3Pos=ControlGetPos($GUI,"",$Line3)
$Line4Pos=ControlGetPos($GUI,"",$Line4)
$Line5Pos=ControlGetPos($GUI,"",$Line5)
$LineLeftPos=ControlGetPos($GUI,"",$LineLeft)
$LineRightPos=ControlGetPos($GUI,"",$LineRight)

GUICtrlSetPos($Line1,$Line1Pos[0],$Line1Pos[1]+8)
GUICtrlSetPos($Line2,$Line2Pos[0],$Line2Pos[1]+8)
GUICtrlSetPos($Line3,$Line3Pos[0],$Line3Pos[1]+8)
GUICtrlSetPos($Line4,$Line4Pos[0],$Line4Pos[1]+8)
GUICtrlSetPos($Line5,$Line5Pos[0],$Line5Pos[1]+8)

If $Line1Pos[1] > 450 Then GUICtrlSetPos($Line1,$Line1Pos[0],-50)
If $Line2Pos[1] > 450 Then GUICtrlSetPos($Line2,$Line2Pos[0],-50)
If $Line3Pos[1] > 450 Then GUICtrlSetPos($Line3,$Line3Pos[0],-50)
If $Line4Pos[1] > 450 Then GUICtrlSetPos($Line4,$Line4Pos[0],-50)
If $Line5Pos[1] > 450 Then GUICtrlSetPos($Line5,$Line5Pos[0],-50)
    
GUICtrlSetPos($Car2,$Car2Pos[0],$Car2Pos[1]+$Speed)
Sleep($Rate)

If _IsCollide($Car1Pos[0],$Car1Pos[1],$Car1Pos[2],$Car1Pos[3],$Car2Pos[0],$Car2Pos[1],$Car2Pos[2],$Car2Pos[3])=1 Then
GUICtrlSetPos($Car1,300,300)
GUICtrlSetPos($Car2,$Car2Pos[0],-110)
$Life-=1
GUICtrlSetData($LableLife,"Lives: " & $Life)
MsgBox(0,"","You Crashed!" & @CRLF & "Lost One Life",3)
EndIf

If $Car2Pos[1] > 500 Then
$Score += 1
If $Rate >30 Then
$Rate -=15
Else
$Rate -=5
EndIf
If $Rate < 6 Then $Rate -= 1
If $Rate < 3 Then $Rate = 2
$RandPos=Random(210,390,1)
GUICtrlSetData($LableScore,"Score: " & $Score)
GUICtrlSetPos($Car2,$RandPos,-110)
EndIf

If $Life < 1 Then
MsgBox(0,"","Game Over!" & @CRLF & "You Scored " & $Score & " Points.")
Exit
EndIf
WEnd

Func Left()
If _IsCollide($Car1Pos[0],$Car1Pos[1],$Car1Pos[2],$Car1Pos[3],$LineLeftPos[0],$LineLeftPos[1],$LineLeftPos[2],$LineLeftPos[3])<>1 Then GUICtrlSetPos($Car1,$Car1Pos[0]-5,$Car1Pos[1])
EndFunc

Func Right()
If _IsCollide($Car1Pos[0],$Car1Pos[1],$Car1Pos[2],$Car1Pos[3],$LineRightPos[0],$LineRightPos[1],$LineRightPos[2],$LineRightPos[3])<>1 Then GUICtrlSetPos($Car1,$Car1Pos[0]+5,$Car1Pos[1])
EndFunc

Func _IsCollide ($left1, $top1, $width1, $height1, $left2, $top2, $width2, $height2)
    $lewi1 = $left1 + $width1
    $tohi1 = $height1 + $top1
    $lewi2 = $left2 + $width2
    $tohi2 = $height2 + $top2
    If BitAND($left1 < $lewi2, $lewi1 > $left2, $tohi1 > $top2, $top1 < $tohi2) Then
        Return 1
    Else
        Return 0
    EndIf
EndFunc

I got the collision UDF from another game script, but for the life of me, I can't remember what it was :whistle:

So, enjoy my first script posted here, let me know of any issues or improvements to add.

Oh, and there's the issue of flickering stuff, because it moves all the time...

Car sprites:

http://idusy0.googlepages.com/Car1.bmp

http://idusy0.googlepages.com/Car2.bmp

(Google Images random car + Hue shift)

Very good. Would be nice if the blue car moved about a bit to make it more difficult.

If you like doing this sort of thing have you seen this? It would be ideal for games.


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Fairly cool.

Reminds me of one of the example games from Game Maker :whistle:

You may also need to fix the Yellow Car problem. The white is still around it. What about making the car move up and down as well?

Actually where I got the idea. I lack any creativity in my scripts :lmao:

I originally thought to have the car move a little ways up and down, but ended up not adding it... I might now...

And I'm not sure what you mean by the car problem?

Very good. Would be nice if the blue car moved about a bit to make it more difficult.

If you like doing this sort of thing have you seen this? It would be ideal for games.

Hm, I'll think about that one as well.

Wowie, that likes very helpful, wish I would have known about it before >.<

Edited by idusy

Share this post


Link to post
Share on other sites

Actually where I got the idea. I lack any creativity in my scripts :whistle:

Haha, lacking the old creativity aspect of your brain are we?

I originally thought to have the car move a little ways up and down, but ended up not adding it... I might now...

Good idea, add it.

And I'm not sure what you mean by the car problem?

Around the car, you can see the white background, but not on the blue.

Yeah, ProSpeed is fantastic!

Share this post


Link to post
Share on other sites

Around the car, you can see the white background, but not on the blue.

I'm not getting that? I tried downloading the image and making sure it didn't get messed up, still worked fine.

Haha, lacking the old creativity aspect of your brain are we?

You know, it's funny you can have all sorts of ideas when someone else knows how to do anything, then when you learn it, it's all gone :\ That's how it is for me anyway....

Share this post


Link to post
Share on other sites

Update

Share this post


Link to post
Share on other sites

Agreed!

I never have any ideas, unless someone has done it before.

Edit: You really need to speed up the movement.

The overall Car1 movement or what? I feel if you're driving, you can't accelerate as fast as you can move side to side (Or something, I'm confusing myself...)

The second cars movement gets a tad faster with each respawn.

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

When I start this game it basically does this:

You crashed.

You crashed.

You crashed.

Game over.

Lol, make sure you use the sprites.

I mean, you move, what is it? One pixel at a time? Its to slow.

Made it a little faster. Feel free to edit to your liking.

Edited by idusy

Share this post


Link to post
Share on other sites

Nice game, my highest score is 104. :whistle:

BTW: I found a bug, score label is too small for numbers over 99.

Share this post


Link to post
Share on other sites

Add these lines:

If (Not FileExists("Car1.bmp")) Or (Not FileExists("Car2.bmp")) Then
    MsgBox(0,"Error","Please make sure you download files Car1.bmp and Car2.bmp from the AutoIt forums and put them in the same directory as this script.")
    Exit
EndIf
:whistle:
Did that, but also attempts to download them now.

Share this post


Link to post
Share on other sites

Nice game, my highest score is 104. :whistle:

BTW: I found a bug, score label is too small for numbers over 99.

Fixed.

And thanks for all the positive feedback everyone :lmao:

Share this post


Link to post
Share on other sites

Why reboot? :whistle:

If (Not FileExists("Car1.bmp")) Or (Not FileExists("Car2.bmp")) Then
    InetGet("http://idusy0.googlepages.com/Car1.bmp",@ScriptDir & "\Car1.bmp")
    InetGet("http://idusy0.googlepages.com/Car2.bmp",@ScriptDir & "\Car2.bmp")
EndIf
Oh, thinking it loaded them at the start for some reason. Fixed. And thanks for the suggestions/fixes.

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