c语言编写双人弹跳小球游戏

/*
	日期:2019年3月2日
	项目:双人弹跳球游戏 
*/ 
#include<stdio.h>
#define MAX 100
#include<windows.h>
#include<conio.h>

int width, high;	//游戏边界 
int player1_x, player1_y, player2_x, player2_y;	//定义玩家位置
int ball_x, ball_y;	//球的位置
int map[MAX][MAX]; 
int score1, score2;	//分数 
int speed_x, speed_y;	//小球速度 

void start()	//初始化变量 
{
	width = 40;
	high = 20;
	
	player1_x = 2;
	player1_y = high/2;
	player2_x = width-2;
	player2_y = high/2;
	
	ball_x = width/2;
	ball_y = high/2;
	
	score1 = 0;
	score2 = 0;
	
	speed_x = 1;
	speed_y = 1;
}
/*左右边框为1 下边框为2 玩家挡板为3 小球为4 空白窗体为0*/
void startMap()	//初始化地图 
{
	for(int i=0; i<=high; i++)
		map[i][width] = 1;
	for(int i=0; i<=high; i++)
		map[i][0] = 1; 
	for(int i=1; i<width; i++)
		map[high][i] = 2;
	for(int i=0; i<high; i++)
	{
		for(int j=1; j<width; j++)
		{
			map[i][j] = 0;
		}
	} 
	for(int i=player1_y-2; i<=player1_y+2; i++)
		map[i][player1_x] = 3; 
	for(int i=player2_y-2; i<=player2_y+2; i++)
		map[i][player2_x] = 3;
		
	map[ball_y][ball_x] = 4; 
}
/*         隐藏光标       */ 
void hidCursor()
{
	CONSOLE_CURSOR_INFO cursor_info = { 1, 0 };	//头文件windows.h 
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
} 
/*        清除部分屏幕   */ 
void gotoxy(int x, int y)
{
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); //头文件windows.h
    COORD pos;	//pose表示一个结构体变量,COORD表示一个字符在控制台屏幕上的坐标 
    pos.X = width;
    pos.Y = high;
    SetConsoleCursorPosition(handle, pos);
}
void updateWithoutInput()//与输入无关的更新 
{
	ball_x = ball_x + speed_x;
	ball_y = ball_y + speed_y;
	/*            小球触碰挡板时        */
	for(int i = player1_y-2; i<=player1_y+2; i++)
	{
		if((ball_x == player1_x) && (ball_y == i))
		{
		speed_x = -speed_x;
		printf("\a");
		}	
	}
	for(int i = player2_y-2; i<=player2_y+2; i++)
	{
		if((ball_x == player2_x) && (ball_y == i))
		{
		speed_x = -speed_x;
		printf("\a");
		}	
	}
	/*           小球触碰左右边界时         */ 
	if(ball_x==width)
	{
		score1++;
		printf("\a");
		
		player1_x = 2;
		player1_y = high/2;
		player2_x = 38;
		player2_y = high/2;
	
		ball_x = width/2;
		ball_y = high/2;
	}
	if(ball_x==0)
	{
		score2++;
		printf("\a");
		
		player1_x = 2;
		player1_y = high/2;
		player2_x = 38;
		player2_y = high/2;
	
		ball_x = width/2;
		ball_y = high/2;
	} 
	/*            小球触碰上下边界时      */
	if((ball_y==0) || (ball_y==high))
	{
		speed_y = -speed_y;
	}	
} 

void updateWithInput()	//与输入相关的更新 
{
	char input;
	if(kbhit())
	{
		input = getch();
		
		if(input == 'w')
			player1_y--;
		if(input == 's')
			player1_y++;
		if(input == 'i')
			player2_y--;
		if(input == 'k')
			player2_y++;
	}	
}

void show()
{
	gotoxy(0, 0); 
	for(int i=0; i<=high; i++)
	{
		for(int j=0; j<=width; j++)
		{
			if(map[i][j]==1)
				printf("|");
			if(map[i][j]==2)
				printf("~");
			if(map[i][j]==3)
				printf("*");
			if(map[i][j]==4)
				printf("o");
			if(map[i][j]==0)
				printf(" ");
		}
		printf("\n");
	}
	printf("\n*****	弹跳小球	*****\n");
	printf("玩家一得分:%d	玩家二得分:%d\n",score1, score2);
	printf("操作说明:WS控制玩家一移动 IK控制玩家二移动\n");
	printf("*****	小球弹入左右边框积一分	*****\n");
}
int main(void)
{	
	start();
	while(1)
	{
		hidCursor();
		startMap();
		show();
		updateWithoutInput();
		updateWithInput();
		
	} 
	
	return 0;
} 

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
您好!关于c语言编写斜着弹跳的小球,可以按照以下步骤进行: 1. 定义小球的初始位置、速度和加速度等参数。 2. 使用while循环模拟小球的运动轨迹,每隔一段时间更新小球的位置和速度。 3. 判断小球是否落地,若未落地则继续运动,否则结束循环。 4. 在更新小球位置时,需要考虑小球的弹性碰撞,即小球碰到地面或墙壁时会反弹,反弹后速度方向会发生改变。 下面是一个简单的示例代码,供参考: ```c #include <stdio.h> #include <stdlib.h> #define GRAVITY 9.8 // 重力加速度 #define BOUNCE_FACTOR 0.8 // 弹性系数 int main() { double x = 0, y = 0; // 小球初始位置 double vx = 10, vy = 0; // 小球初始速度 double ax = 0, ay = GRAVITY; // 小球加速度 double dt = 0.01; // 时间步长 double t = 0; // 时间 double ground = 0; // 地面高度 double wall = 100; // 墙壁位置 while (1) { // 计算小球新位置 x += vx * dt; y += vy * dt + 0.5 * ay * dt * dt; // 判断小球是否碰到墙壁 if (x >= wall) { x = wall; vx = -vx * BOUNCE_FACTOR; } // 判断小球是否碰到地面 if (y <= ground) { y = ground; vy = -vy * BOUNCE_FACTOR; } // 计算小球新速度 vx += ax * dt; vy += ay * dt; // 更新时间 t += dt; // 输出小球位置和时间 printf("time: %.2f, x: %.2f, y: %.2f\n", t, x, y); // 判断小球是否落地,若落地则结束循环 if (y == ground && vy < 0) { break; } } return 0; } ``` 希望能够帮助到您!如果还有其他问题,请随时提出。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

怀逸%

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值