c语言小组课设,小游戏集合

保留了推箱子,贪吃蛇,五子棋,2048等

主菜单

#include #include #include void Game_Snake();void Game_2048();void Game_PushBoxs();void Game_Wuziqi();int main(){ printf("\n\n\n"); printf("*************小游戏合集**************\n"); printf("*************************************\n"); printf("**********输入1----2048**************\n"); printf("**********输入2----贪吃蛇************\n"); printf("**********输入3----推箱子************\n"); printf("**********输入4----双人五子棋********\n"); printf("**********输入0----退出游戏**********\n"); printf("*************************************\n"); printf("请输入游戏编号:"); int a,b; begin: scanf("%d",&a); switch (a){ case 0: printf("已退出游戏!\n"); printf("已退出游戏!\n"); printf("已退出游戏!\n"); Sleep(10000); break; case 1: Game_2048();break; case 2: Game_Snake();break; case 3: Game_PushBoxs();break; case 4: Game_Wuziqi();break; default: printf("输入有误,请重新输入:"); goto begin; } return 0;}

2048 BY 


#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
#include//EasyX图形库
using namespace std;
IMAGE img[12];//存储0~2048的图片
static int imgIndex[12] = { 0,2,4,8,16,32,64,128,256,512,1024,2048 };//图片名字
static int i, j;
static int map[4][4] = { 0 };//建立二维数组
void loadResource()//加载资源
{
for (i = 0; i < 12;i++)//批量加载
{
char fileName[20] = "";
sprintf(fileName, "%d.bmp", imgIndex[i]);
loadimage(img + i, fileName,100,100);
}
}
void drawmap() //画图
{
setbkcolor(RGB(150, 210, 255));//背景颜色
cleardevice();
settextcolor(WHITE);//字体颜色
settextstyle(35, 0, "仿宋");//字体格式
outtextxy(50, 10, " 2048小游戏");
int x, y, k;
for ( i = 0; i < 4; i++)
{
for (j = 0;j < 4;j++)
{
x = 100 * j;
y = 100 * i + 100;
for ( k = 0; k < 12; k++)//求元素对应图片编号
{
if (imgIndex[k] == map[i][j])
break;
}
putimage(x, y, img + k);//贴对应图片
}

}
}
int random()//随机产生2和4
{
srand((unsigned int)time(NULL));//随机函数种子
for ( i = 0; i <4 ;i++)
{
for (j = 0;j < 4;j++)
{
if (map[i][j]==0)
{
map[i][j] = (rand() % 3) * 2;
if (map[i][j]==0)
{
continue;
}
return 0;
}
}
}
return 0;
}
int moveR()//向右移动
{
int flag = 0;//GameOver标记
for (i = 0; i < 4; i++)
{
for (j = 4 - 1; j >= 0; j--)
{
int cv = map[i][j];//当前元素的值
if (cv!=0)
{
int k = j - 1;
while (k >= 0)
{
int cvn = map[i][k];//当前元素的下一个
if (cvn != 0)
{
if (cv == cvn)//如果相邻相同,则合并
{
flag = 1;
map[i][j] += map[i][k];
map[i][k] = 0;
}
k = -1;
break;//退出循环
}
k--;//判断其他位置
}
}
}
}
for (i = 0; i < 4; i++)
{
for (j = 4 - 1;j >= 0;j--)
{
int cv = map[i][j];
if (cv == 0)
{
int k = j - 1;
while (k >= 0)
{
int cvn = map[i][k];
if (cvn != 0)
{
flag = 1;
map[i][j] = cvn;//非0元素移到0的位置上
map[i][k] = 0;//移动后变0
k =-1;
}
k--;//判断其他位置
}
}
}
}
if (flag)
return 0;
else
return 4;
}
int moveL()//向左移动
{
int flag = 0;//GameOver标记
for (i = 0; i < 4; i++)
{
for (j = 0; j <4; j++)
{
int currentvalue = map[i][j];//当前元素的值
if (currentvalue != 0)
{
int k = j + 1;
while (k <4)
{
int currentvaluenext = map[i][k];//当前元素的下一个
if (currentvaluenext != 0)
{
if (currentvalue == currentvaluenext)//如果相邻相同,则合并
{
flag = 1;
map[i][j] += map[i][k];
map[i][k] = 0;
}
k = 4;
break;//退出循环
}
k++;//判断其他位置
}
}
}
}
for (i = 0; i < 4; i++)
{
for (j =0;j<4;j++)
{
int currentvalue = map[i][j];
if (currentvalue == 0)
{
int k = j +1;
while (k <4)
{
int currentvaluenext = map[i][k];
if (currentvaluenext != 0)
{
flag = 1;
map[i][j] = currentvaluenext;//非0元素移到0的位置上
map[i][k] = 0;//移动后变0
k = 4;
}
k++;//判断其他位置
}
}
}
}
if (flag)
return 0;
else
return 4;
}
int moveU()//向上移动
{
int flag = 0;//GameOver标记
for (i =0; i <4; i++)
{
for (j = 0; j < 4; j++)
{
int currentvalue = map[i][j];//当前元素的值
if (currentvalue != 0)
{
int k = i + 1;
while (k <4)
{
int currentvaluenext = map[k][j];//当前元素的下一个
if (currentvaluenext != 0)
{
if (currentvalue == currentvaluenext)//如果相邻相同,则合并
{
flag = 1;
map[i][j] += map[k][j];
map[k][j] = 0;
}
k = 4;
break;//退出循环
}
k++;//判断其他位置
}
}
}
}
for (i = 0; i < 4-1; i++)
{
for (j = 0;j < 4;j++)
{
int currentvalue = map[i][j];
if (currentvalue == 0)
{
int k = i + 1;
while (k <4)
{
int currentvaluenext = map[k][j];
if (currentvaluenext != 0)
{
flag = 1;
map[i][j] = currentvaluenext;//非0元素移到0的位置上
map[k][j] = 0;//移动后变0
k = 4;
}
k++;//判断其他位置
}
}
}
}
if (flag)
return 0;
else
return 4;
}
int moveD()//向下移动
{
int flag = 0;//GameOver标记
for (i = 4-1; i>=0; i--)
{
for (j = 0; j <4; j++)
{
int currentvalue = map[i][j];//当前元素的值
if (currentvalue != 0)
{
int k = i - 1;
while (k >= 0)
{
int currentvaluenext = map[k][j];//当前元素的下一个
if (currentvaluenext != 0)
{
if (currentvalue == currentvaluenext)//如果相邻相同,则合并
{
flag = 1;
map[i][j] += map[k][j];
map[k][j] = 0;
}
k = 0;
break;//退出循环
}
k--;//判断其他位置
}
}
}
}
for (i = 4-1; i >0; i--)
{
for (j = 0;j <4;j++)
{
int currentvalue = map[i][j];
if (currentvalue == 0)
{
int k = i - 1;
while (k >= 0)
{
int currentvaluenext = map[k][j];
if (currentvaluenext != 0)
{
flag = 1;
map[i][j] = currentvaluenext;//非0元素移到0的位置上
map[k][j] = 0;//移动后变0
k = 0;
}
k--;//判断其他位置
}
}
}
}
if (flag)
return 0;
else
return 4;
}
void keystrike()//按键响应
{
char key=_getch();
switch (key)
{
case 'w':
case 'W':
case 72:
random();
moveU();
break;
case 's':
case 'S':
case 80:
random();
moveD();
break;
case 'a':
case 'A':
case 75:
random();
moveL();
break;
case 'd':
case 'D':
case 77:
random();
moveR();
break;
}

}

