游戏盒子接球

编程入门 行业动态 更新时间:2024-10-24 12:22:42

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


更多推荐

游戏盒子接球

本文发布于:2023-06-10 23:11:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1351732.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:盒子   游戏

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!