骰子按钮赢了骰子在我的游戏中滚动?

时间:2014-11-20 20:46:12

标签: vb.net dice

我刚刚对此应用的代码进行了一些更改,但由于某种原因,它仍然无效。我的重启按钮没有重新启动表单,我的分数没有显示,结果结果没有显示,骰子没有滚动。

以下是我的GUI链接:https://imageshack.com/i/kpdOTIAQp

基本上,人类玩家正在使用VB内置的随机数生成器来对抗计算机。游戏的目标是让对手获得比其他人更高的分数,以1到99的最佳奇数匹配格式赢得比赛(如1,3,5,7,9,11, 13等等。

问题是单击按钮时骰子图像不会显示。

Public Class DiceBattleForm

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub ExitButton_Click(sender As Object, e As EventArgs) Handles ExitButton.Click
        Dim RESPONSE As MsgBoxResult
        RESPONSE = MsgBox("Do you want to exit?", MsgBoxStyle.YesNo Or MsgBoxStyle.Question)
        If RESPONSE = MsgBoxResult.Yes Then
            Me.Dispose()
        End If
    End Sub

    Private Sub RollButton_Click(sender As Object, e As EventArgs) Handles RollButton.Click
        If GameTextBox.Text <= "0" Then
            MsgBox("Number of Games > 0", MsgBoxStyle.Critical)
            Application.Restart()
        End If

        If GameTextBox.Text > "0" Then
            GameTextBox = Convert.ToInt32(GameTextBox.Text)
        End If


        DisplayDie(PCDice1)
        DisplayDie(PCDice2)
        DisplayDie(PCDice3)

        DisplayDie(YouDice1)
        DisplayDie(YouDice2)
        DisplayDie(YouDice3)
    End Sub

    Sub DisplayDie(diePictureBox As PictureBox)
        'GENERATE random integer in range 1 to 6
        Dim face As Integer = randomObject.Next(1, 7)

        'retrieve specific die image from resources
        Dim pictureResource = My.Resources.ResourceManager.GetObject(String.Format("die{0}", face))

        'Convert pictureResource to type Image and display in ImageBox
        diePictureBox.Image = CType(pictureResource, Image)
    End Sub ' DisplaDie


    Private Sub GameTextBox_Validating(sender As Object, e As EventArgs) Handles GameTextBox.TextChanged
        If GameTextBox.Text > "0" Then
            Do
                MsgBox(GameTextBox.Text + " round(s) will be played enjoy!")
            Loop While GameTextBox.Text = "STOP"
        End If
        GameTextBox.Enabled = False
    End Sub

    Private Sub RestartButton_Click(sender As Object, e As EventArgs) Handles RestartButton.Click
        ' Reset or clear any controls (recommended)
        DiceBattleForm.Clear()
        ' NOT recommended:
        ' Application.Restart()
    End Sub
End Class

2 个答案:

答案 0 :(得分:1)

我会稍微改变一下。不是从资源加载骰子图像,而是从imagelist加载它们,以便索引(由Random结果表示)是您查找图像所需的全部内容。通过拼接和整数和名称从资源加载可能是问题 - 我们无法说明,因为我们无法看到您的资源中命名的模具图像。

此外,此类问题是理想以了解如何调试:在DisplayDie中设置断点并查看为什么它不起作用:例如String.Format结果是否适合图像名称?

滚动骰子,显示ImageList

中的图像
Private Sub btnRoll_Click(sender As Object, e As EventArgs) Handles btnRoll.Click
    ' pbC1 etc are MY picturebox names - use yours
    DisplayDie(pbC1)
    DisplayDie(pbC2)
    DisplayDie(pbC3)

End Sub

Private Sub DisplayDie(diePictureBox As PictureBox)

    Dim face As Integer = randomObject.Next(1, 7)

    ' adjust pip count to image index by subtracting one
    diePictureBox.Image = ImageList1.Images(face - 1)

End Sub

图像以ImageList的pip顺序列出,因此唯一的转换是从pips(1-6)转换为index(0-5)。

enter image description here

答案 1 :(得分:0)

好吧,对于重启按钮,而不是Form1.Clear()

尝试

TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
TextBox4.Clear()

等应该清除文本框,至于。但实际上,我没有看到Application.Restart()

出了什么问题