真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

vb.net貪吃蛇,vb寫貪吃蛇

vb.net編寫的貪吃蛇游戲,帶代碼的

需用VB實(shí)現(xiàn),代碼如下

成都創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供古縣網(wǎng)站建設(shè)、古縣做網(wǎng)站、古縣網(wǎng)站設(shè)計(jì)、古縣網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、古縣企業(yè)網(wǎng)站模板建站服務(wù),10多年古縣做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。

'定義蛇的運(yùn)動(dòng)速度枚舉值

Private Enum tpsSpeed

QUICKLY = 0

SLOWLY = 1

End Enum

'定義蛇的運(yùn)動(dòng)方向枚舉值

Private Enum tpsDirection

D_UP = 38

D_DOWN = 40

D_LEFT = 37

D_RIGHT = 39

End Enum

'定義運(yùn)動(dòng)區(qū)域4個(gè)禁區(qū)的枚舉值

Private Enum tpsForbiddenZone

FZ_TOP = 30

FZ_BOTTOM = 5330

FZ_LEFT = 30

FZ_RIGHT = 5730

End Enum

'定義蛇頭及身體初始化數(shù)枚舉值

Private Enum tpsSnake

SNAKEONE = 1

SNAKETWO = 2

SNAKETHREE = 3

SNAKEFOUR = 4

End Enum

'定義蛇寬度的常量

Private Const SNAKEWIDTH As Integer = 100

'該過(guò)程用于顯示游戲信息

Private Sub Form_Load()

Me.Show

Me.lblTitle = "BS貪食蛇 — (版本 " App.Major "." App.Minor "." App.Revision ")"

Me.Caption = Me.lblTitle.Caption

frmSplash.Show 1

End Sub

'該過(guò)程用于使窗體恢復(fù)原始大小

Private Sub Form_Resize()

If Me.WindowState 1 Then

Me.Caption = ""

Me.Height = 6405 '窗體高度為 6405 緹

Me.Width = 8535 '窗體寬度為 8535 緹

Me.Left = (Screen.Width - Width) \ 2

Me.Top = (Screen.Height - Height) \ 2

End If

End Sub

'該過(guò)程用于重新開始開始游戲

Private Sub cmdGameStart_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)

Beep

msg = MsgBox("您確認(rèn)要重新開始游戲嗎?", 4 + 32, "BS貪食蛇")

If msg = 6 Then Call m_subGameInitialize

End Sub

'該過(guò)程用于暫停/運(yùn)行游戲

Private Sub chkPause_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)

If Me.chkPause.Caption = "暫停游戲(P)" Then

Me.tmrSnakeMove.Enabled = False

Me.tmrGameTime.Enabled = False

Me.picMoveArea.Enabled = False

Me.lblPauseLab.Visible = True

Me.chkPause.Caption = "繼續(xù)游戲(R)"

Else

Me.tmrSnakeMove.Enabled = True

Me.tmrGameTime.Enabled = True

Me.picMoveArea.Enabled = True

Me.lblPauseLab.Visible = False

Me.chkPause.Caption = "暫停游戲(P)"

End If

End Sub

'該過(guò)程用于顯示游戲規(guī)則

Private Sub cmdGameRules_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)

Beep

MsgBox " BS貪食蛇:一個(gè)規(guī)則最簡(jiǎn)單的趣味游戲,您將用鍵盤" Chr(13) _

"上的4個(gè)方向鍵來(lái)控制蛇的運(yùn)動(dòng)方向。在運(yùn)動(dòng)過(guò)程中蛇" Chr(13) _

"不能后退,蛇的頭部也不能接觸到運(yùn)動(dòng)區(qū)域的邊線以外" Chr(13) _

"和蛇自己的身體,否則就游戲失敗。在吃掉隨機(jī)出現(xiàn)的" Chr(13) _

"果子后,蛇的身體會(huì)變長(zhǎng),越長(zhǎng)難度越大。祝您好運(yùn)??!", 0 + 64, "游戲規(guī)則"

End Sub

'該過(guò)程用于顯示游戲開發(fā)信息

