Sign in to follow this  
Followers 0
SilvaSurf

Help with program

7 posts in this topic

Hi, first of all "Hi to everyone" and I'm new, so have mercy on me.

I created a program, that does the following:

If a button "randomize" is pressed it first it picks a random card (out of a deck of 52 playing card), it also shows the image of the card. If I press then another button "result" it automatically gives me the "partner card", which would be a card that added to the first would result in "13". The suits are partnered as Hearts <-> Spades and Clubs <-> Diamonds.

So for example, if randomly get a 3 of Hearts, the "result" would be 10 of spades (10+3=13) and Hearts -> Spades. Another Example would be the Ace (equals "1") of Diamonds, the "result" is King (12) of Clubs.

Now, the program works perfectly except for the images. The first time the "randomize" and then "result" button is pressed, everything works out, I get the images correctly, but if I press again the randomize button, the written output gives me another card, but the images stays on the first image, the same goes for the "result" button. And from now on, it is always one card behind. If I press the "randomize" button a third time, I get the image of the second randomize button, and so on.

I hope I could explain my problem right. Again, the "written" output of the program works perfectly, just the images are always left one card behind (except the first time)

Here is the program:

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.0.0
 Author:         myName

 Script Function:
    Template AutoIt script.

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here

#include <GUIConstantsEx.au3>
Global $counterValue, $counterSuit
Global $imageCounterSuit, $imageCounterValue


GUICreate("The Invisible Deck", 640, 480)
$okbutton = GUICtrlCreateButton("Result", 150, 70, 60)
$resetButton = GUICtrlCreateButton("Randomize", 20, 70, 60)




Func randomCard()
        $RandomCard = Random(1,52,1);

    Switch $RandomCard
        Case 1 to 13    
            $suit = "Clubs"
            $imageSuit = "c"
            $value = $RandomCard
            $counterSuit = "Diamonds"
            $imageCounterSuit = "d"
        Case 14 to 26   
            $suit = "Hearts"
            $imageSuit = "h"
            $value = $RandomCard - 13
            $counterSuit = "Spades"
            $imageCounterSuit = "s"
        Case 27 to 39   
            $suit = "Spades"
            $imageSuit = "s"
            $value = $RandomCard - 26
            $counterSuit = "Hearts"
            $imageCounterSuit = "h"
        Case 40 to 52   
            $suit = "Diamonds"
            $imageSuit = "d"
            $value = $RandomCard - 39
            $counterSuit = "Clubs"
            $imageCounterSuit = "c"
        Case Else
            $suit = "!!TILT!!"
        EndSwitch

    $counterValue = 13 - $value;
    $imageCounterValue = 13 - $value;
    $imageValue = $value

    Switch $value
        Case 1
            $value = "Ace"
            $imageValue = "a"
        Case 11
            $value = "Jack"
            $imageValue = "j"
        Case 12
            $value = "Queen"
            $imageValue = "q"
        Case 13 
            $value = "King"
            $imageValue = "k"
    EndSwitch

    Switch $counterValue
        Case 1
            $counterValue = "Ace"
            $imageCounterValue ="a"
        Case 11
            $counterValue = "Jack"
            $imageCounterValue ="j"
        Case 12
            $counterValue = "Queen"
            $imageCounterValue ="q"
        Case 0 
            $counterValue = "King"
            $imageCounterValue ="k"
    EndSwitch
    GUICtrlCreateLabel("                                                                                         ",5,30)
    GUICtrlCreateLabel("The number is " & $RandomCard & " and the card is the " & $value & " of " & $suit, 5,30)
    GUICtrlCreatePic(".\images\" & $imageValue & $imageSuit & ".gif",350,30,73,98)
    GUISetState()
    
    
EndFunc


Func result()
    GUICtrlCreateLabel("                                                           ",5,220)
    GUICtrlCreateLabel("So look for the " & $counterValue & " of " & $counterSuit, 5, 220)
    GUICtrlCreatePic(".\images\" & $imageCounterValue & $imageCounterSuit & ".gif",350,220,73,98)
    GUISetState()
EndFunc




GUISetState(@SW_SHOW)


While 1
  $msg = GUIGetMsg()

  Select
    Case $msg = $okbutton
        result()
    Case $msg = $resetButton
        randomCard()
    Case $msg = $GUI_EVENT_CLOSE
      ;MsgBox(0, "GUI Event", "You clicked CLOSE! Exiting...")
      ExitLoop
  EndSelect
WEnd

Maybe someone can help me. Oh and btw, the images are named simply as they are. 10S.gif (Ten of Spades) KC.gif (King of Clubs), etc

Thanks you

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Try getting the handle of the picture control and then changing the displayed image rather than creating a new control every time. Also, choose a more descriptive title for your topic. The functions usually follow the main body of the program.

Edited by qazwsx

Share this post


Link to post
Share on other sites

thank you. ok the funcs have to go after the main program. And it may be better to change the image than creating a new one. But these are only semantics, those don't solve my problem. If it does, please elaborate.

As for the title, I know, but I could not think of any title that would fit. The problem is too specific.

Share this post


Link to post
Share on other sites

The things after the also are "semantics". However, using guictrlsetimage rather than creating a new control should solve your problem. What about: "image control not updating properly" or something similar.

Share this post


Link to post
Share on other sites

ok, i will try that when I get home, thanks.

Where is the EDIT button here? Or where can I change the topic? Wanted to follow your advice...

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

The edit button is found at the bottom right of your posts, next to reply.

Edited by qazwsx

Share this post


Link to post
Share on other sites

you were right, I worked with CtrlSetImage and the error is gone. Although I am not quite sure as to why? The behaviour should be the same, whether I update my image with CtrlSetImage or CreatePic. Maybe not as memory efficient or something, but with a program that size it makes no difference IMO. Why did I encounter that problem?

Two more questions:

1.When I initally create the image with GUICtrlCreatePic, I don't want any card shown, because there hasn't been selected one yet. I tried to us dummy.gif as a filename (which doesn't exit), but the I get no image update with SetImage, no images show up at all. Can't I use CreatePic without a filename, just to get the ID?

2. The card images have a 1 px frame around them, however the bottom frame keeps getting cut off, no matter what imagesizes I use. What could that be?

Oh and P.S.: For some reason, I don't have an Edit Button?! HERE is a screenshot!

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