Click here to Skip to main content
15,886,362 members
Articles / Desktop Programming / WPF

Targets

Rate me:
Please Sign up or sign in to vote.
4.94/5 (32 votes)
15 Apr 2011CPOL6 min read 65.2K   2.3K   41  
A WPF Target Shooting Game
Imports System
Imports System.Collections.Generic
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports System.Windows.Media.Imaging
Imports System.Windows.Navigation
Imports System.Windows.Shapes

Partial Public Class Target

    Public Points As Integer

    Public Sub New()
        MyBase.New()

        Me.InitializeComponent()

        ' Insert code required on object creation below this point.
    End Sub

    Private Sub HitTarget(ByVal e As System.Windows.Input.MouseButtonEventArgs)
        Dim x As Double = e.GetPosition(TargetCanvas).X
        Dim y As Double = e.GetPosition(TargetCanvas).Y
        Dim dent As New Dent()

        Canvas.SetLeft(dent, x)
        Canvas.SetTop(dent, y)
        TargetCanvas.Children.Add(dent)
    End Sub

    Private Sub MainArea_MouseLeftButtonDown(ByVal sender As Object, _
                                             ByVal e As System.Windows.Input.MouseButtonEventArgs) _
                                             Handles MainArea.MouseLeftButtonDown
        HitTarget(e)
    End Sub

    Private Sub GreenZone_MouseLeftButtonDown(ByVal sender As Object, _
                                              ByVal e As System.Windows.Input.MouseButtonEventArgs) _
                                              Handles GreenZone.MouseLeftButtonDown
        HitTarget(e)
        Points = 7
    End Sub

    Private Sub BlueZone_MouseLeftButtonDown(ByVal sender As Object, _
                                             ByVal e As System.Windows.Input.MouseButtonEventArgs) _
                                             Handles BlueZone.MouseLeftButtonDown
        HitTarget(e)
        Points = 8
    End Sub

    Private Sub YellowZone_MouseLeftButtonDown(ByVal sender As Object, _
                                               ByVal e As System.Windows.Input.MouseButtonEventArgs) _
                                               Handles YellowZone.MouseLeftButtonDown
        HitTarget(e)
        Points = 9
    End Sub

    Private Sub RedZone_MouseLeftButtonDown(ByVal sender As Object, _
                                            ByVal e As System.Windows.Input.MouseButtonEventArgs) _
                                            Handles RedZone.MouseLeftButtonDown
        HitTarget(e)
        Points = 10
    End Sub

    Public Sub PlayStoryboard()
        Dim targeter As Storyboard
        targeter = CType(Me.Resources("TargetStoryboard"), Storyboard)
        targeter.Begin(Me)
    End Sub
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Kenya Kenya
Experienced C# software developer with a passion for WPF.

Awards,
  • CodeProject MVP 2013
  • CodeProject MVP 2012
  • CodeProject MVP 2021

Comments and Discussions