c++编程小游戏代码
以下是贪吃蛇源代码:
#includeiostream.h
#includewindows.h
#includetime.h
#includestdlib.h
#includeconio.h
#define N 21
void gotoxy(int x,int y)//位置函数{
COORD pos;
pos.X=2*x;
pos.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
void color(int a)//颜色函数{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
void init(int apple[2])//初始化函数(初始化围墙、显示信息、苹果)
{
int i,j;//初始化围墙
int wall[N+2][N+2]={{0}};
for(i=1;i=N;i++)
{
for(j=1;j=N;j++)
wall[i][j]=1;
}
color(11);
for(i=0;iN+2;i++)
{
for(j=0;jN+2;j++)
{
if(wall[i][j])
cout"■";
else cout"□" ;
}
coutendl;
}
gotoxy(N+3,1);//显示信息
color(20);
cout"按 W S A D 移动方向"endl;
gotoxy(N+3,2);
color(20);
cout"按任意键暂停"endl;
gotoxy(N+3,3);
color(20);
cout"得分:"endl;
apple[0]=rand()%N+1;//苹果
apple[1]=rand()%N+1;
gotoxy(apple[0],apple[1]);
color(12);
cout"●"endl;
}
int main()
{
int i,j;
int** snake=NULL;
int apple[2];
int score=0;
int tail[2];
int len=3;
char ch='p';
srand((unsigned)time(NULL));
init(apple);
snake=(int**)realloc(snake,sizeof(int*)*len);
for(i=0;ilen;i++)
snake[i]=(int*)malloc(sizeof(int)*2);
for(i=0;ilen;i++)
{
snake[i][0]=N/2;
snake[i][1]=N/2+i;
gotoxy(snake[i][0],snake[i][1]);
color(14);
cout"★"endl;
}
while(1)//进入消息循环
{
tail[0]=snake[len-1][0];
tail[1]=snake[len-1][1];
gotoxy(tail[0],tail[1]);
color(11);
cout"■"endl;
for(i=len-1;i0;i--)
{
snake[i][0]=snake[i-1][0];
snake[i][1]=snake[i-1][1];
gotoxy(snake[i][0],snake[i][1]);
color(14);
cout"★"endl;
}
if(kbhit())
{
gotoxy(0,N+2);
ch=getche();
}
switch(ch)
{
case 'w':snake[0][1]--;break;
case 's':snake[0][1]++;break;
case 'a':snake[0][0]--;break;
case 'd':snake[0][0]++;break;
default: break;
}
gotoxy(snake[0][0],snake[0][1]);
color(14);
cout"★"endl;
Sleep(abs(200-0.5*score));
if(snake[0][0]==apple[0]snake[0][1]==apple[1])//吃掉苹果后蛇分数加1,蛇长加1
{
score++;
len++;
snake=(int**)realloc(snake,sizeof(int*)*len);
snake[len-1]=(int*)malloc(sizeof(int)*2);
apple[0]=rand()%N+1;
apple[1]=rand()%N+1;
gotoxy(apple[0],apple[1]);
color(12);
cout"●"endl;
gotoxy(N+5,3);
color(20);
coutscoreendl;
}
if(snake[0][1]==0||snake[0][1]==N||snake[0][0]==0||snake[0][0]==N)//撞到围墙后失败
{
gotoxy(N/2,N/2);
color(30);
cout"失败!!!"endl;
for(i=0;ilen;i++)
free(snake[i]);
Sleep(INFINITE);
exit(0);
}
}
return 0;
}
用C++编写的小游戏源代码
以下是贪吃蛇源代码:
#includeiostream.h
#includewindows.h
#includetime.h
#includestdlib.h
#includeconio.h
#define N 21
void gotoxy(int x,int y)//位置函数
{
COORD pos;
pos.X=2*x;
pos.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
void color(int a)//颜色函数
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
void init(int apple[2])//初始化函数(初始化围墙、显示信息、苹果)
{
int i,j;//初始化围墙
int wall[N+2][N+2]={{0}};
for(i=1;i=N;i++)
{
for(j=1;j=N;j++)
wall[i][j]=1;
}
color(11);
for(i=0;iN+2;i++)
{
for(j=0;jN+2;j++)
{
if(wall[i][j])
cout"■";
else cout"□" ;
}
coutendl;
}
gotoxy(N+3,1);//显示信息
color(20);
cout"按 W S A D 移动方向"endl;
gotoxy(N+3,2);
color(20);
cout"按任意键暂停"endl;
gotoxy(N+3,3);
color(20);
cout"得分:"endl;
apple[0]=rand()%N+1;//苹果
apple[1]=rand()%N+1;
gotoxy(apple[0],apple[1]);
color(12);
cout"●"endl;
}
int main()
{
int i,j;
int** snake=NULL;
int apple[2];
int score=0;
int tail[2];
int len=3;
char ch='p';
srand((unsigned)time(NULL));
init(apple);
snake=(int**)realloc(snake,sizeof(int*)*len);
for(i=0;ilen;i++)
snake[i]=(int*)malloc(sizeof(int)*2);
for(i=0;ilen;i++)
{
snake[i][0]=N/2;
snake[i][1]=N/2+i;
gotoxy(snake[i][0],snake[i][1]);
color(14);
cout"★"endl;
}
while(1)//进入消息循环
{
tail[0]=snake[len-1][0];
tail[1]=snake[len-1][1];
gotoxy(tail[0],tail[1]);
color(11);
cout"■"endl;
for(i=len-1;i0;i--)
{
snake[i][0]=snake[i-1][0];
snake[i][1]=snake[i-1][1];
gotoxy(snake[i][0],snake[i][1]);
color(14);
cout"★"endl;
}
if(kbhit())
{
gotoxy(0,N+2);
ch=getche();
}
switch(ch)
{
case 'w':snake[0][1]--;break;
case 's':snake[0][1]++;break;
case 'a':snake[0][0]--;break;
case 'd':snake[0][0]++;break;
default: break;
}
gotoxy(snake[0][0],snake[0][1]);
color(14);
cout"★"endl;
Sleep(abs(200-0.5*score));
if(snake[0][0]==apple[0]snake[0][1]==apple[1])//吃掉苹果后蛇分数加1,蛇长加1
{
score++;
len++;
snake=(int**)realloc(snake,sizeof(int*)*len);
snake[len-1]=(int*)malloc(sizeof(int)*2);
apple[0]=rand()%N+1;
apple[1]=rand()%N+1;
gotoxy(apple[0],apple[1]);
color(12);
cout"●"endl;
gotoxy(N+5,3);
color(20);
coutscoreendl;
}
if(snake[0][1]==0||snake[0][1]==N||snake[0][0]==0||snake[0][0]==N)//撞到围墙后失败
{
gotoxy(N/2,N/2);
color(30);
cout"失败!!!"endl;
for(i=0;ilen;i++)
free(snake[i]);
Sleep(INFINITE);
exit(0);
}
}
return 0;
}参考资料:从C++吧看来的
求生之路 游戏秘籍 代码大全
首先主界面 Options - Keyboard/Mouse - Allow Developers Console
(选项- 键盘/鼠标- 允许开启控制台)
然后游戏中按 ~ 开启控制台,输入 Sv_Cheats 1 回车确认开启秘籍模式。
最后游戏中按 ~ 开启控制台,输入以下秘籍回车确认可得到对应效果
god 1 无敌
noclip 穿墙(朝上可以飞天)
sv_infinite_ammo 1 无限弹药
give pistol 手枪
give pumpshotgun 霰弹枪
give autoshotgun 连散
give rifle M4/M16
give smg 乌兹
give hunting_rifle 狙击
give pipe_bomb 土制炸弹
give molotov 燃烧瓶
give oxygentank 氧气瓶
give propanetank 煤气罐
give gascan 油桶
give pain_pills 药瓶
give first_aid_kit 急救包
give health 满血100
give ammo 弹药
z_spawn hunter(创造Hunter)
z_spawn smoker(创造smoker)
z_spawn boomer (创造boomer)
z_spawn tank (创造tank)
z_spawn witch(创造witch)
ent_fire !self setteam 1 变目击者
ent_fire !self setteam 2 变幸存者
ent_fire !self setteam 3 被感染
changelevel 地图名 快速改变地图
give物品名 制造指定物品(武器等等)
nb_blind 1 让丧尸看不到,不过碰到丧尸的话他们还是会发现
nb_delete_all 清除所有被感染的头目及幸存者
z_add 增加一只丧尸在游戏中
z_spawn bossname 增加一只Boss怪物,bossname分别是:
tank, boomer, smoker, witch, hunter
cl_drawhud 0 消除抬头状态列HUD
r_drawviewmodel 0 使武器隐形
sv_lan 0 启用Internet网络;
在 Campaign Lobby 界面开启
map地图名 启动网络服务器;
在 Campaign Lobby界面开启
openserverbrowser 显示可用的服务器
OP命令全解释
rcon_password **** 获得OP权限 (*=OP密码)
rcon sv_password **** 锁上服务器
rcon sv_password '' 解锁服务器
rcon changelevel 地图名 立即换地图,所有玩家不退出
rcon map 地图名 立即立即换地图,所有玩家退出
z_difficulty Easy 游戏难度简单
z_difficulty Normal 正常
z_difficulty Hard 困难
z_difficulty Impossible 专家
下面列出游戏目前所有的游戏地图名称 方便大家使用(~直接复制粘贴-除掉.bsp):
毫不留情
map l4d_hospital01_apartment.bsp (1.公寓)
主视角截图map l4d_hospital02_subway.bsp (2:地铁)
map l4d_hospital03_sewers.bsp (3:下水道)
map l4d_hospital04_interior.bsp (4:医院)
map l4d_hospital05_rooftop.bsp (5:屋顶终局)
死亡丧钟
map l4d_smalltown01_caves.bsp (1:收费公路)
map l4d_smalltown02_drainage.bsp (2:水沟)
map l4d_smalltown03_ranchhouse.bsp (3:教堂)
map l4d_smalltown04_mainstreet.bsp (4:小镇)
map l4d_smalltown05_houseboat.bsp (5:船库终局)
静寂时分
map l4d_airport01_greenhouse.bsp (1:花房)
map l4d_airport02_offices.bsp (2:起重机)
map l4d_airport03_garage.bsp (3:建筑工地)
map l4d_airport04_terminal.bsp (4:终点)
map l4d_airport05_runway.bsp (5:跑道终局)
血腥收获
map l4d_farm01_hilltop.bsp (1:森林)
map l4d_farm02_traintunnel.bsp (2:隧道)
map l4d_farm03_bridge.bsp (3:桥)
map l4d_farm04_barn.bsp (4:火车站)
map l4d_farm05_cornfield.bsp (5:农舍终局)
生存灯塔
map l4d_sv_lighthouse.bap (生存灯塔地图)
模式变更激活命令
mp_gamemode survival 激活生存模式
mp_gamemode coop 合作模式的命令,然后选一幅合作地图就行了。
mp_gamemode versus 对抗模式地图,然后选一副对抗地图(对抗地图是 l4d_sv_xx
..
..
.
空间小游戏有哪些代码?
小游戏代码:想要的朋友拿去用 http://gamezone.qq.com/flash/2004/20040407tetris.swf http://gamezone.qq.com/flash/2004/200040108pao.swf http://gamezone.qq.com/flash/2003/20030602_meinvgengyi.swf http://gamezone.qq.com/flash/2003/20030915_majiang.swf http://gamezone.qq.com/flash/2003/12/02/zhaocha/zhaocha.swf http://gamezone.qq.com/flash/2003/20031115_taiqiu.swf http://gamezone.qq.com/flash/2004/20040325wuziqi.swf http://gamezone.qq.com/flash/2003/20031030_leidian.swf 9.泡泡堂 http://data1.flash.qq.com/0/0/45/660-1167751882.swf 10.跑跑卡丁车 http://2.xiaoyouxi.com/data/flash2/20060813/popkart.swf 11.街头霸王 http://flash.skyhits.com/UploadFile/2005/04/21/20050421133816569852.swf 12.超级小玛俐 http://222.189.228.50:8080/hhflsh/upload_swf/ftp/0922/19.swf 13.头文字D-赛车 /upimg/20070226/11H5021Q0402H113.swf 14.抢滩登陆战 /upimg/20070226/11H5021S1402RL8.swf 15.宠物连连看 http://218.64.245.14:8082/fs2009/upload_swf/ftp/1130/1.swf 16.拳皇2000 http://219.129.216.32:8080/fsa2008/upload_swf/20045201214822947.swf 17.秀 http://fl.e.zcom.com/games/20060516101001.swf 18.街机三国志FLASH极品版 http://fl.e.zcom.com/games/20051231040741.swf 19.拳皇百神双打版 http://fl.e.zcom.com/games/20060624095554.swf 20.蜡笔小新 http://fl.e.zcom.com/games/20060830111909.swf 21.热血高校 http://game-play.265cms.com/06/09-04/game.265.com.1944112799.swf 22.空间FLASH游戏反恐精英 http://game.xaonline.com/flash/xyx264.swf 23.炫亮劲舞团 http://www.qqfsy.com/yx/UploadFiles_8937/200702/20070220054730564.swf 24.生化危机3 http://game-play.265cms.com/06/09-03/game.265.com.5034598116.swf 25.电脑狂 http://fl.e.zcom.com/games/20060225054025.swf 26.吸引力大测试 http://fl.e.zcom.com/games/20060508132807.swf 27.人品计算器 http://game-play.265cms.com/06/09-03/game.265.com.6918592521.swf 28.水果泡泡 http://fl.e.zcom.com/games/20060812131512.swf 29.是男人就坚持30秒 http://play.pchome.net/data/flash/MM/pp_84657.swf 30.小猫吃鱼 http://fl.e.zcom.com/games/20060623093037.swf 31.乒乓球游戏 http://www.pconline.com.cn/pcedu/carton/games/0406/flash/040609qiu.swf
c语言小游戏代码
最基础的贪吃蛇的代码
#includestdio.h
#includewindows.h//基本型态定义。支援型态定义函数。使用者界面函数 图形装置界面函数。
#includeconio.h //用户通过按键盘产生的对应操作 (控制台)
#includestdlib.h
#includetime.h //日期和时间头文件
#define LEN 30
#define WID 25
int Snake[LEN][WID] = {0}; //数组的元素代表蛇的各个部位
char Sna_Hea_Dir = 'a';//记录蛇头的移动方向
int Sna_Hea_X, Sna_Hea_Y;//记录蛇头的位置
int Snake_Len = 3;//记录蛇的长度
clock_t Now_Time;//记录当前时间,以便自动移动
int Wait_Time ;//记录自动移动的时间间隔
int Eat_Apple = 1;//吃到苹果表示为1
int Level ;
int All_Score = -1;
int Apple_Num = -1;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); //获取标准输出的句柄 windows.h
//句柄 :标志应用程序中的不同对象和同类对象中的不同的实例 方便操控,
void gotoxy(int x, int y)//设置光标位置
{
COORD pos = {x,y}; //定义一个字符在控制台屏幕上的坐标POS
SetConsoleCursorPosition(hConsole, pos); //定位光标位置的函数windows.h
}
void Hide_Cursor()//隐藏光标 固定函数
{
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(hConsole, cursor_info);
}
void SetColor(int color)//设置颜色
{
SetConsoleTextAttribute(hConsole, color);
//是API设置字体颜色和背景色的函数 格式:SetConsoleTextAttribute(句柄,颜色);
}
void Print_Snake()//打印蛇头和蛇的脖子和蛇尾
{
int iy, ix, color;
for(iy = 0; iy WID; ++iy)
for(ix = 0; ix LEN; ++ix)
{
if(Snake[ix][iy] == 1)//蛇头
{
SetColor(0xf); //oxf代表分配的内存地址 setcolor:34行自定义设置颜色的函数
gotoxy(ix*2, iy);
printf("※");
}
if(Snake[ix][iy] == 2)//蛇的脖子
{
color = rand()%15 + 1; //rand()函数是产生随机数的一个随机函数。C语言里还有 srand()函数等。
//头文件:stdlib.h
if(color == 14)
color -= rand() % 13 + 1; //变色
SetColor(color);
gotoxy(ix*2, iy);
printf("■");
}
if(Snake[ix][iy] == Snake_Len)
{
gotoxy(ix*2, iy);
SetColor(0xe);
printf("≈");
}
}
}
void Clear_Snake()//擦除贪吃蛇
{
int iy, ix;
for(iy = 0; iy WID; ++iy)
for(ix = 0; ix LEN; ++ix)
{
gotoxy(ix*2, iy);
if(Snake[ix][iy] == Snake_Len)
printf(" ");
}
}
void Rand_Apple()//随机产生苹果
{
int ix, iy;
do
{
ix = rand() % LEN;
iy = rand() % WID;
}while(Snake[ix][iy]);
Snake[ix][iy] = -1;
gotoxy(ix*2, iy);
printf("⊙");
Eat_Apple = 0;
}
void Game_Over()//蛇死掉了
{
gotoxy(30, 10);
printf("Game Over");
Sleep(3000);
system("pause nul");
exit(0);
}
void Move_Snake()//让蛇动起来
{
int ix, iy;
for(ix = 0; ix LEN; ++ix)//先标记蛇头
for(iy = 0; iy WID; ++iy)
if(Snake[ix][iy] == 1)
{
switch(Sna_Hea_Dir)//根据新的蛇头方向标志蛇头
{
case 'w':
if(iy == 0)
Game_Over();
else
Sna_Hea_Y = iy - 1;
Sna_Hea_X = ix;
break;
case 's':
if(iy == (WID -1))
Game_Over();
else
Sna_Hea_Y = iy + 1;
Sna_Hea_X = ix;
break;
case 'a':
if(ix == 0)
Game_Over();
else
Sna_Hea_X = ix - 1;
Sna_Hea_Y = iy;
break;
case 'd':
if(ix == (LEN - 1))
Game_Over();
else
Sna_Hea_X = ix + 1;
Sna_Hea_Y = iy;
break;
default:
break;
}
}
if(Snake[Sna_Hea_X][Sna_Hea_Y]!=1Snake[Sna_Hea_X][Sna_Hea_Y]!=0Snake[Sna_Hea_X][Sna_Hea_Y]!=-1)
Game_Over();
if(Snake[Sna_Hea_X][Sna_Hea_Y] 0)//吃到苹果
{
++Snake_Len;
Eat_Apple = 1;
}
for(ix = 0; ix LEN; ++ix)//处理蛇尾
for(iy = 0; iy WID; ++iy)
{
if(Snake[ix][iy] 0)
{
if(Snake[ix][iy] != Snake_Len)
Snake[ix][iy] += 1;
else
Snake[ix][iy] = 0;
}
}
Snake[Sna_Hea_X][Sna_Hea_Y] = 1;//处理蛇头
}
void Get_Input()//控制蛇的移动方向
{
if(kbhit())
{
switch(getch())
{
case 87:
Sna_Hea_Dir = 'w';
break;
case 83:
Sna_Hea_Dir = 's';
break;
case 65:
Sna_Hea_Dir = 'a';
break;
case 68:
Sna_Hea_Dir = 'd';
break;
default:
break;
}
}
if(clock() - Now_Time = Wait_Time)//蛇到时间自动行走
{
Clear_Snake();
Move_Snake();
Print_Snake();
Now_Time = clock();
}
}
void Init()//初始化
{
system("title 贪吃毛毛蛇");
system("mode con: cols=80 lines=25");
Hide_Cursor();
gotoxy(61, 4);
printf("You Score:");
gotoxy(61, 6);
printf("You Level:");
gotoxy(61, 8);
printf("The Lenght:");
gotoxy(61, 10);
printf("The Speed:");
gotoxy(61, 12);
printf("Apple Num:");
int i;
for(i = 0; i Snake_Len; ++i)//生成蛇
Snake[10+i][15] = i+1;
int iy, ix;//打印蛇
for(iy = 0; iy WID; ++iy)
for(ix = 0; ix LEN; ++ix)
{
if(Snake[ix][iy])
{
SetColor(Snake[ix][iy]);
gotoxy(ix*2, iy);
printf("■");
}
}
}
void Pri_News()//打印信息
{
SetColor(0xe);
gotoxy(73,4);
All_Score += Level;
printf("%3d", All_Score);
gotoxy(73, 6);
printf("%3d", Level);
gotoxy(73, 8);
printf("%3d",Snake_Len);
gotoxy(73, 10);
printf("0.%3ds", Wait_Time/10);
gotoxy(73, 12);
printf("%d", Apple_Num);
}
void Lev_Sys()//等级系统
{
if(((Apple_Num-1) / 10) == Level)
{
++Level;
if(Wait_Time 50)
Wait_Time -= 50;
else
if(Wait_Time 10)
Wait_Time -= 10;
else
Wait_Time -= 1;
}
}
int main(void)
{
Init();
srand((unsigned)time(NULL));//设置随机数的种子
Now_Time = clock();
int speed1=1000,speed2,a;
printf("\n");
printf("请输入你想要的速度\n");
scanf("%d",speed2);
Level=1;
Wait_Time=speed1-speed2;
printf("请输入你想要的苹果数\n");
scanf("%d",a);
while(a--)
Rand_Apple();
while(1)
{
if(Eat_Apple)
{
++Apple_Num;
Rand_Apple();
Lev_Sys();
Pri_News();
}
Get_Input();
Sleep(10);
}
return 0;
}
小游戏的代码
#includegraphics.h
#includestdlib.h
#includedos.h
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define ESC 0x011b
int i,key;
int score=0;
int gamespeed=32000;
struct Food /*食物的结构体*/
{
int x; /*食物的横坐标*/
int y; /*食物的纵坐标*/
int yes; /*食物是否出现的变量*/
}food;
struct Snack /*蛇的结构体*/
{
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 PrScore(void); /*分数输出函数*/
DELAY(char ch)/*调节游戏速度*/
{
if(ch=='3')
{
delay(gamespeed); /*delay是延迟函数*/
delay(gamespeed);
}
else if(ch=='2')
{
delay(gamespeed);
}
}
Menu()/*游戏开始菜单*/
{
char ch;
printf("Please choose the gamespeed:\n");
printf("1-Fast 2-Normal 3-Slow\n");
printf("\nPlease Press The numbers..\n");
do
{ch=getch();}
while(ch!='1'ch!='2'ch!='3');
clrscr();
return(ch);
}
/*主函数*/
void main(void)
{
int ch;
ch=Menu();
Init();
DrawK();
GamePlay(ch);
Close();
}
void Init(void)
{
int gd=DETECT,gm;
initgraph(gd,gm,"c:\\tc");
cleardevice();
}
void DrawK(void)
{
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(char ch)
{
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) /*可以重复游戏*/
{
while(!kbhit()) /*在没有按键的情况下蛇自己移动*/
{
if(food.yes==1) /*需要食物*/
{
food.x=rand()%400+60;
food.y=rand()%350+60; /*使用rand函数随机产生食物坐标*/
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]; /*贪吃蛇的身体移动算法*/
}
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(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;isnake.node;i++)
rectangle(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]-10);
delay(gamespeed);
DELAY(ch);
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);
}
if(snake.life==1)
break;
key=bioskey(0); /*接受按键*/
if(key==ESC)
break;
else
if(key==UPsnake.direction!=4)/*判断是否改变方向*/
snake.direction=3;
else
if(key==RIGHTsnake.direction!=2)
snake.direction=1;
else
if(key==LEFTsnake.direction!=1)
snake.direction=2;
else
if(key==DOWNsnake.direction!=3)
snake.direction=4;
}
}
void GameOver(void)
{
cleardevice();
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,"scord:%d",score);
outtextxy(55,20,str);
}
void Close(void)
{
getch();
closegraph();
}
贪吃蛇
制作游戏代码
Dim game(3,2),i,j,result,num '定义二维数组 二维长度3, 一维长度2
Dim fso ,ws,f ,logFileWrite,logFileRead, fileStr,flag,flagFailNum, flagIndex ' 定义日志文件
set ws = CreateObject("wscript.shell")
Set fso = CreateObject("scripting.filesystemobject")
If fso.fileexists("C:\Users\18190\Desktop\vbs\测试\game_log.txt") Then
Else
Set f = fso.createtextfile("C:\Users\18190\Desktop\vbs\测试\game_log.txt",true)
'If fso.fileexists("C:\Users\18190\Desktop\vbs\测试\game_log.txt") Then
' Set logFileWrite = fso.OpenTextFile("C:\Users\18190\Desktop\vbs\测试\game_log.txt",8,true)
' logFileWrite.writeLine "数字猜猜猜小游戏-游戏日志"
' end if
End If
For i=0 To 2 ' 关卡赋值
For j=4 To 5
game(i,j-4)= i*3+j
Next
Next
'For i=0 To 2
' For j=0 To 1
'MsgBox "game("i","j"): " game(i,j)
'Next
'Next
' 选择操作
Dim cnum, failNum, sucFlag,t
failNum =0
sucFlag =0
Do While 1=1
If sucFlag=1 Then
Exit Do
End if
If failNum =3 Then
MsgBox "您有连续三次操作失误,系统将直接退出..."
Exit do
end if
cnum = InputBox( "欢迎来到 数字猜猜猜小游戏 请选择操作:"chr(10)" 1.注册 2.登录 3.退出","数字猜猜猜小游戏")
If cnum ="" Then
cnum = "-1"
End if
Select Case cnum
Case 1
' 账户注册
Dim juname, upwd,regStr
regStr = ""
do while regStr = ""
uname = InputBox("请输入注册账号: ","数字猜猜猜小游戏-注册")
upwd = InputBox("请输入注册密码: ","数字猜猜猜小游戏-注册")
If uname "" Then
If upwd "" Then
regStr = uname"#"upwd
Else
regStr =""
MsgBox "您输入的注册账号密码有误,请重新输入"
End If
Else
regStr =""
MsgBox "您输入的注册账号密码有误,请重新输入"
End If
If regStr "" Then
If fso.fileexists("C:\Users\18190\Desktop\vbs\测试\game_user.txt") Then
Set logFileWrite = fso.OpenTextFile("C:\Users\18190\Desktop\vbs\测试\game_user.txt",8,true)
t= Year(now)"-"month(now)"-"day(now)" " Hour(now)":"minute(now)":"second(now)
'logFileWrite.WriteBlankLines 1
logFileWrite.writeLine regStr " "t
logFileWrite.close
MsgBox "注册成功!"
exit do
else
Set f = fso.createtextfile("C:\Users\18190\Desktop\vbs\测试\game_user.txt",true)
If fso.fileexists("C:\Users\18190\Desktop\vbs\测试\game_user.txt") Then
Set logFileWrite = fso.OpenTextFile("C:\Users\18190\Desktop\vbs\测试\game_user.txt",8,true)
logFileWrite.writeLine "数字猜猜猜小游戏-用户数据"
logFileWrite.writeLine regStr
logFileWrite.close
MsgBox "注册成功!"
exit do
end if
End If
end if
loop
'wscript.sleep 1000
case 2