Startsidan  ▸  Texter  ▸  Teknikblogg

Anders Hesselbom

Programmerare, skeptiker, sekulärhumanist, antirasist.
Författare till bok om C64 och senbliven lantis.
Röstar pirat.

First CLI control demo

2009-07-20

To demonstrate the CLI control, I have made a simple guessing game. The source code is below. If you want to run it yourself, place the exe file and a copy of CLIControl.dll in the same folder.

CLI Control

CLI Control demo

Public Class Form1

	'How many times has the user guessed?
	Private GuessCount As Integer = 0

	'The correct answer (can be 1 to 1000).
	Private CorrectAnswer As Integer

	'A random number generator is needed.
	Private Rnd As New Random()

	'Game flag.
	Private GameOn As Boolean = False

	'At startup, set some properties of the CLI control.
	Private Sub Form1_Load(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles MyBase.Load

		'Create a background for the control. If you have a
                'file, use LoadBackgroundFromFile.
		Dim Background As New System.Drawing.Bitmap(20, 50)
		Using G As System.Drawing.Graphics = Graphics.FromImage(Background)
			Using Blue1 As New SolidBrush(Color.FromArgb(255, 20, 40, 60))
				G.FillRectangle(Blue1, 0, 0, 20, 50)
			End Using
			Using Blue2 As New SolidBrush(Color.FromArgb(255, 40, 60, 80))
				G.FillRectangle(Blue2, 0, 10, 20, 10)
			End Using
		End Using
		'Assign the background (not needed if you use LoadBackgroundFromFile).
		Cli1.BackgroundImage = Background

		'Create and assing a brush for the text font.
		Cli1.TextBrush = New System.Drawing.Drawing2D.LinearGradientBrush( _
		 New Point(0, 0), _
		 New Point(0, Cli1.Height), _
		 Color.FromArgb(100, 255, 255, 255), _
		 Color.FromArgb(255, 255, 255, 255))

		'Add a shadow to the text.
		Cli1.TextShadow = True

		'The number that the user is trying to find (1 to 1000).
		Me.CorrectAnswer = Me.Rnd.Next(1, 1001)

		'Say something to the user, and add some lines for decoration.
		Cli1.WriteLine("Welcome! Guess a number between 1 and 1000!")
		Cli1.WriteLine()
		Dim P As New System.Drawing.Pen(Color.Yellow, 2)
		Cli1.GraphicalElements.AddLine("", P, 0, 304, Cli1.Width, 0)
		Cli1.GraphicalElements.AddLine("", P, 0, 323, Cli1.Width, 0)

		'Go!
		Me.GameOn = True
	End Sub

	Private Sub Cli1_UserTyped(ByVal Sender As Object, _
        ByVal Command As String) Handles Cli1.UserTyped
		'If the game is running, assume that the user is guessing
                'on the correct number.
		If GameOn Then
			Try
				Dim I As Integer = Integer.Parse(Command)
				If I < 1 Or I > 1000 Then
					Cli1.WriteLine("The number must be between 1 and 1000!")
				Else

					'Increase the number of guesses.
					Me.GuessCount += 1

					'Display the guess count as an image.
					Me.AddGuessCountImage()

					If I = CorrectAnswer Then

						'Correct!
						Cli1.WriteLine("Correct in " & Me.GuessCount.ToString() & _
                                                        " guesses!")
						Cli1.WriteLine()
						'Add a green line.
						Cli1.GraphicalElements.AddLine("", Pens.Green, 0, _
                                                         323, Cli1.Width, 0)
						Me.GameOn = False

					ElseIf I < CorrectAnswer Then

						Cli1.WriteLine("I am thinking of a larger number.")

					ElseIf I > CorrectAnswer Then

						Cli1.WriteLine("I am thinking of a smaller number.")

					End If

				End If
			Catch ex As Exception
				Cli1.WriteLine("You must type a number between 1 and 1000!")
			End Try
		Else
			Console.WriteLine("Game over!")
		End If
	End Sub

	Private Sub AddGuessCountImage()
		Dim B As New System.Drawing.Bitmap(70, 70)
		Using F As New System.Drawing.Font("Times New Roman", 30, FontStyle.Regular)
			Dim GuessCountString As String = Me.GuessCount.ToString()
			Using G As Graphics = Graphics.FromImage(B)
				G.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
				G.FillEllipse(Brushes.Black, 0, 0, 69, 69)
				G.DrawEllipse(Pens.Red, 0, 0, 69, 69)
				Dim StringSize As SizeF = G.MeasureString(GuessCountString, F)
				Dim X As Single = 35 - (StringSize.Width / 2)
				Dim Y As Single = 35 - (StringSize.Height / 2)
				G.DrawString(GuessCountString, F, Brushes.White, X, Y)
			End Using
		End Using
		Cli1.GraphicalElements.AddPicture("", B, 400 + Me.Rnd.Next(200), 250)
	End Sub

End Class

Categories: Microsoft .NET

Tags: CLIControl

Leave a Reply

Your email address will not be published. Required fields are marked *



En kopp kaffe!

Bjud mig på en kopp kaffe (20:-) som tack för bra innehåll!

Bjud på en kopp kaffe!

Om...

Kontaktuppgifter, med mera, finns här.

Följ mig

Twitter Instagram
GitHub RSS

Public Service

Folkbildning om public service.

Hem   |   linktr.ee/hesselbom   |   winsoft.se   |   80tal.se   |   Filmtips