基于curses庫(kù)實(shí)現(xiàn)彈球游戲
在網(wǎng)上找到,某人在基于Linux終端,用curses庫(kù)實(shí)現(xiàn)的彈球游戲。本人曾經(jīng)也做過(guò)五子棋游戲,分在其它文章中分享。
/*
* 基于curses庫(kù)彈球游戲,編譯gcc xxx.c -lcurses
*/
#include <curses.h>
#include <sys/time.h>
#include <signal.h>
#define RIGHT COLS-1 /*球所能到達(dá)的當(dāng)前屏幕最大水平范圍*/
#define BOTTOM LINES-1 /*球所能到達(dá)的當(dāng)前屏幕最大垂直范圍*/
#define BOARD_LENGTH 10 /*擋板長(zhǎng)度*/
#define LEFT 0 /*當(dāng)前屏幕的最左邊*/
#define TOP 0 /*當(dāng)前屏幕的最上邊*/
char BALL= 'O'; /*球的形狀*/
char BLANK= ' '; /*覆蓋球走過(guò)的軌跡*/
int left_board; /*擋板左側(cè)坐標(biāo)*/
int right_board; /*擋板右側(cè)坐標(biāo)*/
int is_lose=0;
int hdir; /*控制球水平運(yùn)動(dòng)的變量*/
int vdir; /*控制球垂直運(yùn)動(dòng)的變量*/
int pos_X; /*球的橫坐標(biāo)*/
int pos_Y; /*球的縱坐標(biāo)*/
int delay=100;
void moveBall();
void init();
void control();
int main()
{
//初始化 curses
initscr();
crmode(); /*中斷模式*/
noecho(); /*關(guān)閉回顯*/
move(6,28);
attron(A_BOLD);
addstr("Welcome to the BallGame!");
move(8,20);
attroff(A_BOLD);
addstr("Help:");
move(9,23);
addstr("'N':Start a new game.");
move(10,23);
addstr("'Q':Quit game.");
move(11,23);
addstr("'KEY_LEFT' :Control baffle left shift.");
move(12,23);
addstr("'KEY_RIGHT':Control baffle right shift.");
move(13,23);
addstr("'KEY_UP' :Control of the ball speed.");
move(14,23);
addstr("'KEY_DOWN' :Control of the ball reducer.");
int flag=1;
char choice;
move(16,24);
addstr("Please choose your choice!(n/q):");
refresh();
choice=getch();
while(flag){
if(choice=='q'||choice=='Q'||choice=='n'||choice=='N')
flag=0;
else choice=getch();
}
if(choice=='n'||choice=='N'){ /*開(kāi)始游戲*/
clear();
move(10,25);
addstr("BallGame will start! Are you read?");
refresh();
sleep(3);
control();
}
else if(choice=='q'||choice=='Q'){ /*退出游戲*/
clear();
move(10,25);
addstr("You quit the game successfully!");
refresh();
sleep(3);
endwin();
}
endwin(); /*結(jié)束 curses*/
return 0;
}
void init(){
int i,j;
clear();
if(start_color()==OK){ /*改變球和擋板的顏色*/
attron(A_BOLD); /*打開(kāi)粗體*/
init_pair(1,COLOR_YELLOW,COLOR_BLACK);
attron(COLOR_PAIR(1));
}
//初始球
pos_X =22; /*球初始的橫坐標(biāo)*/
pos_Y = BOTTOM-1; /*球初始的縱坐標(biāo)*/
//初始化球的運(yùn)動(dòng)方向,朝右上方運(yùn)動(dòng)
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('-');
}
//初始刷新時(shí)間
signal(SIGALRM,moveBall);
set_ticker(delay);
keypad(stdscr,TRUE); /*打開(kāi) keypad 鍵盤響應(yīng)*/
attroff(A_BLINK); /*關(guān)閉 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;
//改變球的方向時(shí)
if(pos_X >= RIGHT) { /*當(dāng)球橫坐標(biāo)大于右邊邊緣時(shí),球反彈朝左運(yùn)動(dòng)*/
hdir = -1;
beep(); /*球撞墻時(shí),發(fā)出聲音*/
}
if(pos_X <= LEFT) { /*當(dāng)球橫坐標(biāo)大于左邊邊緣時(shí),球反彈朝右運(yùn)動(dòng)*/
hdir = 1;
beep(); /*球撞墻時(shí),發(fā)出聲音*/
}
if(pos_Y <= TOP) { /*當(dāng)球縱坐標(biāo)大于頂部邊緣時(shí),球反彈朝下運(yùn)動(dòng)*/
vdir = 1;
beep(); /*球撞墻時(shí),發(fā)出聲音*/
}
//當(dāng)球在底部的時(shí)候進(jìn)行額外的處理
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*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();
}
}
//不改變球的方向時(shí)
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=='q'||cmd=='Q'||cmd==27) break; //強(qiáng)制退出游戲
//擋板左移
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/=2;
set_ticker(delay);
}
//給球減速
else if(cmd==KEY_DOWN){
delay*=2;
set_ticker(delay);
}
}
else{
//輸?shù)羟蚝蟮奶幚?
int flag=1;
char choice;
move(8,15);
addstr("Game Over!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'){ /*游戲重新開(kāi)始*/
delay=100; /*恢復(fù)球的初始速度*/
init();
continue;
}
else if(choice=='n'||choice=='N'){ /*結(jié)束游戲*/
break;
}
}
}
}
//設(shè)置定時(shí)器
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);
}
用gcc BounceBall.c -lcurses編譯后,生成BounceBall.out , 運(yùn)行 ./BounceBall.out , 游戲截圖如下:


以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于Matlab實(shí)現(xiàn)野狗優(yōu)化算法的示例代碼
野狗優(yōu)化算法(Dingo?Optimization?Algorithm,?DOA)模仿澳大利亞野狗的社交行為。DOA算法的靈感來(lái)源于野狗的狩獵策略,即迫害攻擊、分組策略和食腐行為。本文將通過(guò)Matlab實(shí)現(xiàn)這一算法,感興趣的可以了解一下2022-04-04
C++實(shí)現(xiàn)LeetCode(89.格雷碼)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(89.格雷碼),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
C++ accumulate函數(shù)詳細(xì)介紹和具體案例
這篇文章主要介紹了C++ accumulate函數(shù)詳細(xì)介紹和具體案例,accumulate是numeric庫(kù)中的一個(gè)函數(shù),主要用來(lái)對(duì)指定范圍內(nèi)元素求和,但也自行指定一些其他操作,如范圍內(nèi)所有元素相乘、相除等2022-08-08
數(shù)據(jù)結(jié)構(gòu)串的操作實(shí)例詳解
這篇文章主要介紹了數(shù)據(jù)結(jié)構(gòu)串的操作實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-07-07

