AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

AHK Racing with Bet!
Goto page 1, 2  Next
 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
.AHK



Joined: 26 Apr 2006
Posts: 662
Location: USA

PostPosted: Wed Oct 04, 2006 1:19 pm    Post subject: AHK Racing with Bet! Reply with quote

I was bored with nothing to do and had an idea for a fun game. Why not turn 5 progress bars into a game? Make them race each other, and bet on which racer will win. So here it is. The first racing game made by AHK, I think. It can be some fun too. Let me know what you think about this, please. Thanks.



Code:

; RaceGame.AHK - Made by: .AHK - DeriveFall function Made by: Goyyah

#NoEnv
#SingleInstance Ignore
Balance = 2000
R_Played = 0
Status1 = 1
Status2 = 1
Status3 = 1
Status4 = 1
Status5 = 1
Racer1W = 0
Racer2W = 0
Racer3W = 0
Racer4W = 0
Racer5W = 0
AmountW = 0
AmountL = 0
Racer1 = 0
Racer2 = 0
Racer3 = 0
Racer4 = 0
Racer5 = 0
Gui Font, S11
Gui Add, Progress, w350 h15 cBlue BackgroundWhite Range0-500 vRacer1
Gui Add, Progress, w350 h15 cBlack BackgroundWhite Range0-500 vRacer2
Gui Add, Progress, w350 h15 cRed BackgroundWhite Range0-500 vRacer3
Gui Add, Progress, w350 h15 cYellow BackgroundWhite Range0-500 vRacer4
Gui Add, Progress, w350 h15 cGreen BackgroundWhite Range0-500 vRacer5
Gui Add, Button, W75 H20 gStart, Start Race
Gui Add, Text, x370 y8, Racer #1
Gui Add, Text, y+7, Racer #2
Gui Add, Text, y+7, Racer #3
Gui Add, Text, y+7, Racer #4
Gui Add, Text, y+7, Racer #5
Gui Add, Text, x275 y135, Balance
Gui Add, Edit, x+5 y+-20 r1 W100 +ReadOnly, $%Balance%
Gui Add, Radio, x10 vNoBet Checked, No Bet
Gui Add, Radio, x+5 y+-16 vBet1, Bet #1
Gui Add, Radio, x+5 y+-16 vBet2, Bet #2
Gui Add, Radio, x10 vBet3, Bet #3
Gui Add, Radio, x+9 y+-16 vBet4, Bet #4
Gui Add, Radio, x+5 y+-16 vBet5, Bet #5
Gui Add, Text, x243 y180, Amount to Bet
Gui Add, Edit, vBetAmount x+5 y+-20 r1 W70 Number Limit8, 100
Gui Add, Text, x10 y230, Rounds Played
Gui Add, Edit, x+5 y+-20 r1 W45 +ReadOnly, 0
Gui Add, Text, x+15 y+-20, Amount Won
Gui Add, Edit, x+5 y+-20 r1 W70 +ReadOnly, $0
Gui Add, Text, x+15 y+-20, Amount Lost
Gui Add, Edit, x+5 y+-20 r1 W70 +ReadOnly, $0
Gui Add, Edit, x430 y6 H20 W35 +ReadOnly, 0
Gui Add, Edit, y+3 H20 W35 +ReadOnly, 0
Gui Add, Edit, y+3 H20 W35 +ReadOnly, 0
Gui Add, Edit, y+3 H20 W35 +ReadOnly, 0
Gui Add, Edit, y+3 H20 W35 +ReadOnly, 0
Gui Show, Center, Race Game
Return

GuiClose:
ExitApp
Return

Start:
Gui +OwnDialogs
Gui Submit, NoHide

If betamount IS space
betamount = 0

If betamount > %balance%
{
MsgBox 0, Lower Bet, The amount to bet is above your balance.
Return
}

Loop {
Sleep 75
If (Status1 = 0 AND Status2 = 0 AND Status3 = 0 AND Status4 = 0 AND Status5 = 0)
Break

If Racer1 >= 500
{ Racer1W++
Break
}
If Racer2 >= 500
{ Racer2W++
Break
}
If Racer3 >= 500
{ Racer3W++
Break
}
If Racer4 >= 500
{ Racer4W++
Break
}
If Racer5 >= 500
{ Racer5W++
Break
}

n = 1
Gosub S_progress
If Complete = 1
Break

n = 2
Gosub S_progress
If Complete = 1
Break

n = 3
Gosub S_progress
If Complete = 1
Break

n = 4
Gosub S_progress
If Complete = 1
Break

n = 5
Gosub S_progress
If Complete = 1
Break
}

