当前位置: 移动技术网 > 移动技术>移动开发>IOS > 项目: 双人反弹球游戏

项目: 双人反弹球游戏

2020年08月10日  | 移动技术网移动技术  | 我要评论

一、最终项目描述和效果

项目描述:   实现双人玩的弹跳球游戏

最终效果图如下:
在这里插入图片描述

二、基本框架实现

代码如下:

#include<conio.h>
#include<graphics.h>
#define High 480//游戏画面尺寸
#define Width 640

//全局变量
int ball1_x,ball1_y;//小球1的坐标
int ball2_x,ball2_y;//小球2的坐标
int radius;

void startup()//数据的初始化
{
	ball1_x=Width/3;
	ball1_y=High/3;
	ball2_x=Width*2/3;
	ball2_y=High*2/3;
	radius=20;
	initgraph(Width,High);
	BeginBatchDraw();
}

void clean()//消除画面
{
	setcolor(BLACK);
	setfillcolor(BLACK);
	fillcircle(ball1_x,ball1_y,radius);
	fillcircle(ball2_x,ball2_y,radius);
}

void show()//显示画面
{
	setcolor(GREEN);
	setfillcolor(GREEN);
	fillcircle(ball1_x,ball1_y,radius);//绘制绿圆
	setcolor(RED);
	setfillcolor(RED);
	fillcircle(ball2_x,ball2_y,radius);//绘制红圆
	FlushBatchDraw();
	Sleep(3);
}

void updateWithoutInput()//与用户输入无关的更新
{

}

void updateWithInput()//与用户输入有关的更新
{
	char input;
	if(kbhit())//判断是否有输入
	{
		input=getch();
		int step=10;
		if(input=='a')
			ball1_x-=step;
		if(input=='d')
			ball1_x+=step;
		if(input=='w')
			ball1_y-=step;
		if(input=='s')
			ball1_y+=step;

		if(input=='4')
			ball2_x-=step;
		if(input=='6')
			ball2_x+=step;
		if(input=='8')
			ball2_y-=step;
		if(input=='5')
			ball2_y+=step;
	}
}

void gameover()
{
	EndBatchDraw();
	closegraph();
}

int main(void)
{
	startup();//数据的初始化
	while(1)
	{
		clean();//把之前绘制的内容取消
		updateWithoutInput();//与用户输入无关的更新
		updateWithInput();//与用户输入有关的更新
		show();//显示新画面
	}
	gameover();//游戏结束,进行后续处理
	return 0;
}
你会发现这有一个弊端:  双方同一时刻只能有一个运行,不能同时运行。

三、异步操作的实现

在这里插入图片描述
代码如下:

#include<conio.h>
#include<graphics.h>
#define High 480//游戏画面尺寸
#define Width 640

//全局变量
int ball1_x,ball1_y;//小球1的坐标
int ball2_x,ball2_y;//小球2的坐标
int radius;

void startup()//数据的初始化
{
	ball1_x=Width/3;
	ball1_y=High/3;
	ball2_x=Width*2/3;
	ball2_y=High*2/3;
	radius=20;
	initgraph(Width,High);
	BeginBatchDraw();
}

void clean()//消除画面
{
	setcolor(BLACK);
	setfillcolor(BLACK);
	fillcircle(ball1_x,ball1_y,radius);
	fillcircle(ball2_x,ball2_y,radius);
}

void show()//显示画面
{
	setcolor(GREEN);
	setfillcolor(GREEN);
	fillcircle(ball1_x,ball1_y,radius);//绘制绿圆
	setcolor(RED);
	setfillcolor(RED);
	fillcircle(ball2_x,ball2_y,radius);//绘制红圆
	FlushBatchDraw();
	Sleep(3);
}

void updateWithoutInput()//与用户输入无关的更新
{

}

void updateWithInput()//与用户输入有关的更新
{
	int step=1;
	if((GetAsyncKeyState(0x41)&0x8000))//a
		ball1_x-=step;
	if((GetAsyncKeyState(0x44)&0x8000))//d
		ball1_x+=step;
	if((GetAsyncKeyState(0x57)&0x8000))//w
		ball1_y-=step;
	if((GetAsyncKeyState(0x53)&0x8000))//s
		ball1_y+=step;
	if((GetAsyncKeyState(VK_LEFT)&0x8000))//左方向键
		ball2_x-=step;
	if((GetAsyncKeyState(VK_RIGHT)&0x8000))//右方向键
		ball2_x+=step;
	if((GetAsyncKeyState(VK_UP)&0x8000))//上方向键
		ball2_y-=step;
	if((GetAsyncKeyState(VK_DOWN)&0x8000))//下方向键
		ball2_y+=step;
}

