導航:首頁 > 源碼編譯 > 貪吃蛇小程序源碼教程

貪吃蛇小程序源碼教程

發布時間:2023-10-08 08:33:33

① 求用vbs編寫的貪吃蛇源代碼

需用VB實現,代碼如下

'定義蛇的運動速度枚舉值
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

'定義運動區域4個禁區的枚舉值
Private Enum tpsForbiddenZone
FZ_TOP = 30
FZ_BOTTOM = 5330
FZ_LEFT = 30
FZ_RIGHT = 5730
End Enum

'定義蛇頭及身體初始化數枚舉值
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 = "繼續游戲(&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

'該過程用於顯示游戲規則
Private Sub cmdGameRules_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Beep
MsgBox " BS貪食蛇:一個規則最簡單的趣味游戲,您將用鍵盤" & Chr(13) & _
"上的4個方向鍵來控制蛇的運動方向。在運動過程中蛇" & Chr(13) & _
"不能後退,蛇的頭部也不能接觸到運動區域的邊線以外" & Chr(13) & _
"和蛇自己的身體,否則就游戲失敗。在吃掉隨機出現的" & Chr(13) & _
"果子後,蛇的身體會變長,越長難度越大。祝您好運!!", 0 + 64, "游戲規則"
End Sub

'該過程用於顯示游戲開發信息
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設計製作" & Chr(13) & _
"CopyRight(C)2002,BestSoft.TCG", 0, "關於本游戲"
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

'該共用過程用於處理窗體控制按鈕組的相關操作_(鎖定、最小化、退出)
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

'該過程用於設置蛇運動速度的快慢
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

'該計時循環過程用於計算游戲耗費的秒數並顯示
Private Sub tmrGameTime_Timer()
g_lngGameTime = g_lngGameTime + 1
Me.lblGameTime.Caption = g_lngGameTime & "秒"
End Sub

