Jump to content

New & Improved RPS Game


Bradness
 Share

Recommended Posts

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

Global $RPSGame, $RockBtn, $ScissorsBtn, $PaperBtn, $RandomBtn, $HumanScore, $ComputerScore, $yScoreCount, $cScoreCount, $yScore = 0, $cScore = 0, $yChoice, $cChoice

#Region ### START Koda GUI section ### Form=c:\users\brad\desktop\programming\autoit\rpsgame.kxf
$RPSGame = GUICreate("Rock, Paper, Scissors!", 302, 81, -1, -1)
$RockBtn = GUICtrlCreateButton("Rock", 0, 0, 100, 35, $WS_GROUP)
$ScissorsBtn = GUICtrlCreateButton("Scissors", 200, 0, 100, 35, $WS_GROUP)
$PaperBtn = GUICtrlCreateButton("Paper", 100, 0, 100, 35, $WS_GROUP)
$RandomBtn = GUICtrlCreateButton("Random", 200, 35, 100, 35, $WS_GROUP)
$HumanScore = GUICtrlCreateLabel("Your Score: ", 36, 40, 63, 17)
$ComputerScore = GUICtrlCreateLabel("Computer's Score:", 7, 60, 90, 17)
$yScoreCount = GUICtrlCreateLabel("0", 104, 40, 10, 17)
$cScoreCount = GUICtrlCreateLabel("0", 104, 60, 10, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            _GUIClose($RPSGame,  20, 0)
            Exit
        Case $RockBtn
            _RPS(1)
        Case $PaperBtn
            _RPS(2)
        Case $ScissorsBtn
            _RPS(3)
        Case $RandomBtn
            _RPS()
    EndSwitch
WEnd

Func _RPS($number = 0)
    If $number <> 0 Then
        $yChoice = $number
    Else
        $yChoice = Ceiling(Random(1, 4)) - 1
    EndIf
    $cChoice = Ceiling(Random(1, 4)) - 1

    If $cChoice = 1 Then
        Switch $yChoice
            Case 1
                MsgBox(0, "You Tied!", "The computer picked rock, as well as you.")
            Case 2
                MsgBox(0, "You Win!", "You suffocated the computer's rock with a piece of paper!")
                givePointTo(1)
            Case 3
                MsgBox(0, "You Lose!", "The computer beat your pair of scissors to smithereens with its rock.")
                givePointTo(2)
        EndSwitch
    ElseIf $cChoice = 2 Then
        Switch $yChoice
            Case 1
                MsgBox(0, "You Lose!", "The computer suffocated your rock with a piece of paper.")
                givePointTo(2)
            Case 2
                MsgBox(0, "You Tied!", "The computer picked paper, as well as you.")
            Case 3
                MsgBox(0, "You Win!", "You cut the computer's paper to bits with your pair of scissors!")
                givePointTo(1)
        EndSwitch
    ElseIf $cChoice = 3 Then
        Switch $yChoice
            Case 1
                MsgBox(0, "You Win!", "You beat the computer's pair of scissors to smithereens with your rock!")
                givePointTo(1)
            Case 2
                MsgBox(0, "You Lose!", "The computer cut your paper to bits with its pair of scissors.")
                givePointTo(2)
            Case 3
                MsgBox(0, "You Tied!", "The computer picked scissors, as well as you.")
        EndSwitch
    EndIf

EndFunc

Func givePointTo($type)
    Switch $type
        Case 1
            $yScore += 1
            GUICtrlSetData($yScoreCount, $yScore)
        Case 2
            $cScore += 1
            GUICtrlSetData($cScoreCount, $cScore)
    EndSwitch
EndFunc

;#############################################################
;Parameters:
;   $Hwnd:      the window handel
;   $fSpeed:    the speed of the fading and shrinking
;   $sCase:     Where the 'suction' point is
;       0  = Center
;       1  = Center-Left
;       2  = Center-right
;       3  = Center-Top
;       4  = Center-Bottom
;       5  = Bottom-Left
;       6  = Bottom-Right
;       7  = Top-Left
;       8  = Top- Right
;       9  = Center-Middle-Vertical
;       10 = Center-Middle-Horizontal
;       11 = Roll-up
;       12 = Roll-down
;       13 = Roll-left
;       14 = Roll-right
;Author: Alek
;#############################################################
Func _GUIClose($HWnd, $fSpeed=30, $scase=0)
    ;Create some needed local $vars
    ;$s_PosnSize is needed to check what the transparency should be
    ;$s_PosnSize2 is needed to move and resize the gui.
    Local $s_PosnSize, $s_PosnSize2

    ;$s_Step is needed to store how much the size should shrink according to the speed
    Local $s_Step[2]

    ;We need $s_Trans to reduce the transparency of the GUI according to its size
    Local $s_Trans = 254

    ;Check and reset the case
    ;if the case isent a number then set it to 0
    ;if will also set it to 0 the you write "3"
    If Not IsInt($scase) Or $scase= -1 Or $scase = Default Then $scase = 0

    ;if case is less then 0 then set it to 0
    If $scase < 0 Then $scase = 0

    ;if case is more then 8 then set it to 8
    If $scase > 14 Then $scase = 14

    ;Now check the speed
    ;if its not a integer or speed = -1 or speed is default then set the speed to default (30)
    If Not IsInt($fSpeed) or $fSpeed = -1 Or $fSpeed = Default Then $fSpeed = 30

    ;if speed is less then 1 then set it to 1
    If $fSpeed < 1 Then $fSpeed = 1

    ;create a top limit, 500 should be high enough
    If $fSpeed > 500 Then $fSpeed = 500

    ;Now get the orginal pos and size of the GUI
    $s_PosnSize = WinGetPos($HWnd)

    ;Set the reduction size according to the speed
    $s_Step[0] = $s_PosnSize[2]/$fSpeed
    $s_Step[1] = $s_PosnSize[3]/$fSpeed

    ;enter a loop
    While 1
        ;Get the current pos of the GUI
        $s_PosnSize2 = WinGetPos($HWnd)

        ;check if the size is less then the reduction size or that the tranparency is less then 1
        If $s_PosnSize2[2] < $s_Step[0] Or $s_PosnSize2[3] < $s_Step[1]Or $s_Trans < 1 Then ExitLoop

        ;move and reduce the size of the GUI according to the case
        Switch $scase
            Case 0 ;Center
                WinMove( $HWnd, "", $s_PosnSize2[0]+($s_Step[0]/2), $s_PosnSize2[1]+($s_Step[1]/2), $s_PosnSize2[2]-$s_Step[0], $s_PosnSize2[3]-$s_Step[1])
            Case 1 ;Center-Left
                WinMove( $HWnd, "", $s_PosnSize2[0], $s_PosnSize2[1]+($s_Step[1]/2), $s_PosnSize2[2]-$s_Step[0],$s_PosnSize2[3]-$s_Step[1])
            Case 2 ;Center-Right
                WinMove( $HWnd, "", $s_PosnSize2[0]+$s_Step[0], $s_PosnSize2[1]+($s_Step[1]/2), $s_PosnSize2[2]-$s_Step[0], $s_PosnSize2[3]-$s_Step[1])
            Case 3 ;Center-Bottom
                WinMove( $HWnd, "", $s_PosnSize2[0]+($s_Step[0]/2), $s_PosnSize2[1], $s_PosnSize2[2]-$s_Step[0], $s_PosnSize2[3]-$s_Step[1])
            Case 4 ;Center-Top
                WinMove( $HWnd, "", $s_PosnSize2[0]+($s_Step[0]/2), $s_PosnSize2[1]+$s_Step[1], $s_PosnSize2[2]-$s_Step[0], $s_PosnSize2[3]-$s_Step[1])
            Case 5 ;Bottom-Left
                WinMove( $HWnd, "", $s_PosnSize2[0], $s_PosnSize2[1]+$s_Step[1], $s_PosnSize2[2]-$s_Step[0], $s_PosnSize2[3]-$s_Step[1])
            Case 6 ;Bottom-Right
                WinMove( $HWnd, "", $s_PosnSize2[0]+$s_Step[0], $s_PosnSize2[1]+$s_Step[1], $s_PosnSize2[2]-$s_Step[0], $s_PosnSize2[3]-$s_Step[1])
            Case 7 ;Top-Left
                WinMove( $HWnd, "", $s_PosnSize2[0] , $s_PosnSize2[1], $s_PosnSize2[2]-$s_Step[0], $s_PosnSize2[3]-$s_Step[1])
            Case 8 ;Top-Right
                WinMove( $HWnd, "", $s_PosnSize2[0]+$s_Step[0], $s_PosnSize2[1], $s_PosnSize2[2]-$s_Step[0], $s_PosnSize2[3]-$s_Step[1])
            Case 9 ;Center-Middle-Vertical
                WinMove( $HWnd, "", $s_PosnSize2[0]+($s_Step[0]/2), $s_PosnSize2[1], $s_PosnSize2[2]-$s_Step[0], $s_PosnSize2[3])
            Case 10 ;Center-Middle-Horizontal
                WinMove( $HWnd, "", $s_PosnSize2[0], $s_PosnSize2[1]+($s_Step[1]/2), $s_PosnSize2[2], $s_PosnSize2[3]-$s_Step[1])
            Case 11 ;Roll-up
                WinMove( $HWnd, "", $s_PosnSize2[0], $s_PosnSize2[1], $s_PosnSize2[2], $s_PosnSize2[3]-$s_Step[1])
            Case 12 ;Roll-down
                WinMove( $HWnd, "", $s_PosnSize2[0], $s_PosnSize2[1]+$s_Step[1], $s_PosnSize2[2], $s_PosnSize2[3]-$s_Step[1])
            Case 13 ;Roll-left
                WinMove( $HWnd, "", $s_PosnSize2[0], $s_PosnSize2[1], $s_PosnSize2[2]-$s_Step[0], $s_PosnSize2[3])
            Case 14 ;Roll-right
                WinMove( $HWnd, "", $s_PosnSize2[0]+$s_Step[0], $s_PosnSize2[1], $s_PosnSize2[2]-$s_Step[0], $s_PosnSize2[3])
        EndSwitch

        ;check what size to use to calculate the transparency
        If $s_PosnSize2[2]<> $s_PosnSize[2] Then

            ;Calculate what the transparency should be.
            $s_Trans = 254/100*(($s_PosnSize2[2]/$s_PosnSize[2])*100)
        ElseIf $s_PosnSize2[3]<> $s_PosnSize[3] Then


            ;Calculate what the transparency should be.
            $s_Trans = 254/100*(($s_PosnSize2[3]/$s_PosnSize[3])*100)
        EndIf

        ;Set the transparency of the window
        WinSetTrans($HWnd,"",$s_Trans)

        ;Sleep to slow down the process so you can see the window shrinking and fadeing
        Sleep(1)
    WEnd

    Return 1
EndFunc

Credits to Alek for the nice close effect.

I had created a Rock Paper Scissors script before, which after time stopped working. This one is much nicer anyways.

Enjoi.

My Programs:Rock Paper ScissorsMy UDFs:NONE
Link to comment
Share on other sites

A few things:

1) Once you reach double figures, it no longer has room in the score label

2) I lost 235 - 237 to the cpu by pressing random 1650 times. (not a valid comment I know)

That fade out affect is different, but I don't think it did exactly what you wanted it to on my computer (slow and old :D )

Other than that... How about playing over tcp?

MDiesel

Link to comment
Share on other sites

Because I don't like TrayTip.

:D:D:D

Só o que posso lhe dizer, bom é quando faz mal!My work:Au3Irrlicht - Irrlicht for AutoItMsAgentLib - An UDF for MSAgentAu3GlPlugin T2 - A 3D plugin for AutoIt...OpenGl Plugin - The old version of Au3GlPlugin.MAC Address Changer - Changes the MAC AddressItCopter - A dragonfly R/C helicopter simulator

VW Bug user

Pinheiral (Pinewood) city:

http://pt.wikipedia.org/wiki/Pinheiral

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...