Sie sind auf Seite 1von 35

1 / 35

Individual assignment for Introduction to Visual Basic.net(IVBN) (Part 1 and Part 2)

Module: CE00328-1-IVBN Intake:TP1F0810COM Student: HuPu TP016188 Date due:25,SEP,2009

TP016188 HuPu

Individual Assignment for IVBN

2 / 35

Contents
Task 1 Welcome screen..3-4 Task 2 Greet User5-8 Task 3 Draw Spike.9-11 Task 4 Flag..13-14 Task 5 Car Class..15-21 Task 6 Link Screens..22-35

TP016188 HuPu

Individual Assignment for IVBN

3 / 35

Task 1 Welcome Screen


Screen Shots & Description When the application runs, the form displayed as follows.

When user either click or press hotkey of each button, the background color changed to relevant color.

TP016188 HuPu

Individual Assignment for IVBN

4 / 35

Codes Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Set back color to red while form loading Me.BackColor = Color.Red End Sub Private Sub btns_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRed.Click, btnBlue.Click, btnGreen.Click, btnYellow.Click 'Set background color of form while relevant button clicked Select Case sender.text Case "&Red" Me.BackColor = Color.Red Case "&Blue" Me.BackColor = Color.Blue Case "&Green" Me.BackColor = Color.Green Case "&Yellow" Me.BackColor = Color.Yellow End Select End Sub End Class

TP016188 HuPu

Individual Assignment for IVBN

5 / 35

Task 2 Greet User


Screen Shots & Description When the application runs, the form displayed as follows.The button Continue is disabled.

When user either click or press hotkey of each button, the background color changed to relevant color. At same time the textbox down there will get focus.

When user input something in textbox, the button Continue turned to enabled. (If the user clear the textbox, the buttonll be disabled again.)

TP016188 HuPu

Individual Assignment for IVBN

6 / 35

Then if the user click the button Continue, a messagebox with the name input,the scheme selected will be displayed in two lines as follows.

TP016188 HuPu

Individual Assignment for IVBN

7 / 35

Codes Public Class Form1 'Declare two form-level variable as user's name and the chosen color Dim user_name As String Dim user_color As String = "Red" Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Set default back color to red 'Disable the continue button at beginning Me.BackColor = Color.Red btnc.Enabled = False TextBox1.Focus() End Sub Private Sub btns_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRed.Click, btnBlue.Click, btnGreen.Click, btnYellow.Click 'Set back color to the relevant button clicked Select Case sender.text Case "Red" Me.BackColor = Color.Red Case "Blue" Me.BackColor = Color.Blue Case "Green" Me.BackColor = Color.Green Case "Yellow" Me.BackColor = Color.Yellow End Select 'send the chosen color's name to variable user_color = sender.text 'set textbox focus after choosing an backcolor TextBox1.Focus() End Sub Private Sub btnc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnc.Click 'display message box while button "continue" clicked user_name = TextBox1.Text MsgBox("Hello " & user_name & (Chr(13)) & "You have chosen the " & user_color & " scheme", MsgBoxStyle.Information, "Greeting") End Sub Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged 'Set button "continue" only enabled when Name textbox's not empty

TP016188 HuPu

Individual Assignment for IVBN

8 / 35

If TextBox1.Text <> "" Then btnc.Enabled = True Else : btnc.Enabled = False End If End Sub End Class

TP016188 HuPu

Individual Assignment for IVBN

9 / 35

Task 3 Draw Spike


Screen Shots & Description When the application runs, the form displayed as follows.

When user press button Draw after typing correct values, the spike with specific location and size is drawn on the left area.

TP016188 HuPu

Individual Assignment for IVBN

10 / 35

If there are blanks or invalid input type, the application will display error message after button Draw clicked.

If the user input values correctly but the graphic will be out of the graphic area, an error message also comes out.

TP016188 HuPu

Individual Assignment for IVBN

11 / 35

