我们专注攀枝花网站设计 攀枝花网站制作 攀枝花网站建设
成都网站建设公司服务热线:400-028-6601

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

贪吃蛇vb.net代码,贪吃蛇vb程序

VB代码求注释

Option Explicit '如果变量不被定义,在该模块中禁止使用!

创新互联建站是专业的洪洞网站建设公司,洪洞接单;提供成都做网站、网站制作,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行洪洞网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

Dim COL As Integer, CTCO As Integer '定义变量

Dim CTS As Integer '定义变量

'可以这样写: dim COL ,CTCO ,CTS as integer

Private Sub Form_Activate() '当窗口激活时

SUCCESS = sndPlaySound(App.Path + "\sound\start.WAV", H1) '把 SUCCESS 作为返回值 ,并执行 sndPlaySound 子程序或自定义函数

End Sub

Private Sub Form_Load() '当窗口读取时

Load FrmSna '读取FRMSNA

CTS = 1 '赋值1到CTS

FrmSna.Timer1.Enabled = False '禁用窗体frmSNA的TIMER1

Label1.Top = Picture1.Top + Picture1.Height '标签LABEL1与窗体顶的距离 = PICTURE1的顶的距离+ PICTURE1的高度

CTCO = 2 '把2赋值到CTCP

COL = 0 '同理

Me.Picture = LoadPicture("") '设置窗体的图像

End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) '当鼠标在窗体上移动时

Shape1.Visible = False 'shape1不可见

End Sub

Private Sub Label1_Click() '当鼠标点击label1的时候

Timer1.Enabled = False '禁用timer1

Picture1.Visible = False '同理,禁用Picture1

End Sub

Private Sub OP1_Click() '当鼠标点击OP1的时候

Me.Hide '隐藏"我"

FrmSna.Timer1.Enabled = True 'frmSNA的timer1控件不可用

FrmSna.Show 'frmSna显示

End Sub

Private Sub OP1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) '当鼠标在OP1上移动时

Shape1.Visible = True 'shape1可见

Shape1.Top = 960 'shape1与顶的距离为960

End Sub

Private Sub OP2_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) '当鼠标在OP2上移动时

Shape1.Visible = True 'shape1可见

Shape1.Top = 1440 'shape1与顶的距离为1440

End Sub

Private Sub OP3_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) '当鼠标在OP3上移动时

Shape1.Visible = True 'shape1可见

Shape1.Top = 1920 'shape1与顶的距离为1920

End Sub

Private Sub OP2_Click() '单机OP2的时候

Label1.Top = Picture1.Top + Picture1.Height 'LABEL1与窗体顶的距离 = PICTURE1的顶的距离+ PICTURE1的高度

Picture1.Visible = True 'PICTURE1为可见

Timer1.Enabled = True 'timer1为可见

Label1.Caption = Space(10) "贪吃蛇" Space(16) "贪吃蛇小游戏!" Chr(13) "操作说明:方向键控制方向、Escape暂停" Chr(13) "ZYX" 'label1的caption属性值为 = ..

'space(10) 、Space(16)为输出空格。chr(13)也表空格

'

'

End Sub

Private Sub Picture1_Click() '当点击PCITURE1时

Timer1.Enabled = False 'Timer1不可用

Picture1.Visible = False 'picture1不可见

End Sub

Private Sub Timer1_Timer() '触发timer1 执行的代码

Label1.Top = Label1.Top - 10 'label1往下移十个单位

If S.Left + S.Width Me.ScaleLeft + Me.Width Then CTS = 2 '如果s与顶的距离+S的宽度大于 "我"窗体的左水平坐标+"我"的宽则CTS =2

If S.Left Me.ScaleLeft Then CTS = 1 '如果s与容器左边的距离小于"我"的左水平坐标,则CTS =1

If CTS = 1 Then S.Left = S.Left + 50 '如果CTS =1则S向左移50个单位

If CTS = 2 Then S.Left = S.Left - 50 '如果CTS =2则S向右移50个单位

End Sub

Private Sub Timer2_Timer() '当触发Timer2的时候

'标题的淡出淡入

If CTCO = 1 Then COL = COL - 5 '如果CTCO=1 则COL减5

If CTCO = 2 Then COL = COL + 5 '如果CTCO=2 则COL加5

If COL 240 Then CTCO = 1 '如果COL大于240 CTCO则等于1

If COL 10 Then CTCO = 2 '如果COL小于10 CTCO则等于2

