需用VB實(shí)現(xiàn),代碼如下
發(fā)展壯大離不開(kāi)廣大客戶長(zhǎng)期以來(lái)的信賴與支持,我們將始終秉承“誠(chéng)信為本、服務(wù)至上”的服務(wù)理念,堅(jiān)持“二合一”的優(yōu)良服務(wù)模式,真誠(chéng)服務(wù)每家企業(yè),認(rèn)真做好每個(gè)細(xì)節(jié),不斷完善自我,成就企業(yè),實(shí)現(xiàn)共贏。行業(yè)涉及塔吊租賃等,在網(wǎng)站建設(shè)、全網(wǎng)營(yíng)銷推廣、WAP手機(jī)網(wǎng)站、VI設(shè)計(jì)、軟件開(kāi)發(fā)等項(xiàng)目上具有豐富的設(shè)計(jì)經(jīng)驗(yàn)。
'定義蛇的運(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ò)程用于重新開(kāi)始開(kāi)始游戲
Private Sub cmdGameStart_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Beep
msg = MsgBox("您確認(rèn)要重新開(kāi)始游戲嗎?", 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ò)程用于顯示游戲開(kāi)發(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
g?=?Me.picDisplay.CreateGraphics
Dim?mybrush?As?Brush?=?New?SolidBrush(Map_Empty_Color)
Dim?rect2?As?System.Drawing.Rectangle?=?New?System.Drawing.Rectangle(0,?0,?Map_Width,?Map_Height)
g.FillRectangle(mybrush,?rect2)
我用多線程寫(xiě)的,你可以參考下。
系統(tǒng)界面圖如下:(注:重置功能為清除已經(jīng)中獎(jiǎng)號(hào)碼,回歸1-100都可以中獎(jiǎng)狀態(tài)。)
代碼:
public?partial?class?Form1?:?Form
{
public?Form1()
{
InitializeComponent();
}
System.Threading.Thread?g_Thread?=?null;
Listint?g_list?=?new?Listint();
int?g_Num?=?0;
delegate?void?g_delegate();
///?summary
///?開(kāi)始
///?/summary
///?param?name="sender"/param
///?param?name="e"/param
private?void?button2_Click(object?sender,?EventArgs?e)
{
if(g_list.Count==100)
{
MessageBox.Show("所有數(shù)字已使用,請(qǐng)重置!");
return;
}
button3.Enabled?=?true;
Random?r?=?new?Random();
g_delegate?dele=?ChangeTxt;
g_Thread?=?new?System.Threading.Thread(()?=
{
while(this.IsDisposed==false)
{
g_Num?=?r.Next(1,?100);
if(g_list.Contains(g_Num))
{
g_Num?=??1;
}
while(g_list.Contains(g_Num))
{
g_Num++;
}
this.Invoke(dele);
System.Threading.Thread.Sleep(50);
}
});
g_Thread.Start();
}
void?ChangeTxt()
{
label2.Text?=?g_Num.ToString();
}
///?summary
///?停止
///?/summary
///?param?name="sender"/param
///?param?name="e"/param
private?void?button3_Click(object?sender,?EventArgs?e)
{
//if(g_Thread.ThreadState==System.Threading.ThreadState.Running)
{
g_Thread.Abort();
g_list.Add(Convert.ToInt32(label2.Text));
}
button3.Enabled?=?false;
}
///?summary
///?重置
///?/summary
///?param?name="sender"/param
///?param?name="e"/param
private?void?button1_Click(object?sender,?EventArgs?e)
{
g_list.Clear();
}
private?void?Form1_FormClosed(object?sender,?FormClosedEventArgs?e)
{
System.Environment.Exit(0);
}
}
網(wǎng)站標(biāo)題:vb.netg代碼 vbnet using
網(wǎng)頁(yè)網(wǎng)址:http://m.newbst.com/article12/doihddc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、、靜態(tài)網(wǎng)站、定制開(kāi)發(fā)、網(wǎng)站排名、網(wǎng)站設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)