Private Sub cmdAbout_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)

Beep

MsgBox "BS貪食蛇" "(V-" App.Major "." App.Minor "版本)" Chr(13) Chr(13) _

"" Chr(13) Chr(13) _

"由PigheadPrince設(shè)計(jì)制作" Chr(13) _

"CopyRight(C)2002,BestSoft.TCG", 0, "關(guān)于本游戲"

End Sub

'該過(guò)程用于退出游戲

Private Sub cmdExit_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)

Beep

msg = MsgBox("您要退出本游戲嗎?", 4 + 32, "BS貪食蛇")

Select Case msg

Case 6

End

Case 7

Me.chkWindowButton(2).Value = 0

Exit Sub

End Select

End Sub

'該過(guò)程用于拖動(dòng)窗體_(點(diǎn)擊圖標(biāo))

Private Sub imgWindowTop_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)

ReleaseCapture

SendMessage Me.hwnd, WM_SYSCOMMAND, SC_MOVE, 0

End Sub

'該共用過(guò)程用于處理窗體控制按鈕組的相關(guān)操作_(鎖定、最小化、退出)

Private Sub chkWindowButton_MouseUp(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)

If Button 1 Then Exit Sub

Select Case Index

Case 0 '鎖定窗體

If Me.chkWindowButton(0).Value = 1 Then

Me.imgWindowTop.BorderStyle = 0

Me.imgWindowTop.Enabled = False

Else

Me.imgWindowTop.BorderStyle = 1

Me.imgWindowTop.Enabled = True

End If

Case 1 '最小化

Me.WindowState = 1

Me.chkWindowButton(1).Value = 0

Me.Caption = "BS貪食蛇 — (V-" App.Major "." App.Minor "版本)"

Case 2 '退出

Beep

msg = MsgBox("您要退出本游戲嗎?", 4 + 32, "BS貪食蛇")

Select Case msg

Case 6

End

Case 7

Me.chkWindowButton(2).Value = 0

Exit Sub

End Select

End Select

End Sub

'該過(guò)程用于設(shè)置蛇運(yùn)動(dòng)速度的快慢

Private Sub hsbGameSpeed_Change()

Me.tmrSnakeMove.Interval = Me.hsbGameSpeed.Value

End Sub

'該過(guò)程用于通過(guò)鍵盤的方向鍵改變蛇的運(yùn)動(dòng)方向

Private Sub picMoveArea_KeyDown(KeyCode As Integer, Shift As Integer)

Select Case g_intDirection

Case D_UP

If KeyCode = D_DOWN Then Exit Sub

Case D_DOWN

If KeyCode = D_UP Then Exit Sub

Case D_LEFT

If KeyCode = D_RIGHT Then Exit Sub

Case D_RIGHT

If KeyCode = D_LEFT Then Exit Sub

End Select

g_intDirection = KeyCode

End Sub

'該計(jì)時(shí)循環(huán)過(guò)程用于計(jì)算游戲耗費(fèi)的秒數(shù)并顯示

Private Sub tmrGameTime_Timer()

g_lngGameTime = g_lngGameTime + 1

Me.lblGameTime.Caption = g_lngGameTime "秒"

End Sub

'該計(jì)時(shí)循環(huán)過(guò)程用于控制蛇的行動(dòng)軌跡

Private Sub tmrSnakeMove_Timer()

Dim lngSnakeX As Long, lngSnakeY As Long, lngSnakeColor As Long

Dim lngPointX As Long, lngPointY As Long, lngPointColor As Long

Randomize

Me.picMoveArea.SetFocus

Me.picMoveArea.Cls

'確認(rèn)蛇頭的運(yùn)動(dòng)方向并獲取新的位置

Select Case g_intDirection

Case D_UP '向上運(yùn)動(dòng)

g_udtSnake(SNAKEONE).Snake_CurX = g_udtSnake(SNAKEONE).Snake_OldX

g_udtSnake(SNAKEONE).Snake_CurY = g_udtSnake(SNAKEONE).Snake_OldY