void Game_2048()
{
loadResource();
initgraph(100*4,100*5);
drawmap();
while (1)
{
keystrike();
drawmap();
}
system("pause");
}

贪吃蛇 BY

#include #include #include #include #include#define snake_size 100#define Map_width 78#define Map_height 24// 控制台按键所代表的数字// ↑ : 72// ↓ :80// ← :75// → :77 static int key = 72; static int changeFlag = 0; static int sorce = 0; static int i;struct food { int x; int y;} food;struct snake { int speed; int len; int x[snake_size]; int y[snake_size];}snake;void gotoxy(int x, int y){ COORD coord; coord.X = x; coord.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);}void menu1(){ printf("\n\n"); printf("$$$$$$$\t\t\t\t上面是游戏界面! \t\t$$$$$$$\n"); printf("*******************************************************************************\n"); printf("*******************************************************************************\n"); printf("*******************************************************************************\n"); printf("********************************欢迎来玩贪吃蛇*********************************\n"); printf("******************************* play *********************************\n"); printf("*****************************输入1-------进行游戏******************************\n"); printf("*****************************输入0-------退出游戏******************************\n"); printf("*******************************************************************************\n"); printf("*******************************************************************************\n"); printf("*******************************************************************************\n"); printf("*********************************************************************-小时制作\n"); printf("*********************************************************-最终解释权归小时所有\n");}void menu2(){ int input; while (1){ printf("\n请输入(1或0);即可:(开始游戏)或(退出游戏):"); scanf("%d",&input); if (input == 1){ printf("进入游戏\n"); break; } else if (input == 0){ printf("\n"); printf("*****************已退出游戏!******************\n"); printf("*****************已退出游戏!******************\n"); printf("*****************已退出游戏!******************\n"); Sleep(10000); } else{ printf("*********输入有误!已退出游戏!************\n"); printf("*********输入有误!已退出游戏!************\n"); printf("*********输入有误!已退出游戏!************\n"); Sleep(10000); exit(0); } }}void drawMap1(){ system("mode con cols=80 lines=45"); for (i = 0; i <= Map_width; i += 2)//横向占用的是两个位置 { gotoxy(i, 0); printf("■"); gotoxy(i,Map_height); printf("■"); } for (i = 1; i < Map_height; i++) { gotoxy(0, i); printf("■"); gotoxy(Map_width, i); printf("■"); }}void drawMap2(){ while (1) { srand((unsigned int)time(NULL)); food.x = rand() % (Map_width - 4) + 2; food.y = rand() % (Map_height - 2) + 1; if (food.x % 2 == 0) break; } gotoxy(food.x, food.y); printf("@"); snake.x[0] = Map_width / 2 + 1; snake.y[0] = Map_height / 2; gotoxy(snake.x[0], snake.y[0]); printf("■"); for (i = 1; i < snake.len; i++) { snake.x[i] = snake.x[i - 1] + 2; snake.y[i] = snake.y[i - 1]; gotoxy(snake.x[i], snake.y[i]); printf("■"); } snake.speed=100; snake.len=3;}void keyDown(){ int old_key = key; if (kbhit()) { fflush(stdin);//清空缓冲区 key = getch(); key = getch();//getch()会返回两次值,第二次为真,第一次为0或224 } if (changeFlag == 0) { gotoxy(snake.x[snake.len - 1], snake.y[snake.len - 1]); printf(" "); } //向前移动 for (i = snake.len - 1; i > 0; i--) { snake.x[i] = snake.x[i - 1]; snake.y[i] = snake.y[i - 1]; } if (old_key == 72 && key == 80) key = 72; if (old_key == 80 && key == 72) key = 80; if (old_key == 75 && key == 77) key = 75; if (old_key == 77 && key == 75) key = 77; switch (key) { case 75: snake.x[0] -= 2;//往左 break; case 77: snake.x[0] += 2;//往右 break; case 72: snake.y[0]--;//往上 break; case 80: snake.y[0]++;//往下 break; } gotoxy(snake.x[0], snake.y[0]); printf("■"); gotoxy(0, 0); changeFlag = 0;}void createFood(){ if (snake.x[0] == food.x && snake.y[0] == food.y) { while (1) { int flag = 1; food.x = rand() % (Map_width - 4) + 2; food.y = rand() % (Map_height - 2) + 1; for (i = 0; i < snake.len; i++) { if (snake.x[i] == food.x && snake.y[i] == food.y) { flag = 0; break; } } if (flag && food.x % 2 == 0) break; } gotoxy(food.x, food.y); printf("@"); snake.len++; sorce +=10; snake.speed -=3; changeFlag = 1;//因为吃到了食物,就不用再擦除蛇尾的那一节,以此来造成蛇身体增长的效果 }}bool snakeStatus(){ if (snake.y[0] == 0 || snake.y[0] == Map_height) return false; if (snake.x[0] == 0 || snake.x[0] == Map_width) return false; for (i = 1; i < snake.len; i++) { if (snake.x[i] == snake.x[0] && snake.y[i] == snake.y[0]) return false; } return true;}void end(){ gotoxy(Map_width / 2, Map_height / 2); printf("兄弟,你嗝屁辽~~~\n"); gotoxy(Map_width / 2, Map_height / 2 + 1); printf("本次游戏得分为:%d\n", sorce); gotoxy(Map_width / 2, Map_height / 2 + 2); printf("要不再来一次,小兄弟!"); Sleep(100000);}void Game_Snake(){ drawMap1(); menu1(); menu2(); drawMap2(); while (1) { keyDown(); if (!snakeStatus()) break; createFood(); Sleep(snake.speed); } end();}

