cmd最简单的小游戏代码(经典小游戏代码)
求一个用C语言编写的小游戏代码
#include graphics.h
#include conio.h
#include time.h
/////////////////////////////////////////////
// 定义常量、枚举量、结构体、全局变量
/////////////////////////////////////////////
#define WIDTH 10 // 游戏区宽度
#define HEIGHT 22 // 游戏区高度
#define SIZE 20 // 每个游戏区单位的实际像素
// 定义操作类型
enum CMD
{
CMD_ROTATE, // 方块旋转
CMD_LEFT, CMD_RIGHT, CMD_DOWN, // 方块左、右、下移动
CMD_SINK, // 方块沉底
CMD_QUIT // 退出游戏
};
// 定义绘制方块的方法
enum DRAW
{
SHOW, // 显示方块
HIDE, // 隐藏方块
FIX // 固定方块
};
// 定义七种俄罗斯方块
struct BLOCK
{
WORD dir[4]; // 方块的四个旋转状态
COLORREF color; // 方块的颜色
} g_Blocks[7] = { {0x0F00, 0x4444, 0x0F00, 0x4444, RED}, // I
{0x0660, 0x0660, 0x0660, 0x0660, BLUE}, // 口
{0x4460, 0x02E0, 0x0622, 0x0740, MAGENTA}, // L
{0x2260, 0x0E20, 0x0644, 0x0470, YELLOW}, // 反L
{0x0C60, 0x2640, 0x0C60, 0x2640, CYAN}, // Z
{0x0360, 0x4620, 0x0360, 0x4620, GREEN}, // 反Z
{0x4E00, 0x4C40, 0x0E40, 0x4640, BROWN}}; // T
// 定义当前方块、下一个方块的信息
struct BLOCKINFO
{
byte id; // 方块 ID
char x, y; // 方块在游戏区中的坐标
byte dir:2; // 方向
} g_CurBlock, g_NextBlock;
// 定义游戏区
BYTE g_World[WIDTH][HEIGHT] = {0};
/////////////////////////////////////////////
// 函数声明
/////////////////////////////////////////////
void Init(); // 初始化游戏
void Quit(); // 退出游戏
void NewGame(); // 开始新游戏
void GameOver(); // 结束游戏
CMD GetCmd(); // 获取控制命令
void DispatchCmd(CMD _cmd); // 分发控制命令
void NewBlock(); // 生成新的方块
bool CheckBlock(BLOCKINFO _block); // 检测指定方块是否可以放下
void DrawBlock(BLOCKINFO _block, DRAW _draw = SHOW); // 画方块
void OnRotate(); // 旋转方块
void OnLeft(); // 左移方块
void OnRight(); // 右移方块
void OnDown(); // 下移方块
void OnSink(); // 沉底方块
/////////////////////////////////////////////
// 函数定义
/////////////////////////////////////////////
// 主函数
void main()
{
Init();
CMD c;
while(true)
{
c = GetCmd();
DispatchCmd(c);
// 按退出时,显示对话框咨询用户是否退出
if (c == CMD_QUIT)
{
HWND wnd = GetHWnd();
if (MessageBox(wnd, _T("您要退出游戏吗?"), _T("提醒"), MB_OKCANCEL | MB_ICONQUESTION) == IDOK)
Quit();
}
}
}
// 初始化游戏
void Init()
{
initgraph(640, 480);
srand((unsigned)time(NULL));
// 显示操作说明
setfont(14, 0, _T("宋体"));
outtextxy(20, 330, _T("操作说明"));
outtextxy(20, 350, _T("上:旋转"));
outtextxy(20, 370, _T("左:左移"));
outtextxy(20, 390, _T("右:右移"));
outtextxy(20, 410, _T("下:下移"));
outtextxy(20, 430, _T("空格:沉底"));
outtextxy(20, 450, _T("ESC:退出"));
// 设置坐标原点
setorigin(220, 20);
// 绘制游戏区边界
rectangle(-1, -1, WIDTH * SIZE, HEIGHT * SIZE);
rectangle((WIDTH + 1) * SIZE - 1, -1, (WIDTH + 5) * SIZE, 4 * SIZE);
// 开始新游戏
NewGame();
}
// 退出游戏
void Quit()
{
closegraph();
exit(0);
}
// 开始新游戏
void NewGame()
{
// 清空游戏区
setfillstyle(BLACK);
bar(0, 0, WIDTH * SIZE - 1, HEIGHT * SIZE - 1);
ZeroMemory(g_World, WIDTH * HEIGHT);
// 生成下一个方块
g_NextBlock.id = rand() % 7;
g_NextBlock.dir = rand() % 4;
g_NextBlock.x = WIDTH + 1;
g_NextBlock.y = HEIGHT - 1;
// 获取新方块
NewBlock();
}
// 结束游戏
void GameOver()
{
HWND wnd = GetHWnd();
if (MessageBox(wnd, _T("游戏结束。\n您想重新来一局吗?"), _T("游戏结束"), MB_YESNO | MB_ICONQUESTION) == IDYES)
NewGame();
else
Quit();
}
// 获取控制命令
DWORD m_oldtime;
CMD GetCmd()
{
// 获取控制值
while(true)
{
// 如果超时,自动下落一格
DWORD newtime = GetTickCount();
if (newtime - m_oldtime = 500)
{
m_oldtime = newtime;
return CMD_DOWN;
}
// 如果有按键,返回按键对应的功能
if (kbhit())
{
switch(getch())
{
case 'w':
case 'W': return CMD_ROTATE;
case 'a':
case 'A': return CMD_LEFT;
case 'd':
case 'D': return CMD_RIGHT;
case 's':
case 'S': return CMD_DOWN;
case 27: return CMD_QUIT;
case ' ': return CMD_SINK;
case 0:
case 0xE0:
switch(getch())
{
case 72: return CMD_ROTATE;
case 75: return CMD_LEFT;
case 77: return CMD_RIGHT;
case 80: return CMD_DOWN;
}
}
}
// 延时 (降低 CPU 占用率)
Sleep(20);
}
}
// 分发控制命令
void DispatchCmd(CMD _cmd)
{
switch(_cmd)
{
case CMD_ROTATE: OnRotate(); break;
case CMD_LEFT: OnLeft(); break;
case CMD_RIGHT: OnRight(); break;
case CMD_DOWN: OnDown(); break;
case CMD_SINK: OnSink(); break;
case CMD_QUIT: break;
}
}
// 生成新的方块
void NewBlock()
{
g_CurBlock.id = g_NextBlock.id, g_NextBlock.id = rand() % 7;
g_CurBlock.dir = g_NextBlock.dir, g_NextBlock.dir = rand() % 4;
g_CurBlock.x = (WIDTH - 4) / 2;
g_CurBlock.y = HEIGHT + 2;
// 下移新方块直到有局部显示
WORD c = g_Blocks[g_CurBlock.id].dir[g_CurBlock.dir];
while((c 0xF) == 0)
{
g_CurBlock.y--;
c = 4;
}
// 绘制新方块
DrawBlock(g_CurBlock);
// 绘制下一个方块
setfillstyle(BLACK);
bar((WIDTH + 1) * SIZE, 0, (WIDTH + 5) * SIZE - 1, 4 * SIZE - 1);
DrawBlock(g_NextBlock);
// 设置计时器,用于判断自动下落
m_oldtime = GetTickCount();
}
// 画方块
void DrawBlock(BLOCKINFO _block, DRAW _draw)
{
WORD b = g_Blocks[_block.id].dir[_block.dir];
int x, y;
int color = BLACK;
switch(_draw)
{
case SHOW: color = g_Blocks[_block.id].color; break;
case HIDE: color = BLACK; break;
case FIX: color = g_Blocks[_block.id].color / 3; break;
}
setfillstyle(color);
for(int i=0; i16; i++)
{
if (b 0x8000)
{
x = _block.x + i % 4;
y = _block.y - i / 4;
if (y HEIGHT)
{
if (_draw != HIDE)
bar3d(x * SIZE + 2, (HEIGHT - y - 1) * SIZE + 2, (x + 1) * SIZE - 4, (HEIGHT - y) * SIZE - 4, 3, true);
else
bar(x * SIZE, (HEIGHT - y - 1) * SIZE, (x + 1) * SIZE - 1, (HEIGHT - y) * SIZE - 1);
}
}
b = 1;
}
}
// 检测指定方块是否可以放下
bool CheckBlock(BLOCKINFO _block)
{
WORD b = g_Blocks[_block.id].dir[_block.dir];
int x, y;
for(int i=0; i16; i++)
{
if (b 0x8000)
{
x = _block.x + i % 4;
y = _block.y - i / 4;
if ((x 0) || (x = WIDTH) || (y 0))
return false;
if ((y HEIGHT) (g_World[x][y]))
return false;
}
b = 1;
}
return true;
}
// 旋转方块
void OnRotate()
{
// 获取可以旋转的 x 偏移量
int dx;
BLOCKINFO tmp = g_CurBlock;
tmp.dir++; if (CheckBlock(tmp)) { dx = 0; goto rotate; }
tmp.x = g_CurBlock.x - 1; if (CheckBlock(tmp)) { dx = -1; goto rotate; }
tmp.x = g_CurBlock.x + 1; if (CheckBlock(tmp)) { dx = 1; goto rotate; }
tmp.x = g_CurBlock.x - 2; if (CheckBlock(tmp)) { dx = -2; goto rotate; }
tmp.x = g_CurBlock.x + 2; if (CheckBlock(tmp)) { dx = 2; goto rotate; }
return;
rotate:
// 旋转
DrawBlock(g_CurBlock, HIDE);
g_CurBlock.dir++;
g_CurBlock.x += dx;
DrawBlock(g_CurBlock);
}
// 左移方块
void OnLeft()
{
BLOCKINFO tmp = g_CurBlock;
tmp.x--;
if (CheckBlock(tmp))
{
DrawBlock(g_CurBlock, HIDE);
g_CurBlock.x--;
DrawBlock(g_CurBlock);
}
}
// 右移方块
void OnRight()
{
BLOCKINFO tmp = g_CurBlock;
tmp.x++;
if (CheckBlock(tmp))
{
DrawBlock(g_CurBlock, HIDE);
g_CurBlock.x++;
DrawBlock(g_CurBlock);
}
}
// 下移方块
void OnDown()
{
BLOCKINFO tmp = g_CurBlock;
tmp.y--;
if (CheckBlock(tmp))
{
DrawBlock(g_CurBlock, HIDE);
g_CurBlock.y--;
DrawBlock(g_CurBlock);
}
else
OnSink(); // 不可下移时,执行“沉底方块”操作
}
// 沉底方块
void OnSink()
{
int i, x, y;
// 连续下移方块
DrawBlock(g_CurBlock, HIDE);
BLOCKINFO tmp = g_CurBlock;
tmp.y--;
while (CheckBlock(tmp))
{
g_CurBlock.y--;
tmp.y--;
}
DrawBlock(g_CurBlock, FIX);
// 固定方块在游戏区
WORD b = g_Blocks[g_CurBlock.id].dir[g_CurBlock.dir];
for(i = 0; i 16; i++)
{
if (b 0x8000)
{
if (g_CurBlock.y - i / 4 = HEIGHT)
{ // 如果方块的固定位置超出高度,结束游戏
GameOver();
return;
}
else
g_World[g_CurBlock.x + i % 4][g_CurBlock.y - i / 4] = 1;
}
b = 1;
}
// 检查是否需要消掉行,并标记
int row[4] = {0};
bool bRow = false;
for(y = g_CurBlock.y; y = max(g_CurBlock.y - 3, 0); y--)
{
i = 0;
for(x = 0; x WIDTH; x++)
if (g_World[x][y] == 1)
i++;
if (i == WIDTH)
{
bRow = true;
row[g_CurBlock.y - y] = 1;
setfillstyle(WHITE, DIAGCROSS2_FILL);
bar(0, (HEIGHT - y - 1) * SIZE + SIZE / 2 - 2, WIDTH * SIZE - 1, (HEIGHT - y - 1) * SIZE + SIZE / 2 + 2);
}
}
if (bRow)
{
// 延时 200 毫秒
Sleep(200);
// 擦掉刚才标记的行
IMAGE img;
for(i = 0; i 4; i++)
{
if (row[i])
{
for(y = g_CurBlock.y - i + 1; y HEIGHT; y++)
for(x = 0; x WIDTH; x++)
{
g_World[x][y - 1] = g_World[x][y];
g_World[x][y] = 0;
}
getimage(img, 0, 0, WIDTH * SIZE, (HEIGHT - (g_CurBlock.y - i + 1)) * SIZE);
putimage(0, SIZE, img);
}
}
}
// 产生新方块
NewBlock();
}
求CMD指令一些好玩的东西。。
开始-运行-命令大全
1. gpedit.msc-----组策略
2. sndrec32-------录音机
3. Nslookup-------IP地址侦测器
4. explorer-------打开资源管理器
5. logoff---------注销命令
6. tsshutdn-------60秒倒计时关机命令
7. lusrmgr.msc----本机用户和组
8. services.msc---本地服务设置
9. oobe/msoobe /a----检查XP是否激活
10. notepad--------打开记事本
11. cleanmgr-------垃圾整理
12. net start messenger----开始信使服务
13. compmgmt.msc---计算机管理
14. net stop messenger-----停止信使服务
15. conf-----------启动netmeeting
16. dvdplay--------DVD播放器
17. charmap--------启动字符映射表
18. diskmgmt.msc---磁盘管理实用程序
19. calc-----------启动计算器
20. dfrg.msc-------磁盘碎片整理程序
21. chkdsk.exe-----Chkdsk磁盘检查
22. devmgmt.msc--- 设备管理器
23. regsvr32 /u *.dll----停止dll文件运行
24. drwtsn32------ 系统医生
25. rononce -p ----15秒关机
26. dxdiag---------检查DirectX信息
27. regedt32-------注册表编辑器
28. Msconfig.exe---系统配置实用程序
29. rsop.msc-------组策略结果集
30. mem.exe--------显示内存使用情况
31. regedit.exe----注册表
32. winchat--------XP自带局域网聊天
33. progman--------程序管理器
34. winmsd---------系统信息
35. perfmon.msc----计算机性能监测程序
36. winver---------检查Windows版本
37. sfc /scannow-----扫描错误并复原
38. taskmgr-----任务管理器(2000/xp/2003
39. winver---------检查Windows版本
40. wmimgmt.msc----打开windows管理体系结构(WMI)
41. wupdmgr--------windows更新程序
42. wscript--------windows脚本宿主设置
43. write----------写字板
44. winmsd---------系统信息
45. wiaacmgr-------扫描仪和照相机向导
46. winchat--------XP自带局域网聊天
47. mem.exe--------显示内存使用情况
48. Msconfig.exe---系统配置实用程序
49. mplayer2-------简易widnows media player
50. mspaint--------画图板
51. mstsc----------远程桌面连接
52. mplayer2-------媒体播放机
53. magnify--------放大镜实用程序
54. mmc------------打开控制台
55. mobsync--------同步命令
56. dxdiag---------检查DirectX信息
57. drwtsn32------ 系统医生
58. devmgmt.msc--- 设备管理器
59. dfrg.msc-------磁盘碎片整理程序
60. diskmgmt.msc---磁盘管理实用程序
61. dcomcnfg-------打开系统组件服务
62. ddeshare-------打开DDE共享设置
63. dvdplay--------DVD播放器
64. net stop messenger-----停止信使服务
65. net start messenger----开始信使服务
66. notepad--------打开记事本
67. nslookup-------网络管理的工具向导
68. ntbackup-------系统备份和还原
69. narrator-------屏幕“讲述人”
70. ntmsmgr.msc----移动存储管理器
71. ntmsoprq.msc---移动存储管理员操作请求
72. netstat -an----(TC)命令检查接口
73. syncapp--------创建一个公文包
74. sysedit--------系统配置编辑器
75. sigverif-------文件签名验证程序
76. sndrec32-------录音机
77. shrpubw--------创建共享文件夹
78. secpol.msc-----本地安全策略
79. syskey---------系统加密,一旦加密就不能解开,保护windows xp系统的双重密码
80. services.msc---本地服务设置
81. Sndvol32-------音量控制程序
82. sfc.exe--------系统文件检查器
83. sfc /scannow---windows文件保护
84. tsshutdn-------60秒倒计时关机命令
85. tourstart------xp简介(安装完成后出现的漫游xp程序)
86. taskmgr--------任务管理器
87. eventvwr-------事件查看器
88. eudcedit-------造字程序
89. explorer-------打开资源管理器
90. packager-------对象包装程序
91. perfmon.msc----计算机性能监测程序
92. progman--------程序管理器
93. regedit.exe----注册表
94. rsop.msc-------组策略结果集
95. regedt32-------注册表编辑器
96. rononce -p ----15秒关机
97. regsvr32 /u *.dll----停止dll文件运行
98. regsvr32 /u zipfldr.dll------取消ZIP支持
99. cmd.exe--------CMD命令提示符
100. chkdsk.exe-----Chkdsk磁盘检查
101. certmgr.msc----证书管理实用程序
102. calc-----------启动计算器
103. charmap--------启动字符映射表
104. cliconfg-------SQL SERVER 客户端网络实用程序
105. Clipbrd--------剪贴板查看器
106. conf-----------启动netmeeting
107. compmgmt.msc---计算机管理
108. cleanmgr-------垃圾整理
109. ciadv.msc------索引服务程序
110. osk------------打开屏幕键盘
111. odbcad32-------ODBC数据源管理器
112. oobe/msoobe /a----检查XP是否激活
113. lusrmgr.msc----本机用户和组
114. logoff---------注销命令
115. iexpress-------木马捆绑工具,系统自带
116. Nslookup-------IP地址侦测器
117. fsmgmt.msc-----共享文件夹管理器
118. utilman--------辅助工具管理器
119. gpedit.msc-----组策略
有没有可以帮我写c语言贪吃蛇的代码 cmd运行的
//code?by?wlfryq??71693456@qq.com
#include?stdio.h
#include?stdlib.h
#include?conio.h
#include?windows.h
#include?time.h
#include?direct.h
#include?stdbool.h
#define?W?80????//屏幕宽度?
#define?H?37????//屏幕高度?
#define?SNAKE_ALL_LENGTH?200???//蛇身最长为?
void?CALLBACK?TimerProc(
HWND?hwnd,???????
????????UINT?message,?????
????????UINT?idTimer,?????
????????DWORD?dwTime);
void?start();
struct?MYPOINT
{
int?x;
int?y;
}?s[SNAKE_ALL_LENGTH]?,?head,?end,?food;
int?max_count=0;?//历史最高分,如果countmax_count,?则max_count=count
int?old_max_count=0;??//历史最高分,不会变动,?用于死亡时判断max_count是否大于old_max_count,如果大于,则写入文件?
int?count=0;??//得分?
int?len=20;???//当前蛇身长度?
int?direct=0;?//方向:?0-向右,?1-向下,?2-向左,?3-向上
int?speed=200;??//速度:毫秒?
bool?isfood=false;?//食物是否存在
int?timerID;
bool?stop=false;???//暂停?
char*?ini_path;????//数据文件绝对路径?
void?setxy(int?x,?int?y)??//设置CMD窗口光标位置
{
???COORD?coord?=?{x,?y};
???SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),?coord);
}
void?hide_cursor()?//隐藏CMD窗口光标
{
CONSOLE_CURSOR_INFO?cci;
cci.bVisible?=?FALSE;
cci.dwSize?=?sizeof(cci);
HANDLE?handle?=?GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorInfo(handle,?cci);
}
void?set_food()??????//设置食物坐标?
{
if(isfood==true)
{
return;
}
int?x,y,i;
bool?flag=false;
while(1)
{
flag=false;
x=rand()%(W-14)+6;
y=rand()%(H-12)+6;
for(i=0;ilen;i++)??????//判断食物是否落在蛇身上?
{
if(s[i].x==x??s[i].y==y)
{
flag=true;
break;
}
}
if(flag==true)
{
continue;
}
else
{
food.x=x;
food.y=y;
break;
}
}
setxy(food.x,food.y);
printf("*");
isfood=true;
}
void?check_board()????//检测蛇身是否越界和相交?
{
int?i;
if(s[0].x=W-3?||?s[0].x=2?||?s[0].y=H-1?||?s[0].y=2)
{
setxy(W/2-5,0);
printf("game?over\n");
stop=true;
if(old_max_countmax_count)
{
char?t[5]={'\0'};
sprintf(t,"%d",max_count);
WritePrivateProfileString("MAX_COUNT","max_count",t,ini_path);
}
}
for(i=1;ilen;i++)
{
if(s[i].x==s[0].x??s[i].y==s[0].y)
{
setxy(W/2-5,0);
printf("game?over\n");
stop=true;
if(old_max_countmax_count)
{
char?t[5]={'\0'};
sprintf(t,"%d",max_count);
WritePrivateProfileString("MAX_COUNT","max_count",t,ini_path);
}
break;
}
}
if(stop==true)
{
KillTimer(NULL,timerID);
int?c;
while(1)
{
fflush(stdin);
c=_getch();
if(c=='n'?||?c=='N')
{
start();
}
else?if(c=='q'?||?c=='Q')
{
exit(0);
}
else?continue;
}
}
}
void?printf_body(bool?is_first)??//打印蛇身?
{
if(is_first==true)?????//如果是第一次打印蛇身?
{
int?i;
for(i=0;ilen;i++)
{
setxy(s[i].x,s[i].y);
printf("O");
}
}
else??????????????????//如果不是第一次打印蛇身?
{
setxy(end.x,end.y);
printf("?");
setxy(s[0].x,s[0].y);
printf("O");
}
if(food.x==s[0].x??food.y==s[0].y)??//如果吃到食物?
{
count++;
isfood=false;?????????????????????//重置食物?
set_food();
len++;
KillTimer(NULL,timerID);
if(speed100)?speed-=10;
else?if(speed50)?speed-=5;
else?if(speed30)?speed-=2;
else?if(speed16)?speed-=1;
else?;
setxy(0,0);
if(max_countcount) max_count=count;
printf("??speed?:?%d?ms?????score?:?%d???????????????????????????????????best?score:%d??",speed,count,max_count);
timerID=SetTimer(NULL,001,speed,TimerProc);
}
}
void?change_body_pos(int?x,?int?y)???//改变蛇身的坐标数据?
{
end.x=s[len-1].x;
end.y=s[len-1].y;
int?i;
for(i=len-1;i0;i--)
{
s[i].x=s[i-1].x;
s[i].y=s[i-1].y;
}
s[0].x=x;
s[0].y=y;
}
void?CALLBACK?TimerProc(
HWND?hwnd,???????
????????UINT?message,?????
????????UINT?idTimer,?????
????????DWORD?dwTime)
{
switch(direct)
{
case?0:
head.x++;
change_body_pos(head.x,head.y);
printf_body(false);
check_board();
break;
case?1:
head.y++;
change_body_pos(head.x,head.y);
printf_body(false);
check_board();
break;
case?2:
head.x--;
change_body_pos(head.x,head.y);
printf_body(false);
check_board();
break;
case?3:
head.y--;
change_body_pos(head.x,head.y);
printf_body(false);
check_board();
break;
}
}
void?start()
{
int?i;
KillTimer(NULL,timerID);
count=0;??//得分?
len=20;???//当前蛇身长度?
direct=0;?//方向:?0-向右,?1-向下,?2-向左,?3-向上
speed=200;??//速度:毫秒?
isfood=false;?//食物是否存在
stop=false;???//停止?
system("cls");
setxy(1,4);
printf("┌─────────────────────────────────────┐\n");
for(i=0;i33;i++)
{
printf("?│??????????????????????????????????????????????????????????????????????????│\n");
}
printf("?└─────────────────────────────────────┘");
head.x=len-1+5;
head.y=H/2;
for(i=0;ilen;i++)
{
s[i].x=head.x-i;
s[i].y=head.y;
}
setxy(0,0);
printf("??speed?:?%d:ms?????score?:?%d???????????????????????????????????best?score:%d??",speed,count,max_count);
printf_body(true);
set_food();
timerID=SetTimer(NULL,001,speed,TimerProc);
int?c;
MSG?msg;
while(GetMessage(msg,NULL,0,0))
{
if(stop==true) break;
if(_kbhit())???//如果按下的是方向键或功能键,?_getch()要调用两次,第一次返回0XE0或0?
{
fflush(stdin);
c=_getch();???//上:?72?下:80??左:75??右:77?
if(c==0XE0?||?c==0)
{
c=_getch();
if(c==72??direct!=1??direct!=3)
{
direct=3;
}
else?if(c==80??direct!=1??direct!=3)
{
direct=1;
}
else?if(c==75??direct!=0??direct!=2)
{
direct=2;
}
else?if(c==77??direct!=0??direct!=2)
{
direct=0;
}
}
else?if(c=='?')
{
setxy(W/2-10,0);
system("pause");
setxy(W/2-10,0);
printf("????????????????????");
}
}
if(msg.message==WM_TIMER)
{
DispatchMessage(msg);
}
}
}
int?main()
{
ini_path=(char*)malloc(sizeof(char)*50);
srand((unsigned)time(0));
getcwd(ini_path,50);//取得当前程序绝对路径
ini_path=strcat(ini_path,"snake.dat");
max_count=GetPrivateProfileInt("MAX_COUNT","max_count",0,ini_path);
old_max_count=max_count;
char?cmd[50];
sprintf(cmd,"mode?con?cols=%d?lines=%d\0",W,H);
system(cmd);//改变CMD窗口大小
hide_cursor();
start();
return?0;
}
编写一个在cmd环境下运行的Java小游戏
24点。。。。
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class calculate24 extends JFrame{
private javax.swing.JPanel jContentPane = null;
private JLabel jLabel = null;
private JLabel jLabel1 = null;
private JTextField jTextField = null;
private JTextField jTextField1 = null;
private JTextArea jTextArea = null;
private JLabel jLabel2 = null;
private JButton jButton = null;
private JScrollPane jScrollPane = null;
private JButton jButton1 = null;
private JButton jButton2 = null;
private JButton jButton3 = null;
private JButton jButton4 = null;
private JButton jButton5 = null;
private JButton jButton6 = null;
private JButton jButton7 = null;
private JButton jButton8 = null;
private JButton jButton9 = null;
private JButton jButton10 = null;
/**
* This is the default constructor
*/
public calculate24() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
this.setBounds(200, 200, 565, 452);
this.setContentPane(getJContentPane());
this.setTitle("24点");
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private javax.swing.JPanel getJContentPane() {
if (jContentPane == null) {
jLabel2 = new JLabel();
jLabel1 = new JLabel();
jLabel = new JLabel();
jContentPane = new javax.swing.JPanel();
jContentPane.setLayout(null);
jLabel.setBounds(66, 52, 150, 45);
jLabel.setText("please unter four number");
jLabel1.setBounds(253, 52, 282, 45);
jLabel1.setText("please unter how many result do you want to get");
jLabel2.setBounds(354, 201, 70, 36);
jLabel2.setText("result");
jContentPane.add(getJButton(), null);
jContentPane.add(jLabel, null);
jContentPane.add(jLabel1, null);
jContentPane.add(getJTextField(), null);
jContentPane.add(getJTextField1(), null);
jContentPane.add(jLabel2, null);
jContentPane.add(getJScrollPane(), null);
jContentPane.add(getJButton1(), null);
jContentPane.add(getJButton2(), null);
jContentPane.add(getJButton3(), null);
jContentPane.add(getJButton4(), null);
jContentPane.add(getJButton5(), null);
jContentPane.add(getJButton6(), null);
jContentPane.add(getJButton7(), null);
jContentPane.add(getJButton8(), null);
jContentPane.add(getJButton9(), null);
jContentPane.add(getJButton10(), null);
}
return jContentPane;
}
/**
* This method initializes jTextField
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField() {
if (jTextField == null) {
jTextField = new JTextField();
jTextField.setBounds(67, 84, 149, 41);
jTextField.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent e) {
jTextField.select(0,jTextField.getText().length());
}
});
}
return jTextField;
}
/**
* This method initializes jTextField1
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField1() {
if (jTextField1 == null) {
jTextField1 = new JTextField();
jTextField1.setBounds(293, 81, 161, 41);
jTextField1.setNextFocusableComponent(jButton);
}
return jTextField1;
}
/**
* This method initializes jTextArea
*
* @return javax.swing.JTextArea
*/
private JTextArea getJTextArea() {
if (jTextArea == null) {
jTextArea = new JTextArea();
jTextArea.setTabSize(8);
}
return jTextArea;
}
public static String bbb(List list1, List list2) {
float result = 0;
for (int i = list1.size(); i 0; i-- ) {
if (list1.contains("*")) {
int j = list1.indexOf("*");
result = Float.parseFloat((String)list2.get(j))
* Float.parseFloat((String)list2.get(j + 1));
list1.remove(j);
list2.remove(j);
list2.remove(j);
list2.add(j, String.valueOf(result));
} else if (list1.contains("/")) {
int j = list1.indexOf("/");
result = Float.parseFloat((String)list2.get(j))
/ Float.parseFloat((String)list2.get(j + 1));
list1.remove(j);
list2.remove(j);
list2.remove(j);
list2.add(j, String.valueOf(result));
} else if (list1.contains("+")) {
int j = list1.indexOf("+");
result = Float.parseFloat((String)list2.get(j))
+ Float.parseFloat((String)list2.get(j + 1));
list1.remove(j);
list2.remove(j);
list2.remove(j);
list2.add(j, String.valueOf(result));
} else if (list1.contains("-")) {
int j = list1.indexOf("-");
result = Float.parseFloat((String)list2.get(j))
- Float.parseFloat((String)list2.get(j + 1));
list1.remove(j);
list2.remove(j);
list2.remove(j);
list2.add(j, String.valueOf(result));
}
}
return (String)list2.get(0);
}
private static void bbb(String str, String sPrint, List list) {
if (!"".equals(str.trim()) ? false : list.add(sPrint))
;
for (int i = 0; i str.length() ( !"".equals(str.trim()) ); i++ )
if (str.charAt(i) != ' ')
bbb(str.replace(str.charAt(i), ' '), sPrint + str.charAt(i),
list);
}
private static List bbb(String str, List list) {
List result = new ArrayList();
String a1 = str.substring(0, 1);
String b1 = str.substring(1, 2);
String c1 = str.substring(2, 3);
String d1 = str.substring(3, 4);
String[] a11 = new String[] { a1, b1, c1, d1 };
for (int i = 0; i list.size(); i++ ) {
String temp = (String)list.get(i);
int a = Integer.parseInt(temp.substring(0, 1));
int b = Integer.parseInt(temp.substring(1, 2));
int c = Integer.parseInt(temp.substring(2, 3));
int d = Integer.parseInt(temp.substring(3, 4));
String tempStr = a11[a] + a11[b] + a11[c] + a11[d];
if(!result.contains(tempStr)){
result.add(tempStr);
}
}
return result;
}
public List test(String param, int x) {
int y = 0;
List result = new ArrayList();
List a11 = new ArrayList();
calculate24.bbb("0123", "", a11);
List a1 = calculate24.bbb(param, a11);
for (int m = 0; m a1.size(); m++ ) {
String param1 = (String)a1.get(m);
int[] a = new int[] { Integer.parseInt(param1.substring(0, 1)),
Integer.parseInt(param1.substring(1, 2)),
Integer.parseInt(param1.substring(2, 3)),
Integer.parseInt(param1.substring(3, 4)) };
String[] e = new String[] { "*", "/", "+", "-" };
for (int i = 0; i 4; i++ ) {
for (int j = 0; j 4; j++ ) {
for (int k = 0; k 4; k++ ) {
List aa = new ArrayList();
aa.add(String.valueOf(a[0]));
aa.add(String.valueOf(a[1]));
aa.add(String.valueOf(a[2]));
aa.add(String.valueOf(a[3]));
List bb = new ArrayList();
bb.add(e[i]);
bb.add(e[j]);
bb.add(e[k]);
String s = a[0] + e[i] + a[1] + e[j] + a[2] + e[k]
+ a[3];
String tempS = s;
s = calculate24.bbb(bb, aa);
if (Float.parseFloat(s) == 24) {
y++ ;
result.add(tempS + "=24");
if (y == x) {
return result;
}
}
List temp1 = new ArrayList();
List temp2 = new ArrayList();
temp1.add(String.valueOf(a[0]));
temp1.add(String.valueOf(a[1]));
temp2.add(e[i]);
String temp = calculate24.bbb(temp2, temp1);
aa.clear();
aa.add(temp);
aa.add(String.valueOf(a[2]));
aa.add(String.valueOf(a[3]));
bb.clear();
bb.add(e[j]);
bb.add(e[k]);
s = "(" + a[0] + e[i] + a[1] + ")" + e[j] + a[2] + e[k]
+ a[3];
tempS = s;
s = calculate24.bbb(bb, aa);
if (Float.parseFloat(s) == 24) {
y++ ;
result.add(tempS + "=24");
if (y == x) {
return result;
}
}
temp1.clear();
temp2.clear();
temp1.add(String.valueOf(a[1]));
temp1.add(String.valueOf(a[2]));
temp2.add(e[j]);
temp = calculate24.bbb(temp2, temp1);
aa.clear();
aa.add(String.valueOf(a[0]));
aa.add(temp);
aa.add(String.valueOf(a[3]));
bb.clear();
bb.add(e[i]);
bb.add(e[k]);
s = a[0] + e[i] + "(" + a[1] + e[j] + a[2] + ")" + e[k]
+ a[3];
tempS = s;
s = calculate24.bbb(bb, aa);
if (Float.parseFloat(s) == 24) {
y++ ;
result.add(tempS + "=24");
if (y == x) {
return result;
}
}
temp1.clear();
temp2.clear();
temp1.add(String.valueOf(a[2]));
temp1.add(String.valueOf(a[3]));
temp2.add(e[k]);
temp = calculate24.bbb(temp2, temp1);
aa.clear();
aa.add(String.valueOf(a[0]));
aa.add(String.valueOf(a[1]));
aa.add(temp);
bb.clear();
bb.add(e[i]);
bb.add(e[j]);
s = a[0] + e[i] + a[1] + e[j] + "(" + a[2] + e[k]
+ a[3] + ")";
tempS = s;
s = calculate24.bbb(bb, aa);
if (Float.parseFloat(s) == 24) {
y++ ;
result.add(tempS + "=24");
if (y == x) {
return result;
}
}
temp1.clear();
temp2.clear();
temp1.add(String.valueOf(a[0]));
temp1.add(String.valueOf(a[1]));
temp1.add(String.valueOf(a[2]));
temp2.add(e[i]);
temp2.add(e[j]);
temp = calculate24.bbb(temp2, temp1);
aa.clear();
aa.add(temp);
aa.add(String.valueOf(a[3]));
bb.clear();
bb.add(e[k]);
s = "(" + a[0] + e[i] + a[1] + e[j] + a[2] + ")" + e[k]
+ a[3];
tempS = s;
s = calculate24.bbb(bb, aa);
if (Float.parseFloat(s) == 24) {
y++ ;
result.add(tempS + "=24");
if (y == x) {
return result;
}
}
temp1.clear();
temp2.clear();
temp1.add(String.valueOf(a[1]));
temp1.add(String.valueOf(a[2]));
temp1.add(String.valueOf(a[3]));
temp2.add(e[j]);
temp2.add(e[k]);
temp = calculate24.bbb(temp2, temp1);
aa.clear();
aa.add(String.valueOf(a[0]));
aa.add(temp);
bb.clear();
bb.add(e[i]);
s = a[0] + e[i] + "(" + a[1] + e[j] + a[2] + e[k]
+ a[3] + ")";
tempS = s;
s = calculate24.bbb(bb, aa);
if (Float.parseFloat(s) == 24) {
y++ ;
result.add(tempS + "=24");
if (y == x) {
return result;
}
}
temp1.clear();
temp2.clear();
temp1.add(String.valueOf(a[0]));
temp1.add(String.valueOf(a[1]));
temp2.add(e[i]);
temp = calculate24.bbb(temp2, temp1);
List temp3 = new ArrayList();
List temp4 = new ArrayList();
temp3.add(String.valueOf(a[2]));
temp3.add(String.valueOf(a[3]));
temp4.add(e[k]);
String temp11 = calculate24.bbb(temp4, temp3);
aa.clear();
aa.add(temp);
aa.add(temp11);
bb.clear();
bb.add(e[j]);
s = "(" + a[0] + e[i] + a[1] + ")" + e[j] + "(" + a[2]
+ e[k] + a[3] + ")";
tempS = s;
s = calculate24.bbb(bb, aa);
if (Float.parseFloat(s) == 24) {
y++ ;
result.add(tempS + "=24");
if (y == x) {
return result;
}
}
}
}
}
}
return result;
}
public static boolean check(String param1) {
Pattern pattern = Pattern.compile("[0-9]{4}");
Matcher matcher = pattern.matcher((CharSequence)param1);
boolean result = matcher.matches();
if (result == false) {
JOptionPane.showMessageDialog(null, "please enter correct number");
return false;
} else {
return true;
}
}
public static boolean check1(String param2) {
if(param2 == null){
JOptionPane.showMessageDialog(null, "please enter correct number");
return false;
}
Pattern pattern = Pattern.compile("[0-9]{0,99}");
Matcher matcher = pattern.matcher((CharSequence)param2);
boolean result = matcher.matches();
if (result == false) {
JOptionPane.showMessageDialog(null, "please enter correct number");
return false;
} else {
return true;
}
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(81, 275, 110, 54);
jButton.setText("calculate");
jButton.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent e) {
if(e.getKeyCode()==10){
if (check(jTextField.getText())
check1(jTextField1.getText())) {
if(!jTextField1.getText().equals("0")){
List b = test(jTextField.getText(), Integer
.parseInt(jTextField1.getText()));
String temp = "";
for (int i = 0; i b.size(); i++ ) {
temp = temp + b.get(i) + "\n";
}
if (b.size() == 0) {
jTextArea.setText("NO RESULT");
} else {
jTextArea.setText(temp);
}
}else{
JOptionPane.showMessageDialog(null, "please enter correct number");
}
}
}
}
});
jButton.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
if (check(jTextField.getText())
check1(jTextField1.getText())) {
if(!jTextField1.getText().equals("0")){
List b = test(jTextField.getText(), Integer
.parseInt(jTextField1.getText()));
String temp = "";
for (int i = 0; i b.size(); i++ ) {
temp = temp + b.get(i) + "\n";
}
if (b.size() == 0) {
jTextArea.setText("NO RESULT");
} else {
jTextArea.setText(temp);
}
}else{
JOptionPane.showMessageDialog(null, "please enter correct number");
}
}
}
});
}
return jButton;
}
/**
* This method initializes jScrollPane
*
* @return javax.swing.JScrollPane
*/
private JScrollPane getJScrollPane() {
if (jScrollPane == null) {
jScrollPane = new JScrollPane();
jScrollPane.setBounds(267, 238, 216, 124);
jScrollPane.setViewportView(getJTextArea());
}
return jScrollPane;
}
/**
* This method initializes jButton1
*
* @return javax.swing.JButton
*/
private JButton getJButton1() {
if (jButton1 == null) {
jButton1 = new JButton();
jButton1.setBounds(40, 148, 42, 28);
jButton1.setText("1");
jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
jTextField.setText(jTextField.getText()+"1");
}
});
}
return jButton1;
}
/**
* This method initializes jButton2
*
* @return javax.swing.JButton
*/
private JButton getJButton2() {
if (jButton2 == null) {
jButton2 = new JButton();
jButton2.setBounds(90, 148, 42, 28);
jButton2.setText("2");
jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
jTextField.setText(jTextField.getText()+"2");
}
});
}
return jButton2;
}
/**
* This method initializes jButton3
*
* @return javax.swing.JButton
*/
private JButton getJButton3() {
if (jButton3 == null) {
jButton3 = new JButton();
jButton3.setBounds(140, 148, 42, 28);
jButton3.setText("3");
jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
jTextField.setText(jTextField.getText()+"3");
}
});
}
return jButton3;
}
/**
* This method initializes jButton4
*
* @return javax.swing.JButton
*/
private JButton getJButton4() {
if (jButton4 == null) {
jButton4 = new JButton();
jButton4.setBounds(190, 148, 42, 28);
jButton4.setText("4");
jButton4.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
jTextField.setText(jTextField.getText()+"4");
}
});
}
return jButton4;
}
/**
* This method initializes jButton5
*
* @return javax.swing.JButton
*/
private JButton getJButton5() {
if (jButton5 == null) {
jButton5 = new JButton();
jButton5.setBounds(240, 148, 42, 28);
jButton5.setText("5");
jButton5.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
jTextField.setText(jTextField.getText()+"5");
}
});
}
return jButton5;
}
/**
* This method initializes jButton6
*
* @return javax.swing.JButton
*/
private JButton getJButton6() {
if (jButton6 == null) {
jButton6 = new JButton();
jButton6.setBounds(40, 188, 42, 28);
jButton6.setText("6");
jButton6.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
jTextField.setText(jTextField.getText()+"6");
}
});
}
return jButton6;
}
/**
* This method initializes jButton7
*
* @return javax.swing.JButton
*/
private JButton getJButton7() {
if (jButton7 == null) {
jButton7 = new JButton();
jButton7.setBounds(90, 188, 42, 28);
jButton7.setText("7");
jButton7.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
jTextField.setText(jTextField.getText()+"7");
}
});
}
return jButton7;
}
/**
* This method initializes jButton8
*
* @return javax.swing.JButton
*/
private JButton getJButton8() {
if (jButton8 == null) {
jButton8 = new JButton();
jButton8.setBounds(140, 188, 42, 28);
jButton8.setText("8");
jButton8.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
jTextField.setText(jTextField.getText()+"8");
}
});
}
return jButton8;
}
/**
* This method initializes jButton9
*
* @return javax.swing.JButton
*/
private JButton getJButton9() {
if (jButton9 == null) {
jButton9 = new JButton();
jButton9.setBounds(190, 188, 42, 28);
jButton9.setText("9");
jButton9.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
jTextField.setText(jTextField.getText()+"9");
}
});
}
return jButton9;
}
/**
* This method initializes jButton10
*
* @return javax.swing.JButton
*/
private JButton getJButton10() {
if (jButton10 == null) {
jButton10 = new JButton();
jButton10.setBounds(240, 188, 42, 28);
jButton10.setText("0");
jButton10.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
jTextField.setText(jTextField.getText()+"0");
}
});
}
return jButton10;
}
/**
* Launches this application
*/
public static void main(String[] args) {
calculate24 application = new calculate24();
application.show();
}
} // @jve:decl-index=0:visual-constraint="10,10"
抖音最火电脑cmd代码
1、新建一个文本文件,命名为表白.txt 输入代码
(以下是示例)
msgbox("做我女朋友好吗?")
msgbox("房产证写你名字...")
msgbox("保大...")
msgbox("我妈会游泳..")
x=msgbox("做我女朋友好不好",VbOkCancel)
if x=VbOk then
msgbox("爱你,么么哒")
elseif x=VbCancel then
msgbox("哼,再见")
end if
2、鼠标左键点击文本左上角文件,再点击另存为。
3、点击后会弹出以下窗口,文件名改成:表白.vbs,保存类型改为:所有文件,然后点击确定。
4、点击确定后,完成制作,电脑桌面上会显示一个vbs文件。鼠标左键双击单开刚才保存vbs文件,就会像抖音里面的一样了。