|
-
Jan 10th, 2013, 08:55 PM
#1
[vb.net] Simon
This is the source code and full project(minus the .exe)
Features:
Lets you play the classic game Simon
Drawbacks:
None that I can think of.
Plans:
Notes:
I really want to add sound because when I'm playing it, I think of the sounds as I'm doing it!
Full Project:
Simon.zip
Source Code:
Code:
Option Strict On
Option Explicit On
Public Class Form1
#Region "Variables"
Private pnl_list As New List(Of Panel)
Private play_list As New List(Of Panel)
Private game_tmr As New Timer
Private blink_tmr As New Timer
Private clicked_pnl As Panel
Private r As New Random
Private counter As Integer = 0
#End Region
#Region "Control Events"
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
'Set the properties of me
With Me
.BackColor = Color.Black
.FormBorderStyle = Windows.Forms.FormBorderStyle.FixedToolWindow
.Size = New Size(300, 300)
.StartPosition = FormStartPosition.CenterScreen
.Text = "Simon"
End With
'Set the game timer & blink timer intervals
game_tmr.Interval = 650
blink_tmr.Interval = 275
'Declare new instances of 4 panels
Dim pnl_blue, pnl_green, pnl_red, pnl_yellow As New Panel
'Add the panels to the list
pnl_list.AddRange({pnl_blue, pnl_green, pnl_red, pnl_yellow})
'Set the properties of the panels
With pnl_blue
.BackColor = Color.Blue
.Size = New Size(CInt(Me.Width / 2 - 2), CInt(Me.Height / 2 - 2))
.Location = New Point(0, 0)
.Tag = Color.Blue
.Name = "PanelBlue"
End With
With pnl_green
.BackColor = Color.Green
.Size = New Size(CInt(Me.Width / 2 - 2), CInt(Me.Height / 2 - 2))
.Location = New Point(CInt(Me.Width / 2 - 2), 0)
.Tag = Color.Green
.Name = "PanelGreen"
End With
With pnl_red
.BackColor = Color.Red
.Size = New Size(CInt(Me.Width / 2 - 2), CInt(Me.Height / 2 - 2))
.Location = New Point(0, CInt(Me.Height / 2 - 2))
.Tag = Color.Red
.Name = "PanelRed"
End With
With pnl_yellow
.BackColor = Color.Yellow
.Size = New Size(CInt(Me.Width / 2 - 2), CInt(Me.Height / 2 - 2))
.Location = New Point(CInt(Me.Width / 2 - 2), CInt(Me.Height / 2 - 2))
.Tag = Color.Yellow
.Name = "PanelYellow"
End With
'Add the panels to me
Me.Controls.AddRange(pnl_list.ToArray)
'Generate the click event for the 4 panels
For Each pnl As Panel In pnl_list
AddHandler pnl.Click, AddressOf Panel_Click
Next
'Generate the tick event for the two timers
AddHandler game_tmr.Tick, AddressOf GameTimer_Tick
AddHandler blink_tmr.Tick, AddressOf BlinkTimer_Tick
'Start a new game
Call AddItem()
game_tmr.Enabled = True
End Sub
Private Sub Panel_Click(sender As Object, e As EventArgs)
'If the timer isn't enabled then enable it
If blink_tmr.Enabled = False Then
'Set the clicked panel
clicked_pnl = DirectCast(sender, Panel)
'Change it's color
Select Case clicked_pnl.BackColor
Case Color.Blue
clicked_pnl.BackColor = Color.DarkBlue
Case Color.Green
clicked_pnl.BackColor = Color.DarkGreen
Case Color.Red
clicked_pnl.BackColor = Color.DarkRed
Case Color.Yellow
clicked_pnl.BackColor = Color.Gold
End Select
'Start the blink
blink_tmr.Enabled = True
'Check if the clicked panel is a match
If CheckMatch(clicked_pnl) Then
counter += 1
Else
counter = 0
MessageBox.Show("You guessed wrong. When you click OK a new game will start.", Me.Text, MessageBoxButtons.OK)
play_list.Clear()
Call AddItem()
game_tmr.Enabled = True
End If
'Check to see if we've guessed all the panels
If counter = play_list.Count Then
counter = 0
Call AddItem()
game_tmr.Enabled = True
End If
End If
End Sub
#End Region
#Region "Timers"
Private Sub GameTimer_Tick(sender As Object, e As EventArgs)
'Static i, or declare it as Private i outside of the sub
Static i As Integer = 0
'If i is less than the play_list count...
If i < play_list.Count Then
'Don't allow the panel's the be enabled
For Each pnl As Panel In pnl_list
pnl.Enabled = False
Next
'The panel to be clicked will be the current item in the list
clicked_pnl = play_list.Item(i)
'Change it's color
Select Case clicked_pnl.BackColor
Case Color.Blue
clicked_pnl.BackColor = Color.DarkBlue
Case Color.Green
clicked_pnl.BackColor = Color.DarkGreen
Case Color.Red
clicked_pnl.BackColor = Color.DarkRed
Case Color.Yellow
clicked_pnl.BackColor = Color.Gold
End Select
'Blink!
blink_tmr.Enabled = True
'Increment i by 1
i += 1
Else
'If i is equal to the playlist count...
'Set i back to 0
i = 0
'Stop the game timer
game_tmr.Enabled = False
'Set the panel's back to enabled
For Each pnl As Panel In pnl_list
pnl.Enabled = True
Next
End If
End Sub
Private Sub BlinkTimer_Tick(sender As Object, e As EventArgs)
'Stop the timer
blink_tmr.Enabled = False
'Set the panel's color back to it's original color
Select Case DirectCast(clicked_pnl.Tag, Color)
Case Color.Blue
clicked_pnl.BackColor = Color.Blue
Case Color.Green
clicked_pnl.BackColor = Color.Green
Case Color.Red
clicked_pnl.BackColor = Color.Red
Case Color.Yellow
clicked_pnl.BackColor = Color.Yellow
End Select
End Sub
#End Region
#Region "Private Subs/Functions"
Private Sub AddItem()
'Random int from 0 - 3
Dim i As Integer = r.Next(0, 4)
'Add the panel to the playlist
play_list.Add(pnl_list.Item(i))
End Sub
Private Function CheckMatch(ByVal pnl As Panel) As Boolean
If play_list.Item(counter).Name = pnl.Name Then
Return True
Else
Return False
End If
End Function
#End Region
End Class
Last edited by dday9; May 29th, 2013 at 09:40 AM.
-
May 29th, 2013, 09:43 AM
#2
Re: [vb.net] Simon
Update!
I updated the source code quite a bit. The major change was I switched out the background workers to timers. The way I got how the panel's blink is from JMcIlhinney's thread here. Also, you don't need to add any controls to your form, you can just copy and paste the source code and it should compile. The only thing I'd look for if you do that is to make sure Visual Studio's doesn't remove the Form_Load event handler. Also, another thing that I've made sure to do is to properly comment my code.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|