| View previous topic :: View next topic |
| Author |
Message |
choco-late Newbie
Joined: 18 Aug 2005 Posts: 12
|
Posted: Aug 18th, 2005 04:04 AM Post subject: Help for programming a dice game |
|
|
OK, i am a beginner with vb and need to do an assignment for a dice game. right now i have no idea of how to code this.
well, this game is a simple dice game. Its a game where there's 5 dices and i need to roll the dice,if a 2 or 5 appear then that dice would disappear, the dice im showing its an image. I really need help at how to randomise the 5 dice and hide the dice if a 2 or 5 appear. |
|
| Back to top |
|
DoobieKeebler Moderator

Joined: 17 Jun 2005 Posts: 254 Location: 181°15'2.003"W, 93°5'16.956"N
|
Posted: Aug 18th, 2005 08:57 AM Post subject: |
|
|
To generate a number between 1 and X you'd write:
Y = Int((X * Rnd) + 1)
You should put Randomize on a line by itself before you generate the random numbers. I'll post some code below to pull everything together for you.
Image boxes and Picture boxes have a Visible property that can be set by your code. The following code would make an image box named imgTelephone "disappear" and then "reappear":
imgTelephone.Visible = False
imgTelephone.Visible = True
Obviously that would happen so fast that you couldn't see it. The code below generates a random number for my CashOnHand and ItemPrice between $1 and $50,000. If my CashOnHand is less than the ItemPrice then VB will hide imgMercedes which is an image box that shows a picture of a Mercedes.
| Code: | Randomize
CashOnHand = Int((50000 * Rnd) + 1)
ItemPrice = Int((50000 * Rnd) + 1)
If CashOnHand < ItemPrice Then
imgMercedes.Visible = False
End If |
Hopefully that will get you going. (It sounds like a class assignment, and in those cases I try to give an explanation of what's going on and some sample code that gives a good demonstration but can't be simply copied and pasted.) |
|
| Back to top |
|
choco-late Newbie
Joined: 18 Aug 2005 Posts: 12
|
Posted: Aug 19th, 2005 02:08 AM Post subject: |
|
|
ok thanx !
I started coding my game and i got another problem.
| Code: | Private Sub cmdRollDice_Click()
Dim dice1 As Integer
Dim dice2 As Integer
dice1 = Int(Rnd * 6) + 1
Select Case dice1
Case 1
imgDice1.Picture = LoadPicture("a:\Dice1.jpg")
Case 2
imgDice1.Picture = LoadPicture("a:\Dice2.jpg")
Case 3
imgDice1.Picture = LoadPicture("a:\Dice3.jpg")
Case 4
imgDice1.Picture = LoadPicture("a:\Dice4.jpg")
Case 5
imgDice1.Picture = LoadPicture("a:\Dice5.jpg")
Case 6
imgDice1.Picture = LoadPicture("a:\Dice6.jpg")
End Select
If dice1 = 2 Or 5 Then
imgDice1.Visible = False
End If |
This is my code, somehow whenever i click on the button to randomize the image dissappear straight away... And i figure out that it got something to do with the last paragraph of the code. |
|
| Back to top |
|
DoobieKeebler Moderator