ControlSetText Edit1, $%balance%

If balance = 0
{
MsgBox 4, Game Over, You have lost all your money. Restart the game?
IfMsgBox Yes
Reload
Else
ExitApp
}

R_Played++
ControlSetText Edit3, %R_Played%
ControlSetText Edit4, $%AmountW%
ControlSetText Edit5, $%AmountL%
ControlSetText Edit6, %Racer1W%
ControlSetText Edit7, %Racer2W%
ControlSetText Edit8, %Racer3W%
ControlSetText Edit9, %Racer4W%
ControlSetText Edit10, %Racer5W%
GuiControl,, Racer1, 0
GuiControl,, Racer2, 0
GuiControl,, Racer3, 0
GuiControl,, Racer4, 0
GuiControl,, Racer5, 0
Racer1 = 0
Racer2 = 0
Racer3 = 0
Racer4 = 0
Racer5 = 0
Complete = 0
Status1 = 1
Status2 = 1
Status3 = 1
Status4 = 1
Status5 = 1
Return

DeriveFall() {
Tn = 0
Loop 6 {
     Random, Rn, 0,2
    Tn += Rn
   }
Return Tn
}

S_progress:
If Status%n% <> 0
Status%n% := DeriveFall()
If Status%n% = 0
Return
Random count, 1, 10
GuiControl,, Racer%n%, +%count%
Racer%n% := (Racer%n% + count)
If Racer%n% >= 500
  If bet%n% = 1
  {
  balance := (balance + betamount)
  amountw := (amountw + betamount)
  Racer%n%W += 1
  MsgBox 0, Win, You won $%betamount%
  Complete = 1
  } Else {
  If nobet = 0
  {
 balance := (balance - betamount)
 amountl := (amountl + betamount)
 Racer%n%W += 1
 MsgBox 0, Loss, You lost $%betamount%
 Complete = 1
 }
}
Return


I had accidently removed part of the script that was needed. I have readded it. No Bet did not work properly without it.


Last edited by .AHK on Fri Oct 06, 2006 9:11 pm; edited 6 times in total
Back to top
View user's profile Send private message Visit poster's website AIM Address
PhiLho



Joined: 27 Dec 2005
Posts: 6721
Location: France (near Paris)

PostPosted: Wed Oct 04, 2006 1:25 pm    Post subject: Reply with quote

Funny...
If you are still bored, you can try to improve your script by avoiding repeating several times similar code (hard to maintain) but by looping (perhaps putting common code in a subroutine or function).
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Back to top
View user's profile Send private message Visit poster's website
.AHK



Joined: 26 Apr 2006
Posts: 662
Location: USA

PostPosted: Wed Oct 04, 2006 1:29 pm    Post subject: Reply with quote

I use notepad to write my scripts by hand, so the repeating code is a common thing until I later optimize the script. Which, I will do sometime today.
Quote:
Funny...

Just funny, not even interesting? Smile
Back to top
View user's profile Send private message Visit poster's website AIM Address
SKAN



Joined: 26 Dec 2005
Posts: 5887

PostPosted: Wed Oct 04, 2006 1:38 pm    Post subject: Re: AHK Racing with Bet! Reply with quote

.AHK wrote:
Let me know what you think about this, please. Thanks.


I made $1300 in 10 races - where should I claim it? Rolling Eyes

Laughing

Very nice and interesting ...

If you plan to extend it - I suggest you put a horse & jockey.bmp
and ControlMove it.

Thanks for writing and sharing this!

Regards, Smile

PS: Also thanks to Takuji Nishimura and Makoto Matsumoto, Shawn Cokus, Matthe Bellew and Isaku Wada
_________________
SKAN - Suresh Kumar A N
Back to top
View user's profile Send private message
.AHK



Joined: 26 Apr 2006
Posts: 662
Location: USA

PostPosted: Wed Oct 04, 2006 1:45 pm    Post subject: Reply with quote

Quote:
I made $1300 in 10 races - where should I claim it?