五子棋 BY

#include #include #include //棋盘初始化函数//Chessboard棋盘数组,ln=棋盘大小,成功返回Chessboard,不成功NULLvoid init_Chessboard(char Chessboard[][7], int ln){ printf("\n任意键开始"); printf("\n时洪江组双人五子棋"); if ((Chessboard != NULL) && (ln>0)){ int i = 0, j = 0; for (i = 0; i 0)); int i = 0, j = 0; for (i = 0; i= 0) && (y >= 0)){ if ('\t' == Chessboard[x][y]){ Chessboard[x][y] = ChessPieces; return 1; //成功 } else { return 0; }//end if2 }//end if1 return 0;} //满盘判断//棋盘满了判断//满了就返回-1int full_Chess(char Chessboard[][7], int ln){ int i = 0, j = 0; for (i = 0; i 0)); if((XS =0) && (YS >=0) && (dx != 0 || dy != 0)) //坐标增量不为同时0 { if (((XS + dx * 4) > ln) || ((XS + dx * 4)<0) || //判断终点坐标 ((YS + dy * 4)>ln) || ((YS + dy * 4) < 0) || //在棋盘外 ('\t' == Chessboard[XS][YS])) { return 0; //不在棋盘内,或者起点是没下子 } else { int i = 0; for (i = 1; i < 5; ++i){ if (Chessboard[XS][YS] != Chessboard[XS + (dx * i)][YS + (dy * i)]) { return 0; //如果不是连续5个一样的 }//end if3 }//end for1 return 1; //五个都一样,且都在棋盘内 }//end if 2 } return 0; //其他情况}//裁判函数//Chessboard 棋盘数组,ln=棋盘宽度//赢了返回1,否则返回0int judga(char Chessboard[][7], int ln){ assert((NULL != Chessboard) && (ln>0)); int i = 0, j = 0; //纵向成五子连线判断 for (i = 0; i<(ln - 4); ++i){ for (j = 0; j(ln-4); --i){ for (j = 0; j <(ln - 4); ++j){ if (judga_line(Chessboard, ln, i, j, -1, 1)){ return 1; } }//end for_j }//end for_i return 0; //没能赢} //主函数 void Game_Wuziqi(){ char CB[7][7]; char nameA[50] = "玩家A"; char nameB[50] = "玩家B"; int x = -1, y = -1; //初始化 init_Chessboard(CB, 7); printf("\n请输入player1的名字:"); scanf("%s", nameA); printf("\n请输入plauer2的名字:"); scanf("%s", nameB); //显示棋盘 show_Chessboard(CB, 7); while (1){ //判断是否棋盘已满 if (full_Chess(CB, 7)){ puts("\n下满了,告辞吧,下次再战!"); break; //跳出最外层while }//end if //玩家A下子 while (1){ printf("\n请玩家 %s 下子 #\n", nameA); printf("棋子行坐标X="); scanf("%d", &x); printf("棋子列坐标Y="); scanf("%d", &y); if (play(CB, 7, x, y, '#')){ //@ ascii=64 break; //下子成功 } else { puts("下子失败,请重新选定位置下子"); continue; //下子不成功,重新下子 }//end if }//end while A //显示棋盘 show_Chessboard(CB, 7); //判断玩家A是否胜利 if (judga(CB, 7)){ printf("\n恭喜玩家 %s 成功取得胜利!!鲜花\n", nameA); getchar(); getchar(); break; //跳出最外层while }//不用下了 //玩家B下子 while (1){ printf("\n请玩家 %s 下子 *\n", nameB); printf("棋子行坐标X="); scanf("%d", &x); printf("棋子列坐标Y="); scanf("%d", &y); if ((play(CB, 7, x, y, '*'))){ //O ascii=79 break; //下子成功 } else { puts("下子失败,请重新选定位置下子"); continue; //下子不成功,重新下子 }//end if }//end while B //显示棋盘 show_Chessboard(CB, 7); //判断玩家B是否胜利 if (judga(CB, 7)){ printf("\n恭喜玩家 %s 成功取得胜利!! 鲜花\n", nameA); getchar(); getchar(); break; //跳出最外层while }//不用下了 } } 

