‘壹’ C语言的贪吃蛇源代码
#include <bits/stdc++.h>
#include <windows.h>
#include <conio.h>
using namespace std;
void gotoxy(int x,int y) {COORD pos={x,y}; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);}//光标定位
class Food {//食物类
private: int m_x; int m_y;
public:
void randfood() {//随机产生一个食物
srand((int)time(NULL));//利用时间添加随机数种子,需要ctime头文件
L1:{m_x=rand()%(85)+2;//2~86
m_y=rand()%(25)+2;//2~26
if(m_x%2) goto L1;//如果食物的x坐标不是偶数则重新确定食物的坐标
gotoxy(m_x,m_y);//在确认好的位置输出食物
cout << "★";}}
int getFoodm_x() {return m_x;}//返回食物的x坐标
int getFoodm_y() {return m_y;}};//返回食物的y坐标
class Snake {
private:
struct Snakecoor {int x; int y;};//定义一个蛇的坐标机构
vector<Snakecoor> snakecoor;//将坐标存入vector容器中
//判断并改变前进方向的函数
void degdir(Snakecoor&nexthead) {//定义新的蛇头变量
static char key='d';//静态变量防止改变移动方向后重新改回来
if(_kbhit()) {
char temp=_getch();//定义一个临时变量储存键盘输入的值
switch(temp) {//如果临时变量的值为wasd中的一个,则赋值给key
default: break;//default是缺省情况,只有任何条件都不匹配的情况下才会执行 必须写在前面!不然蛇无法转向
case'w': case'a': case's': case'd':
//如果temp的方向和key的方向不相反则赋值 因为两次移动方向不能相反 将蛇设置为初始向右走
if(key=='w' && temp!='s' || key=='s' && temp!='w' || key=='a' && temp!='d' || key=='d' && temp!='a') key=temp;}}
switch (key) {//根据key的值来确定蛇的移动方向
case'd': nexthead.x=snakecoor.front().x+2; nexthead.y=snakecoor.front().y; break;
//新的蛇头的头部等于容器内第一个数据(旧蛇头)x坐标+2 因为蛇头占两个坐标,移动一次加2
case'a': nexthead.x=snakecoor.front().x-2; nexthead.y=snakecoor.front().y; break;
case'w': nexthead.x=snakecoor.front().x; nexthead.y=snakecoor.front().y-1; break;
//因为控制台的x长度是y的一半,所以用两个x做蛇头,需要的坐标是二倍
case's': nexthead.x=snakecoor.front().x; nexthead.y=snakecoor.front().y+1;}}
//游戏结束时设计一个界面输出“游戏结束”以及分数
void finmatt(const int score) {
system("cls"); gotoxy(40, 14);//清屏然后输出
cout << "游戏结束"; gotoxy(40, 16);
cout << "得分:" << score; gotoxy(0, 26);
exit(0);}//exit为C++的退出函数 exit(0)表示程序正常退出,非0表示非正常退出
void finishgame(const int score) {//游戏结束
if(snakecoor[0].x>=88 || snakecoor[0].x<0 || snakecoor[0].y>=28 || snakecoor[0].y<0) finmatt(score);//撞墙
for(int i=1;i<snakecoor.size();i++) if(snakecoor[0].x==snakecoor[i].x && snakecoor[0].y==snakecoor[i].y) finmatt(score);}//撞到自身
public://构造初始化蛇的位置
Snake() { Snakecoor temp;//临时结构变量用于创建蛇
for(int i=5;i>=0;i--) {//反向创建初始蛇身,初始蛇头朝右
temp.x=16+(i<<1); temp.y=8;//偶数 在蛇头左移生成蛇身
snakecoor.push_back(temp);}}//在蛇尾尾插入临时变量
void move(Food&food, int& score) {//蛇运动的函数
Snakecoor nexthead;//新蛇头变量
degdir(nexthead);//判断和改变蛇前进的方向
snakecoor.insert(snakecoor.begin(), nexthead);//将蛇头插入容器的头部
gotoxy(0, 0); cout << "得分:" << score;//每次移动都在左上角刷新得分
gotoxy(0, 2); cout << "蛇的长度为:" << snakecoor.size();//长度用来测试
finishgame(score);//判断游戏结束,输出分数
//吃到食物蛇的变化
if(snakecoor[0].x==food.getFoodm_x() && snakecoor[0].y==food.getFoodm_y()) {//蛇头与食物重合
gotoxy(snakecoor[0].x, snakecoor[0].y); cout << "●";//吃到食物时这次蛇没有移动,所以蛇会卡顿一下
gotoxy(snakecoor[1].x, snakecoor[1].y); cout << "■";//重新输出一下蛇头和第一节蛇身让蛇不卡顿
score++; food.randfood(); return;}//吃到食物得分+1,如果蛇头坐标和食物坐标重合则重新产生一个食物并直接结束本次移动
for(int i=0;i<snakecoor.size();i++) {//遍历容器,判断食物与蛇身是否重合并输出整条蛇
gotoxy(snakecoor[i].x, snakecoor[i].y);
if (!i) cout << "●"; else cout << "■";//头部输出圆形否则输出方块
if (snakecoor[i].x==food.getFoodm_x() && snakecoor[i].y==food.getFoodm_y())food.randfood();}//如果食物刷新到了蛇身上,则重新产生一个
gotoxy(snakecoor.back().x,snakecoor.back().y);cout << " ";snakecoor.pop_back();}};
//数据与画面是分开,的在容器尾部的地方输出空格 清除画面上的蛇尾,删除容器中最后一个数据 清除数据上的蛇尾
void HideCursor() {CONSOLE_CURSOR_INFO cursor_info={1,0};SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);}//隐藏光标
int main() {system("mode con cols=88 lines=28"); system("title 贪吃蛇"); HideCursor();//光标隐藏,设置控制台窗口大小、标题
int score=0; Food food; food.randfood(); Snake snake;//得分变量,食物对象,开局随机产生一个食物,蛇对象
while(true) {snake.move(food, score);Sleep(150);}return 0;}//蛇移动,游戏速度
‘贰’ 贪吃蛇代码
TC下的代码
#include <dos.h>
#include<alloc.h>
#include <stdlib.h>
#include <stdio.h>
#include "graphics.h"
#include<math.h>
typedef struct
{
int x;
int y;
}node;
struct
{
node info[40];
int length;
}snake;
char light[60][48];
void main()
{
int search(int m,int n);
void Light(int m,int n,int color);
int settle(int key);
char met=1;
int i,j,direction,key1,key2=0,rm,rn;
int driver=DETECT,mode=0;
initgraph(&driver,&mode," ");
setcolor(10);
rectangle(0,0,580,450);
snake.length=3;
for(i=0;i<60;i++)
for(j=0;j<48;j++)
light[i][j]=0;
snake.info[0].x=32;
snake.info[1].x=32;
snake.info[2].x=32;
snake.info[0].y=24;
snake.info[1].y=25;
snake.info[2].y=26;
for(i=0;i<3;i++)
Light(snake.info[i].x,snake.info[i].y,8);
do
{
if(met==1)
{
do
{
rm=random(57);
rn=random(44);
met=0;
}while(search(rm,rn));
}
Light(rm,rn,10);
if(!bioskey(1))
{
key1=key2;
}
else
{
key1=bioskey(0);
if((key2==0x4b00&&key1==0x4d00)||(key1==0x4b00&&key2==0x4d00)||(key1==0x4800&&key2==0x5000)||(key1==0x5000&&key2==0x4800)||(key1==0x5000&&key2==0))
key1=key2;
else key2=key1;
}
if(key1) met=settle(key1);
if(met==2) return;
for(i=0;i<snake.length;i++)
Light(snake.info[i].x,snake.info[i].y,8);
for(i=0;i<13-snake.length*0.3;i++) delay(12000);
}while(snake.length<40);
getch();
}
void Light(int m,int n,int color)
{
setfillstyle(1,color);
bar(10*m,10*n,10*m+8,10*n+8);
if(color==8) light[m][n]=2;
if(color==10) light[m][n]=1;
if(color==0) light[m][n]=0;
}
int search(int m,int n)
{
if(light[m][n]==2)
return(1);
return 0;
}
int settle(int key)
{
int i,rx,ry;
rx=snake.info[snake.length-1].x;ry=snake.info[snake.length-1].y;
for(i=snake.length-1;i>0;i--)
{
snake.info[i].x=snake.info[i-1].x;
snake.info[i].y=snake.info[i-1].y;
}
switch(key)
{
case 0x4800:
snake.info[i].y--;
break;
case 0x5000:
snake.info[i].y++;
break;
case 0x4b00:
snake.info[i].x--;
break;
case 0x4d00:
snake.info[i].x++;
break;
}
if(light[snake.info[i].x][snake.info[i].y]==1)
{
snake.length++;
snake.info[snake.length-1].x=rx;
snake.info[snake.length-1].y=ry;
Light(snake.info[i].x,snake.info[i].y,0);
return(1);
}
if(snake.info[i].x>58||snake.info[i].y>44||snake.info[i].x<0||snake.info[i].y<0||light[snake.info[i].x][snake.info[i].y]==2)
{
return(2);
}
Light(rx,ry,0);
return(0);
}
‘叁’ 求c语言控制面板贪吃蛇源代码
//******友情提示:如想速度快点,请改小_sleep(500)函数中参数*****
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <time.h>
const int H = 8; //地图的高
const int L = 16; //地图的长
char GameMap[H][L]; //游戏地图
int key; //按键保存
int sum = 1, over = 0; //蛇的长度, 游戏结束(自吃或碰墙)
int dx[4] = {0, 0, -1, 1}; //左、右、上、下的方向
int dy[4] = {-1, 1, 0, 0};
struct Snake //蛇的每个节点的数据类型
{
int x, y; //左边位置
int now; //保存当前节点的方向, 0,1,2,3分别为左右上下
}Snake[H*L];
const char Shead = '@'; //蛇头
const char Sbody = '#'; //蛇身
const char Sfood = '*'; //食物
const char Snode = '.'; //'.'在地图上标示为空
void Initial(); //地图的初始化
void Create_Food(); //在地图上随机产生食物
void Show(); //刷新显示地图
void Button(); //取出按键,并判断方向
void Move(); //蛇的移动
void Check_Border(); //检查蛇头是否越界
void Check_Head(int x, int y); //检查蛇头移动后的位置情况
int main()
{
Initial();
Show();
return 0;
}
void Initial() //地图的初始化
{
int i, j;
int hx, hy;
system("title 贪吃蛇"); //控制台的标题
memset(GameMap, '.', sizeof(GameMap)); //初始化地图全部为空'.'
system("cls");
srand(time(0)); //随机种子
hx = rand()%H; //产生蛇头
hy = rand()%L;
GameMap[hx][hy] = Shead;
Snake[0].x = hx; Snake[0].y = hy;
Snake[0].now = -1;
Create_Food(); //随机产生食物
for(i = 0; i < H; i++) //地图显示
{
for(j = 0; j < L; j++)
printf("%c", GameMap[i][j]);
printf("\n");
}
printf("\n小小C语言贪吃蛇\n");
printf("按任意方向键开始游戏\n");
getch(); //先接受一个按键,使蛇开始往该方向走
Button(); //取出按键,并判断方向
}
void Create_Food() //在地图上随机产生食物
{
int fx, fy;
while(1)
{
fx = rand()%H;
fy = rand()%L;
if(GameMap[fx][fy] == '.') //不能出现在蛇所占有的位置
{
GameMap[fx][fy] = Sfood;
break;
}
}
}
void Show() //刷新显示地图
{
int i, j;
while(1)
{
_sleep(500); //延迟半秒(1000为1s),即每半秒刷新一次地图
Button(); //先判断按键在移动
Move();
if(over) //自吃或碰墙即游戏结束
{
printf("\n**游戏结束**\n");
printf(" >_<\n");
getchar();
break;
}
system("cls"); //清空地图再显示刷新吼的地图
for(i = 0; i < H; i++)
{
for(j = 0; j < L; j++)
printf("%c", GameMap[i][j]);
printf("\n");
}
printf("\n小小C语言贪吃蛇\n");
printf("按任意方向键开始游戏\n");
}
}
void Button() //取出按键,并判断方向
{
if(kbhit() != 0) //检查当前是否有键盘输入,若有则返回一个非0值,否则返回0
{
while(kbhit() != 0) //可能存在多个按键,要全部取完,以最后一个为主
key = getch(); //将按键从控制台中取出并保存到key中
switch(key)
{ //左
case 75: Snake[0].now = 0;
break;
//右
case 77: Snake[0].now = 1;
break;
//上
case 72: Snake[0].now = 2;
break;
//下
case 80: Snake[0].now = 3;
break;
}
}
}
void Move() //蛇的移动
{
int i, x, y;
int t = sum; //保存当前蛇的长度
//记录当前蛇头的位置,并设置为空,蛇头先移动
x = Snake[0].x; y = Snake[0].y; GameMap[x][y] = '.';
Snake[0].x = Snake[0].x + dx[ Snake[0].now ];
Snake[0].y = Snake[0].y + dy[ Snake[0].now ];
Check_Border(); //蛇头是否越界
Check_Head(x, y); //蛇头移动后的位置情况,参数为: 蛇头的开始位置
if(sum == t) //未吃到食物即蛇身移动哦
for(i = 1; i < sum; i++) //要从蛇尾节点向前移动哦,前一个节点作为参照
{
if(i == 1) //尾节点设置为空再移动
GameMap[ Snake[i].x ][ Snake[i].y ] = '.';
if(i == sum-1) //为蛇头后面的蛇身节点,特殊处理
{
Snake[i].x = x;
Snake[i].y = y;
Snake[i].now = Snake[0].now;
}
else //其他蛇身即走到前一个蛇身位置
{
Snake[i].x = Snake[i+1].x;
Snake[i].y = Snake[i+1].y;
Snake[i].now = Snake[i+1].now;
}
GameMap[ Snake[i].x ][ Snake[i].y ] = '#'; //移动后要置为'#'蛇身
}
}
void Check_Border() //检查蛇头是否越界
{
if(Snake[0].x < 0 || Snake[0].x >= H
|| Snake[0].y < 0 || Snake[0].y >= L)
over = 1;
}
void Check_Head(int x, int y) //检查蛇头移动后的位置情况
{
if(GameMap[ Snake[0].x ][ Snake[0].y ] == '.') //为空
GameMap[ Snake[0].x ][ Snake[0].y ] = '@';
else
if(GameMap[ Snake[0].x ][ Snake[0].y ] == '*') //为食物
{
GameMap[ Snake[0].x ][ Snake[0].y ] = '@';
Snake[sum].x = x; //新增加的蛇身为蛇头后面的那个
Snake[sum].y = y;
Snake[sum].now = Snake[0].now;
GameMap[ Snake[sum].x ][ Snake[sum].y ] = '#';
sum++;
Create_Food(); //食物吃完了马上再产生一个食物
}
else
over = 1;
}
‘肆’ 有贪吃蛇的源代码 拜托了 !
你是用什么编的VC?还是turbo c?
我在网上找了下,发现了一个trubo c 的应该可以用,在VC上调试就一个错误,画图函数不支持,你试一试吧。
#define N 200
#include <graphics.h>
#include <stdlib.h>
#include <dos.h>
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define ESC 0x011b
int i,key;
int score=0;/*得分*/
int gamespeed=50000;/*游戏速度自己调整*/
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=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc");
cleardevice();
}
/*开始画面,左上角坐标为(50,40),右下角坐标为(610,460)的围墙*/
void DrawK(void)
{
/*setbkcolor(LIGHTGREEN);*/
setcolor(11);
setlinestyle(SOLID_LINE,0,THICK_WIDTH);/*设置线型*/
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)
{
randomize();/*随机数发生器*/
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;i>0;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;i<snake.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.x&&snake.y[0]==food.y)/*吃到食物以后*/
{
setcolor(0);/*把画面上的食物东西去掉*/
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(4);/*画出蛇*/
for(i=0;i<snake.node;i++)
rectangle(snake.x[i],snake.y[i],snake.x[i]+10,
snake.y[i]-10);
delay(gamespeed);
setcolor(0);/*用黑色去除蛇的的最后一节*/
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=bioskey(0);/*接收按键*/
if(key==ESC)/*按ESC键退出*/
break;
else
if(key==UP&&snake.direction!=4)
/*判断是否往相反的方向移动*/
snake.direction=3;
else
if(key==RIGHT&&snake.direction!=2)
snake.direction=1;
else
if(key==LEFT&&snake.direction!=1)
snake.direction=2;
else
if(key==DOWN&&snake.direction!=3)
snake.direction=4;
}/*endwhile(1)*/
}
/*游戏结束*/
void GameOver(void)
{
cleardevice();
PrScore();
setcolor(RED);
settextstyle(0,0,4);
outtextxy(200,200,"GAME OVER");
getch();
}
/*输出成绩*/
void PrScore(void)
{
char str[10];
setfillstyle(SOLID_FILL,YELLOW);
bar(50,15,220,35);
setcolor(6);
settextstyle(0,0,2);
sprintf(str,"score:%d",score);
outtextxy(55,20,str);
}
/*图形结束*/
void Close(void)
{
getch();
closegraph();
}
‘伍’ 求用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
接下面的
‘陆’ c语言贪吃蛇源代码怎么用
C语言贪吃蛇源代码必须经过相应的C/C++编译器编译成EXE文件后才能运行。
由于我们通常使用的操作系统是Windows系统,而在该系统下最长用的C/C++编译器是VC++编译器,目前在大专院校常用的版本还是VC++6.0
下面就以VC++6.0来说明编译过程:
1.在VC++6.0中通过“File”菜单下的 “Open”子菜单打开贪吃蛇代码
/*
C/C++贪吃蛇游戏,zjlj,2015.3.16
*/
#defineDEBUG0//当程序在调试阶段时DEBUG为1
#include<iostream>
#include<windows.h>
#include<time.h>
#include<conio.h>
usingnamespacestd;
voidreadini(FILE**fphead,int*score,char*argv[])//创建或打开一个和运行文件对应的ini文件,读取最高纪录
{
charfilename[200],*pfilename;
intflag=-1,i;
strcpy(filename,argv[0]);
for(i=0;filename[i]!='