Solved duplicating events (1 Viewer)

murray83

Games Collector
Local time
Today, 08:56
Joined
Mar 31, 2017
Messages
728
Not sure if this is the correct place or if it should be on forms, if so i apologies

so get straight to it, i have a button which rolls 2 die, they then get added together which is the damaged dealt which is then taken from the health figure which is 100. so far so good and quite easy, but now here comes the head scratcher

if i wanted to have this done again for the players health from the bad guys attack, would you suggest duplicating the code and die images, just naming them different and put it at the end of the button press code

you can see my demo which is just you attacking but i would like it for you to attack and then you get hit for random amount
 

Attachments

  • BattleDemo.accdb
    764 KB · Views: 73

murray83

Games Collector
Local time
Today, 08:56
Joined
Mar 31, 2017
Messages
728
just had to upload the file again as i had forgot to include the die image gallery soz
 

plog

Banishment Pending
Local time
Today, 02:56
Joined
May 11, 2011
Messages
11,634
In general, when you feel the need to copy code, its time for a function/method. The better programmer produces less lines of code and lets it work for them.

First lets clean up this code:

Code:
Select Case iRandomNumber

Case 1
imgDie2.Picture = 1
Me.txtDiceRoll2.Value = 1
Case 2
imgDie2.Picture = 2
Me.txtDiceRoll2.Value = 2
Case 3
imgDie2.Picture = 3
Me.txtDiceRoll2.Value = 3
Case 4
imgDie2.Picture = 4
Me.txtDiceRoll2.Value = 4
Case 5
imgDie2.Picture = 5
Me.txtDiceRoll2.Value = 5
Case 6
imgDie2.Picture = 6
Me.txtDiceRoll2.Value = 6

End Select

That should all go away and become this:

Code:
imgDie2.Picture = iRandomNumber
Me.txtDiceRoll2.Value = iRandomNumber


You essentially copied that code (once for Die1 and once for Die2. I advise you make a sub, pass it iRandomNumber value and the die to set. It's signature line would look like this:

Sub set_Die(ByVal in_Value As Integer, ByVal in_Die As Integer)

It would take the code I just cleaned up and use in_Value and in_Die appropriately. Check out this link:


That's the general idea for how you re-use code without copying it all over the place. You move it to a function/sub and generalize it passing it the values it needs to do its job.
 

plog

Banishment Pending
Local time
Today, 02:56
Joined
May 11, 2011
Messages
11,634
Also, why are you building this in Access? What you posted doesn't really need a database (and your tables aren't normalized properly) unless there are more tables that you didn't post.

It seems like you either want to build a simple game or you want to learn programming, which is great. But I think both those aims would be better served programming this in a web framework. That's not to say you can't or shouldn't build this in Access, just want to make sure you effectively accomplish whatever your aim is.
 

murray83

Games Collector
Local time
Today, 08:56
Joined
Mar 31, 2017
Messages
728
Also, why are you building this in Access? What you posted doesn't really need a database (and your tables aren't normalized properly) unless there are more tables that you didn't post.

It seems like you either want to build a simple game or you want to learn programming, which is great. But I think both those aims would be better served programming this in a web framework. That's not to say you can't or shouldn't build this in Access, just want to make sure you effectively accomplish whatever your aim is.

you are quite right i am trying to make a game see attached of progress so far ( not fully done ) and i fixed the battle in my way as, yes yours looked neater but didn't want to go changing to much code as it already worked
 

Attachments

  • silver.zip
    4.1 MB · Views: 65

June7

AWF VIP
Local time
Yesterday, 23:56
Joined
Mar 9, 2014
Messages
5,463
Use CurrentDb.Execute instead of DoCmd.RunSQL and don't have to deal with warnings. But will have to concatenate textbox. Since there is only one record in each table, WHERE clause is not needed. Use just "Health" for field name in both tables. However, these tables could be combined to one and then WHERE clause would be needed.

