需用VB實現(xiàn),代碼如下
創(chuàng)新互聯(lián)建站長期為上1000+客戶提供的網(wǎng)站建設(shè)服務,團隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務;打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為臨猗企業(yè)提供專業(yè)的做網(wǎng)站、網(wǎng)站建設(shè),臨猗網(wǎng)站改版等技術(shù)服務。擁有十余年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。
'定義蛇的運動速度枚舉值
Private Enum tpsSpeed
QUICKLY = 0
SLOWLY = 1
End Enum
'定義蛇的運動方向枚舉值
Private Enum tpsDirection
D_UP = 38
D_DOWN = 40
D_LEFT = 37
D_RIGHT = 39
End Enum
'定義運動區(qū)域4個禁區(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
'該過程用于顯示游戲信息
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
'該過程用于使窗體恢復原始大小
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
'該過程用于重新開始開始游戲
Private Sub cmdGameStart_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Beep
msg = MsgBox("您確認要重新開始游戲嗎?", 4 + 32, "BS貪食蛇")
If msg = 6 Then Call m_subGameInitialize
End Sub
'該過程用于暫停/運行游戲
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ī)則
Private Sub cmdGameRules_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Beep
MsgBox " BS貪食蛇:一個規(guī)則最簡單的趣味游戲,您將用鍵盤" Chr(13) _
"上的4個方向鍵來控制蛇的運動方向。在運動過程中蛇" Chr(13) _
"不能后退,蛇的頭部也不能接觸到運動區(qū)域的邊線以外" Chr(13) _
"和蛇自己的身體,否則就游戲失敗。在吃掉隨機出現(xiàn)的" Chr(13) _
"果子后,蛇的身體會變長,越長難度越大。祝您好運!!", 0 + 64, "游戲規(guī)則"
End Sub
'該過程用于顯示游戲開發(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è)計制作" Chr(13) _
"CopyRight(C)2002,BestSoft.TCG", 0, "關(guān)于本游戲"
End Sub
'該過程用于退出游戲
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
'該過程用于拖動窗體_(點擊圖標)
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ā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
'該過程用于設(shè)置蛇運動速度的快慢
Private Sub hsbGameSpeed_Change()
Me.tmrSnakeMove.Interval = Me.hsbGameSpeed.Value
End Sub
'該過程用于通過鍵盤的方向鍵改變蛇的運動方向
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
'該計時循環(huán)過程用于計算游戲耗費的秒數(shù)并顯示
Private Sub tmrGameTime_Timer()
g_lngGameTime = g_lngGameTime + 1
Me.lblGameTime.Caption = g_lngGameTime "秒"
End Sub
'該計時循環(huán)過程用于控制蛇的行動軌跡
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
'確認蛇頭的運動方向并獲取新的位置
Select Case g_intDirection
Case D_UP '向上運動
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 '向下運動
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 '向左運動
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 '向右運動
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
'移動蛇身體其他部分的位置
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
'更新蛇舊的坐標位置
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
'判斷蛇在移動中是否到了禁區(qū)而導致游戲失敗
If m_funMoveForbiddenZone(g_udtSnake(SNAKEONE).Snake_CurX, g_udtSnake(SNAKEONE).Snake_CurY) Then
Beep
MsgBox "您的蛇移動到了禁區(qū),游戲失??!", 0 + 16, "BS貪食蛇"
Me.tmrSnakeMove.Enabled = False
Me.tmrGameTime.Enabled = False
Me.picMoveArea.Visible = False
Exit Sub
End If
'判斷蛇在移動中是否碰到了自己的身體而導致游戲失敗
If m_funTouchSnakeBody(g_udtSnake(SNAKEONE).Snake_CurX, g_udtSnake(SNAKEONE).Snake_CurY) Then
Beep
MsgBox "您的蛇在移動中碰到了自己的身體,游戲失敗!", 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 '加長蛇的身體
Call m_subGetPoint '獲取下一個果子的位置和顏色
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
'該私有子過程用于初始化游戲
Private Sub m_subGameInitialize()
Erase g_udtSnake '清空蛇的結(jié)構(gòu)數(shù)組
g_intPlayerScore = 0 '清空玩家的得分
g_lngGameTime = 0 '清空游戲耗費的秒數(shù)
g_intDirection = D_DOWN '設(shè)定蛇的初始運動方向為下
g_intSnakeLength = 4 '設(shè)定蛇的初始長度
ReDim g_udtSnake(1 To g_intSnakeLength) '重新定義蛇的長度
'定義蛇頭部的數(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 '獲取第一個果子的位置和顏色
End Sub
1、向上前進的時候,對代碼進行一個詳解。
2、向上前進時,x坐標不動,y坐標-1,如果下一個有食物 下一個位置的坐標和食物的坐標相同。把食物轉(zhuǎn)化成蛇的身體。
3、如果蛇吃到了食物,就開始加速,并且食物的得分+2。
4、如果沒有吃到食物,蛇可以正常往前走,恢復原來的方塊。
5、向下前進時,x坐標不動,y坐標+1。
6、如果有食物就把食物轉(zhuǎn)化為身體。
7、如果沒有吃到食物,蛇可以正常往前走,恢復原來的方塊。
朋友給段代碼給你,這個代碼不要任何控件,你只需要把以下代碼復制到你的窗體代碼中就行了,代碼很簡單,相信你能看懂!僅僅用了一個結(jié)構(gòu)(ShenTi)一個數(shù)組(ZhuangTai(23, 23) As Long) 一個變量(GFangXiang)
代碼中自動生成了Timer1,Label1控件。以及幾個函數(shù)(Randomize,Circle,Line,Erase...) 相信這些對于你來說很簡單,只是沒想到用這些方法來實現(xiàn)而已,現(xiàn)在你開始看看效果吧!
代碼如下:
'貪吃蛇代碼(無控件、全代碼)
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
'按鍵反應 ←↑↓→
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é)束,點“是”重新開始游戲,點“否”", 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
'隨機產(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
#include graphics.h
#include stdlib.h
#include conio.h
#include time.h
#include stdio.h
#define LEFT 'a'
#define RIGHT 'd'
#define DOWN 's'
#define UP 'w'
#define ESC 27
#define N 200 /*蛇的最大長度*/
int i;
char key;
int score=0; /*得分*/
int gamespeed=100; /*游戲速度自己調(diào)整*/
struct Food
{
int x; /*食物的橫坐標*/
int y; /*食物的縱坐標*/
int yes; /*判斷是否要出現(xiàn)食物的變量*/
}food; /*食物的結(jié)構(gòu)體*/
struct Snake
{
int x[N];
int y[N];
int node; /*蛇的節(jié)數(shù)*/
int direction; /*蛇移動方向*/
int life; /* 蛇的生命,0活著,1死亡*/
}snake;
void Init(void); /*圖形驅(qū)動*/
void Close(void); /*圖形結(jié)束*/
void DrawK(void); /*開始畫面*/
void GameOver(void); /*結(jié)束游戲*/
void GamePlay(void); /*玩游戲具體過程*/
void PrScore(void); /*輸出成績*/
/*主函數(shù)*/
void main(void)
{
Init(); /*圖形驅(qū)動*/
DrawK(); /*開始畫面*/
GamePlay(); /*玩游戲具體過程*/
Close(); /*圖形結(jié)束*/
}
/*圖形驅(qū)動*/
void Init(void)
{
int gd=9,gm=2;
initgraph(gd,gm," ");
cleardevice();
}
/*開始畫面,左上角坐標為(50,40),右下角坐標為(610,460)的圍墻*/
void DrawK(void)
{
/*setbkcolor(LIGHTGREEN);*/
setcolor(LIGHTCYAN);
setlinestyle(PS_SOLID,0,1); /*設(shè)置線型*/
for(i=50;i=600;i+=10) /*畫圍墻*/
{
rectangle(i,40,i+10,49); /*上邊*/
rectangle(i,451,i+10,460); /*下邊*/
}
for(i=40;i=450;i+=10)
{
rectangle(50,i,59,i+10); /*左邊*/
rectangle(601,i,610,i+10); /*右邊*/
}
}
/*玩游戲具體過程*/
void GamePlay(void)
{
srand(time(NULL)); /*隨機數(shù)發(fā)生器*/
food.yes=1; /*1表示需要出現(xiàn)新食物,0表*/
snake.life=0; /*活著*/
snake.direction=1; /*方向往右*/
snake.x[0]=100;snake.y[0]=100; /*蛇頭*/
snake.x[1]=110;snake.y[1]=100;
snake.node=2; /*節(jié)數(shù)*/
PrScore(); /*輸出得分*/
while(1) /*可以重復玩游戲,壓ESC鍵*/
{
while(!kbhit()) /*在沒有按鍵的情況下,蛇自*/
{
if(food.yes==1) /*需要出現(xiàn)新食物*/
{
food.x=rand()%400+60;
food.y=rand()%350+60;
while(food.x%10!=0) /*食物隨機出現(xiàn)后必須讓食物*/
food.x++;
while(food.y%10!=0)
food.y++;
food.yes=0; /*畫面上有食物了*/
}
if(food.yes==0) /*畫面上有食物了就要顯示*/
{
setcolor(GREEN);
rectangle(food.x,food.y,food.x+10,food.y-10);
}
for(i=snake.node-1;i0;i--) /*蛇的每個環(huán)節(jié)往前移動,也法/
{
snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1];
}
/*1,2,3,4表示右,左,上,下四個方向,通過這個判斷來移動蛇頭*/
switch(snake.direction)
{
case 1: snake.x[0]+=10;break;
case 2: snake.x[0]-=10;break;
case 3: snake.y[0]-=10;break;
case 4: snake.y[0]+=10;break;
}
/*從蛇的第四節(jié)開始判斷是否撞到自己了,因為蛇頭為兩節(jié),第三節(jié)不可*/
for(i=3;isnake.node;i++)
{
if(snake.x[i]==snake.x[0]snake.y[i]==snake.y[0])
{
GameOver(); /*顯示失敗*/
snake.life=1;
break;
}
}
if(snake.x[0]55||snake.x[0]595||snake.y[0]55||snake.y[0]455) /*蛇是否撞到墻壁*/
{ GameOver(); /*本次游戲結(jié)束*/
snake.life=1; /*蛇死*/
}
if(snake.life==1) /*以上兩種判斷以后,如果蛇*/
break;
if(snake.x[0]==food.xsnake.y[0]==food.y)/*吃到食物以后*/
{
setcolor(BLACK); /*把畫面上的食物東西去*/
rectangle(food.x,food.y,food.x+10,food.y-10);
snake.x[snake.node]=-20;snake.y[snake.node]=-20;
/*新的一節(jié)先放在看不見的位置,下次循環(huán)就取前一節(jié)的位置*/
snake.node++; /*蛇的身體長一節(jié)*/
food.yes=1; /*畫面上需要出現(xiàn)新的食物*/
score+=10;
PrScore(); /*輸出新得分*/
}
setcolor(RED); /*畫出蛇*/
for(i=0;isnake.node;i++)
rectangle(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]-10);
Sleep(gamespeed);
setcolor(BLACK); /*用黑色去除蛇的的最后*/
rectangle(snake.x[snake.node-1],snake.y[snake.node-1],
snake.x[snake.node-1]+10,snake.y[snake.node-1]-10);
} /*endwhile(!kbhit)*/
if(snake.life==1) /*如果蛇死就跳出循環(huán)*/
break;
key=getch(); /*接收按鍵*/
if (key == ESC) break; /*按ESC鍵退出*/
switch(key)
{
case UP:
if(snake.direction!=4) /*判斷是否往相反的方向移動*/
snake.direction=3;
break;
case RIGHT:
if(snake.direction!=2)
snake.direction=1;
break;
case LEFT:
if(snake.direction!=1)
snake.direction=2;
break;
case DOWN:
if(snake.direction!=3)
snake.direction=4;
break;
}
}/*endwhile(1)*/
}
/*游戲結(jié)束*/
void GameOver(void)
{
cleardevice();
PrScore();
setcolor(RED);
setfont(56,0,"黑體");
outtextxy(200,200,"GAME OVER");
getch();
}
/*輸出成績*/
void PrScore(void)
{
char str[10];
setfillstyle(YELLOW);
bar(50,15,220,35);
setcolor(BROWN);
setfont(16,0,"宋體");
sprintf(str,"score:%d",score);
outtextxy(55,16,str);
}
/*圖形結(jié)束*/
void Close(void)
{
closegraph();
}
'定義蛇的運動速度枚舉值
Private Enum tpsSpeed
QUICKLY = 0
SLOWLY = 1
End Enum
'定義蛇的運動方向枚舉值
Private Enum tpsDirection
D_UP = 38
D_DOWN = 40
D_LEFT = 37
D_RIGHT = 39
End Enum
'定義運動區(qū)域4個禁區(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
'該過程用于顯示游戲信息
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
'該過程用于使窗體恢復原始大小
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
'該過程用于重新開始開始游戲
Private Sub cmdGameStart_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Beep
msg = MsgBox("您確認要重新開始游戲嗎?", 4 + 32, "BS貪食蛇")
If msg = 6 Then Call m_subGameInitialize
End Sub
'該過程用于暫停/運行游戲
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ī)則
Private Sub cmdGameRules_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Beep
MsgBox " BS貪食蛇:一個規(guī)則最簡單的趣味游戲,您將用鍵盤" Chr(13) _
"上的4個方向鍵來控制蛇的運動方向。在運動過程中蛇" Chr(13) _
"不能后退,蛇的頭部也不能接觸到運動區(qū)域的邊線以外" Chr(13) _
"和蛇自己的身體,否則就游戲失敗。在吃掉隨機出現(xiàn)的" Chr(13) _
"果子后,蛇的身體會變長,越長難度越大。祝您好運!!", 0 + 64, "游戲規(guī)則"
End Sub
'該過程用于顯示游戲開發(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è)計制作" Chr(13) _
"CopyRight(C)2002,BestSoft.TCG", 0, "關(guān)于本游戲"
End Sub
'該過程用于退出游戲
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
'該過程用于拖動窗體_(點擊圖標)
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ā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
'該過程用于設(shè)置蛇運動速度的快慢
Private Sub hsbGameSpeed_Change()
Me.tmrSnakeMove.Interval = Me.hsbGameSpeed.Value
End Sub
'該過程用于通過鍵盤的方向鍵改變蛇的運動方向
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
'該計時循環(huán)過程用于計算游戲耗費的秒數(shù)并顯示
Private Sub tmrGameTime_Timer()
g_lngGameTime = g_lngGameTime + 1
Me.lblGameTime.Caption = g_lngGameTime "秒"
End Sub
'該計時循環(huán)過程用于控制蛇的行動軌跡
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
'確認蛇頭的運動方向并獲取新的位置
Select Case g_intDirection
Case D_UP '向上運動
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 '向下運動
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 '向左運動
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 '向右運動
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
'移動蛇身體其他部分的位置
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
'更新蛇舊的坐標位置
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
'判斷蛇在移動中是否到了禁區(qū)而導致游戲失敗
If m_funMoveForbiddenZone(g_udtSnake(SNAKEONE).Snake_CurX, g_udtSnake(SNAKEONE).Snake_CurY) Then
Beep
MsgBox "您的蛇移動到了禁區(qū),游戲失敗!", 0 + 16, "BS貪食蛇"
Me.tmrSnakeMove.Enabled = False
Me.tmrGameTime.Enabled = False
Me.picMoveArea.Visible = False
Exit Sub
End If
'判斷蛇在移動中是否碰到了自己的身體而導致游戲失敗
If m_funTouchSnakeBody(g_udtSnake(SNAKEONE).Snake_CurX, g_udtSnake(SNAKEONE).Snake_CurY) Then
Beep
MsgBox "您的蛇在移動中碰到了自己的身體,游戲失?。?, 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 '加長蛇的身體
Call m_subGetPoint '獲取下一個果子的位置和顏色
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
'該私有子過程用于初始化游戲
Private Sub m_subGameInitialize()
Erase g_udtSnake '清空蛇的結(jié)構(gòu)數(shù)組
g_intPlayerScore = 0 '清空玩家的得分
g_lngGameTime = 0 '清空游戲耗費的秒數(shù)
g_intDirection = D_DOWN '設(shè)定蛇的初始運動方向為下
g_intSnakeLength = 4 '設(shè)定蛇的初始長度
ReDim g_udtSnake(1 To g_intSnakeLength) '重新定義蛇的長度
'定義蛇頭部的數(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 '獲取第一個果子的位置和顏色
End Sub
'該私有子過程用于返回獲取的果子的位置和顏色信息
Private Sub m_subGetPoint()
Dim lngRedValue As Long, lngGreenValue As Long, lngBlueValue As Long
Dim lngPointX As Long, lngPointY As Long, lngPointColor As Long
'隨機獲取果子的顏色
lngRedValue = Int((255 - 0 + 1) * Rnd + 0)
lngGreenValue = Int((255 - 0 + 1) * Rnd + 0)
lngBlueValue = Int((255 - 0 + 1) * Rnd + 0)
lngPointColor = RGB(lngRedValue, lngGreenValue, lngBlueValue)
'隨機獲取果子的位置
lngPointX = Int((FZ_LEFT - FZ_RIGHT + 1) * Rnd + FZ_RIGHT)
lngPointY = Int((FZ_TOP - FZ_BOTTOM + 1) * Rnd + FZ_BOTTOM)
Me.PSet (lngPointX, lngPointY), lngPointColor
'設(shè)置函數(shù)返回值
With g_udtPoint
.Point_X = lngPointX
.Point_Y = lngPointY
.Point_Color = lngPointColor
End With
End Sub
'該私有子過程用于加長蛇的身體
Private Sub m_subAddSnake()
Dim udtSnakeTemp() As Snake
Dim lngSnakeX As Long, lngSnakeY As Long, lngSnakeColor As Long
'備份蛇原先身體的數(shù)據(jù)并使蛇的身體加長
ReDim udtSnakeTemp(1 To g_intSnakeLength)
For k = 1 To g_intSnakeLength
With udtSnakeTemp(k)
.Snake_CurX = g_udtSnake(k).Snake_CurX
.Snake_CurY = g_udtSnake(k).Snake_CurY
.Snake_OldX = g_udtSnake(k).Snake_OldX
.Snake_OldY = g_udtSnake(k).Snake_OldY
.Snake_Color = g_udtSnake(k).Snake_Color
End With
Next k
g_intSnakeLength = g_intSnakeLength + 1
ReDim g_udtSnake(g_intSnakeLength)
'將備份蛇身體的數(shù)據(jù)返回到加長的蛇身數(shù)組中
For l = 1 To g_intSnakeLength - 1
With g_udtSnake(l)
.Snake_CurX = udtSnakeTemp(l).Snake_CurX
.Snake_CurY = udtSnakeTemp(l).Snake_CurY
.Snake_OldX = udtSnakeTemp(l).Snake_OldX
.Snake_OldY = udtSnakeTemp(l).Snake_OldY
.Snake_Color = udtSnakeTemp(l).Snake_Color
End With
Next l
'寫入新加入的身體數(shù)據(jù)
Select Case g_intDirection
Case D_UP
With g_udtSnake(g_intSnakeLength)
.Snake_OldX = g_udtSnake(g_intSnakeLength - 1).Snake_CurX + SNAKEWIDTH
.Snake_OldY = g_udtSnake(g_intSnakeLength - 1).Snake_CurY
.Snake_Color = g_udtPoint.Point_Color
End With
Case D_DOWN
With g_udtSnake(g_intSnakeLength)
.Snake_OldX = g_udtSnake(g_intSnakeLength - 1).Snake_CurX - SNAKEWIDTH
.Snake_OldY = g_udtSnake(g_intSnakeLength - 1).Snake_CurY
.Snake_Color = g_udtPoint.Point_Color
End With
Case D_LEFT
With g_udtSnake(g_intSnakeLength)
.Snake_OldX = g_udtSnake(g_intSnakeLength - 1).Snake_CurX
.Snake_OldY = g_udtSnake(g_intSnakeLength - 1).Snake_CurY + SNAKEWIDTH
.Snake_Color = g_udtPoint.Point_Color
End With
Case D_RIGHT
With g_udtSnake(g_intSnakeLength)
.Snake_OldX = g_udtSnake(g_intSnakeLength - 1).Snake_CurX
.Snake_OldY = g_udtSnake(g_intSnakeLength - 1).Snake_CurY - SNAKEWIDTH
.Snake_Color = g_udtPoint.Point_Color
End With
End Select
lngSnakeX = g_udtSnake(g_intSnakeLength).Snake_CurX
lngSnakeY = g_udtSnake(g_intSnakeLength).Snake_CurY
lngSnakeColor = g_udtSnake(g_intSnakeLength).Snake_Color
Me.picMoveArea.PSet (lngSnakeX, lngSnakeY), lngSnakeColor
End Sub
'該自定義函數(shù)用于返回運動的蛇是否到達禁區(qū)而導致游戲失敗
Private Function m_funMoveForbiddenZone(SnakeX As Long, SnakeY As Long) As Boolean
If (SnakeX = FZ_LEFT And SnakeX = FZ_RIGHT) And (SnakeY = FZ_TOP And SnakeY = FZ_BOTTOM) Then
m_funMoveForbiddenZone = False
Else
m_funMoveForbiddenZone = True
End If
End Function
'該自定義函數(shù)用于返回運動的蛇是否碰到自己的身體而導致游戲失敗
Private Function m_funTouchSnakeBody(SnakeX As Long, SnakeY As Long) As Boolean
For m = 2 To g_intSnakeLength
If SnakeX = g_udtSnake(m).Snake_CurX And SnakeY = g_udtSnake(m).Snake_CurY Then
m_funTouchSnakeBody = True
Exit For
Else
m_funTouchSnakeBody = False
End If
Next m
End Function
'該自定義函數(shù)用于返回運動的蛇是否吃到了果子
Private Function m_funEatPoint(SnakeX As Long, SnakeY As Long) As Boolean
If Abs(SnakeX - g_udtPoint.Point_X) = SNAKEWIDTH And Abs(SnakeY - g_udtPoint.Point_Y) = SNAKEWIDTH Then
m_funEatPoint = True
Else
m_funEatPoint = False
End If
End Function
'(API函數(shù)調(diào)用過程_用以實現(xiàn)無標題窗體的拖動操作)---------------------------------
'RleaseCapture函數(shù)用以釋放鼠標捕獲
Public Declare Function ReleaseCapture Lib "user32" () As Long
'SendMessage函數(shù)用作向Windows發(fā)送移動窗體的消息
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As _
Long, ByVal wMsg As Long, ByVal wParam As Long, IParam As Any) As Long
Public Const WM_SYSCOMMAND = H112 '聲明向Windows發(fā)送消息的常量
Public Const SC_MOVE = HF012 '聲明控制移動窗體常量
'(游戲變量聲明部分)-------------------------------------------------------------
'定義蛇的數(shù)據(jù)類型結(jié)構(gòu)
Public Type Snake
Snake_OldX As Long
Snake_OldY As Long
Snake_CurX As Long
Snake_CurY As Long
Snake_Color As Long
End Type
'定義果子的數(shù)據(jù)類型結(jié)構(gòu)
Public Type Point
Point_X As Long
Point_Y As Long
Point_Color As Long
End Type
'定義蛇的動態(tài)數(shù)組
Public g_udtSnake() As Snake
'定義果子
Public g_udtPoint As Point
'定義蛇的長度
Public g_intSnakeLength As Integer
'定義蛇的顏色
Public g_lngSnakeColor As Long
'定義蛇的運動方向
Public g_intDirection As Integer
'定義玩家的得分
Public g_intPlayerScore As Integer
'定義游戲耗費的秒數(shù)
Public g_lngGameTime As Long
如果只是簡單的貪吃蛇的話,把蛇的基本圖形設(shè)置成圓(或正方形),蛇身由一串基本圖形構(gòu)成,每過一秒,檢測前面是什么,如果是空的,那么把頭畫成普通身體的樣子,再在前方(或側(cè)面,如果轉(zhuǎn)彎的話)再畫一個頭,并將其坐標記入數(shù)組,數(shù)組要足夠大,再把尾巴畫成背景色;如果前面是食物,尾巴就不刪除.這里的難點是數(shù)組里的操作和坐標的表示:把頭添在最后一個元素,把第一個元素提取(也就是尾巴).