TXT.ForeColor = RGB(COL, COL, COL) 'txt的文本色或者图片背景色为RGB(col,col,col)

End Sub

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

'该私有子过程用于返回获取的果子的位置和颜色信息

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

'设置函数返回值

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

'备份蛇原先身体的数据并使蛇的身体加长

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)

'将备份蛇身体的数据返回到加长的蛇身数组中

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

'写入新加入的身体数据

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

'该自定义函数用于返回运动的蛇是否到达禁区而导致游戏失败

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

'该自定义函数用于返回运动的蛇是否碰到自己的身体而导致游戏失败

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

'该自定义函数用于返回运动的蛇是否吃到了果子

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函数调用过程_用以实现无标题窗体的拖动操作)---------------------------------

'RleaseCapture函数用以释放鼠标捕获

Public Declare Function ReleaseCapture Lib "user32" () As Long

'SendMessage函数用作向Windows发送移动窗体的消息

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发送消息的常量

Public Const SC_MOVE = HF012 '声明控制移动窗体常量

'(游戏变量声明部分)-------------------------------------------------------------

'定义蛇的数据类型结构

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

'定义果子的数据类型结构

Public Type Point

Point_X As Long

Point_Y As Long

Point_Color As Long

End Type

'定义蛇的动态数组

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

'定义游戏耗费的秒数

Public g_lngGameTime As Long

哪位能告诉我贪吃蛇游戏的全部代码?

//package main;

import java.awt.Color;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.io.BufferedReader;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.InputStreamReader;

import javax.swing.ImageIcon;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

public class TanChiShe implements KeyListener,ActionListener{

/**

* @param args

*/

int max = 300;//蛇长最大值

final int JianJu = 15; //设定蛇的运动网格间距(窗口最大32*28格)

byte fangXiang = 4; //控制蛇的运动方向,初始为右

int time = 500; //蛇的运动间隔时间

int jianTime = 2;//吃一个减少的时间

int x,y;//蛇的运动坐标,按网格来算

int x2,y2;//暂存蛇头的坐标

int jiFenQi = 0;//积分器

boolean isRuned = false;//没运行才可设级别

boolean out = false;//没开始运行?

boolean run = false;//暂停运行

String JiBie = "中级";

JFrame f = new JFrame("贪吃蛇 V1.0");

JPanel show = new JPanel();

JLabel Message = new JLabel("级别:中级 蛇长:5 时间500ms 分数:00");

// JButton play = new JButton("开始");

JLabel sheTou;

JLabel shiWu;

JLabel sheWei[] = new JLabel[max];

static int diJi = 4; //第几个下标的蛇尾要被加上

ImageIcon shang = new ImageIcon("tuPian\\isSheTouUp.png");//产生四个上下左右的蛇头图案

ImageIcon xia = new ImageIcon("tuPian\\isSheTouDown.png");

ImageIcon zhuo = new ImageIcon("tuPian\\isSheTouLeft.png");

ImageIcon you = new ImageIcon("tuPian\\isSheTouRight.png");

JMenuBar JMB = new JMenuBar();

JMenu file = new JMenu("开始游戏");

JMenuItem play = new JMenuItem(" 开始游戏 ");

JMenuItem pause = new JMenuItem(" 暂停游戏 ");

JMenu hard = new JMenu("游戏难度");

JMenuItem gao = new JMenuItem("高级");

JMenuItem zhong = new JMenuItem("中级");

JMenuItem di = new JMenuItem("低级");

JMenu about = new JMenu(" 关于 ");

JMenuItem GF = new JMenuItem("※高分榜");

JMenuItem ZZ = new JMenuItem("关于作者");

JMenuItem YX = new JMenuItem("关于游戏");

JMenuItem QK = new JMenuItem("清空记录");

static TanChiShe tcs = new TanChiShe();

public static void main(String[] args) {

// TanChiShe tcs = new TanChiShe();

tcs.f();

}

public void f(){

f.setBounds(250,100,515,530);

f.setLayout(null);

f.setAlwaysOnTop(true);//窗口始终保持最前面

f.setBackground(new Color(0,0,0));

f.setDefaultCloseOperation(0);

f.setResizable(false);

f.setVisible(true);

// f.getContentPane().setBackground(Color.BLACK);

f.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

System.exit(0);//退出程序

}

});

f.setJMenuBar(JMB);

JMB.add(file);

file.add(play);

