打字游戏

bravo 发表于 2007-11-02 21:32:15

有人想学打字,于是闲来写了个打字游戏.家里的机器只有VB6,和.NET有些不同~

新建工程,在主窗口中添加以下控件:
7个Command控件,分别命名为start,restart,pause,slow,fast,easy,hard,exit
其中除了pause的Caption属性请设置为"暂停游戏",其余请自行设置,
3条Line控件,分别命名为r,l,b,其中r竖在右侧,l竖在左侧,b横在下方,组成一个倒写的π
1组拥有15个元素的Label控件数组,分别命名为Label1(0),Label1(1)......Label1(14)
2个Timer控件,无需重命名,随后添加一些Label控件用于显示游戏状态及游戏成绩

//  关于VB6以及VS2003以后版本的控件及其属性变动说明
//  VS2003以后版本Command控件改为Button控件,Caption属性改为Text属性
//  VS2003以后版本没有Line控件和控件数组,且Timer控件的属性也有所不同

以下为全部代码
'其中Label8表示游戏字母个数
'Label6表示打对的字母个数
'Label7表示没有打的字母个数
'Label9表示字母下降的速度
'Label11表示打错的字母个数

Dim speed
Dim time
Dim number
Dim n
Dim miss
Dim done
Dim mistake
Dim game

Private Sub easy_Click()
    number = number - 1
    n = n - 1
    If n < 1 Then n = 1
    Label8.Caption = number
    If number = 2 Then easy.Enabled = False
    If hard.Enabled = False Then hard.Enabled = True
    For i = Label1.LBound To Label1.UBound
        If Label1(i).Visible = True Then Exit For
        If i = Label1.UBound Then Exit Sub
    Next
    Label1(i).Visible = False
End Sub

Private Sub exit_Click()
    End
End Sub

Private Sub fast_Click()
    speed = speed - 1
    Label9.Caption = 11 - speed
    If speed = 1 Then fast.Enabled = False
    If slow.Enabled = False Then slow.Enabled = True
    Timer1.Interval = speed * 150
End Sub

Private Sub Form_KeyPress(KeyAscii As Integer)
    If game = 1 Then
        For i = Label1.LBound To Label1.UBound
            If Label1(i).Caption = Chr$(KeyAscii - 32) Then
                finish (i)
                done = done + 1
                Label6.Caption = done
                Exit Sub
            End If
        Next
        mistake = mistake + 1
        Label11.Caption = mistake
    End If
End Sub

Private Sub Form_Load()
    n = 1
    number = 8
    speed = 7
    Timer1.Interval = 0
    done = 0
    miss = 0
    game = 0
    mistake = 0
   
    For i = Label1.LBound To Label1.UBound
        Label1(i).Visible = False
    Next
    Label6.Caption = done
    Label7.Caption = miss
    Label8.Caption = number
    Label9.Caption = 11 - speed
    Label11.Caption = mistake
   
    pause.Enabled = False
    restart.Enabled = False
End Sub

Private Sub hard_Click()
    number = number + 1
    Label8.Caption = number
    Timer2.Interval = 1500
    If number = 15 Then hard.Enabled = False
    If easy.Enabled = False Then easy.Enabled = True
End Sub

Private Sub pause_Click()
    If pause.Caption = "继续游戏" Then
        pause.Caption = "暂停游戏"
        Timer1.Interval = speed * 150
        Timer2.Interval = 1100
        start.Enabled = True
        game = 1
    Else
        pause.Caption = "继续游戏"
        Timer1.Interval = 0
        Timer2.Interval = 0
        start.Enabled = False
        game = 0
    End If
End Sub

Private Sub restart_Click()
    n = 0
    done = 0
    miss = 0
    number = 8
    speed = 7
    mistake = 0
    game = 1
   
    For i = Label1.LBound To Label1.UBound
        Label1(i).Visible = False
    Next
    Label6.Caption = done
    Label7.Caption = miss
    Label8.Caption = number
    Label9.Caption = 11 - speed
    Label11.Caption = mistake
   
    fast.Enabled = True
    slow.Enabled = True
    easy.Enabled = True
    hard.Enabled = True
   
    Timer1.Interval = speed * 150
    Timer2.Interval = 1100
End Sub

Private Sub slow_Click()
    speed = speed + 1
    Label9.Caption = 11 - speed
    If speed = 10 Then slow.Enabled = False
    If fast.Enabled = False Then fast.Enabled = True
    Timer1.Interval = speed * 150
End Sub

Private Sub start_Click()
    game = 1
    Timer1.Interval = speed * 150
    Timer2.Interval = 1100
    pause.Enabled = True
    restart.Enabled = True
End Sub

Private Sub Timer1_Timer()
    For i = Label1.LBound To Label1.UBound
        Label1(i).Top = Label1(i).Top + 200
        If (Label1(i).Top >= b.Y1 - Label1(i).Height) And (Label1(i).Visible = True) Then
            Beep
            miss = miss + 1
            Label7.Caption = miss
            finish (i)
        End If
    Next
End Sub

Private Sub Timer2_Timer()
continue:
    If n >= number Then Timer2.Interval = 0
   
    i = Int(Rnd() * 15)
    If Label1(i).Visible = True Then
         GoTo continue
    End If
   
    finish (i)
    Label1(i).Visible = True
    n = n + 1
End Sub

Private Function letter()
    Randomize
    i = Int(Rnd() * 26) + 1
    letter = Chr$(64 + i)
End Function

Public Function finish(i%)
        Label1(i).Top = 0
        Label1(i).Caption = letter()
        Randomize
        Label1(i).Left = Int(Rnd() * (r.X1 - l.X1 - Label1(i).Width - 100) + l.X1 + 50)
End Function

(以上全部)


注明,代码环境 VB6

最新评论

发表评论

*昵称

已经注册过? 请登录

Email
网址
*评论