现在的位置: 首页 > 综合 > 正文

Linux C 弹球游戏

2018年03月20日 ⁄ 综合 ⁄ 共 5161字 ⁄ 字号 评论关闭
#include <signal.h>  
#include <sys/time.h>  
#include <stdio.h>  
#include <sys/time.h>  


void init();
void wrap_up();
void ball_move();

struct Ball ball;	// 小球
struct Baffle baffle;   // 挡板
int matrix[5][15];		// 记录砖块区域信息的矩阵


int main()
{
	int ch;
	
	init();

	while(1)
	{

	}

}


void init()
{
	/*
		该函数主要完成初始化的工作

	*/

	int i,j;

	initscr();
	crmode();
	noecho();

	// 初始矩阵
	for(i = 0; i < 5; i++)
	{
		for(j = 0; j < 15; j++)
		{
			matrix[i][j] = 0;
		}
	}
	initscr();
	box(stdscr, ACS_VLINE, ACS_HLINE);
	noecho();
	crmode();
		

	// 初始小球信息
	ball.x_poi = X_INIT_BALL;
	ball.y_poi = Y_INIT_BALL;
	ball.x_speed = X_SPEED_INIT; 
	ball.y_speed = Y_SPEED_INIT;
	ball.symbol = 'O';

	// 初始挡板信息
	baffle.x_left = X_INIT_LEFT;
	baffle.y_right = Y_INIT_LEFT;
	baffle.speed = SPEED_INIT_BAFFLE;


	// 绘制砖块,并修改矩阵的值(左上到右下的部分)
	attron(A_REVERSE);
	for(i = BRICK_TOP, j = BRICK_LEFT; i <= BRICK_BOTTOM; i++, j += 3)
	{
		move(i, j);
		addstr(BRICK_SYMBOL);
		matrix[i - BRICK_TOP][j - BRICK_LEFT] = matrix[i - BRICK_TOP][j - BRICK_LEFT + 1] = matrix[i - BRICK_TOP][j - BRICK_LEFT + 2] = 1;
	}

	// 左下到右上的部分
	for(i = BRICK_BOTTOM, j = BRICK_LEFT; i >= BRICK_TOP; i--, j+= 3)
	{
		move(i. j);
		addstr(BRICK_SYMBOL);
		matrix[i - BRICK_TOP][j - BRICK_LEFT] = matrix[i - BRICK_TOP][j - BRICK_LEFT + 1] = matrix[i - BRICK_TOP][j - BRICK_LEFT + 2] = 1;
	}

	// 中间横向的部分
	for(i = (BRICK_TOP + BRICK_BOTTOM) / 2, j = BRICK_LEFT; j <= BRICK_RIGHT; j += 6)
	{
		move(i, j);
		addstr(BRICK_SYMBOL);
		matrix[i - BRICK_TOP][j - BRICK_LEFT] = matrix[i - BRICK_TOP][j - BRICK_LEFT + 1] = matrix[i - BRICK_TOP][j - BRICK_LEFT + 2] = 1;
	}
	attroff(A_REVERSE);
	refresh();

	// 绘制小球和挡板
	move(ball.y_poi, ball.x_poi);
	addch(SYMBOL_BALL);

	move(BOTTOM, baffle.x_left);
	addstr(SYMBOL_BAFFLE);

	move(BOTTOM, 0);
	refresh();

	signal(SIGALRM, ball_move);
	set_ticker(100);


}




int set_ticker(int n_msecs){
	struct itimerval new_timeset;	
	long n_sec,n_usecs;
	n_sec=n_msecs/1000;
	n_usecs=(n_msecs%1000)*1000L;
	new_timeset.it_interval.tv_sec=n_sec;
	new_timeset.it_interval.tv_usec=n_usecs;
	new_timeset.it_value.tv_sec=n_sec;
	new_timeset.it_value.tv_usec=n_usecs;
	return setitimer(ITIMER_REAL,&new_timeset,NULL);

}

#include <curses.h>


/*
	关于游戏区域的常量定义
*/

#define TOP 0			// 游戏区域的上边界
#define BOTTOM LINES - 1		// 游戏区域的下边界
#define LEFT 0			// 游戏区域的左边界
#define RIGHT COLS - 1;    // 游戏区域的右边界



/*
 * 关于挡板的常量定义
*/

#define BLANK_BAFFLE "        "    // 用于擦除使用
#define SYMBOL_BAFFLE "========"   // 挡板的字符化表示
#define X_INIT_LEFT COLS / 2 - 4		// 挡板左部 X 坐标的初始值
#define X_INIT_RIGHT COLS / 2 + 3		// 挡板右部 X 坐标的初始值
#define SPEED_BAFFLE_INIT 1



/*
	关于球的常量定义
*/

#define BLANK_BALL ' '		// 用于擦除使用
#define SYMBOL_BALL 'O'		// 球的字符化表示
#define X_INIT_BALL COLS / 2		// 球 X 坐标的初始值
#define Y_INIT_BALL LINES - 1		// 球 Y 坐标的初始值
#define X_INIT_SPEED 1		// 球 X 坐标上的速度初始值
#define Y_INIT_SPEED 1		// 球 Y 坐标上的速度初始值



/*
	关于砖块区域的常量定义
*/