Codes Public Class Form1 Private Sub btnDraw_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDraw.Click 'Catch any errors of input,such as valid type. Try Dim x As Integer = CInt(txtX.Text) Dim y As Integer = CInt(txtY.Text) Dim size As Integer = CInt(txtSize.Text) 'Check if the graphic out of bounds. If x >= 0 And y >= 0 And size > 0 And x + size * 1.5 <= 200 And y + size <= 200 Then 'Declare variables and draw spiker Dim p As Graphics p = picSpike.CreateGraphics p.Clear(Color.White) Dim redbrush As New SolidBrush(Color.Red) Dim redpen As New Pen(Color.Red, CInt(size * 0.1)) Dim blackbrush As New SolidBrush(Color.Black) 'Draw body p.FillEllipse(redbrush, CInt(x + size * 0.25), y, size, size) 'Draw arms p.DrawLine(redpen, x, y, CInt(x + size * 1.5), y + size) p.DrawLine(redpen, x, y + size, CInt(x + size * 1.5), y) p.DrawLine(redpen, x, CInt(y + size * 0.5), CInt(x + size * 1.5), CInt(y + size * 0.5)) 'Draw features p.FillEllipse(blackbrush, CInt(x + size * 0.45), CInt(y + size * 0.25), CInt(size * 0.2), CInt(size * 0.2)) p.FillEllipse(blackbrush, CInt(x + size * 0.85), CInt(y + size * 0.25), CInt(size * 0.2), CInt(size * 0.2)) p.FillRectangle(blackbrush, CInt(x + size * 0.55), CInt(y + size * 0.65), CInt(size * 0.4), CInt(size * 0.1)) Else 'Display error message. MsgBox("Graphic out of bounds!Please check your input.") End If Catch 'Display error message. MsgBox("Invalid!Please check your input.") End Try End Sub End Class

TP016188 HuPu

Individual Assignment for IVBN

12 / 35

TP016188 HuPu

Individual Assignment for IVBN

13 / 35

Task 4 Flag
Screen Shots & Description When the application runs, the form displayed as follows. Only button Flag is enabled.

When button Flag is clicked, a flag displayed randomly. Then radio buttons turn to enabled, button Flag disabled.

Once any radio button being clicked, a message includes correct or incorrect information displayed on down-left side. Then button Flag and radio buttons will turn to default state.

TP016188 HuPu

Individual Assignment for IVBN

14 / 35

Codes Form1.vb Public Class Form1 Dim randomnum As Integer Dim chosennum As Integer Dim randomname As String Dim countries() As String = {"Belgium", "Canada", "China", "Finland", "France", "Sudan", "Zambia"} Dim flag As Graphics Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load pnlCountries.Enabled = False flag = picFlag.CreateGraphics End Sub Private Sub btnFlag_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFlag.Click flag.Clear(Me.BackColor) lblMessage.Text = "" 'Get a random number from 0 to 6 Dim myrandom As New Random randomnum = CInt(myrandom.Next(0, 7)) 'Set randomname by the random number randomname = countries(randomnum) 'Display revelant picture of flag flag.DrawImage(New Bitmap(My.Application.Info.DirectoryPath & "\flags\pic" & randomname & ".png"), 0, 0) 'Set button "flag" disabled,radio buttons enabled btnFlag.Enabled = False pnlCountries.Enabled = True End Sub Private Sub rdb_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdbBelgium.Click, rdbCanada.Click, _ rdbChina.Click, rdbFinland.Click, rdbFrance.Click, rdbSudan.Click, rdbZambia.Click 'Judge the user's choice If sender.text = randomname Then lblMessage.Text = sender.text & " is Correct!" Else : lblMessage.Text = sender.text & " is Incorrect!" & (Chr(13)) & "Correct answer is " & randomname & "!" End If 'Set radio buttons disabled and unchecked,button "flag" enabled. sender.checked = False pnlCountries.Enabled = False btnFlag.Enabled = True End Sub End Class

TP016188 HuPu

Individual Assignment for IVBN