Need to set something that will flag which opponent to update. This can be a textbox or a global variable or a TempVars. After each attack, change this variable to 0 or 1. Might need redesign of form and how textbox values are calculated.
CurrentDb.Execute "Update " & IIf(Me.txtOpp = 0, "tbl_GoodGuy", "tbl_BadGuys") & " Set [Health] = " & <some code here to determine which textbox to pull value from>
Me.txtOpp = IIf(Me.txtOpp = 1, 0, 1)


Of course, if you do want multiple good and/or bad guys, this will have to be modified.

The Case structures in cmdRoll_Click procedure are not needed.
imgDie1.Picture = iRandomNumber
Me.txtDiceRoll = iRandomNumber


Misspelled "defeat" as "defat" on opening form. Game Over script needs a space in "infact".
Proper indentation of code will make it easier to read.
 
Last edited:

murray83

Games Collector
Local time
Today, 08:56
Joined
Mar 31, 2017
Messages
728
Misspelled "defeat" as "defat" on opening form. Game Over script needs a space in "infact".
Proper indentation of code will make it easier to read.

thanks for the Spellings, i shall go fix that
 

murray83

Games Collector
Local time
Today, 08:56
Joined
Mar 31, 2017
Messages
728
with slight change to the battle screen

cheers June7 but i think ill stick with my code but one last thing, what would i do say if both numbers drop below zero at same time, has only happend once so far

prob another screen for draw lol
 

Attachments

  • silver.zip
    4.1 MB · Views: 66

murray83

Games Collector
Local time
Today, 08:56
Joined
Mar 31, 2017
Messages
728
well this is my current build

and the fight bit works well but if the health of both drops to 0 or below then get a win and lose screen
 

Attachments

  • silver.zip
    4.1 MB · Views: 67

June7

AWF VIP
Local time
Yesterday, 23:56
Joined
Mar 9, 2014
Messages
5,463
Misspelling in: "set of North" should be "off".

On Village screen: "residents is" should be "residents are".

On Clearing screen: "and even closer" should be "an even closer". Yes button doesn't do anything.

On Final battle: "Heros" should be "Hero's"

Okay, enough nitpicking.

After each opponent attack, include code that checks if health is at or below 0 and if it is terminate battle and declare a winner.

Get rid of unnecessary Select Case as already advised.

Code:
Private Sub Form_Load()

    Randomize
 
Me.imgDie1.Visible = False
Me.imgDie2.Visible = False
Me.imgDie3.Visible = False
Me.imgDie4.Visible = False
 
End Sub


Private Sub cmdRoll_Click()

''show die or hide the dice up to you
'Me.imgDie1.Visible = True
'Me.imgDie2.Visible = True
'Me.imgDie3.Visible = True
'Me.imgDie4.Visible = True

'---data for damage hero caused to boss-------

Dim iRandomNumber As Integer

'Generates random number for dice number 1
iRandomNumber = Int((6 * Rnd) + 1)
Me.imgDie1.Picture = iRandomNumber
Me.txtDiceRoll.Value = iRandomNumber

'Generates random number for dice number 2
iRandomNumber = Int((6 * Rnd) + 1)
Me.imgDie2.Picture = iRandomNumber
Me.txtDiceRoll2.Value = iRandomNumber

'sql for updating the health so it goes down correct to zero
CurrentDb.Execute "Update tbl_BadGuys set [BossHealth] = " & Me.Text111 & " WHERE [BossName] = 'EndBoss'" 'deals damage to his health and shows it
Me.Refresh
If Me.txtBossHealth.Value <= 0 Then
    Me.Visible = False
    DoCmd.OpenForm "Winner"
    Exit Sub
End If

'---data for damage boss caused to hero-------

Dim iRandomNumber2 As Integer

'Generates random number for dice number 1
iRandomNumber2 = Int((6 * Rnd) + 1)
Me.imgDie3.Picture = iRandomNumber2
Me.txtDiceRoll3.Value = iRandomNumber2

