Jump to content

Simon


Paulie
 Share

Recommended Posts

Hey guys, I was bored. Threw this game together in about an hour. Has a few bad coding practices like using global vars in funcs, but it works pretty well I'm really horrible at it though, can't get over 10.

Maybe sometime I will get around to adding a pretty intro screen and score display or maybe a repeat system? :)

Controls:

Use mouse

or

Arrow keys:

Left = Red

Up = Green

Down = Blue

Right = Yellow

(Used gafrost's round GUI corners to make the circle, thank him for that!)

Enjoy:

Version: 2.0

;========== SIMON the Memory Game ==========
;   Created By: Paulie
;   Version: 2.0
;===========================================
#Region === INCLUDES, OPTS, & HOTKEYS ===
;Includes
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <StaticConstants.au3>
#include <Misc.au3>
;Opts
Opt('GuiOnEventMode', 1)
;Hotkeys
HotKeySet("{UP}", "HClick")
HotKeySet("{DOWN}", "HClick")
HotKeySet("{LEFT}", "HClick")
HotKeySet("{RIGHT}", "HClick")
#EndRegion === INCLUDES, OPTS, & HOTKEYS ===
#Region === GLOBAL VARIABLES/SETTINGS ===
$UseSounds = 1
$TimePeriod = 2000 ;miliseconds
$RedFreq = 1000
$GreenFreq = 750
$BlueFreq = 500
$YellowFreq = 250
$Score = 0
$Current = 0
$Clicked = 0
$Duration = 250;miliseconds
$Last = 0
$GameOn = 0
$Max = 100;game ends after
Dim $Pattern[$Max + 1]
For $i = 1 To $Max
    $Pattern[$i] = Random(1, 4, 1)
    If $i > 1 Then
        If $Pattern[$i - 1] = $Pattern[$i] Then $Pattern[$i] = Random(1, 4, 1)
    EndIf
Next
#EndRegion === GLOBAL VARIABLES/SETTINGS ===
;GUI
$GUI = GUICreate("Simon", 450, 250)
GUISetOnEvent($GUI_EVENT_CLOSE, "Quit")
GUISetBkColor(0x9DB9EB)
GUICtrlCreateLabel("Simon", 0, 0, 450, 73, $SS_CENTER)
GUICtrlSetFont(-1, 48, 400, 0, "Pump Demi Bold LET")
GUICtrlCreateLabel("Think Fast! It's SIMON!" & @CRLF & "Score points by correctly repeating the ever-" & _
        "lengthening sequence of random lights and sounds that SIMON generates. Beat the High Score!", 0, 75, 450, 100, $SS_CENTER)
GUICtrlSetFont(-1, 10, 400, 0, "Arial Black")
$Button1 = GUICtrlCreateButton("Play", 175, 180, 100, 49, 0)
GUICtrlSetOnEvent(-1, "GameOn")
GUISetState(@SW_SHOW)
;GUI Loop
While Not $GameOn = 1
    Sleep(100)
WEnd
GUIDelete($GUI)

;GUI 1 (Board)
$Simon = GUICreate("Simon", 450, 350, -1, -1, $WS_POPUP)
GuiRoundCorners($Simon, 0, 0, 450, 350)
$Red = GUICtrlCreateLabel("", 0, 0, 225, 175)
GUICtrlSetBkColor(-1, 0xAA0000)
GUICtrlSetOnEvent(-1, "Clicked")
$Green = GUICtrlCreateLabel("", 225, 0, 225, 175)
GUICtrlSetBkColor(-1, 0x00AA00)
GUICtrlSetOnEvent(-1, "Clicked")
$Blue = GUICtrlCreateLabel("", 0, 175, 225, 175)
GUICtrlSetBkColor(-1, 0x0000AA)
GUICtrlSetOnEvent(-1, "Clicked")
$Yellow = GUICtrlCreateLabel("", 225, 175, 225, 175)
GUICtrlSetBkColor(-1, 0xAAAA00)
GUICtrlSetOnEvent(-1, "Clicked")
GUISetState(@SW_SHOW, $Simon)
;GUI 2 (Scoreboard)
$Scoreboard = GUICreate("Info", 272, 106, -1, 100, -1, $WS_EX_TOOLWINDOW, $Simon)
GUISetBkColor(0x9DB9EB)
GUISetOnEvent($GUI_EVENT_CLOSE, "Quit")
GUICtrlCreateLabel("Score: ", 40, 16, 38, 17)
$Turn = GUICtrlCreateInput("Computer's Turn", 40, 48, 193, 21, BitOR($ES_CENTER, $ES_READONLY))
$Time = GUICtrlCreateLabel("2.0", 0, 80, 272, 20, $SS_CENTER)
GUICtrlCreateLabel("High Score: ", 144, 16, 63, 17)
$ScoreDisp = GUICtrlCreateLabel("0", 88, 16, 15, 17)
$HScoreDisp = GUICtrlCreateLabel(GetHighScore(), 216, 16, 15, 17)
GUISetState(@SW_SHOW)

While $Current <= $Max
    $Current += 1
    If $Current = 1 Then Splash()
    Computer($Current)
    If Not Human($Current) Then
        $Prob = $Last
        If IsArray($Prob) Then
            Lose($Prob[0], $Prob[1])
        Else
            Lose(0)
        EndIf
    EndIf
    If WinActive($Simon) Then WinSetOnTop($Scoreboard, "", 1)
WEnd

Func Computer($Current)
    GUICtrlSetData($Turn, "Computer's Turn")
    GUICtrlSetColor($Turn, 0xFF0000)
    Buttons(0)
    GUICtrlSetData($Time, "2.0")
    Sleep(1000)
    For $i = 1 To $Current
        Blink($Pattern[$i])
        Sleep(500)
    Next
EndFunc   ;==>Computer
Func Human($Current)
    GUICtrlSetData($Turn, "Your Turn")
    GUICtrlSetColor($Turn, 0x0000FF)
    For $i = 1 To $Current
        $Clicked = 0
        $Timer = TimerInit()
        Buttons(1)
        Do
            If GUICtrlRead($Time) <> 2 - (Round(TimerDiff($Timer) / 1000, 1)) Then GUICtrlSetData($Time, 2 - (Round(TimerDiff($Timer) / 1000, 1)))
            Sleep(10)
        Until $Clicked > 0 Or TimerDiff($Timer) >= $TimePeriod
        If Not $Clicked > 0 And TimerDiff($Timer) >= $TimePeriod Then
            $Last = -1
            Return 0
        EndIf
        If $Clicked = $Pattern[$i] Then
            If $i = $Current Then
                $Score += 1
                Update()
            EndIf
        Else
            Dim $Last[2]
            $Last[0] = $Clicked
            $Last[1] = $Pattern[$i]
            Return 0
        EndIf
        $Clicked = 0
    Next
    Return 1
EndFunc   ;==>Human
Func Lose($1 = 0, $2 = 0)
    Buttons(0)
    If $1 = 0 Then
        $Reason = "You ran out of time."
    Else
        Switch $1
            Case 1
                $String1 = "Red"
            Case 2
                $String1 = "Green"
            Case 3
                $String1 = "Blue"
            Case 4
                $String1 = "Yellow"
        EndSwitch
        Switch $2
            Case 1
                $String2 = "Red"
            Case 2
                $String2 = "Green"
            Case 3
                $String2 = "Blue"
            Case 4
                $String2 = "Yellow"
        EndSwitch
        $Reason = "You clicked " & $String1 & " when you should have clicked " & $String2 & "."
    EndIf
    If CheckHighScore($Score) Then
        SetHighScore($Score)
        $Retry = MsgBox(4, "", "Game Over. " & @CRLF & $Reason & @CRLF & "Score: " & $Score & @CRLF & "Congratulations! New High Score!" & @CRLF & "Do you want to try again?")
        If $Retry = 6 Then
            Retry()
        Else
            Exit
        EndIf
    Else
        $Retry = MsgBox(4, "", "Game Over." & @CRLF & $Reason & @CRLF & "Score: " & $Score & @CRLF & "Sorry! You didn't beat the high score of " & GetHighScore() & @CRLF & "Do you want to try again?")
        If $Retry = 6 Then
            Retry()
        Else
            Exit
        EndIf
    EndIf
EndFunc   ;==>Lose
Func Retry()
    $Score = 0
    GUICtrlSetData($ScoreDisp, $Score)
    For $i = 1 To $Max
        $Pattern[$i] = Random(1, 4, 1)
        If $i > 1 Then
            If $Pattern[$i - 1] = $Pattern[$i] Then $Pattern[$i] = Random(1, 4, 1)
        EndIf
    Next
    $Current = 0
EndFunc   ;==>Retry
Func Update()
    GUICtrlSetData($ScoreDisp, $Score)
    If CheckHighScore($Score) Then GUICtrlSetData($HScoreDisp, $Score)
EndFunc   ;==>Update
Func Splash()
    $GameOn = 0
    Buttons(0)
    $GUI = GUICreate("Splash", 200, 100, -1, -1, $WS_POPUP, $WS_EX_TOOLWINDOW)
    GUISetOnEvent($GUI_EVENT_PRIMARYUP, "GameOn")
    GUISetBkColor(0x9DB9EB)
    GUICtrlCreateLabel("Ready" & @CRLF & "Click to begin", 0, 25, 200, 50, $SS_CENTER)
    GUICtrlSetFont(-1, 15, 450)
    GUISetState()
    While Not $GameOn
        Sleep(100)
    WEnd
    GUIDelete($GUI)
    Buttons(1)
EndFunc   ;==>Splash
Func Quit()
    Exit
EndFunc   ;==>Quit
Func Filler()
    ;Do Nothing
EndFunc   ;==>Filler
Func GameOn()
    $GameOn = 1
EndFunc   ;==>GameOn
Func Buttons($on_off)
    If $on_off = 0 Then
        HotKeySet("{UP}", "Filler")
        HotKeySet("{DOWN}", "Filler")
        HotKeySet("{LEFT}", "Filler")
        HotKeySet("{RIGHT}", "Filler")
        GUICtrlSetOnEvent($Red, "Filler")
        GUICtrlSetOnEvent($Green, "Filler")
        GUICtrlSetOnEvent($Blue, "Filler")
        GUICtrlSetOnEvent($Yellow, "Filler")
    Else
        GUICtrlSetOnEvent($Red, "Clicked")
        GUICtrlSetOnEvent($Green, "Clicked")
        GUICtrlSetOnEvent($Blue, "Clicked")
        GUICtrlSetOnEvent($Yellow, "Clicked")
        HotKeySet("{UP}", "HClick")
        HotKeySet("{DOWN}", "HClick")
        HotKeySet("{LEFT}", "HClick")
        HotKeySet("{RIGHT}", "HClick")
    EndIf
EndFunc   ;==>Buttons
Func HClick()
    If @HotKeyPressed = "{LEFT}" Then $Num = 1
    If @HotKeyPressed = "{UP}" Then $Num = 2
    If @HotKeyPressed = "{DOWN}" Then $Num = 3
    If @HotKeyPressed = "{RIGHT}" Then $Num = 4
    Switch $Num
        Case 1
            ControlClick("Simon", "", $Red)
        Case 2
            ControlClick("Simon", "", $Green)
        Case 3
            ControlClick("Simon", "", $Blue)
        Case 4
            ControlClick("Simon", "", $Yellow)
    EndSwitch
EndFunc   ;==>HClick
Func Clicked()
    $Color = @GUI_CtrlId
    If $Color = $Red Then $Num = 1
    If $Color = $Green Then $Num = 2
    If $Color = $Blue Then $Num = 3
    If $Color = $Yellow Then $Num = 4
    Switch $Num
        Case 1
            Buttons(0)
            GUICtrlSetBkColor($Red, 0xFF0000)
            If $UseSounds Then
                Beep($RedFreq, $Duration)
            Else
                Sleep($Duration)
            EndIf
            GUICtrlSetBkColor($Red, 0xAA0000)
            $Clicked = 1
        Case 2
            Buttons(0)
            GUICtrlSetBkColor($Green, 0x00FF00)
            If $UseSounds Then
                Beep($GreenFreq, $Duration)
            Else
                Sleep($Duration)
            EndIf
            GUICtrlSetBkColor($Green, 0x00AA00)
            $Clicked = 2
        Case 3
            Buttons(0)
            GUICtrlSetBkColor($Blue, 0x0000FF)
            If $UseSounds Then
                Beep($BlueFreq, $Duration)
            Else
                Sleep($Duration)
            EndIf
            GUICtrlSetBkColor($Blue, 0x0000AA)
            $Clicked = 3
        Case 4
            Buttons(0)
            GUICtrlSetBkColor($Yellow, 0xFFFF00)
            If $UseSounds Then
                Beep($YellowFreq, $Duration)
            Else
                Sleep($Duration)
            EndIf
            GUICtrlSetBkColor($Yellow, 0xAAAA00)
            $Clicked = 4
    EndSwitch
EndFunc   ;==>Clicked
Func Blink($Color)
    Switch $Color
        Case 1
            GUICtrlSetBkColor($Red, 0xFF0000)
            If $UseSounds Then
                Beep($RedFreq, $Duration)
            Else
                Sleep($Duration)
            EndIf
            GUICtrlSetBkColor($Red, 0xAA0000)
        Case 2
            GUICtrlSetBkColor($Green, 0x00FF00)
            If $UseSounds Then
                Beep($GreenFreq, $Duration)
            Else
                Sleep($Duration)
            EndIf
            GUICtrlSetBkColor($Green, 0x00AA00)
        Case 3
            GUICtrlSetBkColor($Blue, 0x0000FF)
            If $UseSounds Then
                Beep($BlueFreq, $Duration)
            Else
                Sleep($Duration)
            EndIf
            GUICtrlSetBkColor($Blue, 0x0000AA)
        Case 4
            GUICtrlSetBkColor($Yellow, 0xFFFF00)
            If $UseSounds Then
                Beep($YellowFreq, $Duration)
            Else
                Sleep($Duration)
            EndIf
            GUICtrlSetBkColor($Yellow, 0xAAAA00)
    EndSwitch
EndFunc   ;==>Blink
Func GetHighScore()
    $HScore = RegRead("HKCU\Software\Simon", "High")
    If @error Then Return 0
    Return $HScore
EndFunc   ;==>GetHighScore
Func CheckHighScore($Value)
    If $Value > GetHighScore() Then
        Return 1
    Else
        Return 0
    EndIf
EndFunc   ;==>CheckHighScore
Func SetHighScore($Value)
    RegWrite("HKCU\Software\Simon", "High", "REG_SZ", $Value)
EndFunc   ;==>SetHighScore
;===============================================================================
; Function Name: GuiRoundCorners()
; Author(s): gaFrost
;===============================================================================
Func GuiRoundCorners($h_win, $i_x1, $i_y1, $i_x3, $i_y3)
    Dim $pos, $ret, $ret2
    $pos = WinGetPos($h_win)
    $ret = DllCall("gdi32.dll", "long", "CreateRoundRectRgn", "long", $i_x1, "long", $i_y1, "long", $pos[2], "long", $pos[3], "long", $i_x3, "long", $i_y3)
    If $ret[0] Then
        $ret2 = DllCall("user32.dll", "long", "SetWindowRgn", "hwnd", $h_win, "long", $ret[0], "int", 1)
        If $ret2[0] Then
            Return 1
        Else
            Return 0
        EndIf
    Else
        Return 0
    EndIf
EndFunc   ;==>GuiRoundCorners
Edited by Paulie
Link to comment
Share on other sites

Looks great but I have no idea what I'm doing, mind giving a brief explanation? I'll keep playing around with it.. the flashing lights and blinking noises amaze me :)