file.add(pause);

JMB.add(hard);

hard.add(gao);

hard.add(zhong);

hard.add(di);

JMB.add(about);

about.add(GF);

GF.setForeground(Color.blue);

about.add(ZZ);

about.add(YX);

about.add(QK);

QK.setForeground(Color.red);

f.add(show);

show.setBounds(0,f.getHeight()-92,f.getWidth(),35);

// show.setBackground(Color.green);

// f.add(play);

// play.setBounds(240,240,80,25);

play.addActionListener(this);

pause.addActionListener(this);

gao.addActionListener(this);

zhong.addActionListener(this);

di.addActionListener(this);

GF.addActionListener(this);

ZZ.addActionListener(this);

YX.addActionListener(this);

QK.addActionListener(this);

show.add(Message);

Message.setForeground(Color.blue);

f.addKeyListener(this);

// show.addKeyListener(this);

play.addKeyListener(this);

sheChuShi();

}

public void sheChuShi(){//蛇初始化

sheTou = new JLabel(you);//用向右的图来初始蛇头

f.add(sheTou);

sheTou.setBounds(JianJu*0,JianJu*0,JianJu,JianJu);

// System.out.println("ishere");

shiWu = new JLabel("■");

f.add(shiWu);

shiWu.setBounds(10*JianJu,10*JianJu,JianJu,JianJu);

for(int i=0;i=diJi;i++) {

sheWei[i] = new JLabel("■");

f.add(sheWei[i]);

sheWei[i].setBounds(-1*JianJu,0*JianJu,JianJu,JianJu);

}

while(true){

if(out == true){

yunXing();

break;

}

try{

Thread.sleep(200);

}catch(Exception ex){

ex.printStackTrace();

}

}

}

public void sheJiaChang(){//蛇的长度增加

if(diJi max){

sheWei[++diJi] = new JLabel(new ImageIcon("tuPian\\isSheWei.jpg"));

f.add(sheWei[diJi]);

sheWei[diJi].setBounds(sheWei[diJi-1].getX(),sheWei[diJi-1].getY(),JianJu,JianJu);

// System.out.println("diJi "+diJi);

}

}

public void pengZhuanJianCe(){//检测蛇的碰撞情况

if(sheTou.getX()0 || sheTou.getY()0 ||

sheTou.getX()f.getWidth()-15 || sheTou.getY()f.getHeight()-105 ){

gameOver();

// System.out.println("GameOVER");

}

if(sheTou.getX() == shiWu.getX() sheTou.getY() == shiWu.getY()){

out: while(true){

shiWu.setLocation((int)(Math.random()*32)*JianJu,(int)(Math.random()*28)*JianJu);

for(int i=0;i=diJi;i++){

if(shiWu.getX()!= sheWei[i].getX() shiWu.getY()!=sheWei[i].getY()

sheTou.getX()!=shiWu.getX() sheTou.getY()!= shiWu.getY()){//如果食物不在蛇身上则退出循环,产生食物成功

break out;

}

}

}

sheJiaChang();

// System.out.println("吃了一个");

if(time100 ){

time -= jianTime;

}

else{}

Message.setText("级别:"+JiBie+" 蛇长:"+(diJi+2)+" 时间:"+time+"ms 分数:"+(jiFenQi+=10)+"");

}

for(int i=0;i=diJi;i++){

if(sheTou.getX() == sheWei[i].getX() sheTou.getY() == sheWei[i].getY()){

gameOver();

// System.out.println("吃到尾巴了");

}

}

}

public void yunXing(){

while(true){

while(run){

if(fangXiang == 1){//上

y-=1;

}

if(fangXiang == 2){//下

y+=1;

}

if(fangXiang == 3){//左

x-=1;

}

if(fangXiang == 4){//右

x+=1;

}

x2 = sheTou.getX();

y2 = sheTou.getY();

sheTou.setLocation(x*JianJu,y*JianJu); //设置蛇头的坐标 网格数*间隔

for(int i=diJi;i=0;i--){

if(i==0){

sheWei[i].setLocation(x2,y2);

// System.out.println(i+" "+sheTou.getX()+" "+sheTou.getY());

}

else{

sheWei[i].setLocation(sheWei[i-1].getX(),sheWei[i-1].getY());

// System.out.println(i+" "+sheWei[i].getX()+" "+sheWei[i].getY());

}

}

pengZhuanJianCe();

try{

Thread.sleep(time);

}catch(Exception e){

e.printStackTrace();

}

}

Message.setText("级别:"+JiBie+" 蛇长:"+(diJi+2)+" 时间:"+time+"ms 分数:"+(jiFenQi+=10)+"");

try{

Thread.sleep(200);

}catch(Exception e){

e.printStackTrace();

}

}

}

