游戏盒子接球

用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 21

struct 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;
}


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值