Laughing You mean you lost $700 in 10 races. Oh wait nevermind. You had a Balance of $3300 after 10 races. Im a bad gambler and waste all my money fast on this game. Smile This gave me idea of adding an area to show how many games you have played, thanks.

Quote:
Very nice and interesting ...

If you plan to extend it - I suggest you put a horse & jockey.bmp
and ControlMove it.

Thanks for writing and sharing this!

Im glad you like it. I do plan on extending it, and your suggestion was actually how I planed on first doing it. I then thought it would be better to not include any images at this time. Eventually I will do this though.

Does anyone else find it strange that the 5 progress bars keep a relatively close race? With a random factor of 1-10 you would think there would be a differnt outcome.

I plan on adding easy, medium, and difficult levels. Depending on the level you will have to win up to a certain amount of money betting to win the game.

I made $8600 in 7 races but bet it all and lost. Rolling Eyes
Back to top
View user's profile Send private message Visit poster's website AIM Address
koro



Joined: 24 Sep 2006
Posts: 60

PostPosted: Wed Oct 04, 2006 4:18 pm    Post subject: Reply with quote

.AHK wrote:

Does anyone else find it strange that the 5 progress bars keep a relatively close race? With a random factor of 1-10 you would think there would be a differnt outcome.


If i got it right, you have 5 counters and you keep adding a random number between 1 and 10 until some counter reaches 500. If you use any number N instead of 500, for large values of N the average speed in all the bars will be roughly the same (if you make N go to infinity, the average speed converges to a constant, namely 5.5) Since 500 is a fairly large number, you get a 'fairly close race'
This property is known in probability theory as the 'law of large numbers'.
Back to top
View user's profile Send private message
.AHK



Joined: 26 Apr 2006
Posts: 662
Location: USA

PostPosted: Wed Oct 04, 2006 4:39 pm    Post subject: Reply with quote

Quote:
If i got it right, you have 5 counters and you keep adding a random number between 1 and 10 until some counter reaches 500.

Yes, this is how the script works. First progress bar to finish (500) wins.
Quote:
If you use any number N instead of 500, for large values of N the average speed in all the bars will be roughly the same (if you make N go to infinity, the average speed converges to a constant, namely 5.5) Since 500 is a fairly large number, you get a 'fairly close race'
This property is known in probability theory as the 'law of large numbers'.

I see now, and understand, for the most part. Thanks.
Back to top
View user's profile Send private message Visit poster's website AIM Address
PhiLho



Joined: 27 Dec 2005
Posts: 6721
Location: France (near Paris)

PostPosted: Wed Oct 04, 2006 4:59 pm    Post subject: Reply with quote

.AHK wrote:
Does anyone else find it strange that the 5 progress bars keep a relatively close race? With a random factor of 1-10 you would think there would be a differnt outcome.
It shows that the random number generator is quite good... Chris used the well known MT19937 (Mersenne Twister) generator.
Oh, I was wondering who Goyyah was thanking, they are those which contributed to this generator! Smile

