ReadMe:

弹球游戏按键说明(注意大小写):

Q End Up Game 游戏停止
P Play Again  再玩一次

f 速度x轴方向减速
s 速度x轴方向加速
F 速度y轴方向减速
S 速度y轴方向加速

J 移动挡板变短
L 移动挡板变长

4 移动挡板向左移动
6 移动挡板向右移动

补充说明:
游戏屏幕左上角显示当前得分和游戏状态( GameOn or GameOver )
游戏过程中忽略ctrl+c和ctrl+\信号,游戏结束后恢复信号

/×××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××/

    分。。。。割。。。。线

/×××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××/

 #include <stdio.h>

 #include <stdlib.h>

 #include <string.h>

 #include <curses.h>

 #include <signal.h>

 #include "bounce.h"

 #include <sys/time.h>

 bool set_up( bool );

 void wrap_up();

 void ball_move();

 void show_baffle();

 void in_bound( struct BallInfo * );

 int set_ticker( int );

 bool judge_in_bound( int ,int );

 void show_baffle();

 void try_move_my_moving_baffle( char );

 void show_my_moving_baffle();

 void cover_my_moving_baffle();

 void show_score_gameon();

 void show_score_gameoff();

 void clear_up();

 void solve_sigint();

 void solve_sigquit();

 bool ball_crash_brick();

 void show_my_moving_brick( int ,int );

 void cover_my_moving_brick( int ,int );

 struct BallInfo my_moving_ball;

 struct BaffleInfo my_moving_Baffle;

 struct Brick my_moving_brick[];

 int score;

 void clear_up(){

        int i,j;

        for( i=;i<;i++ ){

               for( j=;j<;j++ ){

                      mvaddch( i,j,BLANK );

               }

        }

        return ;

 }

 /********************************************************

        clear up the whole screen

 *********************************************************/

 bool set_up( bool flag ){

        char od;

        my_moving_ball.x_pos = startX;

        my_moving_ball.y_pos = startY;

        my_moving_ball.x_Time1 = my_moving_ball.x_Time2 = X_Time;

        my_moving_ball.y_Time1 = my_moving_ball.y_Time2 = Y_Time;

        my_moving_ball.x_dir = ;

        my_moving_ball.y_dir = ;

        my_moving_ball.ball_symbol = BALL;

        my_moving_Baffle.len = BaffleLen;

        my_moving_Baffle.baffle_symbol = BaffleSymbol;

        my_moving_Baffle.x = startX_;

        my_moving_Baffle.y = startY_;

        my_moving_brick[].x = TopRow+DELTA;

        my_moving_brick[].y = LeftCol+;

        my_moving_brick[].speed = speed1;

        my_moving_brick[].dir = ;

        my_moving_brick[].flag = true;

        score = ;

        initscr();

        noecho();

        crmode();

        keypad( stdscr,true );

        //attroff( A_BLINK );

        if( flag==true ){

               mvaddstr( startX-,startY,"Do you want play game (y/n)?" );

               mvaddstr( startX-,startY,"Press 's' to slow (x-axis)");

               mvaddstr( startX-,startY,"Press 'f' to fast (x-axis)");

               mvaddstr( startX,startY,"Press 'S' to slow (y-axis)");

               mvaddstr( startX+,startY,"Press 'F' to slow (y-axis)");

               mvaddstr( startX+,startY,"Press '4' to the left");

               mvaddstr( startX+,startY,"Press '6' to the right");

               mvaddstr( startX+,startY,"(Please refer to the ReadMe.)");

               move( LINES-,COLS- );

               refresh();

               od = getch();

               if( od=='y'||od=='Y' ){ clear_up(); }

               else {

                      wrap_up();

                      return false;

               }

        }

        /* 游戏最开始进入界面 */      

        show_score_gameon();

        signal( SIGINT,solve_sigint );

        signal( SIGQUIT,solve_sigquit );

        mvaddch( my_moving_ball.y_pos, my_moving_ball.x_pos, my_moving_ball.ball_symbol );

        show_baffle();

        show_my_moving_baffle();

        show_my_moving_brick( my_moving_brick[].x,my_moving_brick[].y );

        refresh();

        signal( SIGALRM, ball_move );

        //signal( SIGALRM, brick_move );

        set_ticker(  / TICKS_PER_SEC );

        return true;

 }

 /********************************************************

        启动二维弹球

        set up the game

 *********************************************************/

 void wrap_up(){

        set_ticker(  );

        beep();

        endwin();

 }

 /********************************************************

        终止二维弹球

        end up the game

 *********************************************************/

 void solve_sigint(){

        mvaddstr( ,,"You enter ctrl+C");

        return ;

 }

 /********************************************************

        solve the signal :ctrl + C

 *********************************************************/

 void show_my_moving_brick( int x,int y ){

        if( my_moving_brick[].flag==true ){

               mvaddstr( x,y,BRICK );

               move( LINES-,COLS- );

        }

        return ;

 }

 void cover_my_moving_brick( int x,int y ){

        mvaddstr( x,y,"     " );

        move( LINES-,COLS- );

        return ;

 }

 /********************************************************

        show my moving brick

 *********************************************************/

 void solve_sigquit(){

        mvaddstr( ,,"You enter ctrl+\\");

        return ;

 }

 /********************************************************

        solve the signal :ctrl + \

 *********************************************************/

 void show_score_gameon(){

        move( , );

        addstr( Tip1 );

        char str1[  ],str2[  ];

        int cnt1 = ,cnt2 = ;

        int tp = score/;

        int i;

        while( tp ){

               str1[ cnt1++ ] = tp% + '';

               tp /= ;

        }

        for( i=cnt1-;i>=;i-- ){

               str2[ cnt2++ ] = str1[ i ];

        }

        if( cnt2== ) str2[ cnt2++ ] = '';

        str2[ cnt2 ] = '\0';

        addstr( "\nYour Score:" );

        addstr( str2 );

        move( LINES-,COLS- );

 }

 /********************************************************

        显示得分

        show the score

 *********************************************************/

 void show_score_gameoff(){

        move( , );

        addstr( Tip2 );

        char str1[  ],str2[  ];

        int cnt1 = ,cnt2 = ;

        int tp = score;

        int i;

        while( tp ){

               str1[ cnt1++ ] = tp% + '';

               tp /= ;

        }

        for( i=cnt1-;i>=;i-- ){

               str2[ cnt2++ ] = str1[ i ];

        }

        if( cnt2== ) str2[ cnt2++ ] = '';

        str2[ cnt2 ] = '\0';

        move( LINES/,COLS/ );

        addstr( Tip2 );

        mvaddstr( LINES/+,COLS/,"You can input 'P' to play again." );

        move( LINES-,COLS- );

 }

 /********************************************************

        show game over

 *********************************************************/

 void ball_move(){

        signal( SIGALRM,SIG_IGN );

        bool moved = false;

        int x_cur = my_moving_ball.x_pos;

        int y_cur = my_moving_ball.y_pos;

        bool crash_ed ;

        int yy_cur = my_moving_brick[].y;

        if( (my_moving_brick[].speed--)== ){

               my_moving_brick[].y +=my_moving_brick[].dir;

               if( my_moving_brick[].y+==RightCol||my_moving_brick[].y==LeftCol ) my_moving_brick[].dir *= (-);

               my_moving_brick[].speed = speed1;

        }

        cover_my_moving_brick( my_moving_brick[].x,yy_cur );

        show_my_moving_brick( my_moving_brick[].x,my_moving_brick[].y );

        move( LINES-,COLS- );

        refresh();

        //my_moving_brick

        if( my_moving_ball.x_Time1> && ((my_moving_ball.x_Time2--) == ) ){

               moved = true;

               my_moving_ball.x_pos += my_moving_ball.x_dir;

               my_moving_ball.x_Time2 = my_moving_ball.x_Time1;

        }

        if( my_moving_ball.y_Time1> && ((my_moving_ball.y_Time2--) == ) ){

               moved = true;

               my_moving_ball.y_pos += my_moving_ball.y_dir;

               my_moving_ball.y_Time2 = my_moving_ball.y_Time1;

        }

        if( moved == true ){

               crash_ed = ball_crash_brick();

               mvaddch( y_cur,x_cur,BLANK );

               mvaddch( my_moving_ball.y_pos,my_moving_ball.x_pos,my_moving_ball.ball_symbol );

               in_bound( &my_moving_ball );

               move( LINES-,COLS- );

               refresh();

        }

        signal( SIGALRM,ball_move );

        return ;

 }

 /********************************************************

        try move the ball

 *********************************************************/

 bool ball_crash_brick(){

        if( my_moving_ball.y_pos==my_moving_brick[].x ){

               if( my_moving_ball.x_pos>=my_moving_brick[].y&&my_moving_ball.x_pos<=my_moving_brick[].y+ ){

                      cover_my_moving_brick( my_moving_brick[].x,my_moving_brick[].y );

                      my_moving_brick[].flag = false;

                      return true;

               }

        }

        else

               return false;

 }

 /********************************************************

        when the ball crash with the brick

 *********************************************************/

 void in_bound( struct BallInfo *myballPoint ){

        bool return_dir = false;

        if( myballPoint->x_pos==LeftCol ){

               myballPoint->x_dir = ;

               return_dir = true;

        }//left

        else if( myballPoint->x_pos==RightCol ){

               myballPoint->x_dir = -;

               return_dir = true;

        }//right

        if( myballPoint->y_pos==TopRow ){

               myballPoint->y_dir = ;

               return_dir = true;

        }//up

        else if( myballPoint->y_pos==BotRow ){

               if( ((myballPoint->x_pos)>=my_moving_Baffle.y) && ((myballPoint->x_pos)<=my_moving_Baffle.y+my_moving_Baffle.len) ){

                      myballPoint->y_dir = -;

                      score ++;

                      show_score_gameon();

                      return_dir = true;

               }

               else{

                      show_score_gameoff();

                      wrap_up();

               }

        }//down

 }

 /********************************************************

        rebound the ball

 *********************************************************/

 int set_ticker( int n_msecs ){

        struct itimerval new_timeset;

        long n_sec, n_usecs;

        n_sec = n_msecs / ;

        n_usecs = ( n_msecs % ) * 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 );

 }

 /********************************************************

        set the timer

 *********************************************************/

 bool judge_in_bound( int x,int y ){

        if( x>=BotRow && x<=TopRow && y>=LeftCol && y<=RightCol )

               return true;

        else

               return false;

 }

 void show_baffle(){

        int i;

        for( i=TopRow-;i<=BotRow+;i++ ){

               mvaddch( i,LeftCol-,Baffle );

               mvaddch( i,RightCol+,Baffle );

        }

        for( i=LeftCol-;i<=RightCol+;i++ ){

               mvaddch( TopRow-,i,Baffle );

        }

        return ;

 }

 /********************************************************

        show the baffle

 *********************************************************/

 void try_move_my_moving_baffle( char od ){

        if( od==LEFT ){

               if( my_moving_Baffle.y- >= LeftCol ){

                      cover_my_moving_baffle();

                      my_moving_Baffle.y --;

                      show_my_moving_baffle();

               }

        }

        /* left */

        if( od==RIGHT ){

               if( my_moving_Baffle.y+my_moving_Baffle.len<=RightCol ){

                      cover_my_moving_baffle();

                      my_moving_Baffle.y ++;

                      show_my_moving_baffle();

               }

        }

        /* right */

 }

 /********************************************************

        solve my moving baffle's direction

 *********************************************************/

 void cover_my_moving_baffle(){

        int i;

        for( i=;i<my_moving_Baffle.len;i++ )

               mvaddch( my_moving_Baffle.x,my_moving_Baffle.y+i,BLANK );

        return ;

 }

 void show_my_moving_baffle(){

        int i;

        for( i=;i<my_moving_Baffle.len;i++ )

               mvaddch( my_moving_Baffle.x,my_moving_Baffle.y+i,my_moving_Baffle.baffle_symbol );

        return ;

 }

 /********************************************************

        show my moving baffle

 *********************************************************/

 int main(){

        char od;

        bool flag = set_up( true );

        /* start the game */

        if( flag==false )

               return ;

        /* the user dont want to play the game */

        while( ( od=getch() )!='Q' ){

               if( od=='f'&&my_moving_ball.x_Time1>Min ) my_moving_ball.x_Time1 --;

               else if( od=='s'&&my_moving_ball.x_Time1<Max ) my_moving_ball.x_Time1 ++;

               else if( od=='F'&&my_moving_ball.y_Time1>Min ) my_moving_ball.y_Time1 --;

               else if( od=='S'&&my_moving_ball.y_Time1<Max ) my_moving_ball.y_Time1 ++;

               /* change the ball's speed */

               if( od=='J'&&my_moving_Baffle.len>= ) {

                      cover_my_moving_baffle();

                      my_moving_Baffle.len --;

                      show_my_moving_baffle();

               }

               else if( od=='L'&&my_moving_Baffle.y+my_moving_Baffle.len<RightCol ){

                      cover_my_moving_baffle();

                      my_moving_Baffle.len ++;

                      show_my_moving_baffle();

               }

               /* change my moving baffle's len */

               if( od=='P' ) {

                      clear_up();

                      set_up( false );

               }

               /* play again the game */

               if( od==LEFT||od==RIGHT ){

                      try_move_my_moving_baffle( od );

               }

               /* move my moving baffle */

        }

        wrap_up();

        /* end up the game */

        return ;

 }