#define BRICK_LEFT = COLS / 2 - 6;
#define BRICK_RIGHT = COLS / 2 + 8;
#define BRICK_TOP = 1;
#define BRICK_BOTTOM = 5;
#define BRICK_SYMBOL "   ";



struct Ball
{
	int x_poi, y_poi;		// 球的 X,Y 坐标
	int	x_speed, y_speed;	// 球在 X,Y 方向的速度
	
	char symbol;			// 球的字符化表示
};


struct Baffle
{
	int x_left, x_right;		// 挡板的 X,Y 坐标
	int length;				// 挡板的长度
	int speed;				// 挡板的速度(仅在 X 方向上)
	char *symbol;			// 挡板的字符化表示
};

#include <curses.h>
#include <sys/time.h>
#include <signal.h>
#include <stdlib.h>

#define LEFT 0
#define RIGHT COLS-1
#define TOP 0
#define BOTTOM LINES-1
#define WIDE RIGHT-LEFT+1
#define BOARD_LENGTH   10

char BALL= 'O';
char BLANK= ' ';

int hdir;
int vdir;
int pos_X ;
int pos_Y;
	
int left_board;
int right_board;
int is_lose=0;

int delay=100;
int ndelay;
void moveBall();

void init(){
	int i,j;

	clear();

	//初始球
	pos_X =20;
	pos_Y = BOTTOM-1;
	hdir=1;
	vdir=-1;

	//初始挡板
	left_board=20;
	right_board=left_board+BOARD_LENGTH;
	for(i=left_board;i<=right_board;i++){
		move(BOTTOM,i);
		addch('=');
	}

	//初始刷新时间
	ndelay=2;
	signal(SIGALRM,moveBall);
	set_ticker(delay*ndelay);

	keypad(stdscr,TRUE);
	attroff(A_BLINK);
	is_lose=0;

	move(pos_Y,pos_X);
	addch(BALL);
	move(LINES-1, COLS-1);
	refresh();
	usleep(100000);
	move(LINES-1,COLS-1);
	refresh();
}

void moveBall(){
	if(is_lose) return;
	signal(SIGALRM,moveBall);
	move(pos_Y,pos_X);
	addch(BLANK);
	pos_X += hdir;
	pos_Y += vdir;

	//改变方向
	if(pos_X >= RIGHT)
		hdir = -1;
	if(pos_X <= LEFT)
		hdir = 1;
	if(pos_Y <= TOP)
		vdir = 1;

	//当球在底部的时候进行额外的处理
	if(pos_Y >= BOTTOM-1){
		if(pos_X>=left_board&&pos_X<=right_board)
			vdir=-1;
		else{
			is_lose=1;
			move(pos_Y,pos_X);
			addch(BALL);
			move(LINES-1, COLS-1);
			refresh();
			usleep(delay*ndelay*1000);
			move(pos_Y,pos_X);
			addch(BLANK);
			pos_X += hdir;
			pos_Y += vdir;
			move(pos_Y,pos_X);
			addch(BALL);
			move(LINES-1, COLS-1);
			refresh();
		}
	}

	move(pos_Y,pos_X);
	addch(BALL);
	move(LINES-1, COLS-1);
	refresh();
	
}
void control(){
	init();
	int cmd;
	while (1)
	{	
		if(!is_lose){
			cmd=getch();
			if(cmd==27) break;//退出
			//挡板左移
			else if(cmd==KEY_LEFT){
				if(left_board>0){
					move(BOTTOM,right_board);
					addch(' ');
					right_board--;
					left_board--;
					move(BOTTOM,left_board);
					addch('=');
					move(BOTTOM,RIGHT);
					refresh();
				}
			}	
			//挡板右移
			else if(cmd==KEY_RIGHT){
				if(right_board<RIGHT){
					move(BOTTOM,left_board);
					addch(' ');
					right_board++;
					left_board++;
					move(BOTTOM,right_board);
					addch('=');
					move(BOTTOM,RIGHT);
					refresh();
				}
			}
			else if(cmd==KEY_UP){
				delay=delay/2;
				if(delay<10)delay=delay*2;	
			}
			else if(cmd==KEY_DOWN){
				delay=delay*2;
				if(delay>200)delay=delay/2;
			}
		
		}
		else{
			//输掉球后的处理
			int flag=1;
			char choice;
			move(6,10);
			addstr("You failed,try again?(y/n):");
			refresh();
			choice=getch();


			while(flag){
				if(choice=='y'||choice=='Y'||choice=='n'||choice=='N')
				 	flag=0;
				else  choice=getch();
			}
			if(choice=='y'||choice=='Y'){
					init();
					continue;
			}
			else if(choice=='n'||choice=='N'){
					break;
			}
		}
	}
}

int set_ticker(int n_msecs){
	struct itimerval new_timeset;	
	long n_sec,n_usecs;
	n_sec=n_msecs/1000;
	n_usecs=(n_msecs%1000)*1000L;
	new_timeset.it_interval.tv_sec=n_sec;
	new_timeset.it_interval.tv_usec=n_usecs;
	new_timeset.it_value.tv_sec=n_sec;
	new_timeset.it_value.tv_usec=n_usecs;
	return setitimer(ITIMER_REAL,&new_timeset,NULL);

}

int main()
{
	initscr();
	crmode();
	noecho();
	control();
	endwin();
	return 0;
}

【上篇】
【下篇】

抱歉!评论已关闭.