'Generates random number for dice number 2
iRandomNumber2 = Int((6 * Rnd) + 1)
Me.imgDie4.Picture = iRandomNumber2
Me.txtDiceRoll4.Value = iRandomNumber2

CurrentDb.Execute "Update tbl_GoodGuy set [YourHealth] = " & Me.Text133 & " WHERE [GoodGuy] = 'Player'" 'deals damage to his health and shows it
Me.Refresh
If Me.txtHeroHealth.Value <= 0 Then
    Me.Visible = False
    DoCmd.OpenForm "GameOver"
End If

End Sub

What exactly does Randomize do?
 
Last edited:

murray83

Games Collector
Local time
Today, 08:56
Joined
Mar 31, 2017
Messages
728
Misspelling in: "set of North" should be "off".

On Village screen: "residents is" should be "residents are".

On Clearing screen: "and even closer" should be "an even closer". Yes button doesn't do anything.

On Final battle: "Heros" should be "Hero's"

Okay, enough of that.

After each opponent attack, include code that checks if health is at or below 0 and if it is terminate battle and declare a winner.

Get rid of unnecessary Select Case as already advised.

Code:
Private Sub Form_Load()

    Randomize
  
Me.imgDie1.Visible = False
Me.imgDie2.Visible = False
Me.imgDie3.Visible = False
Me.imgDie4.Visible = False
  
End Sub


Private Sub cmdRoll_Click()

''show die or hide the dice up to you
'Me.imgDie1.Visible = True
'Me.imgDie2.Visible = True
'Me.imgDie3.Visible = True
'Me.imgDie4.Visible = True

'---data for damage hero caused to boss-------

Dim iRandomNumber As Integer

'Generates random number for dice number 1
iRandomNumber = Int((6 * Rnd) + 1)
Me.imgDie1.Picture = iRandomNumber
Me.txtDiceRoll.Value = iRandomNumber

'Generates random number for dice number 2
iRandomNumber = Int((6 * Rnd) + 1)
Me.imgDie2.Picture = iRandomNumber
Me.txtDiceRoll2.Value = iRandomNumber

'sql for updating the health so it goes down correct to zero
CurrentDb.Execute "Update tbl_BadGuys set [BossHealth] = " & Me.Text111 & " WHERE [BossName] = 'EndBoss'" 'deals damage to his health and shows it
Me.Refresh
If Me.txtBossHealth.Value <= 0 Then
    Me.Visible = False
    DoCmd.OpenForm "Winner"
    Exit Sub
End If

'---data for damage boss caused to hero-------

Dim iRandomNumber2 As Integer

'Generates random number for dice number 1
iRandomNumber2 = Int((6 * Rnd) + 1)
Me.imgDie3.Picture = iRandomNumber2
Me.txtDiceRoll3.Value = iRandomNumber2

'Generates random number for dice number 2
iRandomNumber2 = Int((6 * Rnd) + 1)
Me.imgDie4.Picture = iRandomNumber2
Me.txtDiceRoll4.Value = iRandomNumber2
DoCmd.SetWarnings False 'turns off the warning message about updating lines

CurrentDb.Execute "Update tbl_GoodGuy set [YourHealth] = " & Me.Text133 & " WHERE [GoodGuy] = 'Player'" 'deals damage to his health and shows it
Me.Refresh
If Me.txtHeroHealth.Value <= 0 Then
    Me.Visible = False
    DoCmd.OpenForm "GameOver"
End If

End Sub

What exactly does Randomize do?


and thanks for that theres much better code a lot cleaner cheers one and all
 

June7

AWF VIP
Local time
Yesterday, 23:56
Joined
Mar 9, 2014
Messages
5,463
Oh, and don't really need iRandomNumber and iRandomNumber2. iRandomNumber can serve for both.
 

Users who are viewing this thread

Top Bottom