bounce.h

 #include<stdio.h>
#include<curses.h>
#include<stdlib.h> #define BLANK ' '
#define BALL 'o'
#define Baffle '#'
#define BaffleSymbol '_' #define TopRow 5
#define BotRow 20
#define LeftCol 10
#define RightCol 70 #define startX 10
#define startY 10
/* ball start position */ #define startX_ 21
#define startY_ 10
#define BaffleLen 7
/* baffle start position */ #define scorePosx 1
#define scorePosy 1
#define Tip1 "GameOn:"
#define Tip2 "GameOver"
#define Tip3 "YourScore:" #define TICKS_PER_SEC 25 #define X_Time 5
#define Y_Time 8 #define LEFT '4'
#define RIGHT '6' #define Min 1
#define Max 12 #define foreground 34
#define background 0
#define colormode 0 #define speed1 3
#define speed2 3 #define BRICK "BRICK" #define DELTA 5 struct BallInfo{
int x_pos,y_pos;
int x_Time1,y_Time1;
int x_Time2,y_Time2;
int x_dir,y_dir;
char ball_symbol;
}; struct BaffleInfo{
int x,y,len;
char baffle_symbol;
}; struct Brick{
int x,y;
int speed;
int dir;
bool flag;
};

Linux---弹球游戏的更多相关文章

  1. 疯狂java实战演义 弹球游戏代码

    package org.crazyit.ball; import java.awt.Image; import java.io.File; import javax.imageio.ImageIO; ...

  2. 使用html+css+js实现弹球游戏

    使用html+css+js实现弹球游戏 效果图: 代码如下,复制即可使用: <!doctype html> <head> <style type="text/c ...

  3. Python 练习项目1 弹球游戏

    这几天学习了python的基础知识,然后参考了网上的一些资料,完成了一个自己的小游戏,弹球游戏比较简单,但却具备了一些游戏的普遍特征,对于初学者是一个比较合适的锻炼的项目. 下面是效果图: 完整程序: ...

  4. HTML5与Javascript 实现网页弹球游戏

    终于效果图: 1. 使用html 5 的canvas 技术和javascript实现弹球游戏 总体流程图: 1.1 html5 canvas技术的使用 首先在html页面中定义画布. <canv ...

  5. Scratch运动模块——有趣的弹球游戏(一)

    大家好!我是蓝老师,有了前几期Scratch的基础,相信大家早已摩拳擦掌,跃跃欲试了,甚至还有些小伙伴已经编写了非常不错的程序. 学习编程就是这样不断探索.主动思考.解决问题的过程. 本期内容: 课程 ...

  6. 【编程教室】PONG - 100行代码写一个弹球游戏

    大家好,欢迎来到 Crossin的编程教室 ! 今天跟大家讲一讲:如何做游戏 游戏的主题是弹球游戏<PONG>,它是史上第一款街机游戏.因此选它作为我这个游戏开发系列的第一期主题. 游戏引 ...

  7. 使用 Flutter 与 Firebase 制作 I/O 弹球游戏

    文/ Very Good Ventures 团队,5 月 11 日发表于 Flutter 官方博客 为了今年的 Google I/O 大会,Flutter 团队使用 Flutter 以及 Fireba ...

  8. 玩家福音:10款最佳Linux免费游戏

    “我能在Linux平台上游戏吗?”这类疑问正困扰游戏玩家,那么答案就是“快去Linux平台吧!”.开源组织一直以来坚持不懈为Linux操作系统开发不同类型的游戏,在Linux平台下的游戏完全不亚于其他 ...

  9. Linux赛车游戏 SuperTuxKart 1.0 正式发布

    SuperTuxKart是一款受Mario Kart(马里奥赛车)启发并以Linux/Tux为主题的开源赛车游戏,经过12年多的开发,已经达到1.0版本.并且确定这个版本确实是一个重要的里程碑. Su ...

  10. 开源玩家福利:十大Linux免费游戏

    假如当你考虑从Windows平台迁移至Linux平台时,“我能在Linux平台上游戏吗?”这类疑问正困扰着你,那么对此这有一个答案就是“快去Linux平台吧!”.感谢开源组织一直以来坚持不懈为Linu ...

