尝试声明位图图像时参数无效

时间:2017-03-20 10:12:58

标签: vb.net

我为我的A-Level计算项目制作坦克游戏(物理模拟器)。几个星期以来,一切都很顺利,从大约一周前我就得到了一个参数无效"将X和Y值传递给创建新位图时。

这是引发错误的行:

backbuffer = New Bitmap(clientWidth, clientHeight) 'sets the area for the objects to exist in`

到目前为止,这是完整的代码:

Imports System.Threading
Imports System.Drawing
Public Class Form1
    Dim boostart As Boolean
    Dim locationX As Double = 10
    Dim locationy As Double = 550
    Dim circle As [Rectangle]
    Dim Player1 As [Rectangle]

    Dim P1Xlocation As Double = 10
    Dim P1Ylocation As Double = 550

    Dim ballOut As Boolean

    Dim backbuffer As Bitmap
    Dim graphics As Graphics

    Dim clientWidth As Integer = 1500
    Dim clientHeight As Integer = 600

    Dim uHorizontal As Double
    Dim uVertical As Double
    Dim intAngle As Double
    Dim intPower As Integer

    Dim gameClock As Stopwatch
    Dim interval As Integer
    Dim starttick As Long

    Dim t As Double = 0


    Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.KeyPreview = True 'allows for background key presses.

        gameClock = New Stopwatch
        interval = 6

        Player1 = New Rectangle(P1Xlocation, P1Ylocation, 30, 550) 'displays the player on load
        circle = New Rectangle(Player1.Right, Player1.Top, 10, 550)
        gameloop()
    End Sub
    Public Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick 'timer for moving the projectile
        If boostart = True Then
            If Not ballOut = True Then 'if the ball isnt going to moveout of bounds, move the ball
                moveBall(intAngle, intPower)
            End If
        End If
    End Sub

    Public Sub gameloop()
        gameClock.Start()

        Do While (Me.Created) 'infinite loop for the game
            starttick = gameClock.ElapsedMilliseconds
            render() 'renders the scene
            collisions() 'checks for collisions
            Application.DoEvents()
            Do While gameClock.ElapsedMilliseconds - starttick < interval 'to keep the game clock stable to make the game more smooth
            Loop
        Loop
    End Sub
    Public Sub moveBall(theta, mag) 'sub-routine for moving the projectile
        theta = theta * (Math.PI / 180)
        Dim uHorizontal As Double = mag * Math.Cos(theta)
        Dim uVertical As Double = mag * Math.Sin(theta)

        If 550 - ((uVertical * t) + (0.5 * (-9.81) * t ^ 2)) > clientHeight Then    'validation to see if the next pass through will
            Timer1.Stop()                                                           'move the projecile out of bounds.
            MsgBox("Ball will go out of bounds")
            ballOut = True
        Else locationy = 550 - ((uVertical * t) + (0.5 * (-9.81) * t ^ 2)) 'updates the Y value of the ball using the formulae 
        End If                                                             '(1/2(-9.81)t^2)

        If (uHorizontal * t) > clientWidth Then
            Timer1.Stop()
            MsgBox("Ball will go out of bounds")
            ballOut = True
        Else locationX = (uHorizontal * t)                                 'Updates X value using the formulae (u*t)
        End If

        Me.Refresh()
        t += 0.05 'increases t

        Debug.Print("YUvertical = " & uVertical)
        Debug.Print("UHorizontal = " & uHorizontal)
        Debug.Print("Theta =" & theta)
        Debug.Print("X:" & locationX)
        Debug.Print("Y:" & locationy)
        'Threading.Thread.Sleep(1000)
        gameloop()
    End Sub
    Public Sub collisions() 'basic collision detection with borders of the play area
        If circle.X > clientWidth Then
            MsgBox("collision")
        End If
        If circle.Y > clientHeight Then
            MsgBox("collision")
        End If
    End Sub
    Public Sub MoveP1(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        Select Case e.KeyCode 'for moving the players characcter
            Case Keys.W
            Case Keys.S
            Case Keys.A
                Player1.X += -10
            Case Keys.D
                Player1.X += 10
        End Select
    End Sub
    Public Sub render()
        backbuffer = New Bitmap(clientWidth, clientHeight) 'sets the area for the objects to exist in
        graphics = Graphics.FromImage(backbuffer) 'allows to draw on the background
        PbSurface.Image = Nothing 'makes picturebox nothing
        graphics.FillRectangle(Brushes.Green, Player1) 'defines the players square
        graphics.FillEllipse(Brushes.Red, circle) 'defines the projectile
        PbSurface.Image = backbuffer 'sets the picturebox to the new bitmap

        Player1 = New Rectangle(P1Xlocation, P1Ylocation, 30, 30) 'Updates object positions on screen
        circle = New Rectangle(Player1.Right, Player1.Top, 10, 10)
    End Sub
    Public Sub BtnFire_Click(sender As Object, e As EventArgs) Handles BtnFire.Click
        boostart = True 'sets timer off to move the fired projectile
        If IsNumeric(TxtAngle.Text) And IsNumeric(txtPower.Text) _
            And IsNumeric(TxtTime.Text) And IsNumeric(TxtVelocity.Text) Then    'validation for invalid inputs
            t = TxtTime.Text
            uVertical = TxtVelocity.Text
            intAngle = TxtAngle.Text
            intPower = txtPower.Text
        Else : MsgBox("Please check your inputs, the simulator doesnt accept letters Or words.")
        End If
    End Sub
End Class

还有一些表单控件,包括PictureBox(pbSurface),4个TextBoxes和一个Button。

0 个答案:

没有答案