This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2012-02-08 23:17:23

pdubu
New Scratcher
Registered: 2012-01-29
Posts: 19

Hangman Script Help!

Hello everybody, I need some help with a game I'm trying to program. It's basically hangman without the animations.

I'd appreciate any contributions!

So far, I've come up with the part of the script that determines which word to use (provided that the player sets the number of letters). Now, I need to program the part of the game that allows the player to guess a word, and for the sprite to show which letter appears in the appropriate spot (for example, if the word were game, and the player guesses "g", it'd appear on top of the sprite as g _ _ _). However, if the player guesses incorrectly, then the sprite should inform them that the player has lost a life. This should continue per guess, so if the next word the player guesses is a "m", then it should say, g _ m _ . Until the player either runs out of lives, or, guesses the word entirely.

Another concern I have is, once the letter has already been guessed, how would I prevent the player from guessing the same letter again? And if they do decide to input a repeated value, how would I go about alerting them?

Thank you all in advance.

Offline

 

#2 2012-02-09 00:01:03

Magnie
Scratcher
Registered: 2007-12-12
Posts: 1000+

Re: Hangman Script Help!

when gf clicked
set [lives v] to (5)
set [word v] to [word]
delete [all] of [word_letters v]
set [i v] to (0)
repeat <length of [word]>
    add <letter (i) of (word)> to [word_letters v]
    change [i v] by (1)
end
delete [all] of [guesses v]
forever if <(lives) > (0)>
    ask [What letter do you want to guess?] and wait
    if <[guesses v] contains (answer)>
        say [You already guessed that!] for (3) secs
    else
        add (answer) to [guesses v]
        if <[word_letters v] contains (answer)>
            set [i v] to (0)
            repeat <length of [word_letters v]>
                if <(answer) = <item (i) of [word_letters v]>>
                    delete (i) of [word_letters v]
                end
                change [i v] by (1)
             end
        else
             say [That letter is not in the word!] for (3) secs
             change [lives v] by (-1)
        end
    end
    say [You have guessed the following letters] for (3) secs
    say (guesses) for <<length of [guesses v]> / (2)> secs
Also (guesses) is the list, not a variable.  smile

The problem is though, I'm not sure how I would make it do something like this:

Word = test
Word_Display = ____
Letter: t
Word_Display = t__t
Letter: s
Word_Display = t_st
Letter: e
Word_Display = test
Word_Display = You got it!

You could do something like:

when gf clicked
set [word_display v] to []
forever
    set [n v] to (0)
    set [word_display v] to []
    repeat <length of (word)>
        if <[word_letters v] contains <item (n) of [guesses v]>>
            set [word_display v] to <join (word_display) <item (n) of [guesses v]>>
        else
            set [word_display v] to <join (word_display) [_]>
        end
        change [n v] by (1)
    end
    say (word_display) for (3) secs
It should be quite simple to merge those two scripts together. Try 'em out and tell us if it works.  big_smile

Last edited by Magnie (2012-02-09 00:11:51)

Offline

 

#3 2012-02-09 00:13:53

pdubu
New Scratcher
Registered: 2012-01-29
Posts: 19

Re: Hangman Script Help!

Could you explain the script beyond the point of set i to 0, repeat length of word letters, and so on? I somewhat got lost at that point. Also, it appears as though you're deleting the letters from the word list if they match it correctly--if I do that, how will I eventually determine whether they've guessed all the letters correctly?

Thank you for helping, I appreciate it  smile

Offline

 

#4 2012-02-09 01:03:48

Magnie
Scratcher
Registered: 2007-12-12
Posts: 1000+

Re: Hangman Script Help!

Haha, whoops, you can replace the deletion part with a say block saying something like "The letter is in the word!".

Btw: It's 11PM here, so I wasn't/am-not thinking straight.  tongue

when gf clicked
set [lives v] to (5)
set [word v] to [word]
delete [all] of [word_letters v]
set [i v] to (0)
repeat <length of [word]>
    add <letter (i) of (word)> to [word_letters v]
    change [i v] by (1)
end
delete [all] of [guesses v]
forever if <(lives) > (0)>
    ask [What letter do you want to guess?] and wait
    if <[guesses v] contains (answer)>
        say [You already guessed that!] for (3) secs
    else
        add (answer) to [guesses v]
        if <[word_letters v] contains (answer)>
            say [The letter is in the word!] for (3) secs
        else
             say [That letter is not in the word!] for (3) secs
             change [lives v] by (-1)
        end
    end
    say [You have guessed the following letters] for (3) secs
    say (guesses) for <<length of [guesses v]> / (2)> secs

    set [i v] to (0)
    set [word_display v] to []
    repeat <length of (word)>
        if <[word_letters v] contains <item (i) of [guesses v]>>
            set [word_display v] to <join (word_display) <item (i) of [guesses v]>>
        else
            set [word_display v] to <join (word_display) [_]>
        end
        change [i v] by (1)
    end
    say (word_display) for (3) secs
If you want, I can explain all the code later.

Last edited by Magnie (2012-02-09 01:27:25)

Offline

 

#5 2012-02-10 09:33:28

Connor-McIntosh
New Scratcher
Registered: 2012-02-10
Posts: 18

Re: Hangman Script Help!

Hi Everyone I'll Really need to know all the script on this so can u help

Offline

 

#6 2012-02-10 11:14:08

Magnie
Scratcher
Registered: 2007-12-12
Posts: 1000+

Re: Hangman Script Help!

Connor-McIntosh wrote:

Hi Everyone I'll Really need to know all the script on this so can u help

There is a script above, you can use that one. Could you explain what you need help with more thoroughly?

Offline

 

#7 2012-02-10 12:48:46

Connor-McIntosh
New Scratcher
Registered: 2012-02-10
Posts: 18

Re: Hangman Script Help!

Do you know the game eight ball

Offline

 

#8 2012-02-10 13:01:58

Magnie
Scratcher
Registered: 2007-12-12
Posts: 1000+

Re: Hangman Script Help!

Connor-McIntosh wrote:

Do you know the game eight ball

I've played with an Eight Ball before.

Could you please explain you are trying to do with Scratch so we can help?

Offline

 

#9 2012-02-11 09:41:34

Connor-McIntosh
New Scratcher
Registered: 2012-02-10
Posts: 18

Re: Hangman Script Help!

I'll Would Like To Know How To Make A maze on my first Map Then a Hangman on the other 1 Then a Math quiz o  there other but i would like when i start a game u have to press play to start or information for wat to do and leaderboard

Offline

 

#10 2012-02-12 12:06:01

silvershine
Scratcher
Registered: 2010-11-21
Posts: 500+

Re: Hangman Script Help!

Connor-McIntosh wrote:

I'll Would Like To Know How To Make A maze on my first Map Then a Hangman on the other 1 Then a Math quiz o  there other but i would like when i start a game u have to press play to start or information for wat to do and leaderboard

It sounds like what you're doing is pretty complex. To organize all those scripts, I usually create a main script that broadcasts things in the order in which they happen.
On the main menu, create a sprite and program it to start the maze game when it's clicked.

when  [Maze button] clicked
broadcast [Maze v]
In the maze sprites:
when I receive [Maze v] //Maze scripts go under this
As for the various games you want to make, I would recommend searching this website for similar projects, and using those scripts.

Last edited by silvershine (2012-02-12 12:08:25)

Offline

 

Board footer