15 / 35

Task 5 Car Class


Screen Shots & Description When the application runs, the form displayed as follows. Only button Create is enabled.

When button Create is clicked, a car is drawn on the blank area with the direction to right. At this time only button Right and Clear are enabled.

When user click button Right, the car will move 40 pixels to right in series.

TP016188 HuPu

Individual Assignment for IVBN

16 / 35

Once the cars moved to the edge of the blank area, the button with relevant direction will be disabled to protect the car from moving out of the area.

When user click button Left, the car will move 40 pixels to left in series as follows.

The application will be set to default state after button Clear being clicked.

TP016188 HuPu

Individual Assignment for IVBN

17 / 35

Codes Car.vb Public Class car 'Declare variables Private x As Integer Private y As Integer Private width As Integer Private height As Integer Private color As Color Private direction As Integer Private distance As Integer Private g As Graphics 'Set properties: pw()--width,pg()--g,px()--x,py()--y,pc()--height,pc()--color,pd()--direction,pds()--distance Public Property pw() As Integer Get Return width End Get Set(ByVal value As Integer) width = value End Set End Property Public Property pg() As Graphics Get Return g End Get Set(ByVal value As Graphics) g = value End Set End Property Public Property px() As Integer Get Return x End Get Set(ByVal value As Integer) x = value End Set End Property Public Property py() As Integer Get Return y End Get Set(ByVal value As Integer) y = value End Set

TP016188 HuPu

Individual Assignment for IVBN

18 / 35

End Property Public Property pc() As Color Get Return color End Get Set(ByVal value As Color) color = value End Set End Property Public Property ph() As Integer Get Return height End Get Set(ByVal value As Integer) height = value End Set End Property Public Property pd() As Integer Get Return direction End Get Set(ByVal value As Integer) direction = value End Set End Property Public Property pds() As Integer Get Return distance End Get Set(ByVal value As Integer) distance = value End Set End Property 'Constructor Public Sub New(ByVal _x, ByVal _y, ByVal _width, ByVal _height, ByVal _color, ByVal _distance) x = _x y = _y width = _width height = _height color = _color distance = _distance End Sub 'Draw method to draw the car by revelant variables Public Sub draw(ByVal _direction As Integer, ByVal _g As Graphics)

TP016188 HuPu

Individual Assignment for IVBN

19 / 35

direction = _direction g = _g g.Clear(color.White) Dim mybrush As New SolidBrush(color) Dim blackbrush As New SolidBrush(color.Black) Dim lumpx As Integer = CInt(width / 4) Dim lumpy As Integer = CInt(height / 4) 'draw cartop Dim cartop1 As New Point(x + width * direction, y + lumpy) Dim cartop2 As New Point(x + lumpx + 2 * lumpx * direction, y) Dim cartop3 As New Point(x + 3 * lumpx - 2 * lumpx * direction, y + lumpy) Dim cartop As Point() = {cartop1, cartop2, cartop3} g.FillPolygon(mybrush, cartop) 'draw car body g.FillRectangle(mybrush, x, y + lumpy, width, 2 * lumpy) 'draw car head g.FillRectangle(blackbrush, x + 3 * lumpx - 3 * lumpx * direction, y + lumpy, lumpx, CInt(lumpy / 2)) 'draw car tires g.FillEllipse(blackbrush, x + CInt(lumpx / 2), y + 2 * lumpy, lumpx, lumpx) g.FillEllipse(blackbrush, x + CInt(5 * lumpx / 2), y + 2 * lumpy, lumpx, lumpx) End Sub 'Move method Public Sub moveCar(ByVal _d As Integer) direction = _d x = x - direction * 1 + (1 - direction) * 1 draw(direction, g) End Sub End Class

TP016188 HuPu

Individual Assignment for IVBN

20 / 35

