Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Creating a Bouncing Ball Game on Win Form.

Author: DevASP
Download Source Code : 562_Bounceball.zip

This article is about how you can create an application in which you can bounce a ball here and there. This article is useful for the developers who are interested in game programming using .NET.

 Steps you will do.

 Start visual studio and create a new window application.

 Set the following properties of the form.

 

Name = “frmMain”

FormBorderStyle = “FixedSingle”

MaximizeBox = “False”

Text = “Bounce Ball”

 

 

Now drop a picture box control on the form and set its following properties.

 

Name = “pic”

BackColor = “ControlText”

 

 

Now add two buttons on the form and set their properties as below.

 

First button control.

 

Name = “btn”

Text = “Start”

 

 

Second button control.

 

Name = “btn2”

Text = “Stop”

 

  

Now your interface is designed. Open the code window of the form and import the following namespaces as below.

 

Imports System.Drawing

Imports System.Drawing.Drawing2D

Imports System.Threading

 

 

Now declare the following global variables as below.

 

 Private Declare Function Beep Lib "kernel32" Alias "Beep" (ByVal dwFreq As Integer, ByVal dwDuration As Integer) As Integer

 

    Dim t As Thread

    Dim c As Boolean

    Dim a As Random = New Random()

    Dim X As Integer = 1

    Dim Y As Integer = 1

    Dim directionX As Integer = 0

    Dim directionY As Integer = 0

    Dim velocityX As Integer = 1

    Dim velocityY As Integer = 1

    

  

Now create a function in the code as below.

 

Private Sub drawBall()

 

Dim bit As Bitmap = New Bitmap(pic.Width, pic.Height)

 

Dim g As Graphics = Graphics.FromImage(bit)

 

Dim myBrush As SolidBrush = New SolidBrush(Color.Yellow)

 

g.SmoothingMode = SmoothingMode.AntiAlias

 

   Do

    Application.DoEvents()

    t.Sleep(1)

     If Y <= 0 Or directionY = 0 Then

      If Y = 1 Then Beep(500, 2)

        Y = Y + velocityY

        directionY = 0

       End If

 

       If X <= 1 Or directionX = 0 Then

        If X = 1 Then Beep(500, 2)

         X = X + velocityX

         directionX = 0

        End If

 

If Y >= pic.Height - 8 Or directionY = 1 Then

 

If Y = pic.Height - 9 Then Beep(500, 2)

   Y = Y - velocityY

   directionY = 1

End If

 

If X >= pic.Width - 8 Or directionX = 1 Then

  If X = pic.Width - 9 Then Beep(500, 2)

    X = X - velocityX

    directionX = 1

 End If

 

 g.FillEllipse(myBrush, X, Y, 8, 8)

 

 pic.Image = bit

 

 pic.Refresh()

 

 g.Clear(Color.Black)

 

 Loop Until c = True

 

End Sub

 

 

Now in the click event of the start button write the following code to start the ball bounce.

 

c = False

      X = a.Next(1, pic.Width)

      Y = a.Next(1, pic.Height)

      t = New Thread(AddressOf drawBall)

      t.Start()

 

 

Now write the following code in the click event of the stop button to stop the ball from bouncing.

 

  c = True

  t.Abort()

 

  

This is all you will do.

 Now run your application and test it.

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Creating a Tic Tac Game in Visual Basic.Net

Disclaimer - Privacy
© 2002-2017 DevASP.net