public void gameOver(){//游戏结束时处理

int in = JOptionPane.showConfirmDialog(f,"游戏已经结束!\n是否要保存分数","提示",JOptionPane.YES_NO_OPTION);

if(in == JOptionPane.YES_OPTION){

// System.out.println("YES");

String s = JOptionPane.showInputDialog(f,"输入你的名字:");

try{

FileInputStream fis = new FileInputStream("GaoFen.ini");//先把以前的数据读出来加到写的数据前

InputStreamReader isr = new InputStreamReader(fis);

BufferedReader br = new BufferedReader(isr);

String s2,setOut = "";

while((s2=br.readLine())!= null){

setOut =setOut+s2+"\n";

}

FileOutputStream fos = new FileOutputStream("GaoFen.ini");//输出到文件流

s = setOut+s+":"+jiFenQi+"\n";

fos.write(s.getBytes());

}catch(Exception e){}

}

System.exit(0);

}

public void keyTyped(KeyEvent arg0) {

// TODO 自动生成方法存根

}

public void keyPressed(KeyEvent arg0) {

// System.out.println(arg0.getSource());

if(arg0.getKeyCode() == KeyEvent.VK_UP){//按上下时方向的值相应改变

if(fangXiang != 2){

fangXiang = 1;

// sheTou.setIcon(shang);//设置蛇的方向

}

// System.out.println("UP");

}

if(arg0.getKeyCode() == KeyEvent.VK_DOWN){

if(fangXiang != 1){

fangXiang = 2;

// sheTou.setIcon(xia);

}

// System.out.println("DOWN");

}

if(arg0.getKeyCode() == KeyEvent.VK_LEFT){//按左右时方向的值相应改变

if(fangXiang != 4){

fangXiang = 3;

// sheTou.setIcon(zhuo);

}

// System.out.println("LEFT");

}

if(arg0.getKeyCode() == KeyEvent.VK_RIGHT){

if(fangXiang != 3){

fangXiang = 4;

// sheTou.setIcon(you);

}

// System.out.println("RIGHT");

}

}

public void keyReleased(KeyEvent arg0) {

// TODO 自动生成方法存根

}

public void actionPerformed(ActionEvent arg0) {

// TODO 自动生成方法存根

JMenuItem JI = (JMenuItem)arg0.getSource();

if(JI == play){

out = true;

run = true;

isRuned = true;

gao.setEnabled(false);

zhong.setEnabled(false);

di.setEnabled(false);

}

if(JI == pause){

run = false;

}

if(isRuned == false){//如果游戏还没运行,才可以设置级别

if(JI == gao){

time = 200;

jianTime = 1;

JiBie = "高级";

Message.setText("级别:"+JiBie+" 蛇长:"+(diJi+2)+" 时间:"+time+"ms 分数:"+jiFenQi);

}

if(JI == zhong){

time = 400;

jianTime = 2;

JiBie = "中级";

Message.setText("级别:"+JiBie+" 蛇长:"+(diJi+2)+" 时间:"+time+"ms 分数:"+jiFenQi);

}

if(JI == di){

time = 500;

jianTime = 3;

JiBie = "低级";

Message.setText("级别:"+JiBie+" 蛇长:"+(diJi+2)+" 时间:"+time+"ms 分数:"+jiFenQi);

}

}

if(JI == GF){

try{

FileInputStream fis = new FileInputStream("GaoFen.ini");

InputStreamReader isr = new InputStreamReader(fis);

BufferedReader br = new BufferedReader(isr);

String s,setOut = "";

while((s=br.readLine())!= null){

setOut =setOut+s+"\n";

}

if(setOut.equals("")){

JOptionPane.showMessageDialog(f,"暂无保存记录!","高分榜",JOptionPane.INFORMATION_MESSAGE);

}

else{

JOptionPane.showMessageDialog(f,setOut);

}

}catch(Exception e){

e.printStackTrace();

}

}

if(JI == ZZ){//关于作者

JOptionPane.showMessageDialog(f,"软件作者:申志飞\n地址:四川省绵阳市\nQQ:898513806\nE-mail:shenzhifeiok@126.com","关于作者",JOptionPane.INFORMATION_MESSAGE);

}

if(JI == YX){//关于游戏

JOptionPane.showMessageDialog(f,"贪吃蛇游戏\n游戏版本 V1.0","关于游戏",JOptionPane.INFORMATION_MESSAGE);

}

if(JI == QK){

try{

int select = JOptionPane.showConfirmDialog(f,"确实要清空记录吗?","清空记录",JOptionPane.YES_OPTION);

if(select == JOptionPane.YES_OPTION){

String setOut = "";

FileOutputStream fos = new FileOutputStream("GaoFen.ini");//输出到文件流

fos.write(setOut.getBytes());

}

}catch(Exception ex){}

}

}

}