Joined: 17 Jun 2005 Posts: 254 Location: 181°15'2.003"W, 93°5'16.956"N
|
Posted: Aug 19th, 2005 08:16 AM Post subject: |
|
|
The problem is your If statement. You have:
If dice1 = 2 or 5 Then
VB sees this line as:
If (dice1 = 2) or (5) Then
This is the same as:
If (dice1 = 2) or True Then
because any non-zero number (in this case 5) is considered to be True. So your If statement will always be true. You just need to rewrite the line like so:
If (dice1 = 2) Or (dice1 = 5) Then |
|
| Back to top |
|
choco-late Newbie
Joined: 18 Aug 2005 Posts: 12
|
Posted: Aug 20th, 2005 06:20 AM Post subject: |
|
|
thanx again! yeh it did work ...
But i got another question =D (i guess im annoying but cant help it) well ... every time i run the game the random dice picture pop up is always the same. So no matter how much time i run the game the end result is the same.
Is there anything i can do to fix it ?? |
|
| Back to top |
|
dougthomas Moderator
Joined: 27 Jul 2005 Posts: 271 Location: Essex, UK
|
Posted: Aug 21st, 2005 05:58 AM Post subject: |
|
|
Hi,
I think you missed a fairly important part of Doobie Keebler's tutorial on Random Numbers.
| Quote: |
You should put Randomize on a line by itself before you generate the random numbers.
|
Take another look at Doobie's example code.
Regards
Doug |
|
| Back to top |
|
choco-late Newbie
Joined: 18 Aug 2005 Posts: 12
|
Posted: Aug 21st, 2005 06:05 AM Post subject: |
|
|
my bad ... may bad ...
Thanx for the help ! ! Yeh, now it all worked out ..... |
|
| Back to top |
|
choco-late Newbie
Joined: 18 Aug 2005 Posts: 12
|
Posted: Aug 22nd, 2005 01:31 AM Post subject: |
|
|
Sorry to bother you peoples again, but i am stuck on what im doing.
this is my code
| Code: | Private Sub cmdRollDice_Click()
Dim dice1 As Integer
Dim dice2 As Integer
Dim dice3 As Integer
Dim dice4 As Integer
Dim dice5 As Integer
Randomize
dice1 = Int(Rnd * 6) + 1
Select Case dice1
Case 1
imgDice1.Picture = LoadPicture("a:\Dice1.jpg")
Case 2
imgDice1.Picture = LoadPicture("a:\Dice2.jpg")
Case 3
imgDice1.Picture = LoadPicture("a:\Dice3.jpg")
Case 4
imgDice1.Picture = LoadPicture("a:\Dice4.jpg")
Case 5
imgDice1.Picture = LoadPicture("a:\Dice5.jpg")
Case 6
imgDice1.Picture = LoadPicture("a:\Dice6.jpg")
End Select
If (dice1 = 2) Or (dice1 = 5) Then
imgDice1.Visible = False
End If
Randomize
dice2 = Int(Rnd * 6) + 1
Select Case dice2
Case 1
imgDice2.Picture = LoadPicture("a:\Dice1.jpg")
Case 2
imgDice2.Picture = LoadPicture("a:\Dice2.jpg")
Case 3
imgDice2.Picture = LoadPicture("a:\Dice3.jpg")
Case 4
imgDice2.Picture = LoadPicture("a:\Dice4.jpg")
Case 5
imgDice2.Picture = LoadPicture("a:\Dice5.jpg")
Case 6
imgDice2.Picture = LoadPicture("a:\Dice6.jpg")
End Select
If (dice2 = 2) Or (dice2 = 5) Then
imgDice2.Visible = False
End If
Randomize
dice3 = Int(Rnd * 6) + 1
Select Case dice3
Case 1
imgDice3.Picture = LoadPicture("a:\Dice1.jpg")
Case 2
imgDice3.Picture = LoadPicture("a:\Dice2.jpg")
Case 3
imgDice3.Picture = LoadPicture("a:\Dice3.jpg")
Case 4
imgDice3.Picture = LoadPicture("a:\Dice4.jpg")
Case 5
imgDice3.Picture = LoadPicture("a:\Dice5.jpg")
Case 6
imgDice3.Picture = LoadPicture("a:\Dice6.jpg")
End Select
If (dice3 = 2) Or (dice3 = 5) Then
imgDice3.Visible = False
End If
Randomize
dice4 = Int(Rnd * 6) + 1
Select Case dice4
Case 1
imgDice4.Picture = LoadPicture("a:\Dice1.jpg")
Case 2
imgDice4.Picture = LoadPicture("a:\Dice2.jpg")
Case 3
imgDice4.Picture = LoadPicture("a:\Dice3.jpg")
Case 4
imgDice4.Picture = LoadPicture("a:\Dice4.jpg")
Case 5
imgDice4.Picture = LoadPicture("a:\Dice5.jpg")
Case 6
imgDice4.Picture = LoadPicture("a:\Dice6.jpg")
End Select
If (dice4 = 2) Or (dice4 = 5) Then
imgDice4.Visible = False
End If
Randomize
dice5 = Int(Rnd * 6) + 1
Select Case dice5
Case 1
imgDice5.Picture = LoadPicture("a:\Dice1.jpg")
Case 2
imgDice5.Picture = LoadPicture("a:\Dice2.jpg")
Case 3
imgDice5.Picture = LoadPicture("a:\Dice3.jpg")
Case 4
imgDice5.Picture = LoadPicture("a:\Dice4.jpg")
Case 5
imgDice5.Picture = LoadPicture("a:\Dice5.jpg")
Case 6
imgDice5.Picture = LoadPicture("a:\Dice6.jpg")
End Select
If (dice5 = 2) Or (dice5 = 5) Then
imgDice5.Visible = False
End If
End Sub |
I want to display the number of dice add up on to a label, but i have no idea because if there is a dice disappear the score is not counted only when there is no dice disappear then the score is added.
Please help and reply as soon as possible. Thank you! |
|
| Back to top |
|
dougthomas Moderator
Joined: 27 Jul 2005 Posts: 271 Location: Essex, UK
|
Posted: Aug 22nd, 2005 03:18 AM Post subject: |
|
|
Ok,
Let's clarify the problem, I'm not sure whether I fully understand what you want to do.
You have a number of dice being displayed, which could be anywhere between Zero and 5 (If the Random numbers were all 2 or 5 then you'd have no dice displayed - unlikely, but it should be taken into account.)
(1) Do you want to only sum the values of the dice that are displayed (that is, ignore the values of the dice not displayed) ?
OR
(2) Only display the total value if NO dice have been hidden ?
OR
(3)Sum all the values whether the dice are displayed or not ?
Whichever you choose, one thing is for sure; you'll need something that is totalling the values of dice1, dice2, dice3 etc and some logic to determine when and if they are to be added into the total or not.
For example, if you wanted to total ALL the values irrespective of whether the dice were displayed or not, you could simply add up the values of dice1 through to dice 5 at the end of your code and display the answer in the Label's .Caption property.
If you only wanted to sum the values of the dice displayed, you can use the same approach, but you'd have to add the values up as you went along, maintaining a running total, and decide whether a particular dice's value should be added in or not. You already know when a dice is not going to be displayed so that should give you a clue - if it's not going to be displayed, then don't add it in to the running total.
If you only wanted to display the total if NO dice have been hidden then you'll have to "remember" whether any have been hidden or not, perhaps using a Boolean variable (like boHidden = True) when you hide a dice, and then testing that variable at the end to decide whether to display the total or not.
There should be enough clues in the above for you to sort it out, have a go and if you still can't get it going come back.
Good luck and regards
Doug |
|
| Back to top |
|
choco-late Newbie
Joined: 18 Aug 2005 Posts: 12
|
Posted: Aug 22nd, 2005 04:16 AM Post subject: |
|
|
I want to only sum the values of the dice that are displayed (that is, ignore the values of the dice not displayed).
I'm not quite understand, can you give me an example. Thank you! |
|
| Back to top |
|
dougthomas Moderator
Joined: 27 Jul 2005 Posts: 271 Location: Essex, UK
|
Posted: Aug 22nd, 2005 05:34 AM Post subject: |
|
|
Ok,
I'm not too good at examples so here's an analysis from which you should be able to code.
Start off with a variable to represent the running total value of the dice and set to to zero.
When you throw a dice if it is a 2 or a 5 you don't want to display it OR add it's value into the running total.
You already have some code which decides when not to display the dice, so you need to add the value into the running total when you decide "not to not to show the dice" in other words when you do show the dice.
If we write your problem out in long hand you can see what you have to do:
"If the dice value is a 2 or a 5 then I'm not going to show it, otherwise, I am going to show the dice and I'll add it's value to the total"
(BIG HINT: what do you know about If - Then and "otherwise" ?)
"When i'm finished, i'm going to display the total value which will be the total of all the values of the dice displayed."
That's about as far as I can go without actually writing the code ! (I don't want to do that as you've managed so well so far without anyone writing code for you)
Regards
Doug |
|
| Back to top |
|
choco-late Newbie
Joined: 18 Aug 2005 Posts: 12
|
Posted: Aug 22nd, 2005 07:08 PM Post subject: |
|
|
I really dont know how to do this.
So far this is what i've done, and still it doesnt work.
| Code: | TotalScore = TotalScore + dice1 Or dice2 Or dice3 Or dice4 Or dice5
If imgDice1.Visible = True Or imgDice2.Visible = True Or imgDice3.Visible = True Or imgDice4.Visible = True Or imgDice5.Visible = True Then
Call Score
End If
End Sub
-----------------------------------------------
Private Sub Score()
TotalScore = TotalScore
lblScore.Caption = TotalScore + dice1 Or dice2 Or dice3 Or dice4 Or dice5
End Sub |
I know this isnt good. But i just want the game work and thats all ....
And i really dont know what to do, i've tried so many other ways!
Please help, thank you ! ! |
|
| Back to top |
|
dougthomas Moderator
Joined: 27 Jul 2005 Posts: 271 Location: Essex, UK
|
Posted: Aug 22nd, 2005 11:25 PM Post subject: |
|
|
Ok,
I don't think you're going to get there by yourself. By "otherwise" I was refering to ELSE.
In your code you decide whether to hide a particular dice with, for example:
If (dice1 = 2) Or (dice1 = 5) Then
imgDice1.Visible = False
End If
What I was suggesting is that you want to add the value into the total if you decide to show the dice:
If (dice1 = 2) Or (dice1 = 5) then
imgDice1.Visible = False
Else
intTotal = intTotal + dice1
End If
Right at the end of your code to display total
lblscore.Caption = Cstr(intTotal)
Regards
Doug |
|
| Back to top |
|
choco-late Newbie
Joined: 18 Aug 2005 Posts: 12
|
Posted: Aug 23rd, 2005 03:23 AM Post subject: |
|
|
THANK YOU ! ! !
ok some weird things are going on now, at the start the score are counted correctly but as more dices disppear the score doesnt sound right.
Also how do i keep the score adding up?? |
|
| Back to top |
|
dougthomas Moderator
Joined: 27 Jul 2005 Posts: 271 Location: Essex, UK
|
Posted: Aug 23rd, 2005 03:58 AM Post subject: |
|
|
Hi,
Check your code to make sure that you've typed the additions to intTotal correctly. You have 5 'if' statements, check you're adding the correct dice'n' value in for each of them.
To keep the score adding up just define intTotal as Public in the Declarations section of the project and only set it to zero at the very beginning of each game.(not each throw)
Regards
Doug |
|
| Back to top |
|
|