栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

基于curses库实现弹球游戏

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

基于curses库实现弹球游戏

在网上找到,某人在基于Linux终端,用curses库实现的弹球游戏。本人曾经也做过五子棋游戏,分在其它文章中分享。


#include 
#include 
#include 
 
#define RIGHT COLS-1 
#define BOTTOM LINES-1 
#define BOARD_LENGTH 10 
#define LEFT 0 
#define TOP 0 
char BALL= 'O'; 
char BLANK= ' '; 
 
 
int left_board; 
int right_board; 
int is_lose=0;
 
 
int hdir; 
int vdir; 
int pos_X; 
int pos_Y; 
  
int delay=100;
void moveBall();
void init();
void control();
 
int main()
{
 //初始化 curses
 initscr();
 crmode(); 
 noecho(); 
  
 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'){ 
  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(); 
 return 0;
}
 
void init(){
 int i,j;
 clear();
 if(start_color()==OK){ 
  attron(A_BOLD); 
  init_pair(1,COLOR_YELLOW,COLOR_BLACK);
  attron(COLOR_PAIR(1));
 }
 //初始球
 pos_X =22; 
 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('-');
 }
 
 //初始刷新时间
 signal(SIGALRM,moveBall);
 set_ticker(delay);
 
 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;
  beep(); 
 }
 if(pos_X <= LEFT) { 
  hdir = 1;
  beep(); 
 }
 if(pos_Y <= TOP) { 
  vdir = 1;
  beep(); 
 }
 
 //当球在底部的时候进行额外的处理
 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();
  }
 }
 //不改变球的方向时
 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; //强制退出游戏
   //挡板左移
   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

用gcc BounceBall.c -lcurses编译后,生成BounceBall.out , 运行 ./BounceBall.out , 游戏截图如下:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/61937.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号