void gameover()
{
	EndBatchDraw();
	closegraph();
}

int main(void)
{
	startup();//数据的初始化
	while(1)
	{
		clean();//把之前绘制的内容取消
		updateWithoutInput();//与用户输入无关的更新
		updateWithInput();//与用户输入有关的更新
		show();//显示新画面
	}
	gameover();//游戏结束,进行后续处理
	return 0;
}

效果图如下:
在这里插入图片描述

四、双人反弹球

代码如下:

#include<conio.h>
#include<graphics.h>
#include<Windows.h>
#define High 480//游戏画面尺寸
#define Width 640

//全局变量
int ball_x,ball_y;//小球的坐标
int ball_vx,ball_vy;//小球2的速度
int bar1_left,bar1_right,bar1_top,bar1_bottom;//挡板1的上下左右位置坐标
int bar2_left,bar2_right,bar2_top,bar2_bottom;//挡板2的上下左右位置坐标
int bar_height,bar_width;//挡板的高度和宽度
int radius;

void startup()//数据的初始化
{
	ball_x=Width/2;
	ball_y=High/2;
	ball_vx=1;
	ball_vy=1;
	radius=20;

	bar_width=Width/30;
	bar_height=High/4;

	bar1_left=Width*1/20;//挡板1的数据初始化
	bar1_top=High/4;
	bar1_right=bar1_left+bar_width;
	bar1_bottom=bar1_top+bar_height;

	bar2_left=Width*18.5/20;//挡板2的数据初始化
	bar2_top=High/4;
	bar2_right=bar2_left+bar_width;
	bar2_bottom=bar2_top+bar_height;
	initgraph(Width,High);
	BeginBatchDraw();
}

void clean()//消除画面
{
	setcolor(BLACK);
	setfillcolor(BLACK);
	fillcircle(ball_x,ball_y,radius);
	fillcircle(ball_x,ball_y,radius);
	bar(bar1_left,bar1_top,bar1_right,bar1_bottom);//绘制挡板
	bar(bar2_left,bar2_top,bar2_right,bar2_bottom);
}

void show()//显示画面
{
	setcolor(GREEN);
	setfillcolor(GREEN);
	fillcircle(ball_x,ball_y,radius);//绘制绿圆
	setcolor(RED);
	setfillcolor(RED);
	bar(bar1_left,bar1_top,bar1_right,bar1_bottom);
	bar(bar2_left,bar2_top,bar2_right,bar2_bottom);
	FlushBatchDraw();
	Sleep(3);
}

void updateWithoutInput()//与用户输入无关的更新
{
	//挡板和小球碰撞,小球反弹
	if(ball_x+radius>=bar2_left&&ball_y+radius>=bar2_top&&ball_y+radius<bar2_bottom)
		ball_vx=-ball_vx;
	else if(ball_x-radius<=bar1_right&&ball_y+radius>=bar1_top&&ball_y+radius<bar1_bottom)
		ball_vx=-ball_vx;

	//更新小球的坐标
	ball_x=ball_x+ball_vx;
	ball_y=ball_y+ball_vy;

	if((ball_x<=radius)||(ball_x>+Width-radius))
		ball_vx=-ball_vx;
	if((ball_y<=radius)||(ball_y>+High-radius))
		ball_vy=-ball_vy;

}

void updateWithInput()//与用户输入有关的更新
{
	int step=1;
	if((GetAsyncKeyState(0x57)&0x8000))//w
		bar1_top-=step;
	if((GetAsyncKeyState(0x53)&0x8000))//s
		bar1_top+=step;
	if((GetAsyncKeyState(VK_UP)&0x8000))//上方向键
		bar2_top-=step;
	if((GetAsyncKeyState(VK_DOWN)&0x8000))//下方向键
		bar2_top+=step;

	if(bar1_top<0)//判断挡板是否超过屏幕
		bar1_top+=step;
	if(bar2_top<0)
		bar2_top+=step;
	if(bar1_top+bar_height>High)
		bar1_top-=step;
	if(bar2_top+bar_height>High)
		bar2_top-=step;

	bar1_bottom=bar1_top+bar_height;
	bar2_bottom=bar2_top+bar_height;
}

void gameover()
{
	EndBatchDraw();
	closegraph();
}

int main(void)
{
	startup();//数据的初始化
	while(1)
	{
		clean();//把之前绘制的内容取消
		updateWithoutInput();//与用户输入无关的更新
		updateWithInput();//与用户输入有关的更新
		show();//显示新画面
	}
	gameover();//游戏结束,进行后续处理
	return 0;
}

效果图如下:
在这里插入图片描述

本文地址:https://blog.csdn.net/qq_46527915/article/details/107882280

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网