A possible way to add interest to the race is to add acceleration: generate a tiny random number, say between -5 and 5, add it to the acceleration. Then add this acceleration to the current speed of the horse, which in turn is added to its position. It should smoothen the race, ie. avoid jumpiness (if there is one, I haven't tried it yet...).
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Back to top
View user's profile Send private message Visit poster's website
.AHK



Joined: 26 Apr 2006
Posts: 662
Location: USA

PostPosted: Wed Oct 04, 2006 5:26 pm    Post subject: Reply with quote

Quote:
A possible way to add interest to the race is to add acceleration: generate a tiny random number, say between -5 and 5, add it to the acceleration. Then add this acceleration to the current speed of the horse, which in turn is added to its position. It should smoothen the race, ie. avoid jumpiness (if there is one, I haven't tried it yet...).

This seems like a good idea, and is likely to be another feature I will add, thanks. There is some noticable jumpiness, but only when a high number 7-10 is generated.
Back to top
View user's profile Send private message Visit poster's website AIM Address
SKAN



Joined: 26 Dec 2005
Posts: 5887

PostPosted: Wed Oct 04, 2006 5:29 pm    Post subject: Reply with quote

Quote:
It shows that the random number generator is quite good... Chris used the well known MT19937 (Mersenne Twister) generator.


True. I won & lost with nail-biting finishes Laughing

The following is a sub-routine in my always-running-script, that I call with a hotkey when I cannot find a coin to toss! Embarassed

Code:
Toss:
Heads=0
Tails=0
Loop, 99 {
Random,HT,1,2
IfEqual,HT,1,SetEnv,Heads, % Heads+1
IfEqual,HT,2,SetEnv,Tails, % Tails+1
Sleep 1
}
IfGreater,Heads,%Tails%,MsgBox,0,HEADS,Heads`t: %Heads%`nTails`t: %Tails%
IfLess   ,Heads,%Tails%,MsgBox,0,TAILS,Heads`t: %Heads%`nTails`t: %Tails%
IfEqual  ,Heads,%Tails%,MsgBox,0,EQUAL,Heads`t: %Heads%`nTails`t: %Tails%
Return


With A best of 99, most of the time the difference would be less than 5 Smile

PhiLho wrote:
A possible way to add interest to the race is to add acceleration: generate a tiny random number, say between -5 and 5, add it to the acceleration. Then add this acceleration to the current speed of the horse, which in turn is added to its position. It should smoothen the race, ie. avoid jumpiness (if there is one, I haven't tried it yet...).


I used to play a dos-based game much similar in which a horse might fall
and be out of the race. It will be nice to have it as a feature.

Regards, Smile
_________________
SKAN - Suresh Kumar A N
Back to top
View user's profile Send private message
.AHK



Joined: 26 Apr 2006
Posts: 662
Location: USA

PostPosted: Wed Oct 04, 2006 5:37 pm    Post subject: Reply with quote

Quote:
I used to play a dos-based game much similar in which a horse might fall
and be out of the race. It will be nice to have it as a feature.

You guys are full of good ideas. Something I never have. Smile So, if I wanted around a 5% chance of a horse falling. Code something like this?

Code:

Random chance, 1, 1000
If chance = 111
Stop progress bar for racer 1.
If chance = 222
Stop progress bar for racer 2.
etc...


If you have a better idea let me know. Planned updates so far:

1. Optimize code.
2. Racing acceleration.
3. Injured horse.
4. Text showing how many games played.
5. Text showing how many times each horse has won.
6. Text showing money made, and money lost.
7. Levels (easy, medium, hard).


Last edited by .AHK on Wed Oct 04, 2006 5:42 pm; edited 2 times in total
Back to top
View user's profile Send private message Visit poster's website AIM Address
SKAN



Joined: 26 Dec 2005
Posts: 5887

PostPosted: Wed Oct 04, 2006 5:39 pm    Post subject: Reply with quote

.AHK wrote:
If you have a better idea let me know.


Have some pity on the player if his balance is too low .. Wink

Laughing

Regards, Smile
_________________
SKAN - Suresh Kumar A N
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 5887

PostPosted: Wed Oct 04, 2006 5:53 pm    Post subject: Reply with quote

Here is an idea:

Code:
Loop {
Running := DeriveFall()
If (!Running) {
  MsgBox, 48, A_Index:%A_Index%, The Horse is in Hell!
  Break
              }
     }
Return

; Above is demonstration
; Below is the function required for you

DeriveFall(LoopVal=3) {
Tn=
Loop, %LoopVal% {
                   Random, Rn, 0,2
                   Tn+= Rn
                 }
Return Tn
}


I have provided two levels of customisation for you to decide on the
Run:Fall ratio

Regards, Smile
_________________
SKAN - Suresh Kumar A N
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 5887

PostPosted: Wed Oct 04, 2006 6:05 pm    Post subject: Reply with quote

Quote:
I had accidently removed part of the script that was needed. I have readded it. No Bet did not work properly without it.


What are you trying to say!

1) Bet did not work properly without it.
2) No Bet did work properly without it.

Regards, Question
_________________
SKAN - Suresh Kumar A N
Back to top
View user's profile Send private message
.AHK



Joined: 26 Apr 2006
Posts: 662
Location: USA

PostPosted: Wed Oct 04, 2006 6:06 pm    Post subject: Reply with quote

Goyyah, I do not understand your function. Can you explain it? Thanks.

Quote:
What are you trying to say!

1) Bet did not work properly without it.
2) No Bet did work properly without it.

Regards,

In the game you have the option to not bet and just watch a race (No Bet). With the code I removed the "No bet" option was not working correctly.
Back to top
View user's profile Send private message Visit poster's website AIM Address
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group