Form1.vb Public Class Form1 Dim mypicroad As Graphics 'Create a new car instance Dim mycar As car Dim currentdistance As Integer = 0 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Disable the left and right button while form loading btnLeft.Enabled = False btnRight.Enabled = False btnClear.Enabled = False mypicroad = picRoad.CreateGraphics() End Sub 'Using constructor to set the properties of "mycar" 'Using draw method of 'mycar' to draw the car on picture box 'Disable Right button and enable create button Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click mycar = New car(20, 20, 100, 50, Color.Orange, 40) mycar.draw(0, mypicroad) btnCreate.Enabled = False btnRight.Enabled = True btnClear.Enabled = True End Sub Private Sub btnLeft_btnRight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLeft.Click, btnRight.Click ' move to left, direction=1 ' move to right, direction=0 If sender.text = "Left" Then mycar.pd = 1 Else : mycar.pd = 0 End If Timer1.Enabled = True End Sub Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click 'Reset the application to default state mypicroad.Clear(Color.White) btnCreate.Enabled = True btnLeft.Enabled = False btnRight.Enabled = False btnClear.Enabled = False End Sub

TP016188 HuPu

Individual Assignment for IVBN

21 / 35

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick mycar.moveCar(mycar.pd) currentdistance += 1 If currentdistance = mycar.pds Then Timer1.Enabled = False currentdistance = 0 End If 'Check if the car is going out of the picture box 'If it is, disable the relevant button to protect it from going out If mycar.px <= 20 Then btnLeft.Enabled = False Timer1.Enabled = False Else : btnLeft.Enabled = True End If If mycar.px + mycar.pw >= 480 Then btnRight.Enabled = False Timer1.Enabled = False Else : btnRight.Enabled = True End If End Sub End Class

TP016188 HuPu

Individual Assignment for IVBN

22 / 35

Task 6 Link Screens


Screen Shots & Description When the application runs, the form displayed as follows. Notice that the window is in center screen.(The other windows are same.) In this case I input the name as hupu and set the background color to blue, then press Continue

After clicking Ok of the messagebox up there. The MDI Form displays as follows. There are three items with pictures in both menu item Tasks and the tool bar.

TP016188 HuPu

Individual Assignment for IVBN

23 / 35

Select each of the task the relevant task will start in a dialog window. Notice that the background colors of them are the color I chose in welcome screen, and none of the windows has a control box, and the back buttons are default disabled.

For each task, Identify Flags as an example, when you fail it and click back, a message You fail the game!Please try again come out, and the item on both menu bar and tool bar are still enabled.

TP016188 HuPu

Individual Assignment for IVBN

24 / 35

When user complete the task and click back, the message Youve completed the game!This game is not available for you any more! come out and the item on both menu bar and tool bar will be disabled.

TP016188 HuPu

Individual Assignment for IVBN

25 / 35

Codes Public.vb Module pubic Public backc As Color Public result_flag As Boolean Public result_spike As Boolean Public result_car As Boolean End Module

fWelcome.vb Public Class fWelcome 'Declare two form-level variable as user's name and the chosen color Dim user_name As String Dim user_color As String = "Red" Private Sub fWelcome_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Set default back color to red 'Disable the continue button at beginning Me.BackColor = Color.Red btnc.Enabled = False TextBox1.Focus() End Sub Private Sub btns_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRed.Click, btnBlue.Click, btnGreen.Click, btnYellow.Click 'Set back color to the relevant button clicked Select Case sender.text Case "Red" Me.BackColor = Color.Red Case "Blue" Me.BackColor = Color.Blue Case "Green" Me.BackColor = Color.Green Case "Yellow" Me.BackColor = Color.Yellow End Select 'send the chosen color's name to variable user_color = sender.text 'set textbox focus after choosing an backcolor TextBox1.Focus() End Sub

TP016188 HuPu

Individual Assignment for IVBN

26 / 35