Ahh: I get it... I got to 15! Nice work! The simplicity makes it awesome! Maybe make your own beep sounds with .mp3 or something.. the Beep() seem kind of cheezy...

Edited by Piano_Man
My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]
Link to comment
Share on other sites

Looks great but I have no idea what I'm doing, mind giving a brief explanation? I'll keep playing around with it.. the flashing lights and blinking noises amaze me :)

You've never played Simon? ^_^

Its a memory game where you have to repeat the sequences of blinking lights. it goes on "My turn" then you repeat it on "your turn"

:) i assumed everyone has played it

Posted Image

Edited by Paulie
Link to comment
Share on other sites

@Paulie: I've noticed you've dropped out of the Game Competition... you could brush this up some and submit it... It would probably be better the a lot of games.. it's definitely pretty addictive (I'm not sure why)...

Edit: Typos

Edited by Piano_Man
My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]
Link to comment
Share on other sites

Paulie, I thought you were out of the competition?

I thought i was too, but i just played the flash version of this game, and the idea just hit me... :)^_^ *Shrug*

@Paulie: I've noticed you've dropped out of the Game Competition... you could brush this up some and submit it... It would probably be better the a lot of games.. it's definitely pretty addictive (I'm not sure why)...

Edit: Typos

i think i'm going to submit it. I linked to this thread in the game competition thread. If i get around to improving this, I'll see how it fairs. Definitely unoriginal though... :)
Link to comment
Share on other sites

