#pragma once //避免重复包含问题,避免头文件交叉包含造成重定义问题#ifndef _HEAD_H#define _HEAD_Hstruct MM
{int data;struct MM* next;};#endif// ! _HEAD_H
#include<stdio.h>
预处理
<1>.编译器完成
条件编译---->兼容性处理
#if#else#elif#endif#ifndef#undef#define NUM 1 //符号常量intmain(){int a =1;#if NUM==1printf("ILoveyou\n");#elseprintf("IMissyou\n");#endif#if 0printf("ILoveyou!\n");#elif 2printf("ILoveyou\n");#endif#define MAX 100printf("Max=%d\n", MAX);#undef MAX //取消宏定义的使用//库中的遗留的宏定义printf("%s\n",__FILE__);printf("%s\n",__TIME__);printf("%s\n",__DATE__);#pragma message("ILoveyou")return0;}
四、15课作业
//问题:滑行的情况 偶然会出现 #include<graphics.h>#include<time.h>int map[4][6]={0};//撞击地图struct board //反弹板{int x;int y;int width;int height;};struct board myBoard ={200,775,200,25};//我的反弹板 全局变量struct ball //撞击球{int x;int y;int r;int dx;int dy;};struct ball myBall ={300,400,10,5,8};//我的撞击球 全局变量voidmodifyMap()//地图板与球相撞的消除{for(int i =0; i <4; i++){for(int j =0; j <6; j++){int x =100* j;//行 int y =25* i;//列 砖块if(map[i][j]!=-1)//此块是否为空{if(x<myBall.x && x +100>myBall.x)//球在块的长 {if(myBall.y-myBall.r <= y +25)//撞板的下面 ,速度的关系{
myBall.dy =-myBall.dy;//球变向
map[i][j]=-1;//板子置空}if(myBall.y - myBall.r == y)//撞板的上面?? 条件不够细 不然把上面的全消除了{
myBall.dy =-myBall.dy;
map[i][j]=-1;}}if(y<myBall.y && y +25>myBall.y)//球在块的高内 撞左右 侧面消除{if(myBall.x - myBall.r == x +100)//撞板的右面{
myBall.dx =-myBall.dx;
map[i][j]=-1;}elseif(myBall.x - myBall.r == x)//撞板的左面{
myBall.dy =-myBall.dy;
map[i][j]=-1;}}}}}//int j = myBall.x / 100;//int i = (myBall.y-myBall.r) / 25;//if (i < 4 && j < 6 && map[i][j] != -1)//{// myBall.dy = -myBall.dy;// map[i][j] = -1; //}//与球上下左右四个方向相撞}voidBall(struct ball* myBall)//撞击球的移动以及绘制{solidcircle(myBall->x, myBall->y, myBall->r);//画球 圆心坐标,半径//球的移动 判断增量变化情况if(myBall->x <= myBall->r|| myBall->x >=600- myBall->r){
myBall->dx =-myBall->dx;//撞击左右墙后 x轴增量变为相反数}if(myBall->y <= myBall->r /*|| myBall->y >= 775 - myBall->r*/){
myBall->dy =-myBall->dy;//撞击上墙后 y轴增量变为相反数}if(myBall->y >= myBoard.y - myBall->r){//如果小球要进入7大于775的范围 是否有反弹板子if(myBoard.x < myBall->x && myBoard.x + myBoard.width>myBall->x){
myBall->dy =-myBall->dy;//有反弹板子变向,没有就继续走,不反弹 }}// 进行移动
myBall->x += myBall->dx;//小球移动
myBall->y += myBall->dy;Sleep(10);}//游戏结束:1.球在地图外面 2.没有目标板intgameOver(){if(myBall.y >=800- myBall.r){return-50;//球在地图外面,返回-50}int result =0;for(int i =0; i <4; i++){for(int j =0; j <6; j++){switch(map[i][j]){case0:case1:case2:case3:
result +=1;//有目标板,result++ 1-24break;case-1:
result -=1;//没有目标板 break;}}}return result;}voidinitMap()//地图的随机生成{for(int i =0; i <4; i++){for(int j =0; j <6; j++){
map[i][j]=rand()%4;}}}voiddrawMap()//撞击地图的绘制{for(int i =0; i <4; i++){for(int j =0; j <6; j++){int x =100* j;int y =25* i;setlinecolor(BLACK);switch(map[i][j]){case0:setfillcolor(RED);fillrectangle(x, y, x +100, y +25);break;case1:setfillcolor(YELLOW);fillrectangle(x, y, x +100, y +25);break;case2:setfillcolor(BLUE);fillrectangle(x, y, x +100, y +25);break;case3:setfillcolor(MAGENTA);fillrectangle(x, y, x +100, y +25);break;}}}}voiddrawBoard()//反弹板的绘制{setfillcolor(WHITE);fillrectangle(myBoard.x, myBoard.y, myBoard.x +200,
myBoard.y +25);}voidmoveBoard()//反弹板的移动{if(GetAsyncKeyState('A')||GetAsyncKeyState(VK_LEFT)){if(myBoard.x >0)
myBoard.x -=25;else
myBoard.x =0;}if(GetAsyncKeyState('D')|| GetAsyncKeyState (VK_RIGHT)){if(myBoard.x <600- myBoard.width)
myBoard.x +=25;else
myBoard.x =600- myBoard.width;}}intmain(){struct ball* tempBall =&myBall;srand((unsignedint)time(0));initMap();initgraph(600,800);while(1){BeginBatchDraw();cleardevice();//按键移动后清屏 打印新的位置 形成动画drawMap();drawBoard();if(gameOver()==-50)//移动反弹版之前 看是否游戏结束{settextcolor(RED);settextstyle(50,0,"楷体");outtextxy(100,250,"游戏结束,失败!");EndBatchDraw();while(1);closegraph();//关闭窗口return0;}if(gameOver()==-24){settextcolor(RED);settextstyle(50,0,"楷体");outtextxy(100,250,"游戏结束,成功!");EndBatchDraw();while(1);closegraph();return0;}Ball(tempBall);moveBoard();modifyMap();EndBatchDraw();}while(1);closegraph();return0;}