Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » My card game doesn't work
- rhinoboy11
-
New Scratcher
7 posts
My card game doesn't work
Hi guys, i am a bit new with scratch but i have given making a card game here. The code looks right to me and i can't understand why the game as a whole doesn't work, as the ‘click to find out button’ seems broken because it doesn't move onto the next backdrop and sends the wrong messages which means my lists get messed up. WARNING there is a hell of a lot of code here, over 2,000 scripts, so if you want to help please be patient
Any help from the community would be much appreciated
Any help from the community would be much appreciated
- deck26
-
Scratcher
1000+ posts
My card game doesn't work
Have you built this up gradually testing as you did so? That's a lot of scripts for others to make sense of.
For this particular problem the issue might be spaces in the values you're checking against - eg is the x-position of cardA = ‘ -116’ (space or spaces before the number).
There's a lot you could do to simplify. For example, instead of all the broadcasts ALwBAP1, ALwDAP1 etc have a variable recording the name of the card and have a single broadcast which does ‘add variable to list’. You also have in most of the card sprites a script for receiving ‘finished game board2’ which does the same thing whether the Player/AI cards contains the card or not.
You could probably also do a lot with costumes - you don't necessarily need a separate sprite for each card (you can clone or stamp to get multiple cards). Hopefully you could then also generalise the scripts in the ‘find out’ sprite - you really shouldn't need to check every case in a separate script!
For this particular problem the issue might be spaces in the values you're checking against - eg is the x-position of cardA = ‘ -116’ (space or spaces before the number).
There's a lot you could do to simplify. For example, instead of all the broadcasts ALwBAP1, ALwDAP1 etc have a variable recording the name of the card and have a single broadcast which does ‘add variable to list’. You also have in most of the card sprites a script for receiving ‘finished game board2’ which does the same thing whether the Player/AI cards contains the card or not.
You could probably also do a lot with costumes - you don't necessarily need a separate sprite for each card (you can clone or stamp to get multiple cards). Hopefully you could then also generalise the scripts in the ‘find out’ sprite - you really shouldn't need to check every case in a separate script!
- deck26
-
Scratcher
1000+ posts
My card game doesn't work
So for each card you have a set of scores for Ballistic, Weapon, Intelligence etc? If each card character has a code number (eg Allectus=1, Barnabus=2 etc) then you can have a list for Ballistic, another list for Weapon etc with the code number of the character acting as an index. You can put the names in a list as well.
So if index=2
item (index) of names = Barnabus
item (index) of Ballistic = 34 and so on
These values should all be loaded in an initialisation script before the game starts.
You can then potentially check for a winner by getting the code number for the player card and the AI card and comparing the appropriate items from the appropriate list. Then you can greatly reduce the scripts for ‘Click to Find out’ which does contain a large number of your scripts.
So if index=2
item (index) of names = Barnabus
item (index) of Ballistic = 34 and so on
These values should all be loaded in an initialisation script before the game starts.
You can then potentially check for a winner by getting the code number for the player card and the AI card and comparing the appropriate items from the appropriate list. Then you can greatly reduce the scripts for ‘Click to Find out’ which does contain a large number of your scripts.
- deck26
-
Scratcher
1000+ posts
My card game doesn't work
https://scratch.mit.edu/projects/57871142/ shows how to load data into lists without having to tediously load each value one code block at a time. That would be useful if you did take up my suggestion regarding loading your data into lists.
I've done some work on a remix to test some of this but am not sure how far I'll take it.
I've done some work on a remix to test some of this but am not sure how far I'll take it.
- deck26
-
Scratcher
1000+ posts
My card game doesn't work
Another area where you could greatly reduce the number of scripts and possibly reduce problems is in the PlayerC1-C10 and AIC1 to AIC10 sprites.
For example, in AIC1 you have 6 scripts for checking when it receives BAwALP1. I'm assuming the AI hand can only contain one instance of the card as otherwise these scripts could be a real problem. What I would do is
but you're also doing exactly the same for lots of other broadcasts in each sprite so you need to look at whether you actually need the broadcast BAwALP1 and DAwALP1 and FEwALP1 and ……….. Potentially 9 x 6 scripts get reduced to 1.
For example, in AIC1 you have 6 scripts for checking when it receives BAwALP1. I'm assuming the AI hand can only contain one instance of the card as otherwise these scripts could be a real problem. What I would do is
when I receive [BAwALP1 v]
set [AI-index v] to [6 ] // new variable but worth it!
repeat (6)
if <(item (AI-index v) of [AI Cards v]) = [Allectus]> then
delete (AI-index v) of [ AI Cards v]
stop [this script v]
end
change [AI-index v] by (-1)
end
but you're also doing exactly the same for lots of other broadcasts in each sprite so you need to look at whether you actually need the broadcast BAwALP1 and DAwALP1 and FEwALP1 and ……….. Potentially 9 x 6 scripts get reduced to 1.
- rhinoboy11
-
New Scratcher
7 posts
My card game doesn't work
Hi, thank you so much for the quick response, but it seems i am not advanced enough to fully understand the 2nd part about selecting a winner? how would this work without inputting every eventuality again?
- deck26
-
Scratcher
1000+ posts
My card game doesn't work
Let's say the player has card 3 (playercard=3) which is Darrius and the computer has card 9 (AIcard=9) which is Swain. Then to compare those two cards for Ballistic skill you would do
The same code is used for any such comparison (you'd also need to check for equality and less than but that's trivial). What you do within that block is up to you but you probably only need simple ‘playerWin’ and ‘AIwin’ scripts.
if <(item (playercard) of [Ballistic v]) > (item (AIcard) of [Ballistic v]) > then
end
The same code is used for any such comparison (you'd also need to check for equality and less than but that's trivial). What you do within that block is up to you but you probably only need simple ‘playerWin’ and ‘AIwin’ scripts.
- deck26
-
Scratcher
1000+ posts
My card game doesn't work
Some idea of how the game works would save me time in remixing. The player chooses 3 cards (face down, randomised?) and the computer is allocated 3 cards according to the Easy/Medium/Difficult selection. Not clear if the player and computer can have the same card - my impression is yes but seems more likely that the player is choosing from what's left after the computer's cards are allocated.
Then the player chooses (plays the first?) one of their cards (face up selection?) and, looking at the card, chooses a category (Strength, Intelligence etc) and that is compared to the computer's card (how is that chosen?).
Does the winner of each round then get the other player's card and the game continue until one player has no cards left?
I may have missed something but having spent enough time deleting around 1800 scripts that I think are unnecessary I don't have the energy to explore the code further today!
Then the player chooses (plays the first?) one of their cards (face up selection?) and, looking at the card, chooses a category (Strength, Intelligence etc) and that is compared to the computer's card (how is that chosen?).
Does the winner of each round then get the other player's card and the game continue until one player has no cards left?
I may have missed something but having spent enough time deleting around 1800 scripts that I think are unnecessary I don't have the energy to explore the code further today!
- rhinoboy11
-
New Scratcher
7 posts
My card game doesn't work
The player chooses 3 cards, each of which are assigned to a character on the number keys, and the computer is allocated 3 cards according to the Easy/Medium/Difficult selection. The player and computer can have the same card, it is assumed they both have duplicate decks
Then the player plays the top card in their hand according to the first number they selected and, not looking at the card (pot luck essentially), chooses a category (Strength, Intelligence etc) and that is compared to the computer's card, which is hard coded in order from the start.
The winner of each round gets the other players card, and the first person to no cards loses.
I hope this helps
Then the player plays the top card in their hand according to the first number they selected and, not looking at the card (pot luck essentially), chooses a category (Strength, Intelligence etc) and that is compared to the computer's card, which is hard coded in order from the start.
The winner of each round gets the other players card, and the first person to no cards loses.
I hope this helps
- deck26
-
Scratcher
1000+ posts
My card game doesn't work
The player chooses 3 cards, each of which are assigned to a character on the number keys, and the computer is allocated 3 cards according to the Easy/Medium/Difficult selection. The player and computer can have the same card, it is assumed they both have duplicate decksOK, I'll have a go along those lines. Not my priority as you'll appreciate but hopefully I can produce something over the next week or so.
Then the player plays the top card in their hand according to the first number they selected and, not looking at the card (pot luck essentially), chooses a category (Strength, Intelligence etc) and that is compared to the computer's card, which is hard coded in order from the start.
The winner of each round gets the other players card, and the first person to no cards loses.
I hope this helps
- rhinoboy11
-
New Scratcher
7 posts
My card game doesn't work
thank you so much, as you can see i honestly had a very little idea of what i was doing, so if you can even get it to work it would be a miracle in my eyes xD
- deck26
-
Scratcher
1000+ posts
My card game doesn't work
thank you so much, as you can see i honestly had a very little idea of what i was doing, so if you can even get it to work it would be a miracle in my eyes xDI'll probably take it so far and leave you to finish but it should hopefully be obvious how to do so. I'll also leave most of the testing to you.
One comment - the allocation of cards to the computer is too predictable and actually means there's one card the computer never has. If the whole game is played blind until the cards are compared that perhaps doesn't matter but it might be better to allocate only 1 or 2 cards automatically and randomise the other(s). In any case the cards need to be placed in random order within the hand.
Also a question - if you have three cards you're presumably playing card 2 and then card 3. So far so good but if you win where does the won card get placed? Wherever you add it is is predictable if the game is watched carefully. Perhaps you're happy not to worry about it.
Not sure why the player is picking cards. If they're randomly arranged face down wouldn't it be easier just to deal three cards?
I suspect I'll have more questions!
- deck26
-
Scratcher
1000+ posts
My card game doesn't work
I'm assuming the lists showing the player/AI cards are just for debugging and wouldn't normally be displayed? Since I'm using numbers instead of names I'd need a second list to display the names.
Apologies if this seems like I'm repeating the same questions - I don't think I am but it may feel like it!
So I think what happens is the player chooses a category and then clicks on the top card of their deck which is displayed. They then click on the AI's top card which is displayed instead (so only one card is ever displayed face up at a time) before the ‘Who Wins’ screen comes up with the button Click to Find out.
Might be better to display the score for the player after showing their card and then do the same for the computer and automatically display a message showing who wins.
I guess the card won and the card played go to the bottom of the winner's pile but the order can vary.
Apologies if this seems like I'm repeating the same questions - I don't think I am but it may feel like it!
So I think what happens is the player chooses a category and then clicks on the top card of their deck which is displayed. They then click on the AI's top card which is displayed instead (so only one card is ever displayed face up at a time) before the ‘Who Wins’ screen comes up with the button Click to Find out.
Might be better to display the score for the player after showing their card and then do the same for the computer and automatically display a message showing who wins.
I guess the card won and the card played go to the bottom of the winner's pile but the order can vary.
- rhinoboy11
-
New Scratcher
7 posts
My card game doesn't work
the allocation of cards to the computer is too predictable and actually means there's one card the computer never has.
yeah, i would like to keep it that way for now if possible, thanks
if you have three cards you're presumably playing card 2 and then card 3. So far so good but if you win where does the won card get placed?
It is placed randomly in the list so it could show up anywhere in the players' hand
Not sure why the player is picking cards. If they're randomly arranged face down wouldn't it be easier just to deal three cards?
It adds i think a bit of interactivity to the game, otherwise it is mostly computer defined and it feels like the player is not doing much
I'm assuming the lists showing the player/AI cards are just for debugging and wouldn't normally be displayed?
Yes, that's correct, to make sure the right ones get put and deleted in the right lists
So I think what happens is the player chooses a category and then clicks on the top card of their deck which is displayed. They then click on the AI's top card which is displayed instead (so only one card is ever displayed face up at a time) before the ‘Who Wins’ screen comes up with the button Click to Find out.
Yes, this is right, but i think when you show the cards it is best to keep the scores secret until the result is shown.
yeah, i would like to keep it that way for now if possible, thanks
if you have three cards you're presumably playing card 2 and then card 3. So far so good but if you win where does the won card get placed?
It is placed randomly in the list so it could show up anywhere in the players' hand
Not sure why the player is picking cards. If they're randomly arranged face down wouldn't it be easier just to deal three cards?
It adds i think a bit of interactivity to the game, otherwise it is mostly computer defined and it feels like the player is not doing much
I'm assuming the lists showing the player/AI cards are just for debugging and wouldn't normally be displayed?
Yes, that's correct, to make sure the right ones get put and deleted in the right lists
So I think what happens is the player chooses a category and then clicks on the top card of their deck which is displayed. They then click on the AI's top card which is displayed instead (so only one card is ever displayed face up at a time) before the ‘Who Wins’ screen comes up with the button Click to Find out.
Yes, this is right, but i think when you show the cards it is best to keep the scores secret until the result is shown.
- deck26
-
Scratcher
1000+ posts
My card game doesn't work
https://scratch.mit.edu/projects/57868870/ is my remix so far. It's not complete but gets as far as comparing the first cards. I've got some other stuff to do that means I've got to leave it for the time being and thought it might be useful for you to see how far I've got.
As you'll see it is very different in terms of number of scripts and sprites but that makes it much more manageable and easier to understand. I may be using some tricks that you wouldn't have thought of yet but don't feel discouraged. You've actually learned an important lesson about re-using code and making things more general and less specific so you don't need to have all the scripts you had. Imagine you were doing a Monopoly game - you don't need to have a separate script for every square you might land on or every possible dice combination but that is roughly what you had. Spotting when you're repeating things and working out how not to do so is important.
Feel free to take a copy of mine and remix to try to take it further. Alternatively have a good look at it and see how much you can work out of what I've done. I'm happy to answer any questions.
As you'll see it is very different in terms of number of scripts and sprites but that makes it much more manageable and easier to understand. I may be using some tricks that you wouldn't have thought of yet but don't feel discouraged. You've actually learned an important lesson about re-using code and making things more general and less specific so you don't need to have all the scripts you had. Imagine you were doing a Monopoly game - you don't need to have a separate script for every square you might land on or every possible dice combination but that is roughly what you had. Spotting when you're repeating things and working out how not to do so is important.
Feel free to take a copy of mine and remix to try to take it further. Alternatively have a good look at it and see how much you can work out of what I've done. I'm happy to answer any questions.
- rhinoboy11
-
New Scratcher
7 posts
My card game doesn't work
thank you so much for the work you have done, it looks great. I would like to wait to see how far you take it before i try to remix it, but thank you for the work you have put in. I have had a look at the code and it makes a lot of sense, and i don't know how i missed some of these things xD This has taught me a lot though, and i can do nothing but thank you for that.
- rhinoboy11
-
New Scratcher
7 posts
My card game doesn't work
oh my goodness, thank you so much!!!!! it works so well, you really are brilliant. Thanks again
- Discussion Forums
- » Help with Scripts
-
» My card game doesn't work