its fun! -I suck at it :) Have you considered making the arror keys behave as clicks? -It was a little hard for me since I am using a laptop mouse when not at work . . .

That I have considered.

Let me try and throw that in now

Edit:

Egads, thats a mess...

Tried to throw in one of these:

HotKeySet("{UP}", "HClick")
HotKeySet("{DOWN}", "HClick")
HotKeySet("{LEFT}", "HClick")
HotKeySet("{RIGHT}","HClick")

Func HClick()   
    IF @HotKeyPressed = "{LEFT}" Then $Num = 1
    IF @HotKeyPressed = "{UP}" Then $Num = 2
    IF @HotKeyPressed = "{DOWN}" Then $Num = 3
    IF @HotKeyPressed = "{RIGHT}" Then $Num = 4
    Switch $Num
        Case 1
            ControlClick("Simon", "", $Red)
        Case 2
            ControlClick("Simon", "", $Green)
        Case 3
            ControlClick("Simon", "", $Blue)
        Case 4
            ControlClick("Simon", "", $Yellow)
    EndSwitch
EndFunc

But that did not activate the @GUI_CtrlID Macro therefore, it didn't work..

Edited by Paulie
Link to comment
Share on other sites

I don't get it.

It goes way to fast for me... I open it up and if I manage to click the right thing then I am sorta lost. You gotta give people like me time to switch from your turn to my turn :)

