|
-
Feb 12th, 2008, 07:47 PM
#1
Thread Starter
New Member
[RESOLVED] Control Backround Images
HI, as a noob & as an assignment I have written a Noughts and crosses
program. It all works fine. I was trying to jazz it up by letting random noughts, crosses and empty squares fill the grid before the game starts.
This code works fine but only for a few timer clicks...and for some reason I always end up with a row of crosses across the middle !
the Cstr(tempPos) lines are just for debugging and they show the code and timer are still working...but the images dont change ??? HELP !
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
tempPos = Int(Rnd() * 9) + 3
If tempPos < 6 Then
Controls(tempPos).BackgroundImage = NoughtsAndCrosses.My.Resources.Resources.nought
lblInfo.BackColor = Color.Red
lblInfo.Text = " <<<<< PLAY NOW >>>>> "
lblInfo.Text = CStr(tempPos)
End If
If tempPos > 5 And tempPos < 9 Then
Controls(tempPos).BackgroundImage = NoughtsAndCrosses.My.Resources.Resources.cross
lblInfo.BackColor = Color.Blue
lblInfo.Text = " <<<<< PLAY NOW >>>>> "
lblInfo.Text = CStr(tempPos)
Else
Controls(tempPos).BackgroundImage = NoughtsAndCrosses.My.Resources.Resources.blank
lblInfo.BackColor = Color.Green
lblInfo.Text = " <<<<< PLAY NOW >>>>> "
lblInfo.Text = CStr(tempPos)
End If
End Sub
Many Thanks
-
Feb 12th, 2008, 09:35 PM
#2
Re: Control Backround Images
Do not use the Randomize and Rnd functions to generate random numbers. Use a Random object. You should create a 2D array for your controls and then you can generate a random X and Y value, e.g.
vb.net Code:
Private noughtImage As Image = My.Resources.Nought Private crossImage As Image = My.Resources.Cross Private blankImage As Image = Nothing Private images As Image() = {Me.noughtImage, _ Me.crossImage, _ Me.blankImage} Private squares As PictureBox(,) Private numberGenerator As New Random Private Sub Form1_Load(ByVal sender As Object, _ ByVal e As EventArgs) Handles MyBase.Load Me.squares = New PictureBox(,) {{Me.PictureBox1, Me.PictureBox2, Me.PictureBox3}, _ {Me.PictureBox4, Me.PictureBox5, Me.PictureBox6}, _ {Me.PictureBox7, Me.PictureBox8, Me.PictureBox8}} End Sub Private Sub Timer1_Tick(ByVal sender As Object, _ ByVal e As EventArgs) Handles Timer1.Tick Dim imageIndex As Integer = Me.numberGenerator.Next(0, Me.images.Length) Dim rowIndex As Integer = Me.numberGenerator.Next(0, Me.squares.GetUpperBound(0)) Dim columnIndex As Integer = Me.numberGenerator.Next(0, Me.squares.GetUpperBound(1)) Me.squares(rowIndex, columnIndex).Image = Me.images(imageIndex) End Sub
There are a few things to note here. Firstly, I create one Random object and call its Next method each time I want a random number.
Second, I access the Images from My.Resources once and once only. Otherwise you're creating new Image objects over and over again, which is very inefficient.
Thirdly, I don't actually use an Image object for blank squares. By using a null reference, i.e. Nothing, I am displaying no Image, which means a blank square. Maybe this isn't appropriate in your case but it very likely is.
Fourthly, I create an array for the Images, so I can reference them by index, and I also create a 2D array of PictureBoxes, so I can refer to them by row and column index.
Finally, each time the Timer Ticks I select a random Image and place it in the square in a random row and a random column. Clean and tidy.
-
Feb 12th, 2008, 10:08 PM
#3
Thread Starter
New Member
Re: Control Backround Images
Thanks...this is far superior.... but, out of interest, do you know why mine only worked for a short while ??
-
Feb 12th, 2008, 10:16 PM
#4
Re: [RESOLVED] Control Backround Images
No idea. If you want to find out then you should debug it. You can't always work out what's happening just by reading code. Just like a car, you have to run it and see it in action to be able to diagnose a lot of problems.
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
|