求助啊,谁有有趣的c语言小程序,并且要有源代码!!
学习“推箱子”C语言编码:
#include <stdio.h>
#include <conio.h>
#include<stdlib.h>
#include<windows.h>
int m =0; //m代表第几关
struct maps{short a[9][11]; };
struct maps map[5]={ 0,0,0,0,0,0,0,0,0,0,0, //共5关,每关9行
0,1,1,1,1,1,1,1,0,0,0,
0,1,0,0,0,0,0,1,1,1,0,
1,1,4,1,1,1,0,0,0,1,0, //0空地,1墙
1,5,0,0,4,0,0,4,0,1,0, //4是箱子,5是人
1,0,3,3,1,0,4,0,1,1,0, //3是目的地
1,1,3,3,1,0,0,0,1,0,0, //7是箱子在目的地(4+3)
0,1,1,1,1,1,1,1,1,0,0, //8是人在目的地(5+3)
0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,
0,0,1,1,1,1,0,0,0,0,0,
0,0,1,5,0,1,1,1,0,0,0,
0,0,1,0,4,0,0,1,0,0,0,
0,1,1,1,0,1,0,1,1,0,0,
0,1,3,1,0,1,0,0,1,0,0,
0,1,3,4,0,0,1,0,1,0,0,
0,1,3,0,0,0,4,0,1,0,0,
0,1,1,1,1,1,1,1,1,0,0,
0,0,0,0,0,0,0,0,0,0,0,
0,0,0,1,1,1,1,1,1,1,0,
0,0,1,1,0,0,1,0,5,1,0,
0,0,1,0,0,0,1,0,0,1,0,
0,0,1,4,0,4,0,4,0,1,0,
0,0,1,0,4,1,1,0,0,1,0,
1,1,1,0,4,0,1,0,1,1,0,
1,3,3,3,3,3,0,0,1,0,0,
1,1,1,1,1,1,1,1,1,0,0,
0,1,1,1,1,1,1,1,1,1,0,
0,1,0,0,1,1,0,0,0,1,0,
0,1,0,0,0,4,0,0,0,1,0,
0,1,4,0,1,1,1,0,4,1,0,
0,1,0,1,3,3,3,1,0,1,0,
1,1,0,1,3,3,3,1,0,1,1,
1,0,4,0,0,4,0,0,4,0,1,
1,0,0,0,0,0,1,0,5,0,1,
1,1,1,1,1,1,1,1,1,1,1,
0,0,0,0,0,0,0,0,0,0,0,
0,0,0,1,1,1,1,1,1,0,0,
0,1,1,1,0,0,0,0,1,0,0,
1,1,3,0,4,1,1,0,1,1,0,
1,3,3,4,0,4,0,0,5,1,0,
1,3,3,0,4,0,4,0,1,1,0,
1,1,1,1,1,1,0,0,1,0,0,
0,0,0,0,0,1,1,1,1,0,0,
0,0,0,0,0,0,0,0,0,0,0 };
void DrMap( ) //绘制地图
{ CONSOLE_CURSOR_INFO cursor_info={1,0}; //隐藏光标的设置
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
printf("\n\n \t\t\b推箱子");
printf("\n \t");
for (int i = 0; i < 9; i++)
{for (int j = 0; j < 11; j++)
{switch (map[m].a[i][j])
{case 0: printf(" "); break;
case 1: printf("■"); break;
case 3: printf("◎");break;
case 4: printf("□"); break;
case 5: printf("♀"); break; //5是人
case 7: printf("□"); break; //4 + 3箱子在目的地中
case 8: printf("♀");break; // 5 + 3人在目的地中
}
}
printf("\n\t");
}
}
void gtxy(int x, int y) //控制光标位置的函数
{ COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void start( ) //开始游戏
{ int r, c; //r,c用于记录人的下标
for (int i = 0; i < 9; i++)
{ for (int j = 0; j < 11; j++)
{if (map[m].a[i][j] == 5||map[m].a[i][j]==8) { r = i; c = j; } } //i j 人的下标
}
char key;
key = getch( );
switch (key)
{case 'W':
case 'w':
case 72:
if (map[m]. a[r - 1][c] == 0|| map[m]. a [r - 1][c] == 3)
{ gtxy(2*c+8,r-1+3); printf("♀"); // gtxy(2*c+8,r-1+3)是到指定位置输出字符
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}
map[m]. a [r - 1][c] += 5; map[m]. a [r][c] -= 5; }
else if (map[m]. a [r - 1][c] == 4 || map[m]. a [r - 1][c] == 7)
{ if (map[m]. a [r - 2][c] == 0 || map[m]. a [r - 2][c] == 3)
{ gtxy(2*c+8,r-2+3); printf("□"); gtxy(2*c+8,r-1+3); printf("♀");
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}
map[m]. a [r - 2][c] += 4; map[m]. a [r - 1][c] += 1;
map[m]. a [r][c] -= 5; }
} break;
case 'S':
case 's':
case 80:
if (map[m]. a [r + 1][c] == 0 || map[m]. a [r + 1][c] == 3)
{ gtxy(2*c+8,r+1+3); printf("♀");
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}
map[m]. a [r + 1][c] += 5; map[m]. a [r][c] -= 5; }
else if (map[m]. a [r + 1][c] == 4 || map[m]. a [r+ 1][c] == 7)
{ if (map[m]. a [r + 2][c] == 0 || map[m]. a [r + 2][c] == 3)
{ gtxy(2*c+8,r+2+3); printf("□"); gtxy(2*c+8,r+1+3); printf("♀");
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}
map[m]. a [r + 2][c] += 4; map[m]. a [r + 1][c] += 1;
map[m]. a [r][c] -= 5; }
}break;
case 'A':
case 'a':
case 75:
if (map[m]. a [r ][c - 1] == 0 || map[m]. a [r ][c - 1] == 3)
{ gtxy(2*(c-1)+8,r+3); printf("♀");
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}
map[m]. a [r ][c - 1] += 5; map[m]. a [r][c] -= 5; }
else if (map[m]. a [r][c - 1] == 4 || map[m]. a [r][c - 1] == 7)
{if (map[m]. a [r ][c - 2] == 0 || map[m]. a [r ][c - 2] == 3)
{ gtxy(2*(c-2)+8,r+3); printf("□"); gtxy(2*(c-1)+8,r+3); printf("♀");
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}
map[m]. a [r ][c - 2] += 4; map[m]. a [r ][c - 1] += 1;
map[m]. a [r][c] -= 5; }
}break;
case 'D':
case 'd':
case 77:
if (map[m]. a [r][c + 1] == 0 || map[m]. a [r][c + 1] == 3)
{ gtxy(2*(c+1)+8,r+3); printf("♀");
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }
if(map[m]. a[r ][c] == 8) {gtxy(2*c+8,r+3); printf("◎");}
map[m]. a [r][c + 1] += 5; map[m]. a [r][c] -= 5; }
else if (map[m]. a [r][c + 1] == 4 || map[m]. a [r][c + 1] == 7)
{ if (map[m]. a [r][c + 2] == 0 || map[m]. a [r][c + 2] == 3)
{ gtxy(2*(c+2)+8,r+3); printf("□"); gtxy(2*(c+1)+8,r+3); printf("♀");
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}
map[m]. a [r][c + 2] += 4; map[m]. a [r][c + 1] += 1;
map[m]. a [r][c] -= 5; }
}break;
}
}
int ifwan( ) //是否完成(1是0否)
{ if(m==0){if(map[m].a[5][2]==7&& map[m].a[5][3]==7&&
map[m].a[6][2]==7&& map[m].a[6][3]==7) return 1;}
if(m==1){if(map[m].a[5][2]==7&& map[m].a[6][2]==7&&
map[m].a[7][2]==7) return 1;}
if(m==2){if(map[m].a[7][1]==7&& map[m].a[7][2]==7&& map[m].a[7][3]==7&&
map[m].a[7][4]==7&& map[m].a[7][5]==7) return 1;}
if(m==3){if(map[m].a[4][4]==7&& map[m].a[4][5]==7&& map[m].a[4][6]==7&&
map[m].a[5][4]==7&& map[m].a[5][5]==7&& map[m].a[5][6]==7) return 1;}
if(m==4){if(map[m].a[3][2]==7&& map[m].a[4][1]==7&& map[m].a[4][2]==7&&
map[m].a[5][1]==7&& map[m].a[5][2]==7) return 1;}
return 0;
}
int main( ) //主函数
{ while (1)
{ system("cls");
DrMap( );
while (1)
{ start( );
if(ifwan()){printf("\007");break;} //完成后响铃
}
m+=1;
}
return 0;
}
2023-06-12 广告
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <Windows.h>
#define WIDTH 78 //地图的宽,x轴
#define HEIGHT 26 //地图的高,y轴
int dire=3; //方向变量,初值为向“左”
int Flag=0; //判断是否吃了食物的标志
int score=0; //玩家得分
struct foods{ int x;
int y;
}food; //结构体food有2个成员
struct snakes{int len;
int speed;
int x[100];
int y[100];
}snake; //结构体snake有4个成员
void gotoxy( int x,int y) //获得句柄,才能控制光标移动
{ COORD coord;
coord.X=x;
coord.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void gotoxy( int x,int y); //以下声明要用到的几个自编函数
void csh( );
void keyDown( );
void Move( );
void putFood( );
int pdOver( );
int main( ) //主函数
{ csh( );
while(1)
{ keyDown( );
Move( );
putFood( );
if(pdOver( ))
{system(“cls”);
gotoxy(WIDTH/2+1,HEIGHT/2);
printf(“游戏结束!T__T”);
gotoxy(WIDTH/2+1,HEIGHT/2+1);
printf(“玩家总分:%d分”,score);
getch( );
break; }
Sleep(snake.speed);
}
return 0;
}
void csh( ) //初始化界面
{ int i;
gotoxy(0,0);
CONSOLE_CURSOR_INFO cursor_info={1,0}; //光标值设为0就隐藏了
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
for(i=0;i<=WIDTH;i=i+2) //横坐标要为偶数,因为这里要打印的字符占2个位置
{ gotoxy(i,0); //打印上边框
printf("■");
gotoxy(i,HEIGHT); //打印下边框
printf("■");
}
for(i=1;i<HEIGHT;i++)
{ gotoxy(0,i); //打印左边框
printf("■");
gotoxy(WIDTH,i); //打印右边框
printf("■");
}
while(1)
{ srand((unsigned int)time(NULL)); //设定种子为当前时间
food.x=rand()%(WIDTH-4)+2;
food.y=rand()%(HEIGHT-2)+1;
if(food.x%2==0)break;
}
gotoxy(food.x,food.y); //到食物坐标处打印初试食物
printf("●");
snake.len=3; //蛇身长
snake.speed=350; //刷新蛇的时间,即是移动速度
snake.x[0]=WIDTH/2+1; //蛇头横坐标为偶数
snake.y[0]=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("■");
}
return;
}
void keyDown( ) //按键操作
{ int key;
if(kbhit( )) //如有按键输入才执行下面操作
{ key=getch( );
if(key==224) //值为224表示按下了方向键,下面要再次获取键值
{ key=getch( );
if(key==72&&dire!=2)dire=1; //72为向上
if(key==80&&dire!=1)dire=2; //80为向下
if(key==75&&dire!=4)dire=3; //75为向左
if(key==77&&dire!=3)dire=4; //77为向右
}
if(key==13)
{ while(1) if((key=getch( ))==13) break; } //13为回车键,这儿用来暂停
}
}
void Move( ) //蛇移动一节
{ if(Flag==0) //如没吃食物,才执行下面操作擦掉蛇尾
{ gotoxy(snake.x[snake.len-1],snake.y[snake.len-1]);
printf(" ");
}
int i;
for (i = snake.len - 1; i > 0; i--) //从蛇尾起每节存储前一节坐标值(蛇头除外)
{ snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1];
}
switch (dire) //以下判断蛇头该往哪个方向移动,并获取最新坐标值
{ case 1: snake.y[0]--; break;
case 2: snake.y[0]++; break;
case 3: snake.x[0]-=2; break;
case 4: snake.x[0]+=2; break;
}
gotoxy(snake.x[0], snake.y[0]); //打印蛇头
printf("■");
if (snake.x[0] == food.x && snake.y[0] == food.y) //如吃到食物执行以下操作
{ snake.len++; score += 50; snake.speed -= 5; Flag = 1;}
else Flag = 0;
if(snake.speed<160) snake.speed= snake.speed+5; //作弊码,不让速度无限加快
}
void putFood( ) //投放食物
{ if(Flag == 1) //如吃到食物才执行以下操作,生成另一个食物
{ while (1)
{ int i,n= 1;
srand((unsigned int)time(NULL)); //设定当前时间,接下产生食物坐标值
food.x = rand( ) % (WIDTH - 4) + 2;
food.y = rand( ) % (HEIGHT - 2) + 1;
for (i = 0; i < snake.len; i++) //随机生成的食物不能在蛇的身体上
{ if (food.x == snake.x[i] &&food.y == snake.y[i])
{ n= 0; break;}
}
if (n && food.x % 2 == 0) break; //n不为0且横坐标为偶数,食物坐标取值成功
}
gotoxy(food.x, food.y); //光标到取得的坐标处打印食物
printf("●");
}
return;
}
int pdOver( ) //判断游戏是否结束
{ int i;
gotoxy(2,HEIGHT+1); //以下打印一些其它信息
printf(“暂停键:Enter.”);
gotoxy(2,HEIGHT+2);
printf(“游戏得分:%d”,score);
if (snake.x[0] == 0 || snake.x[0] == WIDTH) return 1; //蛇头触碰左右边界
if (snake.y[0] == 0 || snake.y[0] == HEIGHT) return 1; //蛇头触碰上下边界
for (i = 1; i < snake.len; i++)
{ if (snake.x[0] == snake.x[i] && snake.y[0] == snake.y[i]) return 1; } //蛇头触碰自身
return 0;
}
#include <stdio.h>
#include <math.h>
#include <time.h>
#define numOfQuestions 10
void main()
{
srand(1);
for(int i=0;i<numOfQuestions ;i++)
{
bool add = rand()%2;
char oper = '-';
if(add) oper = '+';
int a = rand()%100;
int b = rand()%100;
if(!add)
{
while(b>a)b = rand()%100;
}
printf("%d %c %d =?", a, oper, b);
int answer = 0;
scanf("%d", &answer);
bool correct = false;
if(add)
{
if(answer == (a + b)) correct = true;
}
else
{
if(answer == (a - b)) correct = true;
}
if(correct)printf("回答正确!\n");
else printf("回答错误!\n");
}
}