What don't you get?

Right when the script runs, one random label blinks. This is the first turn by the computer.

Now you click that label, and it will respond by playing that one and a new one. and you end up having to remember a long string of them.

It helps if you make it into a song ^_^

Link to comment
Share on other sites

here ya go

HotKeySet("{UP}", "HClick")
HotKeySet("{DOWN}", "HClick")
HotKeySet("{LEFT}", "HClick")
HotKeySet("{RIGHT}","HClick")

Func HClick()   
    IF @HotKeyPressed = "{LEFT}" Then $Num = 1
    IF @HotKeyPressed = "{UP}" Then $Num = 2
    IF @HotKeyPressed = "{DOWN}" Then $Num = 3
    IF @HotKeyPressed = "{RIGHT}" Then $Num = 4
    Switch $Num
        Case 1
            ControlClick("Simon", "", Execute($Red))
        Case 2
            ControlClick("Simon", "", Execute($Green))
        Case 3
            ControlClick("Simon", "", Execute($Blue))
        Case 4
            ControlClick("Simon", "", Execute($Yellow))
    EndSwitch
EndFunc

Edit: Good to hear its working. I am doing better now :)

Edited by Hatcheda
Link to comment
Share on other sites

umm maybe you ca do this with numbers?

lvl1: show randomply placed nr-s from 1 to 10 on the screen ( the user has lets say 5 seconds to click each nr. in its correct order, sample if user starts clicking 1,2,3,5 = error(you missed 4) )