g_udtSnake(SNAKEONE).Snake_CurY = g_udtSnake(SNAKEONE).Snake_CurY - SNAKEWIDTH

Case D_DOWN '向下運(yùn)動(dòng)

g_udtSnake(SNAKEONE).Snake_CurX = g_udtSnake(SNAKEONE).Snake_OldX

g_udtSnake(SNAKEONE).Snake_CurY = g_udtSnake(SNAKEONE).Snake_OldY

g_udtSnake(SNAKEONE).Snake_CurY = g_udtSnake(SNAKEONE).Snake_CurY + SNAKEWIDTH

Case D_LEFT '向左運(yùn)動(dòng)

g_udtSnake(SNAKEONE).Snake_CurX = g_udtSnake(SNAKEONE).Snake_OldX

g_udtSnake(SNAKEONE).Snake_CurX = g_udtSnake(SNAKEONE).Snake_CurX - SNAKEWIDTH

g_udtSnake(SNAKEONE).Snake_CurY = g_udtSnake(SNAKEONE).Snake_OldY

Case D_RIGHT '向右運(yùn)動(dòng)

g_udtSnake(SNAKEONE).Snake_CurX = g_udtSnake(SNAKEONE).Snake_OldX

g_udtSnake(SNAKEONE).Snake_CurX = g_udtSnake(SNAKEONE).Snake_CurX + SNAKEWIDTH

g_udtSnake(SNAKEONE).Snake_CurY = g_udtSnake(SNAKEONE).Snake_OldY

End Select

'根據(jù)新的位置繪制蛇頭

lngSnakeX = g_udtSnake(SNAKEONE).Snake_CurX

lngSnakeY = g_udtSnake(SNAKEONE).Snake_CurY

lngSnakeColor = g_udtSnake(SNAKEONE).Snake_Color

Me.picMoveArea.PSet (lngSnakeX, lngSnakeY), lngSnakeColor

'移動(dòng)蛇身體其他部分的位置

For i = 2 To g_intSnakeLength

g_udtSnake(i).Snake_CurX = g_udtSnake(i - 1).Snake_OldX

g_udtSnake(i).Snake_CurY = g_udtSnake(i - 1).Snake_OldY

lngSnakeX = g_udtSnake(i).Snake_CurX

lngSnakeY = g_udtSnake(i).Snake_CurY

lngSnakeColor = g_udtSnake(i).Snake_Color

Me.picMoveArea.PSet (lngSnakeX, lngSnakeY), lngSnakeColor

Next i

'更新蛇舊的坐標(biāo)位置

For j = 1 To g_intSnakeLength

g_udtSnake(j).Snake_OldX = g_udtSnake(j).Snake_CurX

g_udtSnake(j).Snake_OldY = g_udtSnake(j).Snake_CurY

Next j

'判斷蛇在移動(dòng)中是否到了禁區(qū)而導(dǎo)致游戲失敗

If m_funMoveForbiddenZone(g_udtSnake(SNAKEONE).Snake_CurX, g_udtSnake(SNAKEONE).Snake_CurY) Then

Beep

MsgBox "您的蛇移動(dòng)到了禁區(qū),游戲失??!", 0 + 16, "BS貪食蛇"

Me.tmrSnakeMove.Enabled = False

Me.tmrGameTime.Enabled = False

Me.picMoveArea.Visible = False

Exit Sub

End If

'判斷蛇在移動(dòng)中是否碰到了自己的身體而導(dǎo)致游戲失敗

If m_funTouchSnakeBody(g_udtSnake(SNAKEONE).Snake_CurX, g_udtSnake(SNAKEONE).Snake_CurY) Then

Beep

MsgBox "您的蛇在移動(dòng)中碰到了自己的身體,游戲失??!", 0 + 16, "BS貪食蛇"

Me.tmrSnakeMove.Enabled = False

Me.tmrGameTime.Enabled = False

Me.picMoveArea.Visible = False

Exit Sub

End If

'判斷蛇是否吃到了果子

If m_funEatPoint(g_udtSnake(SNAKEONE).Snake_CurX, g_udtSnake(SNAKEONE).Snake_CurY) Then