Private Sub btnc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnc.Click 'display message box while button "continue" clicked user_name = TextBox1.Text MsgBox("Hello " & user_name & (Chr(13)) & "You have chosen the " & user_color & " scheme", MsgBoxStyle.Information, "Greeting") backc = Me.BackColor Me.Hide() fLink.Show() End Sub Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged 'Set button "continue" only enabled when Name textbox's not empty If TextBox1.Text <> "" Then btnc.Enabled = True Else : btnc.Enabled = False End If End Sub End Class

fLink.vb Imports System.Windows.Forms Public Class fLink Private Sub ExitToolsStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ExitToolStripMenuItem.Click Me.Close() fWelcome.Close() End Sub Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton3.Click ToolStripButton3.Enabled = False CreateCarToolStripMenuItem.Enabled = False fCar.ShowDialog() End Sub Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click ToolStripButton2.Enabled = False IdentifyFlagsToolStripMenuItem.Enabled = False fFlag.ShowDialog()
TP016188 HuPu Individual Assignment for IVBN

27 / 35

End Sub Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click CreateSpikeToolStripMenuItem.Enabled = False ToolStripButton1.Enabled = False fSpike.ShowDialog() End Sub Private Sub fLink_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.BackColor = backc End Sub Private Sub CreateSpikeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CreateSpikeToolStripMenuItem.Click CreateSpikeToolStripMenuItem.Enabled = False ToolStripButton1.Enabled = False fSpike.ShowDialog() End Sub Private Sub IdentifyFlagsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IdentifyFlagsToolStripMenuItem.Click ToolStripButton2.Enabled = False IdentifyFlagsToolStripMenuItem.Enabled = False fFlag.ShowDialog() End Sub Private Sub CreateCarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CreateCarToolStripMenuItem.Click ToolStripButton3.Enabled = False CreateCarToolStripMenuItem.Enabled = False fCar.ShowDialog() End Sub End Class

fSpike.vb
TP016188 HuPu Individual Assignment for IVBN

28 / 35

Public Class fSpike Private Sub fSpike_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.BackColor = backc btnback.Enabled = False End Sub Private Sub btnDraw_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDraw.Click btnback.Enabled = True 'Catch any errors of input,such as valid type. Try Dim x As Integer = CInt(txtX.Text) Dim y As Integer = CInt(txtY.Text) Dim size As Integer = CInt(txtSize.Text) 'Check if the graphic out of bounds. If x >= 0 And y >= 0 And size > 0 And x + size * 1.5 <= 200 And y + size <= 200 Then 'Declare variables and draw spiker Dim p As Graphics p = picSpike.CreateGraphics p.Clear(Color.White) Dim redbrush As New SolidBrush(Color.Red) Dim redpen As New Pen(Color.Red, CInt(size * 0.1)) Dim blackbrush As New SolidBrush(Color.Black) 'Draw body p.FillEllipse(redbrush, CInt(x + size * 0.25), y, size, size) 'Draw arms p.DrawLine(redpen, x, y, CInt(x + size * 1.5), y + size) p.DrawLine(redpen, x, y + size, CInt(x + size * 1.5), y) p.DrawLine(redpen, x, CInt(y + size * 0.5), CInt(x + size * 1.5), CInt(y + size * 0.5)) 'Draw features p.FillEllipse(blackbrush, CInt(x + size * 0.45), CInt(y + size * 0.25), CInt(size * 0.2), CInt(size * 0.2)) p.FillEllipse(blackbrush, CInt(x + size * 0.85), CInt(y + size * 0.25), CInt(size * 0.2), CInt(size * 0.2)) p.FillRectangle(blackbrush, CInt(x + size * 0.55), CInt(y + size * 0.65), CInt(size * 0.4), CInt(size * 0.1)) result_spike = True Else 'Display error message. MsgBox("Graphic out of bounds!Please check your input.") result_spike = False End If Catch 'Display error message. MsgBox("Invalid!Please check your input.") result_spike = False End Try

TP016188 HuPu

Individual Assignment for IVBN

29 / 35

