How to Draw a Smiley Face with Moving Pupils - Visual Basic .NET

Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013

In this tutorial, we will learn how to draw a simple smiley face with moving pupils. The final product will consist of a smiley face and 8 arrow buttons which move the pupils. Four of the buttons control the left pupil, and four buttons will control the right pupil. The buttons will move the pupils up, down, left, and right.

Add 8 Buttons to form. You can adjust the placement of the buttons to position them like in the picture or however else you want. Be sure to leave a blank space above them for the smiley face to go.

*If you are a new Visual Basic user, click here to learn how to start a new project in Visual Basic.

 

 To be able to copy and paste the following code, change the name properties of each button as follows:
LeftPupilLeft      
LeftPupilRight    
LeftPupilUp       
LeftPupilDown  
RightPupilLeft    
RightPupilRight  
RightPupilUp     
RightPupilDown

Add the following declarations below the Public Class Form1

    Dim xLeft As Integer = 100
    Dim xRight As Integer = 143
    Dim yleft As Integer = 75
    Dim yright As Integer = 75

The following code is for the arrow buttons click event.


Private Sub LeftPupilLeft_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LeftPupilLeft.Click
        Dim pnt As Graphics
        pnt = Me.CreateGraphics
        Dim pencil As New Pen(Color.Black, 2)
        pnt.DrawEllipse(pencil, 50, 50, 150, 150)           'face(border)
        pnt.FillEllipse(Brushes.Yellow, 50, 50, 150, 150)   'face(yellow)
        pnt.DrawArc(pencil, 85, 100, 80, 70, 0, 180)        'smile
        pnt.FillEllipse(Brushes.Black, 113, 110, 25, 35)    'nose
        pnt.FillEllipse(Brushes.White, 90, 70, 25, 35)      'left eye(white)
        pnt.DrawEllipse(pencil, 90, 70, 25, 35)             'border of left eye
        pnt.FillEllipse(Brushes.Purple, xLeft, yleft, 15, 22)    'pupil of left eye
        pnt.FillEllipse(Brushes.White, 133, 70, 25, 35)     'right eye(white)
        pnt.DrawEllipse(pencil, 133, 70, 25, 35)            'border of right eye
        pnt.FillEllipse(Brushes.Purple, xRight, yright, 15, 22)    'pupil of right eye
        xLeft -= 1
    End Sub

    Private Sub LeftPupilRight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LeftPupilRight.Click
        Dim pnt As Graphics
        pnt = Me.CreateGraphics
        Dim pencil As New Pen(Color.Black, 2)
        pnt.DrawEllipse(pencil, 50, 50, 150, 150)           'face(border)
        pnt.FillEllipse(Brushes.Yellow, 50, 50, 150, 150)   'face(yellow)
        pnt.DrawArc(pencil, 85, 100, 80, 70, 0, 180)        'smile
        pnt.FillEllipse(Brushes.Black, 113, 110, 25, 35)    'nose
        pnt.FillEllipse(Brushes.White, 90, 70, 25, 35)      'left eye(white)
        pnt.DrawEllipse(pencil, 90, 70, 25, 35)             'border of left eye
        pnt.FillEllipse(Brushes.Purple, xLeft, yleft, 15, 22)    'pupil of left eye
        pnt.FillEllipse(Brushes.White, 133, 70, 25, 35)     'right eye(white)
        pnt.DrawEllipse(pencil, 133, 70, 25, 35)            'border of right eye
        pnt.FillEllipse(Brushes.Purple, xRight, yright, 15, 22)    'pupil of right eye
        xLeft += 1
    End Sub

    Private Sub RightPupilLeft_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RightPupilLeft.Click
        Dim pnt As Graphics
        pnt = Me.CreateGraphics
        Dim pencil As New Pen(Color.Black, 2)
        pnt.DrawEllipse(pencil, 50, 50, 150, 150)           'face(border)
        pnt.FillEllipse(Brushes.Yellow, 50, 50, 150, 150)   'face(yellow)
        pnt.DrawArc(pencil, 85, 100, 80, 70, 0, 180)        'smile
        pnt.FillEllipse(Brushes.Black, 113, 110, 25, 35)    'nose
        pnt.FillEllipse(Brushes.White, 90, 70, 25, 35)      'left eye(white)
        pnt.DrawEllipse(pencil, 90, 70, 25, 35)             'border of left eye
        pnt.FillEllipse(Brushes.Purple, xLeft, yleft, 15, 22)    'pupil of left eye
        pnt.FillEllipse(Brushes.White, 133, 70, 25, 35)     'right eye(white)
        pnt.DrawEllipse(pencil, 133, 70, 25, 35)            'border of right eye
        pnt.FillEllipse(Brushes.Purple, xRight, yright, 15, 22)    'pupil of right eye
        xRight -= 1
    End Sub

    Private Sub RightPupilRight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RightPupilRight.Click
        Dim pnt As Graphics
        pnt = Me.CreateGraphics
        Dim pencil As New Pen(Color.Black, 2)
        pnt.DrawEllipse(pencil, 50, 50, 150, 150)           'face(border)
        pnt.FillEllipse(Brushes.Yellow, 50, 50, 150, 150)   'face(yellow)
        pnt.DrawArc(pencil, 85, 100, 80, 70, 0, 180)        'smile
        pnt.FillEllipse(Brushes.Black, 113, 110, 25, 35)    'nose
        pnt.FillEllipse(Brushes.White, 90, 70, 25, 35)      'left eye(white)
        pnt.DrawEllipse(pencil, 90, 70, 25, 35)             'border of left eye
        pnt.FillEllipse(Brushes.Purple, xLeft, yleft, 15, 22)    'pupil of left eye
        pnt.FillEllipse(Brushes.White, 133, 70, 25, 35)     'right eye(white)
        pnt.DrawEllipse(pencil, 133, 70, 25, 35)            'border of right eye
        pnt.FillEllipse(Brushes.Purple, xRight, yright, 15, 22)    'pupil of right eye
        xRight += 1
    End Sub

    Private Sub LeftPupilDown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LeftPupilDown.Click
        Dim pnt As Graphics
        pnt = Me.CreateGraphics
        Dim pencil As New Pen(Color.Black, 2)
   pnt.DrawEllipse(pencil, 50, 50, 150, 150)           'face(border)
        pnt.FillEllipse(Brushes.Yellow, 50, 50, 150, 150)   'face(yellow)
        pnt.DrawArc(pencil, 85, 100, 80, 70, 0, 180)        'smile
        pnt.FillEllipse(Brushes.Black, 113, 110, 25, 35)    'nose
        pnt.FillEllipse(Brushes.White, 90, 70, 25, 35)      'left eye(white)
        pnt.DrawEllipse(pencil, 90, 70, 25, 35)             'border of left eye
        pnt.FillEllipse(Brushes.Purple, xLeft, yleft, 15, 22)   'pupil of left eye
        pnt.FillEllipse(Brushes.White, 133, 70, 25, 35)     'right eye(white)
        pnt.DrawEllipse(pencil, 133, 70, 25, 35)            'border of right eye
        pnt.FillEllipse(Brushes.Purple, xRight, yright, 15, 22)    'pupil of right eye
        yleft += 1
    End Sub

    Private Sub LeftPupilUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LeftPupilUp.Click
        Dim pnt As Graphics
        pnt = Me.CreateGraphics
        Dim pencil As New Pen(Color.Black, 2)
        pnt.DrawEllipse(pencil, 50, 50, 150, 150)           'face(border)
        pnt.FillEllipse(Brushes.Yellow, 50, 50, 150, 150)   'face(yellow)
        pnt.DrawArc(pencil, 85, 100, 80, 70, 0, 180)        'smile
        pnt.FillEllipse(Brushes.Black, 113, 110, 25, 35)    'nose
        pnt.FillEllipse(Brushes.White, 90, 70, 25, 35)      'left eye(white)
        pnt.DrawEllipse(pencil, 90, 70, 25, 35)             'border of left eye
        pnt.FillEllipse(Brushes.Purple, xLeft, yleft, 15, 22)    'pupil of left eye
        pnt.FillEllipse(Brushes.White, 133, 70, 25, 35)     'right eye(white)
        pnt.DrawEllipse(pencil, 133, 70, 25, 35)            'border of right eye
        pnt.FillEllipse(Brushes.Purple, xRight, yright, 15, 22)    'pupil of right eye
        yleft -= 1
    End Sub

    Private Sub RightPupilUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RightPupilUp.Click
        Dim pnt As Graphics
        pnt = Me.CreateGraphics
        Dim pencil As New Pen(Color.Black, 2)
        pnt.DrawEllipse(pencil, 50, 50, 150, 150)           'face(border)
        pnt.FillEllipse(Brushes.Yellow, 50, 50, 150, 150)   'face(yellow)
        pnt.DrawArc(pencil, 85, 100, 80, 70, 0, 180)        'smile
        pnt.FillEllipse(Brushes.Black, 113, 110, 25, 35)    'nose
        pnt.FillEllipse(Brushes.White, 90, 70, 25, 35)      'left eye(white)
        pnt.DrawEllipse(pencil, 90, 70, 25, 35)             'border of left eye
        pnt.FillEllipse(Brushes.Purple, xLeft, yleft, 15, 22)    'pupil of left eye
        pnt.FillEllipse(Brushes.White, 133, 70, 25, 35)     'right eye(white)
        pnt.DrawEllipse(pencil, 133, 70, 25, 35)            'border of right eye
        pnt.FillEllipse(Brushes.Purple, xRight, yright, 15, 22)    'pupil of right eye
        yright -= 1
    End Sub

    Private Sub RightPupilDown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RightPupilDown.Click
        Dim pnt As Graphics
      pnt = Me.CreateGraphics
        Dim pencil As New Pen(Color.Black, 2)
        pnt.DrawEllipse(pencil, 50, 50, 150, 150)           'face(border)
        pnt.FillEllipse(Brushes.Yellow, 50, 50, 150, 150)   'face(yellow)
        pnt.DrawArc(pencil, 85, 100, 80, 70, 0, 180)        'smile
        pnt.FillEllipse(Brushes.Black, 113, 110, 25, 35)    'nose
        pnt.FillEllipse(Brushes.White, 90, 70, 25, 35)      'left eye(white)
        pnt.DrawEllipse(pencil, 90, 70, 25, 35)             'border of left eye
  pnt.FillEllipse(Brushes.Purple, xLeft, yleft, 15, 22)    'pupil of left eye
        pnt.FillEllipse(Brushes.White, 133, 70, 25, 35)     'right eye(white)
        pnt.DrawEllipse(pencil, 133, 70, 25, 35)            'border of right eye
        pnt.FillEllipse(Brushes.Purple, xRight, yright, 15, 22)    'pupil of right eye
        yright += 1
    End Sub

When you run the code this is what you should see:

By pressing an arrow button, the smiley face will appear.

By pressing the arrow buttons you can make your smiley face look like this:


Share This

Home | About | Contact | Privacy Policy

Copyright visual-basic-tutorials.com 2017 - All Rights Reserved.