Nettrix.vb
上传用户:c_chunwei
上传日期:2022-05-07
资源大小:80k
文件大小:11k
源码类别:

其他智力游戏

开发平台:

Visual Basic

  1. Imports System
  2. Imports System.Drawing
  3. Imports System.Collections
  4. Imports System.ComponentModel
  5. Imports System.Windows.Forms
  6. Namespace Nettrix
  7.     _
  8.     '/ <Summary>
  9.     '/ Summary Description For .
  10.     '/ </Summary>
  11.     Public Class NetTrix
  12.         Inherits System.Windows.Forms.Form
  13.         Friend PicBackground As System.Windows.Forms.PictureBox
  14.         Friend LblScore As System.Windows.Forms.Label
  15.         Friend WithEvents CmdStart As System.Windows.Forms.Button
  16.         Friend PicNextBlock As System.Windows.Forms.PictureBox
  17.         Friend LblNextBlock As System.Windows.Forms.Label
  18.         Friend LblScoreValue As System.Windows.Forms.Label
  19.         Friend WithEvents TmrGameClock As System.Windows.Forms.Timer
  20.         Private Components As System.ComponentModel.IContainer
  21.         Private StillProcessing As Boolean = False
  22.         Private Score As Integer = 0
  23.         Private CurrentBlock As Block
  24.         Private NextBlock As Block
  25.         Public Sub New()
  26.             '
  27.             ' Required For Windows Form Designer Support
  28.             '
  29.             InitializeComponent()
  30.         End Sub 'New
  31.         '/ <Summary>
  32.         '/ Clean Up Any Resources Being Used.
  33.         '/ </Summary>
  34.         Protected Overloads Sub Dispose(ByVal Disposing As Boolean)
  35.             If Disposing Then
  36.                 If Not (Components Is Nothing) Then
  37.                     Components.Dispose()
  38.                 End If
  39.             End If
  40.             MyBase.Dispose(Disposing)
  41.         End Sub 'Dispose
  42.         '/ Required Method For Designer Support - Do Not Modify
  43.         '/ The Contents Of This Method With The Code Editor.
  44.         '/ </Summary>
  45.         Private Sub InitializeComponent()
  46.             Me.Components = New System.ComponentModel.Container
  47.             Me.PicBackground = New System.Windows.Forms.PictureBox
  48.             Me.LblScore = New System.Windows.Forms.Label
  49.             Me.CmdStart = New System.Windows.Forms.Button
  50.             Me.PicNextBlock = New System.Windows.Forms.PictureBox
  51.             Me.LblNextBlock = New System.Windows.Forms.Label
  52.             Me.LblScoreValue = New System.Windows.Forms.Label
  53.             Me.TmrGameClock = New System.Windows.Forms.Timer(Me.Components)
  54.             Me.SuspendLayout()
  55.             '
  56.             'PicBackground
  57.             '
  58.             Me.PicBackground.BackColor = System.Drawing.SystemColors.ControlDark
  59.             Me.PicBackground.Location = New System.Drawing.Point(0, 0)
  60.             Me.PicBackground.Name = "PicBackground"
  61.             Me.PicBackground.Size = New System.Drawing.Size(149, 321)
  62.             Me.PicBackground.TabIndex = 0
  63.             Me.PicBackground.TabStop = False
  64.             '
  65.             'LblScore
  66.             '
  67.             Me.LblScore.Location = New System.Drawing.Point(161, 5)
  68.             Me.LblScore.Name = "LblScore"
  69.             Me.LblScore.Size = New System.Drawing.Size(76, 18)
  70.             Me.LblScore.TabIndex = 3
  71.             Me.LblScore.Text = "Score:"
  72.             '
  73.             'CmdStart
  74.             '
  75.             Me.CmdStart.Location = New System.Drawing.Point(160, 144)
  76.             Me.CmdStart.Name = "CmdStart"
  77.             Me.CmdStart.Size = New System.Drawing.Size(68, 25)
  78.             Me.CmdStart.TabIndex = 9
  79.             Me.CmdStart.Text = "Start!"
  80.             '
  81.             'PicNextBlock
  82.             '
  83.             Me.PicNextBlock.BackColor = System.Drawing.Color.DarkGray
  84.             Me.PicNextBlock.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
  85.             Me.PicNextBlock.Location = New System.Drawing.Point(160, 72)
  86.             Me.PicNextBlock.Name = "PicNextBlock"
  87.             Me.PicNextBlock.Size = New System.Drawing.Size(75, 64)
  88.             Me.PicNextBlock.TabIndex = 6
  89.             Me.PicNextBlock.TabStop = False
  90.             '
  91.             'LblNextBlock
  92.             '
  93.             Me.LblNextBlock.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
  94.             Me.LblNextBlock.Location = New System.Drawing.Point(160, 48)
  95.             Me.LblNextBlock.Name = "LblNextBlock"
  96.             Me.LblNextBlock.Size = New System.Drawing.Size(76, 16)
  97.             Me.LblNextBlock.TabIndex = 7
  98.             Me.LblNextBlock.Text = "Next Block:"
  99.             '
  100.             'LblScoreValue
  101.             '
  102.             Me.LblScoreValue.Location = New System.Drawing.Point(160, 31)
  103.             Me.LblScoreValue.Name = "LblScoreValue"
  104.             Me.LblScoreValue.Size = New System.Drawing.Size(76, 18)
  105.             Me.LblScoreValue.TabIndex = 8
  106.             Me.LblScoreValue.Text = "0"
  107.             Me.LblScoreValue.TextAlign = System.Drawing.ContentAlignment.TopCenter
  108.             '
  109.             'TmrGameClock
  110.             '
  111.             Me.TmrGameClock.Interval = 330
  112.             '
  113.             'NetTrix
  114.             '
  115.             Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
  116.             Me.ClientSize = New System.Drawing.Size(246, 318)
  117.             Me.Controls.Add(Me.CmdStart)
  118.             Me.Controls.Add(Me.PicNextBlock)
  119.             Me.Controls.Add(Me.LblNextBlock)
  120.             Me.Controls.Add(Me.LblScoreValue)
  121.             Me.Controls.Add(Me.LblScore)
  122.             Me.Controls.Add(Me.PicBackground)
  123.             Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow
  124.             Me.Name = "NetTrix"
  125.             Me.Text = ".NETTrix"
  126.             Me.ResumeLayout(False)
  127.         End Sub 'InitializeComponent
  128.         '/ <Summary>
  129.         '/ The Main Entry Point For The Application.
  130.         '/ </Summary>
  131.         <STAThread()> Shared _
  132.         Sub Main()
  133.             Application.Run(New Nettrix)
  134.         End Sub 'Main
  135.         Private Sub TmrGameClock_Tick(ByVal Sender As System.Object, ByVal E As System.EventArgs) Handles TmrGameClock.Tick
  136.             Dim ErasedLines As Integer
  137.             If StillProcessing Then
  138.                 Return
  139.             End If
  140.             StillProcessing = True
  141.             'Manage The Falling Block
  142.             If Not CurrentBlock.Down() Then
  143.                 If CurrentBlock.Top() = 0 Then
  144.                     TmrGameClock.Enabled = False
  145.                     CmdStart.Enabled = True
  146.                     MessageBox.Show("GAME OVER", ".NETTrix", MessageBoxButtons.OK, MessageBoxIcon.Stop)
  147.                     StillProcessing = False
  148.                     Return
  149.                 End If
  150.                 'Increase Score Based On # Of Deleted Lines
  151.                 ErasedLines = GameField.CheckLines()
  152.                 If ErasedLines > 0 Then
  153.                     Score += 100 * ErasedLines
  154.                     LblScoreValue.Text = Score.ToString()
  155.                     'Clear The Game Field And Force The Window To Re-Paint
  156.                     PicBackground.Invalidate()
  157.                     Application.DoEvents()
  158.                     GameField.Redraw()
  159.                 End If
  160.                 'Replace The Current Block...
  161.                 CurrentBlock = New Block(New Point(GameField.SquareSize * 6, 0), NextBlock.BlockType)
  162.                 CurrentBlock.Show(PicBackground.Handle)
  163.                 'Create The Next Block
  164.                 NextBlock.Hide(PicNextBlock.Handle)
  165.                 NextBlock = New Block(New Point(20, 10), Block.BlockTypes.Undefined)
  166.                 NextBlock.Show(PicNextBlock.Handle)
  167.             End If
  168.             StillProcessing = False
  169.         End Sub 'TmrGameClock_Tick
  170.         Private Sub CmdStart_Click(ByVal Sender As Object, ByVal E As System.EventArgs) Handles CmdStart.Click
  171.             TmrGameClock.Enabled = True
  172.             CmdStart.Enabled = False
  173.             LblScoreValue.Text = "0"
  174.             ' Clean The Game Field
  175.             PicBackground.Invalidate()
  176.             Application.DoEvents()
  177.             GameField.Reset()
  178.             GameField.Redraw()
  179.             ' Create And Show The Current And Next Blocks
  180.             CurrentBlock = New Block(New Point(GameField.SquareSize * 6, 50), Block.BlockTypes.Undefined)
  181.             CurrentBlock.Show(PicBackground.Handle)
  182.             NextBlock = New Block(New Point(20, 10), Block.BlockTypes.Undefined)
  183.             NextBlock.Show(PicNextBlock.Handle)
  184.         End Sub 'CmdStart_Click
  185.         Private Sub NetTrix_KeyDown(ByVal Sender As Object, ByVal E As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
  186.             Select Case E.KeyCode
  187.                 Case Keys.Right
  188.                     CurrentBlock.Right()
  189.                 Case Keys.Left
  190.                     CurrentBlock.Left()
  191.                 Case Keys.Up
  192.                     CurrentBlock.Rotate()
  193.                 Case Keys.Down
  194.                     CurrentBlock.Down()
  195.                 Case Keys.Escape
  196.                     TmrGameClock.Enabled = Not TmrGameClock.Enabled
  197.                     If TmrGameClock.Enabled Then
  198.                         Me.Text = ".NETTrix"
  199.                     Else
  200.                         Me.Text = ".NETTrix -- Press 'Esc' To Continue"
  201.                     End If
  202.                 Case Else
  203.             End Select
  204.             Invalidate()
  205.         End Sub 'NetTrix_KeyDown
  206.         Private Sub NetTrix_Load(ByVal Sender As Object, ByVal E As System.EventArgs) Handles MyBase.Load
  207.             GameField.BackColor = PicBackground.BackColor
  208.             GameField.WinHandle = PicBackground.Handle
  209.             'Adjusts The Size Of The Form And Position Of Controls Based On The Class Constants
  210.             'On The Window Height, Sums The Size Of The Window Title Bar
  211.             Height = GameField.Height * GameField.SquareSize + (Height - ClientSize.Height) + 3 '3=Border Width
  212.             Width = GameField.Width * GameField.SquareSize + 120
  213.             PicBackground.Height = GameField.Height * GameField.SquareSize + 4
  214.             PicBackground.Width = GameField.Width * GameField.SquareSize + 4
  215.             PicNextBlock.Left = GameField.Width * GameField.SquareSize + 12
  216.             LblNextBlock.Left = GameField.Width * GameField.SquareSize + 12
  217.             LblScore.Left = GameField.Width * GameField.SquareSize + 12
  218.             LblScoreValue.Left = GameField.Width * GameField.SquareSize + 12
  219.             CmdStart.Left = GameField.Width * GameField.SquareSize + 12
  220.             '
  221.             'Block - Creates The First Block And The First NEXT Block
  222.             '
  223.             GameField.WinHandle = PicBackground.Handle
  224.             GameField.BackColor = PicBackground.BackColor
  225.         End Sub 'NetTrix_Load
  226.         Private Sub NetTrix_Activated(ByVal Sender As Object, ByVal E As System.EventArgs) Handles MyBase.Activated
  227.             '        This Event Occurs When The Window Receives Back The Focus After Losing It To Another Window
  228.             '        So, We Redraw The Whole Game Field
  229.             '        Clear The Game Field
  230.             PicBackground.Invalidate()
  231.             Application.DoEvents()
  232.             GameField.Redraw()
  233.             If Not (NextBlock Is Nothing) Then
  234.                 NextBlock.Show(PicNextBlock.Handle)
  235.             End If
  236.         End Sub 'NetTrix_Activated
  237.     End Class 'NetTrix
  238. End Namespace 'Nettrix