文章标题

来源:互联网 发布:吸引力法则 知乎 编辑:程序博客网 时间:2024/06/03 18:47

大三课程设计 vb

  • 了解基础
    • timer控件
      Me.Timer1.Enabled = True  ’启动控件
      Me.Timer1.Interval = 1000 ‘设定跳动频为1秒。1000=1秒
      Timer1_Tick ‘下写我需要触发的事件
      注意需要Withevents来修饰timer
    • Withevents修饰符
      Handles 子句要求一个在包含类型或它的某个基类型中定义的 WithEvents 变量
      Dim WithEvents a As Form1
      定义了一个名为 a 的 Form1 类型的变量,加上 WithEvents 修饰就是说这个 a 拥有了 Form1 这种类型的属性和方法。也可以写他发生的事件
    • keyPress事件
      Dim a As Char = Convert.ToChar(Keys.Enter)
      If e.KeyChar.Equals(a) Then
      Timer1.Enabled = Not Timer1.Enabled
      End If
      判断是否有回车,且控制timer控件 实时控制
      Private Sub Form1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
      If Asc(e.KeyChar) = 115 Then
      MsgBox(“dsadsad”)
      End If
      End Sub
      keydown: 当用户按下任意键时触发,而且按住不放的话,会重复触发此事件。
      keypress: 当用户按下字符键时触发,而且按住不放的话,会重复触发此事件。按下Esc键也会触发这个事件,Safari3.1之前的版本按下非字符键时也触发。
      keyup: 当用户释放键时触发。
Public Class Form1    Private Score As Integer = 0    Private Vx As Double = 5    Private Vy As Double = 5    Private MDx As Integer    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load        Me.BackColor = Color.FromArgb(204, 232, 207)    End Sub    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick        B.Left += Vx        B.Top += Vy        If B.Left < 0 Then            Vx = Math.Abs(Vx)        End If        If B.Right > Me.ClientSize.Width Then            Vx = -Math.Abs(Vx)        End If        If B.Top < 0 Then            Vy = Math.Abs(Vy)        End If        If B.Bottom > P.Top Then            Dim C As Integer = (B.Left + B.Right) / 2            If C >= P.Left AndAlso C <= P.Right Then                Dim F As Double = (CDbl(C) - CDbl(P.Left)) / CDbl(P.Width)                If Vx < 0 Then                    F = (1.0 - F)                End If                F = F + 0.5                Vx = CInt(Math.Round(Vx * F))                Vy = -Math.Abs(Vy)                Score = Score + 10                Me.Text = "分数:" & Score.ToString()            Else                If B.Bottom > Me.ClientSize.Height Then                    Timer1.Stop()                    MsgBox("Game Over!", , "提示")                    B.Left = 220                    B.Top = 200                    Score = 0                    Vx = 5                    Vy = 5                    Me.Text = "分数:" & Score.ToString()                End If            End If        End If    End Sub    Private Sub P_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles P.MouseDown        Timer1.Start()        Cursor.Current = Cursors.Hand        MDx = e.X    End Sub    Private Sub P_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles P.MouseMove        If e.Button = MouseButtons.Left Then            Dim x As Integer = P.Left + (e.X - MDx)            If x < 0 Then                x = 0            End If            If x > Me.ClientSize.Width - P.Width Then                x = Me.ClientSize.Width - P.Width            End If            P.Left = x        End If    End SubEnd Class
原创粉丝点击