随机推荐

  1. Redis rdb文件CRC64校验算法 Java实现

    查看RDB文件结构,发现最后的8字节是CRC64校验算得,从文件头开始直到8字节校验码前的FF结束码(含),经过CRC64校验计算发现,貌似最后的8字节是小端模式实现的. 参考redis的crc64实 ...

  2. JSON解析保存在类中

    //my.h#ifndef __1_Header_h#define __1_Header_h#define DEBUG 1#define aa 1 #ifdef aa#ifdef DEBUG#defi ...

  3. Mac下安装Twig模版引擎的方法

    本人收集了几种方法: 安装前: twig需要依赖PHP 5.2.7 才能运行. 1 通过composer安装(2种) 方法一: 首先确保你已经安装了composer,如果没有安装,可以参考https: ...

  4. javascript笔记——JavaScript经典实例

    转载自百度文库 http://wenku.baidu.com/view/9a703522bcd126fff7050bfa.html 1. oncontextmenu="window.even ...

  5. SVN全量备份+增量备份脚本

    一.全量备份 环境:一台主SVN,一台备SVN(主要提供备份功能),后续可通过钩子脚本进行实时备份,后续发给大家. 工作原理:通过svn的hotcopy命令过行热备份,并进行一系列的检查,备份后通过r ...

  6. 《APUE》第七章笔记

    这一章主要是要解决这么几个问题: 当执行程序时,main函数是如何被调用的? main函数的原型是: int main(int argc, char *argv[]); 其中argc是命令个数,arg ...

  7. antuomake 生成configure的使用

    configure 作为编译配置脚本,有大量选项可供不同编译需求,这些选项直 接作用到最终生成的Makefile文件 问题:automake默认的gcc编译选项为-Wall -O2 -g,怎么改为我们 ...

  8. GET 和 POST 两种方式来完成Http接口

    程序使用 HTTP 协议和服务器交互主要是进行数据的提交,通常数据的提交是通过 GET 和 POST 两种方式来完成,下面对这两种方式(.net c#)进行一下说明: 1. GET 方式. GET 方 ...

  9. nginx禁止访问某个后缀名的文件

    猛然发现通过http://ip:端口号/路径/config.ini竟然能下载项目代码的配置文件,检查nginx配置,原来是没有加限制,立即加上,并记录如下: location ~* \.(ini|do ...

  10. DISCUZ! X2.5设置仅允许QQ登录注册论坛 加固会员注册机制

    论坛稍微有点起色之后,很多站长就会担心论坛经常被人恶意灌水.注册机.顶贴机等等一些列非法的手段.通常站长都会通过一些后台的设置和插件等等一 切有效的方法预防,但更多的站长会通过限制用户注册会员,需注册 ...