'累加玩家的得分并刷新得分顯示

g_intPlayerScore = g_intPlayerScore + 1

Me.lblYourScore.Caption = g_intPlayerScore "分"

Call m_subAddSnake '加長(zhǎng)蛇的身體

Call m_subGetPoint '獲取下一個(gè)果子的位置和顏色

Else

'繪制果子

lngPointX = g_udtPoint.Point_X

lngPointY = g_udtPoint.Point_Y

lngPointColor = g_udtPoint.Point_Color

Me.picMoveArea.PSet (lngPointX, lngPointY), lngPointColor

End If

End Sub

'該私有子過(guò)程用于初始化游戲

Private Sub m_subGameInitialize()

Erase g_udtSnake '清空蛇的結(jié)構(gòu)數(shù)組

g_intPlayerScore = 0 '清空玩家的得分

g_lngGameTime = 0 '清空游戲耗費(fèi)的秒數(shù)

g_intDirection = D_DOWN '設(shè)定蛇的初始運(yùn)動(dòng)方向?yàn)橄?/p>

g_intSnakeLength = 4 '設(shè)定蛇的初始長(zhǎng)度

ReDim g_udtSnake(1 To g_intSnakeLength) '重新定義蛇的長(zhǎng)度

'定義蛇頭部的數(shù)據(jù)

With g_udtSnake(SNAKEONE)

.Snake_OldX = 530

.Snake_OldY = 530

.Snake_Color = vbBlack

End With

'定義蛇身第2節(jié)的數(shù)據(jù)

With g_udtSnake(SNAKETWO)

.Snake_OldX = 530

.Snake_OldY = 430

.Snake_Color = vbGreen

End With

'定義蛇身第3節(jié)的數(shù)據(jù)

With g_udtSnake(SNAKETHREE)

.Snake_OldX = 530

.Snake_OldY = 330

.Snake_Color = vbYellow

End With

'定義蛇身第4節(jié)的數(shù)據(jù)

With g_udtSnake(SNAKEFOUR)

.Snake_OldX = 530

.Snake_OldY = 230

.Snake_Color = vbRed

End With

Me.picMoveArea.Visible = True

Me.lblYourScore.Caption = g_intPlayerScore "分"

Me.lblGameTime.Caption = g_lngGameTime "秒"

Me.tmrSnakeMove.Interval = Me.hsbGameSpeed.Value

Me.tmrSnakeMove.Enabled = True

Me.tmrGameTime.Enabled = True

Call m_subGetPoint '獲取第一個(gè)果子的位置和顏色

End Sub

用VB編寫貪吃蛇時(shí)可能遇到的難點(diǎn)和重點(diǎn)問題有什么?

如果只是簡(jiǎn)單的貪吃蛇的話,把蛇的基本圖形設(shè)置成圓(或正方形),蛇身由一串基本圖形構(gòu)成,每過(guò)一秒,檢測(cè)前面是什么,如果是空的,那么把頭畫成普通身體的樣子,再在前方(或側(cè)面,如果轉(zhuǎn)彎的話)再畫一個(gè)頭,并將其坐標(biāo)記入數(shù)組,數(shù)組要足夠大,再把尾巴畫成背景色;如果前面是食物,尾巴就不刪除.這里的難點(diǎn)是數(shù)組里的操作和坐標(biāo)的表示:把頭添在最后一個(gè)元素,把第一個(gè)元素提取(也就是尾巴).

怎樣用vb編寫貪吃蛇游戲

1、向上前進(jìn)的時(shí)候,對(duì)代碼進(jìn)行一個(gè)詳解。

2、向上前進(jìn)時(shí),x坐標(biāo)不動(dòng),y坐標(biāo)-1,如果下一個(gè)有食物 下一個(gè)位置的坐標(biāo)和食物的坐標(biāo)相同。把食物轉(zhuǎn)化成蛇的身體。

3、如果蛇吃到了食物,就開始加速,并且食物的得分+2。

4、如果沒有吃到食物,蛇可以正常往前走,恢復(fù)原來(lái)的方塊。