'該計時循環過程用於控制蛇的行動軌跡
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
'根據新的位置繪制蛇頭
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
'判斷蛇在移動中是否到了禁區而導致游戲失敗
If m_funMoveForbiddenZone(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_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 '清空蛇的結構數組
g_intPlayerScore = 0 '清空玩家的得分
g_lngGameTime = 0 '清空游戲耗費的秒數
g_intDirection = D_DOWN '設定蛇的初始運動方向為下
g_intSnakeLength = 4 '設定蛇的初始長度
ReDim g_udtSnake(1 To g_intSnakeLength) '重新定義蛇的長度
'定義蛇頭部的數據
With g_udtSnake(SNAKEONE)
.Snake_OldX = 530
.Snake_OldY = 530
.Snake_Color = vbBlack
End With
'定義蛇身第2節的數據
With g_udtSnake(SNAKETWO)
.Snake_OldX = 530
.Snake_OldY = 430
.Snake_Color = vbGreen
End With
'定義蛇身第3節的數據
With g_udtSnake(SNAKETHREE)
.Snake_OldX = 530
.Snake_OldY = 330
.Snake_Color = vbYellow
End With
'定義蛇身第4節的數據
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

接下面的

② 求"貪吃蛇"小游戲java源代碼一份

貪吃蛇
import
java.awt.*;
import
java.awt.event.*;
public
class
GreedSnake
//主類
{
/**
*
@param
args
*/
public
static
void
main(String[]
args)
{
//
TODO
Auto-generated
method
stub
new
MyWindow();
}
}
class
MyPanel
extends
Panel
implements
KeyListener,Runnable//自定義面板類,繼承了鍵盤和線程介面
{
Button
snake[];
//定義蛇按鈕
int
shu=0;
//蛇的節數
int
food[];
//食物數組
boolean
result=true;
//判定結果是輸
還是贏
Thread
thread;
//定義線程
static
int
weix,weiy;
//食物位置
boolean
t=true;
//判定游戲是否結束
int
fangxiang=0;
//蛇移動方向
int
x=0,y=0;
//蛇頭位置
MyPanel()
{
setLayout(null);
snake=new
Button[20];
food=new
int
[20];
thread=new
Thread(this);
for(int
j=0;j<20;j++)
{
food[j]=(int)(Math.random()*99);//定義20個隨機食物
}
weix=(int)(food[0]*0.1)*60;
//十位*60為橫坐標
weiy=(int)(food[0]%10)*40;
//個位*40為縱坐標
for(int
i=0;i<20;i++)
{
snake[i]=new
Button();
}
add(snake[0]);
snake[0].setBackground(Color.black);
snake[0].addKeyListener(this);
//為蛇頭添加鍵盤監視器
snake[0].setBounds(0,0,10,10);
setBackground(Color.cyan);
}
public
void
run()
//接收線程
{
while(t)
{
if(fangxiang==0)//向右
{
try
{
x+=10;
snake[0].setLocation(x,
y);//設置蛇頭位置
if(x==weix&&y==weiy)
//吃到食物
{
shu++;
weix=(int)(food[shu]*0.1)*60;
weiy=(int)(food[shu]%10)*40;
repaint();
//重繪下一個食物
add(snake[shu]);
//增加蛇節數和位置
snake[shu].setBounds(snake[shu-1].getBounds());
}
thread.sleep(100);
//睡眠100ms
}
catch(Exception
e){}
}
else
if(fangxiang==1)//向左
{
try
{
x-=10;
snake[0].setLocation(x,
y);
if(x==weix&&y==weiy)
{
shu++;
weix=(int)(food[shu]*0.1)*60;
weiy=(int)(food[shu]%10)*40;
repaint();
add(snake[shu]);
snake[shu].setBounds(snake[shu-1].getBounds());
}
thread.sleep(100);
}
catch(Exception
e){}
}
else
if(fangxiang==2)//向上
{
try
{
y-=10;
snake[0].setLocation(x,
y);
if(x==weix&&y==weiy)
{
shu++;
weix=(int)(food[shu]*0.1)*60;
weiy=(int)(food[shu]%10)*40;
repaint();
add(snake[shu]);
snake[shu].setBounds(snake[shu-1].getBounds());
}
thread.sleep(100);
}
catch(Exception
e){}
}
else
if(fangxiang==3)//向下
{
try
{
y+=10;
snake[0].setLocation(x,
y);
if(x==weix&&y==weiy)
{
shu++;
weix=(int)(food[shu]*0.1)*60;
weiy=(int)(food[shu]%10)*40;
repaint();
add(snake[shu]);
snake[shu].setBounds(snake[shu-1].getBounds());
}
thread.sleep(100);
}
catch(Exception
e){}
}
int
num1=shu;
while(num1>1)//判斷是否咬自己的尾巴
{
if(snake[num1].getBounds().x==snake[0].getBounds().x&&snake[num1].getBounds().y==snake[0].getBounds().y)
{
t=false;
result=false;
repaint();
}
num1--;
}
if(x<0||x>=this.getWidth()||y<0||y>=this.getHeight())//判斷是否撞牆
{
t=false;
result=false;
repaint();
}
int
num=shu;
while(num>0)
//設置蛇節位置
{
snake[num].setBounds(snake[num-1].getBounds());
num--;
}
if(shu==15)
//如果蛇節數等於15則勝利
{
t=false;
result=true;
repaint();
}
}
}
public
void
keyPressed(KeyEvent
e)
//按下鍵盤方向鍵
{
if(e.getKeyCode()==KeyEvent.VK_RIGHT)//右鍵
{
if(fangxiang!=1)//如果先前方向不為左
fangxiang=0;
}
else
if(e.getKeyCode()==KeyEvent.VK_LEFT)
{
if(fangxiang!=0)
fangxiang=1;
}
else
if(e.getKeyCode()==KeyEvent.VK_UP)
{
if(fangxiang!=3)
fangxiang=2;
}
else
if(e.getKeyCode()==KeyEvent.VK_DOWN)
{
if(fangxiang!=2)
fangxiang=3;
}
}
public
void
keyTyped(KeyEvent
e)
{
}
public
void
keyReleased(KeyEvent
e)
{
}
public
void
paint(Graphics
g)
//在面板上繪圖
{
int
x1=this.getWidth()-1;
int
y1=this.getHeight()-1;
g.setColor(Color.red);
g.fillOval(weix,
weiy,
10,
10);//食物
g.drawRect(0,
0,
x1,
y1);
//牆
if(t==false&&result==false)
g.drawString("GAME
OVER!",
250,
200);//輸出遊戲失敗
else
if(t==false&&result==true)
g.drawString("YOU
WIN!",
250,
200);//輸出遊戲成功
}
}
class
MyWindow
extends
Frame
implements
ActionListener//自定義窗口類
{
MyPanel
my;
Button
btn;
Panel
panel;
MyWindow()
{
super("GreedSnake");
my=new
MyPanel();
btn=new
Button("begin");
panel=new
Panel();
btn.addActionListener(this);
panel.add(new
Label("begin後請按Tab鍵選定蛇"));
panel.add(btn);
panel.add(new
Label("按上下左右鍵控制蛇行動"));
add(panel,BorderLayout.NORTH);
add(my,BorderLayout.CENTER);
setBounds(100,100,610,500);
setVisible(true);
validate();
addWindowListener(new
WindowAdapter()
{
public
void
windowClosing(WindowEvent
e)
{
System.exit(0);
}
});
}
public
void
actionPerformed(ActionEvent
e)//按下begin按鈕
{
if(e.getSource()==btn)
{
try
{
my.thread.start();
//開始線程
my.validate();
}
catch(Exception
ee){}
}
}
}

③ 求貪吃蛇的c語言代碼,覺得挺好玩的

。。4555555555555

④ scratch貪吃蛇製作教程

scratch貪吃蛇製作教程如下:

1、游戲工亮輪作過程。鍵盤主要控制貪吃蛇的頭部移動,尾巴是沿著軌跡移動就行。貪吃蛇的身體,是用程序畫筆模塊敬差信畫出來的。如果頭部吃到食物,就給游戲分數加分。

6、最終游戲效果:點擊「綠色旗子」開始游戲,按下鍵盤的方向鍵,控制貪吃蛇移動。

吃到食物時,變數「游戲分數」就會加1分。

閱讀全文

與貪吃蛇小程序源碼教程相關的資料

熱點內容
搶答器c程序編程 瀏覽:703
什麼app可以自己玩 瀏覽:76
刨客app是什麼 瀏覽:963
cad輸入命令欄不見了 瀏覽:834
做故事集可以用什麼app 瀏覽:692
qq郵箱發送壓縮包 瀏覽:672
程序員桌面機器人 瀏覽:589
xjr快速開發平台源碼 瀏覽:159
java介面runnable 瀏覽:31
python怎麼運行web伺服器 瀏覽:349
notepad編程代碼 瀏覽:740
什麼安卓的毛病最少 瀏覽:611
hp的pjl設備訪問命令 瀏覽:635
googlewebp圖片壓縮技術 瀏覽:215
tbc薩滿加血宏命令 瀏覽:757
pdf閃 瀏覽:289
手機伺服器地址填什麼 瀏覽:258
lrpython代碼 瀏覽:848
加密照片怎麼也找不到 瀏覽:796
卸載編譯安裝的java 瀏覽:299