End Sub Private Sub btnback_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnback.Click If result_spike Then MsgBox("You've completed the game! This game is not availiable for you any more!") Else fLink.CreateSpikeToolStripMenuItem.Enabled = True fLink.ToolStripButton1.Enabled = True MsgBox("You failed the game! Please try again!") End If Me.Close() End Sub End Class

fFlag.vb Public Class fFlag 'Declare variables Dim randomnum As Integer Dim chosennum As Integer Dim randomname As String Dim countries() As String = {"Belgium", "Canada", "China", "Finland", "France", "Sudan", "Zambia"} Dim flag As Graphics Private Sub fFlag_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Clear lblmessage lblMessage.Text = "" 'Set button back disenabled while form loading btnback.Enabled = False 'Set background color Me.BackColor = backc 'Disable radio buttons at beginning pnlCountries.Enabled = False flag = picFlag.CreateGraphics End Sub Private Sub btnFlag_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFlag.Click 'Clear the flag picture flag.Clear(Me.BackColor) 'Clear lblmessage lblMessage.Text = "" 'Get a random number from 0 to 6
TP016188 HuPu Individual Assignment for IVBN

30 / 35

Dim myrandom As New Random randomnum = CInt(myrandom.Next(0, 7)) 'Set randomname by the random number randomname = countries(randomnum) 'Display revelant picture of flag flag.DrawImage(New Bitmap(My.Application.Info.DirectoryPath & "\flags\pic" & randomname & ".png"), 0, 0) 'Set button "flag" disabled,radio buttons enabled btnFlag.Enabled = False pnlCountries.Enabled = True End Sub Private Sub rdb_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdbBelgium.Click, rdbCanada.Click, _ rdbChina.Click, rdbFinland.Click, rdbFrance.Click, rdbSudan.Click, rdbZambia.Click 'Judge the user's choice If sender.text = randomname Then lblMessage.Text = sender.text & " is Correct!" result_flag = True Else : lblMessage.Text = sender.text & " is Incorrect!" & (Chr(13)) & "Correct answer is " & randomname & "!" result_flag = False End If 'Set button back enabled btnback.Enabled = True 'Set radio buttons disabled and unchecked,button "flag" enabled. sender.checked = False pnlCountries.Enabled = False btnFlag.Enabled = True End Sub Private Sub btnback_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnback.Click If result_flag Then MsgBox("You've completed the game! This game is not availiable for you any more!") Else fLink.ToolStripButton2.Enabled = True fLink.IdentifyFlagsToolStripMenuItem.Enabled = True MsgBox("You failed the game! Please try again!") End If Me.Close() End Sub End Class Car.vb
TP016188 HuPu Individual Assignment for IVBN

31 / 35

Public Class car 'Declare variables Private x As Integer Private y As Integer Private width As Integer Private height As Integer Private color As Color Private direction As Integer Private distance As Integer Private g As Graphics 'Set properties: pw()--width,pg()--g,px()--x,py()--y,pc()--height,pc()--color,pd()--direction,pds()--distance Public Property pw() As Integer Get Return width End Get Set(ByVal value As Integer) width = value End Set End Property Public Property pg() As Graphics Get Return g End Get Set(ByVal value As Graphics) g = value End Set End Property Public Property px() As Integer Get Return x End Get Set(ByVal value As Integer) x = value End Set End Property Public Property py() As Integer Get Return y End Get Set(ByVal value As Integer) y = value End Set End Property

TP016188 HuPu

Individual Assignment for IVBN

32 / 35

Public Property pc() As Color Get Return color End Get Set(ByVal value As Color) color = value End Set End Property Public Property ph() As Integer Get Return height End Get Set(ByVal value As Integer) height = value End Set End Property Public Property pd() As Integer Get Return direction End Get Set(ByVal value As Integer) direction = value End Set End Property Public Property pds() As Integer Get Return distance End Get Set(ByVal value As Integer) distance = value End Set End Property 'Constructor Public Sub New(ByVal _x, ByVal _y, ByVal _width, ByVal _height, ByVal _color, ByVal _distance) x = _x y = _y width = _width height = _height color = _color distance = _distance End Sub 'Draw method to draw the car by revelant variables Public Sub draw(ByVal _direction As Integer, ByVal _g As Graphics) direction = _direction

TP016188 HuPu

Individual Assignment for IVBN

33 / 35

g = _g g.Clear(color.White) Dim mybrush As New SolidBrush(color) Dim blackbrush As New SolidBrush(color.Black) Dim lumpx As Integer = CInt(width / 4) Dim lumpy As Integer = CInt(height / 4) 'draw cartop Dim cartop1 As New Point(x + width * direction, y + lumpy) Dim cartop2 As New Point(x + lumpx + 2 * lumpx * direction, y) Dim cartop3 As New Point(x + 3 * lumpx - 2 * lumpx * direction, y + lumpy) Dim cartop As Point() = {cartop1, cartop2, cartop3} g.FillPolygon(mybrush, cartop) 'draw car body g.FillRectangle(mybrush, x, y + lumpy, width, 2 * lumpy) 'draw car head g.FillRectangle(blackbrush, x + 3 * lumpx - 3 * lumpx * direction, y + lumpy, lumpx, CInt(lumpy / 2)) 'draw car tires g.FillEllipse(blackbrush, x + CInt(lumpx / 2), y + 2 * lumpy, lumpx, lumpx) g.FillEllipse(blackbrush, x + CInt(5 * lumpx / 2), y + 2 * lumpy, lumpx, lumpx) End Sub 'Move method Public Sub moveCar(ByVal _d As Integer) direction = _d x = x - direction * 1 + (1 - direction) * 1 draw(direction, g) End Sub End Class

fCar.vb Public Class fCar Dim mypicroad As Graphics 'Create a new car instance Dim mycar As car Dim currentdistance As Integer = 0 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Disable the left and right button while form loading Me.BackColor = backc btnLeft.Enabled = False btnRight.Enabled = False btnClear.Enabled = False btnback.Enabled = False
TP016188 HuPu Individual Assignment for IVBN

34 / 35

mypicroad = picRoad.CreateGraphics() End Sub 'Using constructor to set the properties of "mycar" 'Using draw method of 'mycar' to draw the car on picture box 'Disable Right button and enable create button Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click mycar = New car(20, 20, 100, 50, Color.Orange, 40) mycar.draw(0, mypicroad) btnCreate.Enabled = False btnRight.Enabled = True btnClear.Enabled = True btnback.Enabled = True result_car = True End Sub Private Sub btnLeft_btnRight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLeft.Click, btnRight.Click ' move to left, direction=1 ' move to right, direction=0 If sender.text = "Left" Then mycar.pd = 1 Else : mycar.pd = 0 End If Timer1.Enabled = True End Sub Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click 'Reset the application to default state mypicroad.Clear(Color.White) btnCreate.Enabled = True btnLeft.Enabled = False btnRight.Enabled = False btnClear.Enabled = False result_car = False End Sub Private Sub btnback_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnback.Click If result_car Then MsgBox("You've completed the game! This game is not availiable for you any more!") Else fLink.ToolStripButton3.Enabled = True fLink.CreateCarToolStripMenuItem.Enabled = True MsgBox("You failed the game! Please try again!")

TP016188 HuPu

Individual Assignment for IVBN

35 / 35

End If Me.Close() End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick mycar.moveCar(mycar.pd) currentdistance += 1 If currentdistance = mycar.pds Then Timer1.Enabled = False currentdistance = 0 End If 'Check if the car is going out of the picture box 'If it is, disable the relevant button to protect it from going out If mycar.px <= 20 Then btnLeft.Enabled = False Timer1.Enabled = False Else : btnLeft.Enabled = True End If If mycar.px + mycar.pw >= 480 Then btnRight.Enabled = False Timer1.Enabled = False Else : btnRight.Enabled = True End If End Sub End Class

TP016188 HuPu

Individual Assignment for IVBN

Das könnte Ihnen auch gefallen