5、向下前進(jìn)時(shí),x坐標(biāo)不動(dòng),y坐標(biāo)+1。

6、如果有食物就把食物轉(zhuǎn)化為身體。

7、如果沒有吃到食物,蛇可以正常往前走,恢復(fù)原來(lái)的方塊。

VB 做一個(gè)非常簡(jiǎn)單的貪吃蛇程序

朋友給段代碼給你,這個(gè)代碼不要任何控件,你只需要把以下代碼復(fù)制到你的窗體代碼中就行了,代碼很簡(jiǎn)單,相信你能看懂!僅僅用了一個(gè)結(jié)構(gòu)(ShenTi)一個(gè)數(shù)組(ZhuangTai(23, 23) As Long) 一個(gè)變量(GFangXiang)

代碼中自動(dòng)生成了Timer1,Label1控件。以及幾個(gè)函數(shù)(Randomize,Circle,Line,Erase...) 相信這些對(duì)于你來(lái)說(shuō)很簡(jiǎn)單,只是沒想到用這些方法來(lái)實(shí)現(xiàn)而已,現(xiàn)在你開始看看效果吧!

代碼如下:

'貪吃蛇代碼(無(wú)控件、全代碼)

Private WithEvents Timer1 As Timer

Private WithEvents Label1 As Label

Dim GFangXiang As Boolean

Dim HWB As Single

Dim She() As ShenTi

Dim X As Long, Y As Long

Dim ZhuangTai(23, 23) As Long

Private Type ShenTi

F As Long

X As Long

Y As Long

End Type

'按鍵反應(yīng) ←↑↓→

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

Dim C As Long

If KeyCode = 27 Then End 'ESC退出

If KeyCode = 32 Then

If Timer1.Enabled = True Then '空格暫停

Timer1.Enabled = False

Label1.Visible = True

Else '空格開始

Timer1.Enabled = True

Label1.Visible = False

End If

End If

C = UBound(She)

If GFangXiang = True Then Exit Sub

Select Case KeyCode

Case 37 '←

If She(C).F = 2 Then Exit Sub

She(C).F = 0

GFangXiang = True

Case 38 '↑

If She(C).F = 3 Then Exit Sub

She(C).F = 1

GFangXiang = True

Case 39 '↑

If She(C).F = 0 Then Exit Sub

She(C).F = 2

GFangXiang = True

Case 40 '→

If She(C).F = 1 Then Exit Sub

She(C).F = 3

GFangXiang = True

End Select

End Sub

Private Sub Form_Load()

Me.AutoRedraw = True

Me.BackColor = HC000

Me.FillColor = 255

Me.FillStyle = 0

Me.WindowState = 2

Set Timer1 = Controls.Add("VB.Timer", "Timer1")

Set Label1 = Controls.Add("VB.Label", "Label1")

Label1.AutoSize = True

Label1.BackStyle = 0

Label1 = "暫停"

Label1.ForeColor = RGB(255, 255, 0)

Label1.FontSize = 50

ChuShiHua '初始化

End Sub

Private Sub Form_Resize()

On Error GoTo 1:

With Me

If .WindowState 1 Then

.Cls

.ScaleMode = 3

HWB = .ScaleHeight / .ScaleWidth

.ScaleWidth = 24

.ScaleHeight = 24

Label1.Move (Me.ScaleWidth - Label1.Width) / 2, (Me.ScaleHeight - Label1.Height) / 2

HuaTu

Me.Line (X, Y)-(X + 1, Y + 1), RGB(255, 255, 0), BF

End If

End With

1:

End Sub

Private Sub Timer1_Timer()

Dim C As Long, I As Long

On Error GoTo 2:

QingChu '清圖

C = UBound(She)

Select Case She(C).F

Case 0

If ZhuangTai(She(C).X - 1, She(C).Y) = 2 Then

C = C + 1

ReDim Preserve She(C)

She(C).F = She(C - 1).F

She(C).X = She(C - 1).X - 1

She(C).Y = She(C - 1).Y

ChanShengShiWu

GoTo 1:

