C语言写简单的小游戏-挡板接小方块

1.写出一个大致的框架,绘图边界和画布。实现小功能,球可以自由的弹。接触到边界和挡板则反向。

	#include <stdio.h>
	#include <stdlib.h>
	#include <Windows.h>


	//清屏,用于代替系统的"cls"清屏功能
	void gotoxy(int x , int y)
	{
		HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
		COORD pos;
		pos.X = x;
		pos.Y = y;
		SetConsoleCursorPosition(handle,pos);
	}

	//隐藏光标
	//void HideCursor()
	//{
	//	CONSOLE_CURSOR_INFO concur_info = {1, 0};
	//	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),concur_info);
	//} 

	//作为全局变量进行定义
	int width,high;//作为画布的大小

	int pos_x,pos_y; //作为滑板的定位
	int radius;  //挡板的半径
	int left,right;  //挡板的左右边界

	int ball_x,ball_y;  //作为球的定位
	int ball_vx,ball_vy;  //作为球的运动速度

	int target_x,target_y;  //作为目标的定位

	void init()  //画面数据初始化
	{
		width = 20;
		high = 25;

		ball_x = 1;
		ball_y = width/2;

		ball_vx =1;
		ball_vy =1;
		
		pos_x = width/2;
		pos_y = high;
		radius = 5;
		left = pos_x - radius;
		right = pos_y + radius;

		//HideCursor();  //隐藏光标
	}

	void show()  //画面展示
	{
		gotoxy(0,0);  //自定义的清屏函数,用于代替系统的“cls”
		for(int i = 0;i <= high ; i++)
		{
			for (int j = 0; j <= width ; j++)
			{
				if((ball_x ==i)  && (ball_y == j))
					printf("*");
				else if (j == width)
					printf("||");
				else if(i == high)
					printf("=");
				else
					printf(" ");
			}
			printf("\n");
		}
	}

	void userInput()  //用户输入
	{
		
	}

	void userWithOut()  //游戏限制和规则
	{
		//球根据球速的变化,更新球的位置
		ball_x = ball_x +ball_vx;   //倾斜弹球
		ball_y = ball_y +ball_vy;

		//碰到了球的边界,则反向
		if((ball_x == 0) || (ball_x == high-1))
			ball_vx = -ball_vx;
		if((ball_y == 0) || (ball_y == width-1))
			ball_vy = -ball_vy;
	}

	int main()
	{
		//初始化
		init();
		while(1)
		{
			//显示画面,绘图
			show();
			//用户输入
			userInput();
			//与用户无关
			userWithOut();
		}
		

	}

 2.实现挡板功能,用于接球。

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <Windows.h>


	//清屏,用于代替系统的"cls"清屏功能
	void gotoxy(int x , int y)
	{
		HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
		COORD pos;
		pos.X = x;
		pos.Y = y;
		SetConsoleCursorPosition(handle,pos);
	}

	//隐藏光标
	void HideCursor()
	{
		CONSOLE_CURSOR_INFO concur_info = {1, 0};
		SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&concur_info);
	} 

	//作为全局变量进行定义
	int width,high;//作为画布的大小

	int pos_x,pos_y; //作为滑板的定位
	int radius;  //挡板的半径
	int left,right;  //挡板的左右边界

	int ball_x,ball_y;  //作为球的定位
	int ball_vx,ball_vy;  //作为球的运动速度
	int ball_num;   //积分

	int target_x,target_y;  //作为目标的定位

	void init()  //画面数据初始化
	{
		width = 40;
		high = 42;

		ball_x = 1;
		ball_y = width/2;

		ball_vx =1;
		ball_vy =1;
		ball_num = 0;
		
		pos_y = width/2;
		pos_x = high;
		radius = 2;
		left = pos_y - radius;
		right = pos_y + radius;

		HideCursor();  //隐藏光标
	}

	void show()  //画面展示
	{
		gotoxy(0,0);  //自定义的清屏函数,用于代替系统的“cls”
		int i,j;
		for( i= 0;i <= high ; i++)
		{
			for (j = 0; j <= width ; j++)
			{
				if((ball_x ==i)  && (ball_y == j))
					printf("o");
				else if (j == width)
					printf("|");
				else if(i == high)
					printf("-");
				else if ((i == high-1) && (j>=left) && (j<=right))
					printf("*");
				else
					printf(" ");
			}
			printf("\n");
			//Sleep(20);
		}
		printf("反弹总分:%d \n",ball_num);
	}

	void userInput()  //用户输入
	{
		char input;
		if(kbhit())
		{
			input =getch();

			if(input =='a')
			{
				pos_y--;
				left = pos_y - radius;
				right = pos_y + radius;
			}
			if(input =='d')
			{
				pos_y++;
				left = pos_y - radius;
				right = pos_y + radius;
			}
		}
		
	}

	void userWithOut()  //游戏限制和规则
	{
		if(ball_x == high-1)
		{
			if ((ball_y>= left) && (ball_y<=right))
			{
				ball_num++;
				//ball_vy = -ball_vy;
				ball_vx = -ball_vx;
			}
			else
			{
				printf("Game Over!!!");
				exit(0);
			}
		}

		//球根据球速的变化,更新球的位置
		ball_x = ball_x +ball_vx;   //倾斜弹球d
		ball_y = ball_y +ball_vy;

		//挡板不能国过界限
		if(pos_y < 0)
			pos_y = 0;
		else if(pos_y>width)
			pos_y = width;



		//碰到了球的边界,则反向
		if((ball_x == 0) || (ball_x == high-1))
			ball_vx = -ball_vx;
		if((ball_y == 0) || (ball_y == width-1))
			ball_vy = -ball_vy;


	}

	int main()
	{
		//初始化
		init();
		while(1)
		{
			//显示画面,绘图
			show();
			//用户输入
			userInput();
			//与用户无关
			userWithOut();
		}
		

	}

 

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个简单弹力游戏的C语言代码,你可以在C语言编译器上运行它: ```c #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <windows.h> #include <time.h> #define PADDLE_WIDTH 10 #define PADDLE_HEIGHT 1 #define BALL_SYMBOL 'O' #define PADDLE_SYMBOL '-' #define BRICK_SYMBOL '#' #define WALL_SYMBOL '|' #define GAME_WIDTH 60 #define GAME_HEIGHT 30 #define MAX_BRICKS 100 #define MAX_LEVELS 3 int paddle_position = GAME_WIDTH / 2 - PADDLE_WIDTH / 2; int ball_x = GAME_WIDTH / 2; int ball_y = GAME_HEIGHT - 2; int ball_speed_x = 1; int ball_speed_y = -1; int bricks[MAX_BRICKS][2]; int num_bricks = 0; int level = 1; int score = 0; void gotoxy(int x, int y) { COORD pos = { x, y }; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos); } void clear_screen() { system("cls"); } void draw_paddle() { int i, j; for (i = 0; i < PADDLE_HEIGHT; i++) { for (j = 0; j < PADDLE_WIDTH; j++) { gotoxy(paddle_position + j, GAME_HEIGHT - 1 - i); printf("%c", PADDLE_SYMBOL); } } } void draw_ball() { gotoxy(ball_x, ball_y); printf("%c", BALL_SYMBOL); } void draw_wall() { int i, j; for (i = 0; i < GAME_HEIGHT; i++) { gotoxy(0, i); printf("%c", WALL_SYMBOL); gotoxy(GAME_WIDTH - 1, i); printf("%c", WALL_SYMBOL); } for (j = 0; j < GAME_WIDTH; j++) { gotoxy(j, 0); printf("%c", WALL_SYMBOL); } } void draw_bricks() { int i; for (i = 0; i < num_bricks; i++) { gotoxy(bricks[i][0], bricks[i][1]); printf("%c", BRICK_SYMBOL); } } void move_paddle_left() { if (paddle_position > 0) { paddle_position--; } } void move_paddle_right() { if (paddle_position < GAME_WIDTH - PADDLE_WIDTH) { paddle_position++; } } void move_ball() { ball_x += ball_speed_x; ball_y += ball_speed_y; if (ball_x == 0 || ball_x == GAME_WIDTH - 1) { ball_speed_x = -ball_speed_x; } if (ball_y == 0) { ball_speed_y = -ball_speed_y; } if (ball_y == GAME_HEIGHT - 1) { printf("Game over!\n"); exit(0); } if (ball_y == GAME_HEIGHT - 2 && ball_x >= paddle_position && ball_x < paddle_position + PADDLE_WIDTH) { ball_speed_y = -ball_speed_y; score++; } int i; for (i = 0; i < num_bricks; i++) { if (ball_x == bricks[i][0] && ball_y == bricks[i][1]) { ball_speed_y = -ball_speed_y; score += 10; bricks[i][0] = -1; bricks[i][1] = -1; } } } void init_bricks() { int i; for (i = 0; i < GAME_WIDTH; i += 2) { bricks[num_bricks][0] = i; bricks[num_bricks][1] = 3; num_bricks++; } } void next_level() { clear_screen(); ball_x = GAME_WIDTH / 2; ball_y = GAME_HEIGHT - 2; ball_speed_x = 1; ball_speed_y = -1; paddle_position = GAME_WIDTH / 2 - PADDLE_WIDTH / 2; num_bricks = 0; level++; init_bricks(); } int main() { srand(time(NULL)); init_bricks(); while (1) { clear_screen(); draw_wall(); draw_bricks(); draw_paddle(); draw_ball(); move_ball(); if (score == num_bricks * 10) { if (level == MAX_LEVELS) { printf("You won!\n"); exit(0); } next_level(); } if (_kbhit()) { char c = _getch(); if (c == 'a') { move_paddle_left(); } if (c == 'd') { move_paddle_right(); } } Sleep(50); } return 0; } ``` 注意:此代码仅供参考。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值