Sign in to follow this  
Followers 0
crzftx

Restart Function

4 posts in this topic

I understand that there is another similar post about a function that restarts the code, but it is a bit different and not answered. I have a simple game and I want it to start over when I press F2. I want a function set from HotKeySet (any alternatives to HotKeySet other than AdlibEnable with _IsPressed??) that would restart the code from a certain spot, or the beginning, if nothing else is possible. I could easily do this if Goto was still around, but it's not. Any suggestions?


If I post a code or snippet, please tell me what I did wrong and/or what I can do better. Tell me my code/idea sucks, if it does. If I ask for help, refrain from only telling me to search the help file and/or the forums. I will search before asking, I just may not have searched with good keywords. If I say that I will look into something or fix something later, understand that I am most likely lying.

Share this post


Link to post
Share on other sites



What if you litterally make it 'restart' the game, close it and open it again?

If it really is a simple game, you could move all the objects back where they were and set scores etc to 0... but we dont know what it is yet

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

maybe something like this:

Global $nRestartCount = 0
_Main($nRestartCount) ;Call the main function

Func _Main($nRestartCountW)
    ;We do our main code in here
    MsgBox(0, "Program Restart Count", $nRestartCountW)
    $hWnd = GUICreate("MyTitle")
    GUISwitch($hWnd)
    $nMyButton = GUICtrlCreateButton("Restart Script", 0, 0, 100)
    GUISetState()
    While (1)
        $uMsg = GUIGetMsg()
        if ($uMsg == -3) then ExitLoop
        if ($uMsg == $nMyButton) Then 
            $nRestartCount += 1
            GUIDelete($hWnd)
            _Main($nRestartCount)
        EndIf
    WEnd
    Return 0    
EndFunc
oÝ÷ Úâ*.­Æ¬y«­¢+Ø)!½Ñ-åMÐ ÅÕ½ÐííÕôÅÕ½Ðì°ÅÕ½Ðí}5¥Ñ½ÈÅÕ½Ðì¤)±½°ÀÌØí¹IÍÑÉÑ
½Õ¹ÐôÀ)±½°ÀÌØí¡]¹ôÀ)}5¥¸ ÀÌØí¹IÍÑÉÑ
½Õ¹Ð¤í
±°Ñ¡µ¥¸Õ¹Ñ¥½¸()Õ¹}5¥¸ ÀÌØí¹IÍÑÉÑ
½Õ¹Ñ¤($í]¼½Õȵ¥¸½¥¸¡É(%5Í    ½à À°ÅÕ½ÐíAɽɴIÍÑÉÐ
½Õ¹ÐÅÕ½Ðì°ÀÌØí¹IÍÑÉÑ
½Õ¹Ñ¤($ÀÌØí¡]¹ôU%
ÉÑ ÅÕ½Ðí5åQ¥Ñ±ÅÕ½Ðì¤(%U%MÝ¥Ñ  ÀÌØí¡]¹¤($ÀÌØí¹5å    ÕÑѽ¸ôU%
Ñɱ
ÉÑ    ÕÑѽ¸ ÅÕ½ÐíIÍÑÉÐMÉ¥ÁÐÅÕ½Ðì°À°À°ÄÀÀ¤(%U%MÑMÑÑ ¤(%]¡¥± Ĥ($$ÀÌØíÕ5ÍôU%Ñ5Í ¤($%¥ ÀÌØíÕ5Íôô´Ì¤Ñ¡¸á¥Ñ1½½À($$í¥ ÀÌØíÕ5ÍôôÀÌØí¹5å  ÕÑѽ¸¤Q¡¸M¹ ÅÕ½ÐííÄÉôÅÕ½Ðì¤(%]¹(%IÑÕɸÀ$)¹Õ¹()Õ¹}5¥Ñ½È ¤($ÀÌØí¹IÍÑÉÑ
½Õ¹Ð¬ôÄ(%U%±Ñ ÀÌØí¡]¹¤(%}5¥¸ ÀÌØí¹IÍÑÉÑ
½Õ¹Ð¤)¹Õ¹
Edited by CHRIS95219

Share this post


Link to post
Share on other sites

Thanks guys, that should help. And here's the unfinished game since you implied interest. I would like to make it with a menu bar and status bar and lose the side info, and make the high scores in a menu. But the main point of this topic is F2 would start a new game.

#region Prerequisites
#NoTrayIcon
#include <GUIConstants.au3>
#include<Misc.au3>
#include<Math.au3>
Opt("PixelCoordMode", 2)
HotKeySet("{esc}","Exit_")
If IniRead("C:\Windows\W0HS.ini","Names","Default","Not Found") = "Not Found" Then
    $Name = InputBox("First Time User","Enter Your Name")
    IniWrite("C:\Windows\W0HS.ini","Names","Default",$Name)
    For $z = 1 To 10
        IniWrite("C:\Windows\W0HS.ini","Names",$z,$Name)
        IniWrite("C:\Windows\W0HS.ini","Scores",$z,0)
    Next
EndIf
#endregion
While 1
#region Variables
$Time1 = TimerInit()
Global $Label[19] = [1]
GUICreate("High Scores",170,220)
GUISetState()
GUISetBkColor(0xE0F0F0)
For $z = 1 To 10
    $Label[$z] = GUICtrlCreateLabel(IniRead("C:\Windows\W0HS.ini","Names",$z,"Not Found") & " " & IniRead("C:\Windows\W0HS.ini","Scores",$z,"Not Found"),0,22*($z-1),170,22,$SS_CENTER)
    GUICtrlSetFont ($Label[$z],12,550,0,"Tohoma")
Next
Do 
    Sleep(100)
Until _IsPressed("0D")
GUIDelete()
$GUI1 = GUICreate("Winder 0",750,320)
GUISetState()
HotKeySet("{right}","Move_Right")
HotKeySet("{left}","Move_Left")
HotKeySet("{up}","Nuke")
HotKeySet("{down}","Pause")
HotKeySet("{space}","Color_Set")
Global $Column[21] = [-30,-30,-30,-30,-30,-30,-30,-30,-30,-30,-30,-30,-30,-30,-30,-30,-30,-30,0,300]
Global $Row[19] = [30,30,60,60,90,90,120,120,150,150,180,180,210,210,240,240,270,270,0]
Global $Label[19] = [1]
Global $S_Label[8] = [1]
Global $Score = 0
Global $Pause = 0
Global $Wall = 0
Global $Nuke = 1
Global $Move_Rate = 40
Global $Special_Rate = 10
Global $Speed = 100
Global $Lose = 0
Global $Special = -1
Global $Level = 1
Global $Bk_Color = IniRead("C:\Windows\W0HS.ini","Specials","Bk_Color",0xE0F0FF)
Global $V_Color = IniRead("C:\Windows\W0HS.ini","Specials","V_Color",0x000000)
Global $O_Color = IniRead("C:\Windows\W0HS.ini","Specials","O_Color",0x000000)
Global $Q_Color = IniRead("C:\Windows\W0HS.ini","Specials","Q_Color",0x00A000)
Global $W_Color = IniRead("C:\Windows\W0HS.ini","Specials","W_Color",0x000000)
$Label[18] = GUICtrlCreateLabel("O",30*$Column[19]+5,300,30,30,$SS_CENTER)
$S_Label[0] = GUICtrlCreateLabel("Speed: " & $Speed,625,90)
$S_Label[1] = GUICtrlCreateLabel("Score: " & $Score,625,10)
$S_Label[2] = GUICtrlCreateLabel("Nukes: " & $Nuke,625,30)
$S_Label[3] = GUICtrlCreateLabel("Move Rate: " & $Move_Rate,625,50)
$S_Label[4] = GUICtrlCreateLabel("Special Rate: " & $Special_Rate,625,70)
$S_Label[5] = GUICtrlCreateLabel("Level: " & $Level,625,110)
$S_Label[6] = 1
$S_Label[7] = 1
For $z = 0 To 5
    GUICtrlSetColor($S_Label[$z],$W_Color)
Next
$Wall_Mark = GUICtrlCreateLabel("]",$Wall-30,300,30,30,$SS_CENTER)
GUISetBkColor($Bk_Color)
#endregion
While 1
    $msg = GUIGetMsg()
    If $msg = $GUI_EVENT_CLOSE Then Exit_()
    For $z = 0 To 17
        $Label[$z] = GUICtrlCreateLabel("V",$Column[$z],$Row[$z],30,30,$SS_CENTER)
        GUICtrlSetColor($Label[$z],$V_Color)
    Next
    Sleep($Speed)
    $Score = $Score+1
    GUICtrlDelete($S_Label[1])
    $S_Label[1] = GUICtrlCreateLabel("Score: " & $Score,625,10)
    GUICtrlSetColor($S_Label[1],$W_Color)
    For $z = 1 To 10
        If $Score > IniRead("C:\Windows\W0HS.ini","Scores",$z,"50000") Then ExitLoop
    Next
    If $z<11 Then 
        GUICtrlDelete($S_Label[7])
        $S_Label[7] = GUICtrlCreateLabel("High Score "&$z,625,180,125)
        GUICtrlSetColor($S_Label[7],$W_Color)
        GUICtrlSetFont ($S_Label[7],12,550)
    EndIf
    If _MathCheckDiv($Score,$Move_Rate) = 2 Then 
        $Wall = $Wall+30
        GUICtrlDelete($Wall_Mark)
        $Wall_Mark = GUICtrlCreateLabel("]",$Wall-30,300,30,30,$SS_CENTER)
        GUICtrlSetFont ($Wall_Mark,12,600)
        GUICtrlSetColor($Wall_Mark,$O_Color)
    EndIf
    If $Special = -1 Then 
        If Random(1,$Special_Rate,1) = 1 Then
        $Column[18] = Random(($Wall/30),19,1)*30
        $Row[18] = 30
        $Special = GUICtrlCreateLabel("?",$Column[18],$Row[18],30,30,$SS_CENTER)
        GUICtrlSetColor($Special,$Q_Color)
        GUICtrlSetFont ($Special,12,600)
        EndIf
    Else
        GUICtrlDelete($Special)
        $Row[18] = $Row[18]+30
        If $Row[18] = 300 Then
            If $Column[18] = $Column[19] Then Special()
            $Special = -1
        Else            
            $Special = GUICtrlCreateLabel("?",$Column[18],$Row[18],30,30,$SS_CENTER)
            GUICtrlSetColor($Special,$Q_Color)
            GUICtrlSetFont ($Special,12,600)
        EndIf
    EndIf
    $Lose = $Lose-2
    If $Lose = -2 Then $Lose = 16
    If $Column[$Lose] = $Column[19] Or $Column[$Lose+1] = $Column[19] Then Exitloop
    For $z = 0 To 17
        $Row[$z] = $Row[$z]+30
        If $Row[$z] = 300 Then 
            $Row[$z] = 30
            $Column[$z] = Random(($Wall/30),19,1)*30
        EndIf
    Next
    For $z = 0 To 17
        GUICtrlDelete($Label[$z])
    Next
    GUICtrlDelete($Label[18])
    If $Column[19] < $Wall Then $Column[19] = 570
    $Label[18] = GUICtrlCreateLabel("O",$Column[19],300,30,30,$SS_CENTER)
    GUICtrlSetColor($Label[18],$O_Color)
WEnd
#region Highscore
$Time2 = TimerDiff($Time1)
For $z = 1 To 10
    If IniRead("C:\Windows\W0HS.ini","Scores",$z,"50000")<$Score Then 
        $Name = InputBox("High score " & ($z),"Enter your name",IniRead("C:\Windows\W0HS.ini","Names","Default",""))
        For $y = 9 To $z Step -1
            IniWrite("C:\Windows\W0HS.ini","Scores",$y+1,IniRead("C:\Windows\W0HS.ini","Scores",$y,"50000"))
            IniWrite("C:\Windows\W0HS.ini","Names",$y+1,IniRead("C:\Windows\W0HS.ini","Names",$y,"50000"))
        Next
        IniWrite("C:\Windows\W0HS.ini","Names",$z,$Name)
        IniWrite("C:\Windows\W0HS.ini","Scores",$z,$Score)
        ExitLoop
    EndIf
Next
$Return = MsgBox(68,"Game Over","Your rating was "&Round(((($Score^3)/(($Time2/1000)^2))/1000),2)&@CRLF&"Play Again?")
GUIDelete()
If $Return = 7 Then ExitLoop
#endregion
WEnd
#region Functions
Func Move_Right()
    $Column[19] = $Column[19] + 30
    If $Column[19] = 600 Then $Column[19] = $Wall
EndFunc
Func Move_Left()
    $Column[19] = $Column[19] - 30
EndFunc
Func Exit_()
    Exit
EndFunc
Func Nuke()
    GUICtrlDelete($S_Label[2])
    If $Nuke > 0 Then
        For $z = 0 To 17
            $Column[$z] = -30
        Next
        $Nuke = $Nuke-1
    EndIf
    $S_Label[2] = GUICtrlCreateLabel("Nukes: " & $Nuke,625,30)
    GUICtrlSetColor($S_Label[2],$W_Color)
EndFunc
Func Pause()
    If $Score-$Pause>10 Then
        $Pause = $Score
        Do 
            Sleep(100)
        Until _IsPressed(25) = 1 Or _IsPressed(26) = 1 Or _IsPressed(27) = 1 Or _IsPressed(60) = 1
    EndIf
EndFunc
Func Color_Set()
$Column[20] = $Column[19]
If WinGetTitle("") = "Winder 0" Then
    Local $Input[5][3]
    Local $Box[5]
    $GUI2 = GUICreate("Color Set",190,130)
    GUISwitch($GUI2)
    GUISetState()
    $Button = GUICtrlCreateButton("Save Changes",0,100,190,30)
    $Input[0][0] = GUICtrlCreateInput(Int($Bk_Color/65536),100,0,30,20)
    $Input[0][1] = GUICtrlCreateInput(Int(($Bk_Color-65536*Int($Bk_Color/65536))/256),130,0,30,20)
    $Input[0][2] = GUICtrlCreateInput($Bk_Color-65536*Int($Bk_Color/65536)-256*Int(($Bk_Color-65536*Int($Bk_Color/65536))/256),160,0,30,20)
    $Input[1][0] = GUICtrlCreateInput(Int($V_Color/65536),100,20,30,20)
    $Input[1][1] = GUICtrlCreateInput(Int(($V_Color-65536*Int($V_Color/65536))/256),130,20,30,20)
    $Input[1][2] = GUICtrlCreateInput($V_Color-65536*Int($V_Color/65536)-256*Int(($V_Color-65536*Int($V_Color/65536))/256),160,20,30,20)
    $Input[2][0] = GUICtrlCreateInput(Int($O_Color/65536),100,40,30,20)
    $Input[2][1] = GUICtrlCreateInput(Int(($O_Color-65536*Int($O_Color/65536))/256),130,40,30,20)
    $Input[2][2] = GUICtrlCreateInput($O_Color-65536*Int($O_Color/65536)-256*Int(($O_Color-65536*Int($O_Color/65536))/256),160,40,30,20)
    $Input[3][0] = GUICtrlCreateInput(Int($Q_Color/65536),100,60,30,20)
    $Input[3][1] = GUICtrlCreateInput(Int(($Q_Color-65536*Int($Q_Color/65536))/256),130,60,30,20)
    $Input[3][2] = GUICtrlCreateInput($Q_Color-65536*Int($Q_Color/65536)-256*Int(($Q_Color-65536*Int($Q_Color/65536))/256),160,60,30,20)
    $Input[4][0] = GUICtrlCreateInput(Int($W_Color/65536),100,80,30,20)
    $Input[4][1] = GUICtrlCreateInput(Int(($W_Color-65536*Int($W_Color/65536))/256),130,80,30,20)
    $Input[4][2] = GUICtrlCreateInput($W_Color-65536*Int($W_Color/65536)-256*Int(($W_Color-65536*Int($W_Color/65536))/256),160,80,30,20)
    GUICtrlCreateLabel("Background",25,0,75,20)
    GUICtrlCreateLabel("V Color",25,20,75,20)
    GUICtrlCreateLabel("O Color",25,40,75,20)
    GUICtrlCreateLabel("? Color",25,60,75,20)
    GUICtrlCreateLabel("Words",25,80,75,20)
    For $z = 0 To 4
        $Box[$z] = GUICtrlCreateLabel("",0,20*$z,20,20)
    Next
    While 1
        $msg = GUIGetMsg()
        For $z = 0 To 4
            GUICtrlSetBkColor($Box[$z],GUICtrlRead($Input[$z][0])*65536+GUICtrlRead($Input[$z][1])*256+GUICtrlRead($Input[$z][2]))
        Next
        Select 
            Case $msg = $GUI_EVENT_CLOSE
                ExitLoop
            Case $msg = $Button
                $Bk_Color = GUICtrlRead($Input[0][0])*65536+GUICtrlRead($Input[0][1])*256+GUICtrlRead($Input[0][2])
                $V_Color = GUICtrlRead($Input[1][0])*65536+GUICtrlRead($Input[1][1])*256+GUICtrlRead($Input[1][2])
                $O_Color = GUICtrlRead($Input[2][0])*65536+GUICtrlRead($Input[2][1])*256+GUICtrlRead($Input[2][2])
                $Q_Color = GUICtrlRead($Input[3][0])*65536+GUICtrlRead($Input[3][1])*256+GUICtrlRead($Input[3][2])
                $W_Color = GUICtrlRead($Input[4][0])*65536+GUICtrlRead($Input[4][1])*256+GUICtrlRead($Input[4][2])
                IniWrite("C:\Windows\W0HS.ini","Specials","Bk_Color",$Bk_Color)
                IniWrite("C:\Windows\W0HS.ini","Specials","V_Color",$V_Color)
                IniWrite("C:\Windows\W0HS.ini","Specials","O_Color",$O_Color)
                IniWrite("C:\Windows\W0HS.ini","Specials","Q_Color",$Q_Color)
                IniWrite("C:\Windows\W0HS.ini","Specials","W_Color",$W_Color)
                ExitLoop
        EndSelect
    WEnd
    GUIDelete()
    GUISwitch($GUI1)
    GUISetState()
    GUISetBkColor($Bk_Color)
    For $z = 0 To 7
        GUICtrlSetColor($S_Label[$z],$W_Color)
    Next
EndIf
$Column[19] = $Column[20]
EndFunc
Func Special()
    For $z = 0 To 6
        GUICtrlDelete($S_Label[$z])
    Next
    Switch Random(1,19+3*Round(Random(0,2,1)/3),1)
        Case 1
            $Move_Rate = $Move_Rate-(8+2*$Level^2)
            $S_Label[6] = GUICtrlCreateLabel("Move Rate: " & $Move_Rate,290,7,90,20,$SS_CENTER)
        Case 2 To 3
            $Move_Rate = $Move_Rate+(8+2*$Level^2)
            $S_Label[6] = GUICtrlCreateLabel("Move Rate: " & $Move_Rate,290,7,90,20,$SS_CENTER)
        Case 4
            $Special_Rate = $Special_Rate+(1+2^($Level-1))
            $S_Label[6] = GUICtrlCreateLabel("Special Rate: " & $Special_Rate,290,7,90,20,$SS_CENTER)
        Case 5 To 6
            $Special_Rate = $Special_Rate-(1+2^($Level-1))
            $S_Label[6] = GUICtrlCreateLabel("Special Rate: " & $Special_Rate,290,7,90,20,$SS_CENTER)
        Case 7
            $Wall = $Wall+30*$Level
            $S_Label[6] = GUICtrlCreateLabel("Walls + "&$Level,290,7,90,20,$SS_CENTER)
            GUICtrlDelete($Wall_Mark)
            $Wall_Mark = GUICtrlCreateLabel("]",$Wall-30,300,30,30,$SS_CENTER)
            GUICtrlSetFont ($Wall_Mark,12,600)
            GUICtrlSetColor($Wall_Mark,$O_Color)
        Case 8 To 9
            $Wall = $Wall-30*$Level
            $S_Label[6] = GUICtrlCreateLabel("Walls - "&$Level,290,7,90,20,$SS_CENTER)
            GUICtrlDelete($Wall_Mark)
            $Wall_Mark = GUICtrlCreateLabel("]",$Wall-30,300,30,30,$SS_CENTER)
            GUICtrlSetFont ($Wall_Mark,12,600)
            GUICtrlSetColor($Wall_Mark,$O_Color)
        Case 10
            $Score = $Score-(10+15*2^($Level-1))
            $S_Label[6] = GUICtrlCreateLabel("Score - "&10+15*$Level,290,7,90,20,$SS_CENTER)
        Case 11 To 12
            $Score = $Score+(10+15*2^($Level-1))
            $S_Label[6] = GUICtrlCreateLabel("Score + "&10+15*$Level,290,7,90,20,$SS_CENTER)
        Case 13
            $Nuke = $Nuke-2^($Level-1)
            $S_Label[6] = GUICtrlCreateLabel("Nukes: " & $Nuke,290,7,90,20,$SS_CENTER)
        Case 14 To 15
            $Nuke = $Nuke+2^($Level-1)
            $S_Label[6] = GUICtrlCreateLabel("Nukes: " & $Nuke,290,7,90,20,$SS_CENTER)
        Case 16
            $Speed = $Speed-(10+10*2^($Level-1))
            $S_Label[6] = GUICtrlCreateLabel("Speeed: " & $Speed,290,7,90,20,$SS_CENTER)
        Case 17 To 18
            $Speed = $Speed+(10+10*2^($Level-1))
            $S_Label[6] = GUICtrlCreateLabel("Speeed: " & $Speed,290,7,90,20,$SS_CENTER)
        Case 19
            $Move_Rate = $Move_Rate+2+3*2^($Level-1)
            $Special_Rate = $Special_Rate-2^($Level-1)
            $Speed = $Speed+5+5*2^($Level-1)
            $S_Label[6] = GUICtrlCreateLabel("Super!",290,7,90,20,$SS_CENTER)
        Case 20
            $Level = $Level-1
            $S_Label[6] = GUICtrlCreateLabel("Level: " & $Level,290,7,90,20,$SS_CENTER)
        Case 21 To 22
            $Level = $Level+1
            $S_Label[6] = GUICtrlCreateLabel("Level: " & $Level,290,7,90,20,$SS_CENTER)
    EndSwitch
    $S_Label[1] = GUICtrlCreateLabel("Score: " & $Score,625,10)
    $S_Label[2] = GUICtrlCreateLabel("Nukes: " & $Nuke,625,30)
    $S_Label[3] = GUICtrlCreateLabel("Move Rate: " & $Move_Rate,625,50)
    $S_Label[4] = GUICtrlCreateLabel("Special Rate: " & $Special_Rate,625,70)
    $S_Label[0] = GUICtrlCreateLabel("Speed: " & $Speed,625,90)
    $S_Label[5] = GUICtrlCreateLabel("Level: " & $Level,625,110)
    For $z = 0 To 6
    GUICtrlSetColor($S_Label[$z],$W_Color)
    Next
EndFunc
#endregion

If I post a code or snippet, please tell me what I did wrong and/or what I can do better. Tell me my code/idea sucks, if it does. If I ask for help, refrain from only telling me to search the help file and/or the forums. I will search before asking, I just may not have searched with good keywords. If I say that I will look into something or fix something later, understand that I am most likely lying.

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