lvl2: flash show for 5 sec some randomply placed nr.s this time only 5 nr-s

lvl3: same as lvl2 just flash lets say 10 nr-s

Feedback:

If i run the game i get this error , after i remove $WS_POPUP the game starts, but if i click on the color, nothing happens.

$Simon = GUICreate ("Simon", 450, 350, -1,-1, $WS_POPUP)
ERROR: $WS_POPUP: undeclared global variable.
My Projects:[list][*]Guide - ytube step by step tut for reading memory with autoitscript + samples[*]WinHide - tool to show hide windows, Skinned With GDI+[*]Virtualdub batch job list maker - Batch Process all files with same settings[*]Exp calc - Exp calculator for online games[*]Automated Microsoft SQL Server 2000 installer[*]Image sorter helper for IrfanView - 1 click opens img & move ur mouse to close opened img[/list]
Link to comment
Share on other sites

Feedback:

If i run the game i get this error , after i remove $WS_POPUP the game starts, but if i click on the color, nothing happens.

$Simon = GUICreate ("Simon", 450, 350, -1,-1, $WS_POPUP)
ERROR: $WS_POPUP: undeclared global variable.
Did you keep the #include line on the top? If you leave that out, That variable will be undefined. Edited by Paulie
Link to comment
Share on other sites

It helps if you make it into a song :)

Not for me... In fact I barely even notice the notes... I just remember the patterns...

And for a suggestion: Maybe you could make difficulties, where it speeds up or slows down.. Or maybe make it speed up after a certain point... Just some ideas..

My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]
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...