推箱子 BY

#include #include #include #define WIDTH 8#define HEIGHT 8static int map[HEIGHT][WIDTH] = { {0, 0, 1, 1, 1, 0, 0, 0}, {0, 0, 1, 4, 1, 0, 0, 0}, {0, 0, 1, 0, 1, 1, 1, 1}, {1, 1, 1, 3, 0, 3, 4, 1}, {1, 4, 0, 3, 2, 1, 1, 1}, {1, 1, 1, 1, 3, 1, 0, 0}, {0, 0, 0, 1, 4, 1, 0, 0}, {0, 0, 0, 1, 1, 1, 0, 0} };//人的位置 static int x, y;//箱子的个数 static int boxs; //声明方法 void initData();void drawMap();void moveUp();void moveLeft();void moveDown();void moveRight();void Game_PushBoxs() { char direction; //存储键盘按的方向 initData(); while(1){ system("cls"); drawMap(); if(!boxs){ break; } direction = getchar(); switch(direction){ case 'w': moveUp(); break; case 'a': moveLeft(); break; case 's': moveDown(); break; case 'd': moveRight(); break; } } printf("恭喜你完成游戏!※");}void initData(){ int i, j; printf("游戏加载中,请稍后........."); //获取人的位置 for(i = 0; i < HEIGHT; i++){ for(j = 0; j < WIDTH; j++){ //遍历到2时,记录人的左边 if(map[i][j] == 2){ x = j; y = i; } //遍历到3时,箱子的数目增加 if(map[i][j] == 3){ boxs++; } } } }void drawMap(){ int i, j; for(i = 0; i < WIDTH; i++){ for(j = 0; j < HEIGHT; j++){ switch(map[i][j]){ case 0: printf(" "); break; case 1: printf("■"); break; case 2: printf("♀"); break; case 3: printf("◆"); break; case 4: printf("●"); break; case 5: printf("★"); break; } } printf("\n"); }}void moveUp(){ //定义变量存放人物上方的坐标 int ux, uy; //当上方没有元素时,直接return if(y == 0){ return; } //记录上方坐标 ux = x; uy = y - 1; //上方为已完成方块 if(map[uy][ux] == 5){ return; } //假设上方为墙,直接return if(map[uy][ux] == 1){ return; } //假设上方为箱子 if(map[uy][ux] == 3){ //判断箱子上方是否为墙 if(map[uy - 1][ux] == 1){ return; } //判断箱子上方是否为球 if(map[uy - 1][ux] == 4){ //将箱子上面内容赋值为5★ map[uy - 1][ux] = 5; map[uy][ux] = 0; //箱子的数目减1 boxs--; }else{ //移动箱子 map[uy - 1][ux] = 3; } } map[y][x] = 0; map[uy][ux] = 2; y = uy; } void moveLeft(){ //定义变量存放人物左边的坐标 int lx, ly; //当左边没有元素时,直接return if(x == 0){ return; } //记录左边坐标 lx = x - 1; ly = y; //左边为已完成方块 if(map[ly][lx] == 5){ return; } //假设左边为墙,直接return if(map[ly][lx] == 1){ return; } //假设左边为箱子 if(map[ly][lx] == 3){ //判断箱子左边是否为墙 if(map[ly][lx - 1] == 1){ return; } //判断箱子左边是否为球 if(map[ly][lx - 1] == 4){ //将箱子左边内容赋值为5★ map[ly][lx - 1] = 5; map[ly][lx] = 0; //箱子的数目减1 boxs--; }else{ //移动箱子 map[ly][lx - 1] = 3; } } map[y][x] = 0; map[ly][lx] = 2; x = lx; }void moveDown(){ //定义变量存放人物下方的坐标 int dx, dy; //当下方没有元素时,直接return if(y == HEIGHT - 1){ return; } //记录下方坐标 dx = x; dy = y + 1; //下方为已完成方块 if(map[dy][dx] == 5){ return; } //假设下方为墙,直接return if(map[dy][dx] == 1){ return; } //假设下方为箱子 if(map[dy][dx] == 3){ //判断箱子下方是否为墙 if(map[dy + 1][dx] == 1){ return; } //判断箱子下方是否为球 if(map[dy + 1][dx] == 4){ //将箱子下面内容赋值为5★ map[dy + 1][dx] = 5; map[dy][dx] = 0; //箱子的数目减1 boxs--; }else{ //移动箱子 map[dy + 1][dx] = 3; } } map[y][x] = 0; map[dy][dx] = 2; y = dy; }void moveRight(){ //定义变量存放人物右边的坐标 int rx, ry; //当右边没有元素时,直接return if(x == WIDTH - 1){ return; } //记录右边坐标 rx = x + 1; ry = y; //右边为已完成方块 if(map[ry][rx] == 5){ return; } //假设右边为墙,直接return if(map[ry][rx] == 1){ return; } //假设右边为箱子 if(map[ry][rx] == 3){ //判断箱子右边是否为墙 if(map[ry][rx + 1] == 1){ return; } //判断箱子左边是否为球 if(map[ry][rx + 1] == 4){ //将箱子右边内容赋值为5★ map[ry][rx + 1] = 5; map[ry][rx] = 0; //箱子的数目减1 boxs--; }else{ //移动箱子 map[ry][rx + 1] = 3; } } map[y][x] = 0; map[ry][rx] = 2; x = rx; }

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

12 + 1 =