ElseIf ZhuangTai(She(C).X - 1, She(C).Y) = 1 Then

GoTo 2:

End If

Case 1

If ZhuangTai(She(C).X, She(C).Y - 1) = 2 Then

C = C + 1

ReDim Preserve She(C)

She(C).F = She(C - 1).F

She(C).X = She(C - 1).X

She(C).Y = She(C - 1).Y - 1

ChanShengShiWu

GoTo 1:

ElseIf ZhuangTai(She(C).X, She(C).Y - 1) = 1 Then

GoTo 2:

End If

Case 2

If ZhuangTai(She(C).X + 1, She(C).Y) = 2 Then

C = C + 1

ReDim Preserve She(C)

She(C).F = She(C - 1).F

She(C).X = She(C - 1).X + 1

She(C).Y = She(C - 1).Y

ChanShengShiWu

GoTo 1:

ElseIf ZhuangTai(She(C).X + 1, She(C).Y) = 1 Then

GoTo 2:

End If

Case 3

If ZhuangTai(She(C).X, She(C).Y + 1) = 2 Then

C = C + 1

ReDim Preserve She(C)

She(C).F = She(C - 1).F

She(C).X = She(C - 1).X

She(C).Y = She(C - 1).Y + 1

ChanShengShiWu

GoTo 1:

ElseIf ZhuangTai(She(C).X, She(C).Y + 1) = 1 Then

GoTo 2:

End If

End Select

ZhuangTai(She(0).X, She(0).Y) = 0

For I = 0 To C

Select Case She(I).F

Case 0

She(I).X = She(I).X - 1

Case 1

She(I).Y = She(I).Y - 1

Case 2

She(I).X = She(I).X + 1

Case 3

She(I).Y = She(I).Y + 1

End Select

Next

TiaoZheng

1:

GFangXiang = False

ZhuangTai(She(C).X, She(C).Y) = 1

HuaTu

Exit Sub

2: '游戲結(jié)束

If MsgBox("得分:" UBound(She) - 2 "分 " vbCrLf "游戲結(jié)束,點(diǎn)“是”重新開始游戲,點(diǎn)“否”", vbYesNo, "貪吃蛇") = vbYes Then

ChuShiHua

Else

End

End If

End Sub

'初始化

Private Sub ChuShiHua()

Me.Cls

Timer1.Enabled = True

Timer1.Interval = 50

Erase ZhuangTai

ReDim She(2)

She(0).F = 2

She(0).X = 9

She(0).Y = 11

ZhuangTai(9, 11) = 1

She(1).F = 2

She(1).X = 10

She(1).Y = 11

ZhuangTai(10, 11) = 1

She(2).F = 2

She(2).X = 11

She(2).Y = 11

ZhuangTai(11, 11) = 1

HuaTu '畫圖

ChanShengShiWu

End Sub

'清圖

Private Sub QingChu()

Dim I As Long

For I = 0 To UBound(She)

Me.Line (She(I).X, She(I).Y)-(She(I).X + 1, She(I).Y + 1), Me.BackColor, BF

Next

End Sub

'畫圖 蛇

Private Sub HuaTu()

Dim I As Long

For I = 0 To UBound(She)

Me.Circle (She(I).X + 0.5, She(I).Y + 0.5), 0.49, RGB(255, 255, 0), , , HWB

Next

End Sub

Private Sub TiaoZheng()

Dim I As Long

For I = 0 To UBound(She) - 1

She(I).F = She(I + 1).F

Next

End Sub

'隨機(jī)產(chǎn)生食物

Private Sub ChanShengShiWu()

Randomize Timer

1:

X = Int(Rnd * 24)

Y = Int(Rnd * 24)

If ZhuangTai(X, Y) 0 Then GoTo 1:

ZhuangTai(X, Y) = 2

Me.Line (X, Y)-(X + 1, Y + 1), RGB(255, 255, 0), BF

End Sub


本文名稱:vb.net貪吃蛇,vb寫貪吃蛇
當(dāng)前網(wǎng)址:http://weahome.cn/article/phjogd.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部