游戏盒子接球

来源:互联网 发布:淘宝诈骗卖家如何应对 编辑:程序博客网 时间:2024/06/11 03:11

用VC写了一个小游戏,盒子接球,就是有很多球会落下,玩家可以控制一个盒子左右移动(方向键),来接住小球,最后会显示得分。代码如下:

/*盒子接球copyright: 圣石date: 2013.12.01*/#include<graphics.h>#include<conio.h>#include<stdio.h>#include<time.h>#include<windows.h>#include<stdlib.h>#define NUM 21struct Ball{int x;int y;int v;bool exist;};typedef struct Ball Ball;int main(){initgraph(640, 480);srand(time(NULL));Ball ball[100];int box_x = 10, box_y = 420,dx, i, n = NUM,score = 0;bool flag = true;setbkcolor(WHITE);cleardevice();for(i=0; i<NUM; i++){ball[i].x = 16 + 30*i; ball[i].y = 8 + rand()%32;ball[i].v = 1 + rand()%5;ball[i].exist = true;}char temp[10], str[] = "your score:";while(flag){//画球和盒子dx = 0;setcolor(GREEN);setfillcolor(BLACK);itoa(n, temp, 10);outtextxy(296, 310, "      ");outtextxy(296, 310, temp);if(!n) flag = false;for(i=0; i<21; i++){if(ball[i].exist){fillcircle(ball[i].x, ball[i].y, 8);if(ball[i].y >= 472){ball[i].exist = false;n--;}if(box_x+8<=ball[i].x && ball[i].x<=box_x+72 && ball[i].y>=412){score++; n--;ball[i].exist = false;}}}fillrectangle(box_x, box_y, box_x+80, box_y+60);if(kbhit()){int c = getch();if(224 == c){c = getch();if(75 == c) dx = -10;if(77 == c) dx = 10;}}Sleep(25);//擦除球和盒子setcolor(WHITE);setfillcolor(WHITE);for(i=0; i<NUM; i++){fillcircle(ball[i].x, ball[i].y, 8);ball[i].y += ball[i].v;}fillrectangle(box_x, box_y, box_x+80, box_y+60);box_x = box_x + dx;}setcolor(GREEN);itoa(score, temp, 10);outtextxy(292, 250, strcat(str, temp));outtextxy(290, 310, "按任意键退出");getchar();closegraph();return 0;}