//是我自己写的,本来里面有图片的,但无法上传,所以把图片去掉了,里面的ImageIcon等语句可以去掉。能正常运行。

怎样用vb编写贪吃蛇游戏

1、向上前进的时候,对代码进行一个详解。

2、向上前进时,x坐标不动,y坐标-1,如果下一个有食物 下一个位置的坐标和食物的坐标相同。把食物转化成蛇的身体。

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

4、如果没有吃到食物,蛇可以正常往前走,恢复原来的方块。

5、向下前进时,x坐标不动,y坐标+1。

6、如果有食物就把食物转化为身体。

7、如果没有吃到食物,蛇可以正常往前走,恢复原来的方块。

VB 做一个非常简单的贪吃蛇程序

朋友给段代码给你,这个代码不要任何控件,你只需要把以下代码复制到你的窗体代码中就行了,代码很简单,相信你能看懂!仅仅用了一个结构(ShenTi)一个数组(ZhuangTai(23, 23) As Long) 一个变量(GFangXiang)

代码中自动生成了Timer1,Label1控件。以及几个函数(Randomize,Circle,Line,Erase...) 相信这些对于你来说很简单,只是没想到用这些方法来实现而已,现在你开始看看效果吧!

代码如下:

'贪吃蛇代码(无控件、全代码)

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: '游戏结束

If MsgBox("得分:" UBound(She) - 2 "分 " vbCrLf "游戏结束,点“是”重新开始游戏,点“否”", 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

'随机产生食物

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代码

#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; /*游戏速度自己调整*/

struct Food

{

int x; /*食物的横坐标*/

int y; /*食物的纵坐标*/

int yes; /*判断是否要出现食物的变量*/

}food; /*食物的结构体*/

struct Snake

{

int x[N];

int y[N];

int node; /*蛇的节数*/

int direction; /*蛇移动方向*/

int life; /* 蛇的生命,0活着,1死亡*/

}snake;

void Init(void); /*图形驱动*/

void Close(void); /*图形结束*/

void DrawK(void); /*开始画面*/

void GameOver(void); /*结束游戏*/

void GamePlay(void); /*玩游戏具体过程*/

void PrScore(void); /*输出成绩*/

/*主函数*/

void main(void)

{

Init(); /*图形驱动*/

DrawK(); /*开始画面*/

GamePlay(); /*玩游戏具体过程*/

Close(); /*图形结束*/

}

/*图形驱动*/

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); /*设置线型*/

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)); /*随机数发生器*/

food.yes=1; /*1表示需要出现新食物,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; /*节数*/

PrScore(); /*输出得分*/

while(1) /*可以重复玩游戏,压ESC键*/

{

while(!kbhit()) /*在没有按键的情况下,蛇自*/

{

if(food.yes==1) /*需要出现新食物*/

{

food.x=rand()%400+60;

food.y=rand()%350+60;

while(food.x%10!=0) /*食物随机出现后必须让食物*/

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--) /*蛇的每个环节往前移动,也法/

{

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;

}

/*从蛇的第四节开始判断是否撞到自己了,因为蛇头为两节,第三节不可*/

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(); /*本次游戏结束*/

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;

/*新的一节先放在看不见的位置,下次循环就取前一节的位置*/

snake.node++; /*蛇的身体长一节*/

food.yes=1; /*画面上需要出现新的食物*/

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) /*如果蛇死就跳出循环*/

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)*/

}

/*游戏结束*/

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);

}

/*图形结束*/

void Close(void)

{

closegraph();

}


新闻标题:贪吃蛇vb.net代码,贪吃蛇vb程序
网站网址:http://mswzjz.cn/article/hscsii.html

其他资讯