
TazG
Veteran
/ Moderator

Feb 6, 2007, 4:54 AM
Post #4 of 13
(388 views)
Shortcut
|
|
Re: [Athlon XP] Generating random words or sentences.
[In reply to]
|
Can't Post
|
|
*stumbles into forum* *downloads code* *opens Form1.vb in mspaint* *stares at it* Holy crap. Man, learn to use arrays (for both objects and variables). Look it up, it's easy. You end up with numbered objects like Label(1), Label(2) instead of numbers hardcoded into their names. The good thing about that is you can replace those numbers with variables, and cut down on the size of your code immensely by turning those repetitive parts into loops. Like, first I'd go like this: (total being the total number of label-thingies you're using) Then you can change this crap
Label1.Top = 20 Label2.Top = 20 Label3.Top = 20 Label4.Top = 20 Label5.Top = 20 to this:
For tazg=1 to total Label(tazg).Top = 20 Next Likewise, I'd condense that monstrous timer sub like this (new stuff in red):
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick danger=false for rofl=1 to total Label(rofl).Top = Label(rofl).Top + 10 If Label(rofl).Top > 160 Then 'Lose ten points Label(rofl).Top = 20 Score = Score - 10 'Display the updated score lblScore.Text = "SCORE: " & Score End If next for lmao=1 to total If Label(lmao).Top > 120 Then 'Display a danger message when a label starts to get too low lblTitle.Text = "Danger!" danger=true end if next if danger=false then 'Change the title back to normal when not in danger lblTitle.Text = "Keep those boxes up there" end if If Score = -50 Then 'End and reset the game if you have -50 points lblTitle.Text = "game Over!" btnStart.Text = "Start" Timer1.Enabled = False For tazg=1 to total Label(tazg).Top = 20 Next End If End Sub For the next part, I'd start by taking out your Const lines in the beginning and replacing it with this beauty:
Const wordz=3 Dim randomword(1 to total, 1 to wordz) As String randomword(1,1)="broom" randomword(1,2)="system" randomword(1,3)="machine" randomword(2,1)="message" randomword(2,2)="cutter" randomword(2,3)="property" randomword(3,1)="inside" randomword(3,2)="emulate" randomword(3,3)="heatsink" randomword(4,1)="plate" randomword(4,2)="sausage" randomword(4,3)="cookie" randomword(5,1)="shoes" randomword(5,2)="hands" randomword(5,3)="fingers" See, randomword is a wonderful 2-dimensional array now. It has 5 variables (total=5), representing the 5 boxes, and each of those contains 3 possible words (wordz=3). It's simply awe-inspiring. Then you can shorten the last part with this marvelous loop:
Private Sub txtInput_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtInput.TextChanged 'Type the words exactly as they appear to send them back up for lol=1 to total If txtInput.Text <> "" Then Randomize() intRandom = Int(wordz * Rnd()) + 1 If txtInput.Text = Label(lol).Text Then Me.Label(lol).Text = randomword(lol, intRandom) txtInput.Text = "" Label(lol).Top = 20 Score = Score + 1 lblScore.Text = "SCORE: " & Score End If next End Sub So try that. I can't test it but I'm pretty sure it works fine. And it shortens your code by like 70% or something. Plus, if you want to add more boxes or words, all you have to do is add to the array and edit the "total" and "wordz" constants accordingly. In fact I would get into the habit of making every reused number a constant at the beginning of the code, so you can make widespread changes easier (such as "Const startingpoint=20" for resetting the boxes).
MY BLOG,
(This post was edited by TazG on Feb 6, 2007, 4:57 AM)
|