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

Linux 游戏

2018年03月20日 ⁄ 综合 ⁄ 共 6178字 ⁄ 字号 评论关闭
#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 is_lose = 0;
int is_win = 0;
int num_bricks = 0;


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

	while( (ch = getch()) != 'q' )
	{
		if(ch == 'j')  // 挡板左移
		{
			if(baffle.x_left >= LEFT)
			{
				move(BOTTOM, baffle.x_right);
				addch(' ');
				baffle.x_left--;
				baffle.x_right--;
				move(BOTTOM, baffle.x_left);
				addch('=');
				move(BOTTOM, 0);
				refresh();
			}
		}

		else if(ch == 'k')		// 挡板右移
		{
			if(baffle.x_right <= RIGHT)
			{
				move(BOTTOM, baffle.x_left);
				addch(' ');
				baffle.x_left++;
				baffle.x_right++;
				move(BOTTOM, baffle.x_right);
				addch('=');
				move(BOTTOM, 0);
				refresh();
			}
		}
	}

}


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;
		num_bricks++;
	}

	// 左下到右上的部分
	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;
		num_bricks++;
	}

	// 中间横向的部分
	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;
		num_bricks++;
	}
	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);


}




void ball_move()
{
	/*
	该函数用于实现小球移动的功能
	*/  

	int x_index, y_index;
	int differ;
	int i;

	signal(SIGALRM,moveBall);
	move(ball.x_poi, ball.y_poi);
	addch(BLANK_BALL);
	ball.x_poi += ball.x_speed;
	ball.y_poi += ball.y_speed;

	if(ball.x_poi >= RIGHT || ball.x_poi <= LEFT)
		ball.x_speed = -ball.x_speed;
	if(ball.y_poi <= TOP)
		ball.y_speed = -ball.y_speed;

	if(ball.y_poi >= BOTTOM - 1)
	{
		if(ball.x_poi >= baffle.x_left && ball.x_poi <= baffle.x_right)
			ball.y_speed = -ball.y_speed;
		else
		{
			// 输掉游戏
		}
	}

	// 假如小球落入砖块区域内
	if(ball.x_poi >= BRICK_LEFT && ball.x_poi <= BRICK_RIGHT && ball.y_poi >= BRICK_TOP && ball.y_poi <= BRICK_BOTTOM)
	{
		x_index = ball.x_poi - BRICK_LEFT;
		y_index = ball.y_poi - BRICK_TOP;
		if(matrix[y_index][x_index] == 1)
		{
			differ = x_index % 3;
			for(i = 0; i < 3; i++)
				matrix[y_index][x_index - differ + i] = 0;
			move(ball.y_poi, ball.x_poi - differ);
			addch("   ");
			move(BOTTOM, 0);
			refresh();
			num_bricks--;
		}
	}

	move(ball.pos_y, ball.pos_x);
	addch(SYMBOL_BALL);
	move(BOTTOM, 0);
	refresh();

}



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 <signal.h>  
#include <sys/time.h>  
#include <stdio.h>  
#include <sys/time.h>  
#include "header.h"

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

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

int is_lose = 0;
int is_win = 0;
int num_bricks = 0;


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

	while( (ch = getch()) != 'q' )
	{
		if(ch == 'j')  // 挡板左移
		{
			if(baffle.x_left >= LEFT)
			{
				move(BOTTOM, baffle.x_right);
				addch(' ');
				baffle.x_left--;
				baffle.x_right--;
				move(BOTTOM, baffle.x_left);
				addch('=');
				move(BOTTOM, 0);
				refresh();
			}
		}

		else if(ch == 'k')		// 挡板右移
		{
			if(baffle.x_right <= RIGHT)
			{
				move(BOTTOM, baffle.x_left);
				addch(' ');
				baffle.x_left++;
				baffle.x_right++;
				move(BOTTOM, baffle.x_right);
				addch('=');
				move(BOTTOM, 0);
				refresh();
			}
		}
	}

}


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

	*/

	int i,j;

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

	// 初始矩阵
	for(i = 0; i < 5; i++)
	{
		for(j = 0; j < 15; j++)
		{
			matrix[i][j] = 0;
		}
	}
	
	clear();
		

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

	// 初始挡板信息
	baffle.x_left = X_INIT_LEFT;
	baffle.x_right = X_INIT_RIGHT;
	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;
		num_bricks++;
	}

	// 左下到右上的部分
	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;
		num_bricks++;
	}

	// 中间横向的部分
	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;
		num_bricks++;
	}
	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);


}




void ball_move()
{
	/*
	该函数用于实现小球移动的功能
	*/  

	int x_index, y_index;
	int differ;
	int i;

	signal(SIGALRM,moveBall);
	move(ball.y_poi, ball.x_poi);
	addch(BLANK_BALL);
	ball.x_poi += ball.x_speed;
	ball.y_poi += ball.y_speed;

	if(ball.x_poi >= RIGHT || ball.x_poi <= LEFT)
		ball.x_speed = -ball.x_speed;
	if(ball.y_poi <= TOP)
		ball.y_speed = -ball.y_speed;

	if(ball.y_poi >= BOTTOM - 1)
	{
		if(ball.x_poi >= baffle.x_left && ball.x_poi <= baffle.x_right)
			ball.y_speed = -ball.y_speed;
		else
		{
			// 输掉游戏
		}
	}

	// 假如小球落入砖块区域内
	if(ball.x_poi >= BRICK_LEFT && ball.x_poi <= BRICK_RIGHT && ball.y_poi >= BRICK_TOP && ball.y_poi <= BRICK_BOTTOM)
	{
		x_index = ball.x_poi - BRICK_LEFT;
		y_index = ball.y_poi - BRICK_TOP;
		if(matrix[y_index][x_index] == 1)
		{
			differ = x_index % 3;
			for(i = 0; i < 3; i++)
				matrix[y_index][x_index - differ + i] = 0;
			move(ball.y_poi, ball.x_poi - differ);
			addstr("   ");
			move(BOTTOM, 0);
			refresh();
			num_bricks--;
		}
	}

	move(ball.y_poi, ball.x_poi);
	addch(SYMBOL_BALL);
	move(BOTTOM, 0);
	refresh();

}



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);

}
【上篇】
【下篇】

抱歉!评论已关闭.