User:Pluke/projectile

From Wikibooks, open books for an open world
Jump to navigation Jump to search
Public Class frmGame
    'add some targets and the ability to adjust the trajectory of the ball

    Dim bx, by As Integer
    Dim t, a, vertdist, horidist, velvert0, velhori0 As Integer

    'As soon as the game starts the projectile variables are set up and the ball given a start position
    Private Sub frmGame_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        a = 1
        t = 0
        velvert0 = -5
        velhori0 = 3

        bx = 0
        by = Me.Height / 2

        tmrBall.Enabled = False

    End Sub

    'On the event of pressing space bar the ball starts, R resets the ball
    Private Sub frmGame_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        If e.KeyCode = Keys.Space Then
            tmrBall.Enabled = True
        End If

        If e.KeyCode = Keys.R Then
            tmrBall.Enabled = False
            t = 0
            velvert0 = -5
            velhori0 = 3
            bx = 0
            by = Me.Height / 2
        End If
        Invalidate()
    End Sub

    'On each tick of the timer, the ball moves across the screen
    Private Sub tmrBall_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrBall.Tick
        'projectile equations
        horidist = velhori0 * t
        vertdist = (velvert0 * t) + (0.5 * a * t * t)

        bx = bx + horidist
        by = by + vertdist

        t = t + 1

        Invalidate()
    End Sub

    'This code runs each time the invalidate() procedure is called
    Private Sub frmGame_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Dim pallete As Drawing.Graphics = Me.CreateGraphics
        Dim brush_black As New SolidBrush(Color.Black)

        pallete.FillRectangle(brush_black, New Rectangle(bx, by, 10, 10))

        pallete.FillRectangle(Brushes.DarkGreen, New Rectangle(2, 2, Me.Width - 2, 10))
        pallete.FillRectangle(Brushes.DarkGreen, New Rectangle(2, Me.Height - 40, Me.Width - 2, 10))

    End Sub
End Class