⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frmmain.vb

📁 VB.net贪吃蛇
💻 VB
📖 第 1 页 / 共 2 页
字号:
Option Strict On
Option Explicit On 

' *******************  SnakeDotNet  *******************
'       File:       frmMain.vb
'       Version:    1.0
'       Author:     Tim Overbay
'       Date:       June 27, 2002
'
' This is a VB.NET version of the classic Snake or
' Nibbles game. I wrote it for fun and because my
' son hogs my cell phone playing Snake all the time.
' 
' This implementation is as simple as possible.
' There's plenty of room for improvements/enhancements.
' Here are some suggested enhancements you could try
' as programming exercises:
'       - Improved graphics. Make the snake actually
'         look like a snake.
'
'       - Add sounds and animations
'
'       - More game objects like bugs to eat for extra
'         points, power ups for the snake, or tunnels/warp points.
'
'       - Add walls/obstacles and the ability for the player
'         to draw and save their own layouts.
'
'       - Two-player where you and a friend can try to
'         box each other in.
'
'       - Add an option for a computer-controlled snake as
'         an exercise in AI programming.
'
'       - Extend two-player mode by adding the ability to play
'         over a network.
'
'       - Make it isometric or 3D!!!!
'
'       - Morph it into a completely different game. The old
'         Tron light cycles come to mind.
'
'       - Convert it into a web-based game.
'
'
' This program is provided free of charge and can
' be used or abused any way you like. All I ask is
' that you acknowledge that I'm the original author
' if you decide to use it in your own projects or
' distribute it to others.
'
' Please contact me at luhar@slipnet.net if you find
' this program useful, you found bugs, or you have
' any suggestions for improvement.
'
' Enjoy...

Imports SlipStream.Games.SnakeDotNet

' Main form for the game
Public Class frmMain
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Timer1 As System.Windows.Forms.Timer
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container()
        Me.PictureBox1 = New System.Windows.Forms.PictureBox()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.Timer1 = New System.Windows.Forms.Timer(Me.components)
        Me.SuspendLayout()
        '
        'PictureBox1
        '
        Me.PictureBox1.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right)
        Me.PictureBox1.BackColor = System.Drawing.Color.FromArgb(CType(0, Byte), CType(64, Byte), CType(0, Byte))
        Me.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
        Me.PictureBox1.Location = New System.Drawing.Point(8, 8)
        Me.PictureBox1.Name = "PictureBox1"
        Me.PictureBox1.Size = New System.Drawing.Size(368, 304)
        Me.PictureBox1.TabIndex = 5
        Me.PictureBox1.TabStop = False
        '
        'Label1
        '
        Me.Label1.Anchor = (System.Windows.Forms.AnchorStyles.Left Or System.Windows.Forms.AnchorStyles.Right)
        Me.Label1.BackColor = System.Drawing.Color.FromArgb(CType(0, Byte), CType(64, Byte), CType(0, Byte))
        Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label1.ForeColor = System.Drawing.Color.Yellow
        Me.Label1.Location = New System.Drawing.Point(88, 128)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(200, 56)
        Me.Label1.TabIndex = 6
        Me.Label1.Text = "Press Enter to Start"
        Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
        '
        'Timer1
        '
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(384, 317)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label1, Me.PictureBox1})
        Me.KeyPreview = True
        Me.Name = "Form1"
        Me.Text = "SnakeDotNet"
        Me.ResumeLayout(False)

    End Sub

#End Region

    ' This determines how many segments are added
    ' to the snake when it grows
    Private Const GROWTH_FACTOR As Integer = 3
    ' The width of each segment, in pixels
    Private Const SNAKE_WIDTH As Integer = 8

    ' This is our snake object
    Private m_snake As Snake
    ' This is used to control the snake's movement
    Private m_control As SnakeControl
    ' Boolean that determines whether the game is running
    Private m_running As Boolean = False
    ' Boolean that determines whether the snake is growing
    Private m_growing As Boolean = False
    ' Rectangle used to store the size & location of the food
    Private m_foodrec As Rectangle
    ' Keeps track of the player's score
    Private m_score As Integer

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Set up the game environment
        InitializeGame()
    End Sub

    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
        ' Make sure the game is running
        If Not m_running Then
            e.Graphics.Clear(PictureBox1.BackColor)
            Exit Sub
        End If

        ' Draw the food
        e.Graphics.FillEllipse(Brushes.Cornsilk, m_foodrec)

        ' temporary segment variable used to
        ' loop through the snake's segments
        Dim thisseg As SnakeSegment

        ' Loop through the snake's segments and draw 'em
        For Each thisseg In m_snake.Segments
            ' Each segment is a simple block of color.
            ' There is definitely room for improved graphics here
            e.Graphics.FillRectangle(Brushes.LimeGreen, thisseg.Rectangle)
        Next
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        ' Update the state of the game
        UpdateGame()

        ' Redraw the playing field (This can be optimized with dirty rectangles
        ' and OnDrawBackgroundImage. Hint: you really only need to draw the new
        ' head, neck, and tial of the snake.)
        PictureBox1.Invalidate()
    End Sub

    ' The sad, yet inevitable subroutine that efficiently
    ' dispatches our snake. Change this to show a really

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -