bounce2d2.c

 /*
* bounce2d 1.0
* bounce a character (default is 'o') around the screen
* defined by some parameters
* user input: s slow down x component, S: slow y component
* f speed up x component, F: speed y component
* Q quit
* blocks on read, but timer tick sends SIGALRM caught by ball_move
* build: cc bounce2d.c set_ticker.c -lcurses -o bounce2d
*/
#include <curses.h>
#include <string.h>
#include <signal.h>
#include "bounce.h" struct ppball the_ball; /** the main loop **/ //int flap_pos = RIGHT_EDGE / 2 - LEFT_EDGE;
int flap_pos =;
int old_pos = ;
void set_up();
void wrap_up();
void move_flap();
int bounce_or_lose(struct ppball *); int main()
{
// printf("LEFT_EDGE: %d, RIGHT_EDGE: %d\n",LEFT_EDGE, RIGHT_EDGE);
// printf("TOP_EDGE: %d, BOT_EDGE: %d\n",TOP_ROW, BOT_ROW);
int c;
set_up();
while (((c = getchar())) != 'Q')
{
if (c == 'f') the_ball.x_ttm--;
else if (c == 's') the_ball.x_ttm++;
else if (c == 'F') the_ball.y_ttm--;
else if (c == 'S') the_ball.y_ttm++;
else if (c == 'a'){
if (flap_pos > LEFT_EDGE){
old_pos = flap_pos;
flap_pos -= FLAP_SPEED;
move_flap();
}
}else if (c == 'd'){
if (flap_pos + (int)strlen(FLAP) < RIGHT_EDGE){
old_pos = flap_pos;
flap_pos += FLAP_SPEED;
move_flap();
}
}
}
wrap_up();
// printf("LINES: %d, COLS: %d\n",LINES, COLS);
return ;
} void set_up()
/*
* init structure and other stuff
*/
{
void ball_move(int);
the_ball.y_pos = Y_INIT;
the_ball.x_pos = X_INIT;
the_ball.y_ttg = the_ball.y_ttm = Y_TIM;
the_ball.x_ttg = the_ball.x_ttm = X_TIM;
the_ball.y_dir = ;
the_ball.x_dir = ;
the_ball.symbol = DFL_SYMBOL;
the_ball.x_moved = the_ball.y_moved = false; initscr();
noecho();
crmode(); signal(SIGINT, SIG_IGN);
mvaddch(the_ball.y_pos, the_ball.x_pos, the_ball.symbol);
move_flap();
signal(SIGALRM, ball_move);
set_ticker( / TICKS_PER_SEC);
} void wrap_up()
{
set_ticker();
endwin();
} void move_flap()
{
move(BOT_ROW + , old_pos);
addstr(FLAP); //FLAP is blank. Here it is used to clear its old existence. move(BOT_ROW + , flap_pos);
standout();
addstr(FLAP);
standend();
refresh();
} void ball_move(int signum)
{
int y_cur, x_cur, moved; signal(SIGALRM, SIG_IGN);
x_cur = the_ball.x_pos;
y_cur = the_ball.y_pos;
moved = ; if (the_ball.y_ttm > && the_ball.y_ttg-- == ){
the_ball.y_pos += the_ball.y_dir; /* move */
the_ball.y_ttg = the_ball.y_ttm; /* reset */
the_ball.y_moved = ;
moved = ;
} if (the_ball.x_ttm > && the_ball.x_ttg-- == ){
the_ball.x_pos += the_ball.x_dir; /* move */
the_ball.x_ttg = the_ball.x_ttm; /* reset */
the_ball.x_moved = ;
moved = ;
} if (moved){
mvaddch(y_cur, x_cur, BLANK);
mvaddch(y_cur, x_cur, BLANK);
mvaddch(the_ball.y_pos, the_ball.x_pos, the_ball.symbol);
if(bounce_or_lose(&the_ball)){
// signal(SIGALRM, SIG_IGN);
move(LINES / , COLS / );
addstr("GAME OVER");
refresh();
return;
}
move(LINES-, COLS-);
if (the_ball.x_moved && the_ball.y_moved){
refresh();
the_ball.x_moved = the_ball.y_moved = false; /* reset */
}
}
signal(SIGALRM, ball_move);
} int bounce_or_lose(struct ppball *bp)
/*
* 1 lose
* 0 not lose
*/
{
int return_val = ; if (bp->y_pos == TOP_ROW){
bp->y_dir = ;
}else if (bp->y_pos == BOT_ROW){
bp->y_dir = -;
if (!(bp->x_pos >= flap_pos && bp->x_pos <= (flap_pos + (int)strlen(FLAP)))){
return_val = ;
}
} if (bp->x_pos == LEFT_EDGE){
bp->x_dir = ;
}else if (bp->x_pos == RIGHT_EDGE){
bp->x_dir = -;
}
return return_val;
}

bounce.h

 #define BLANK ' '
#define DFL_SYMBOL 'o'
#define TOP_ROW 0
#define BOT_ROW 20
#define LEFT_EDGE 0
#define RIGHT_EDGE 81
#define X_INIT 3
#define Y_INIT 5
#define TICKS_PER_SEC 50
#define Y_TIM 8
#define X_TIM 8
#define FLAP " "
//#define FLAP_LEN 21
#define FLAP_SPEED 1 struct ppball {
int x_ttg; // x 轴下次重画还要等待多少个计时器
int y_ttg; // y 轴下次重画还要等待多少个计时器
int x_ttm; // x 轴移动需要等待的信号间隔
int y_ttm; // y 轴移动絮叨等待的信号间隔
int y_pos;
int x_pos;
int y_dir;
int x_dir;
int x_moved;
int y_moved;
char symbol;
};

set_ticker.c

 #include    <stdio.h>
#include <sys/time.h>
#include <signal.h>
#include <stdlib.h> /*
* set_ticker.c
* set_ticker( number_of_milliseconds )
* arranges for the interval timer to issue
* SIGALRM's at regular intervals
* returns -1 on error, 0 for ok
*
* arg in milliseconds, converted into micro seoncds
*/ set_ticker( 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; /* set reload */
new_timeset.it_interval.tv_usec = n_usecs; /* new ticker value */
new_timeset.it_value.tv_sec = n_sec ; /* store this */
new_timeset.it_value.tv_usec = n_usecs ; /* and this */ return setitimer(ITIMER_REAL, &new_timeset, NULL);
}

====================================================================================================

A different version:

 #include <curses.h>
#include <sys/time.h>
#include <signal.h> #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=; int hdir; /*控制球水平运动的变量*/
int vdir; /*控制球垂直运动的变量*/
int pos_X; /*球的横坐标*/
int pos_Y; /*球的纵坐标*/ int delay=;
void moveBall();
void init();
void control(); int main()
{
//初始化 curses
initscr();
crmode(); /*中断模式*/
noecho(); /*关闭回显*/ move(,);
attron(A_BOLD);
addstr("Welcome to the BallGame!");
move(,);
attroff(A_BOLD);
addstr("Help:");
move(,);
addstr("'N':Start a new game.");
move(,);
addstr("'Q':Quit game.");
move(,);
addstr("'KEY_LEFT' :Control baffle left shift.");
move(,);
addstr("'KEY_RIGHT':Control baffle right shift.");
move(,);
addstr("'KEY_UP' :Control of the ball speed.");
move(,);
addstr("'KEY_DOWN' :Control of the ball reducer.");
int flag=;
char choice;
move(,);
addstr("Please choose your choice!(n/q):");
refresh();
choice=getch();
while(flag){
if(choice=='q'||choice=='Q'||choice=='n'||choice=='N')
flag=;
else choice=getch();
}
if(choice=='n'||choice=='N'){ /*开始游戏*/
clear();
move(,);
addstr("BallGame will start! Are you ready?");
refresh();
sleep();
control();
}
else if(choice=='q'||choice=='Q'){ /*退出游戏*/
clear();
move(,);
addstr("You quit the game successfully!");
refresh();
sleep();
endwin();
}
endwin(); /*结束 curses*/
return ;
} void init(){
int i,j;
clear();
if(start_color()==OK){ /*改变球和挡板的颜色*/
attron(A_BOLD); /*打开粗体*/
init_pair(,COLOR_YELLOW,COLOR_BLACK);
attron(COLOR_PAIR());
}
//初始球
pos_X =; /*球初始的横坐标*/
pos_Y = BOTTOM-; /*球初始的纵坐标*/
//初始化球的运动方向,朝右上方运动
hdir=;
vdir=-; //初始挡板
left_board=;
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); /*打开 keypad 键盘响应*/
attroff(A_BLINK); /*关闭 A_BLINK 属性*/ is_lose=;
move(pos_Y,pos_X);
addch(BALL);
move(LINES-, COLS-);
refresh();
usleep(); /*睡眠*/
move(LINES-,COLS-);
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 = -;
beep(); /*球撞墙时,发出声音*/
}
if(pos_X <= LEFT) { /*当球横坐标大于左边边缘时,球反弹朝右运动*/
hdir = ;
beep(); /*球撞墙时,发出声音*/
}
if(pos_Y <= TOP) { /*当球纵坐标大于顶部边缘时,球反弹朝下运动*/
vdir = ;
beep(); /*球撞墙时,发出声音*/
} //当球在底部的时候进行额外的处理
if(pos_Y >= BOTTOM-){
if(pos_X>=left_board&&pos_X<=right_board) /*球在挡板处*/
vdir=-;
else{ /*球不在挡板处*/
is_lose=;
move(pos_Y,pos_X);
addch(BALL);
move(LINES-, COLS-);
refresh();
usleep(delay*); /*睡眠*/
move(pos_Y,pos_X);
addch(BLANK);
pos_X += hdir;
pos_Y += vdir;
move(pos_Y,pos_X);
addch(BALL);
move(LINES-, COLS-);
refresh();
}
}
//不改变球的方向时
move(pos_Y,pos_X);
addch(BALL);
move(LINES-, COLS-);
refresh();
}
void control(){
init();
int cmd;
while ()
{
if(!is_lose){
cmd=getch();
if(cmd=='q'||cmd=='Q'||cmd==) break; //强制退出游戏
//挡板左移
if(cmd==KEY_LEFT){
if(left_board>){
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/=;
set_ticker(delay);
}
//给球减速
else if(cmd==KEY_DOWN){
delay*=;
set_ticker(delay);
} }
else{
//输掉球后的处理
int flag=;
char choice;
move(,);
addstr("Game Over!try again?(y/n):");
refresh();
choice=getch(); while(flag){
if(choice=='y'||choice=='Y'||choice=='n'||choice=='N')
flag=;
else choice=getch();
}
if(choice=='y'||choice=='Y'){ /*游戏重新开始*/
delay=; /*恢复球的初始速度*/
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/;
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);
}

pong game using ncurses的更多相关文章

  1. HDU 2492 Ping pong (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2492 Ping pong Problem Description N(3<=N<=2000 ...

  2. POJ3928Ping pong[树状数组 仿逆序对]

    Ping pong Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3109   Accepted: 1148 Descrip ...

  3. UVALive 4329 Ping pong

                                      Ping pong Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Fo ...

  4. ncurses库的一些函数

    为了实现一个简单的聊天程序,如果使用普通的输入输出函数,会很凌乱.so,便想着能不能用下 ncurses这个字符图形库 总结一下,就是这样. 使用ncurses时,先需要初始化窗口,程序结束时,主动调 ...

  5. Mini projects #4 ---- Pong

    课程全名:An Introduction to Interactive Programming in Python,来自 Rice University 授课教授:Joe Warren, Scott ...

  6. POJ 3928 Ping pong(树状数组)

                                                                          Ping pong Time Limit: 1000MS   ...

  7. (转) Deep Reinforcement Learning: Pong from Pixels

    Andrej Karpathy blog About Hacker's guide to Neural Networks Deep Reinforcement Learning: Pong from ...

  8. Linux编译内核提示'make menuconfig' requires the ncurses libraries错误

    原来使用的ubuntu 11.10系统由于误操作,导致系统崩溃,重新安装了ubuntu 11.10: 在编译内核的时候,提示如下错误: dingq@wd-u1110:~/hwsvn/2sw/1prj_ ...

  9. LA4329 Ping pong(树状数组与组合原理)

    N (3N20000)ping pong players live along a west-east street(consider the street as a line segment). E ...

随机推荐

  1. Vue笔记:使用 Yarn 管理依赖包

    上年10月份, Facebook 发布了新的 node.js 包管理器 Yarn 用以替代 npm ,它比npm更快.更高效. Yarn VS npm 1.yarn.lock 文件 在 npm 中同样 ...

  2. scala-02-数组的操作

    scala中的数组和 java中的数组一样, 定义了长度后不可改变 1, 产生一个数组: 有3种创建数组的方式, 分别直接new, 直接赋值, 或者使用 Array中的rang来产生 /** * 获取 ...

  3. *2 FastCGI sent in stderr: "PHP message: PHP Parse error: syntax error, unexpected '[' in /application/nginx-1.6.3/html/zabbix/index.php on line 32" while reading response header from upstream, clien

    今天呢想学习一下zabbix监控一下我的服务情况,然后就开始安装我的zabbix服务,首先LNMP环境准备好了,Nginx版本为1.6.3,php版本为5.3.27,MySQL版本为二进制包安装的5. ...

  4. tomcat 虚拟目录

    在webapps同级目录 下建立一个webapps_abc的目录,将网站根目录abc文件夹放入webapps_abc目录下: 找到conf目录下的,server.xml文件,在service节点下添加 ...

  5. 经典SQL语句大全(转)

    一.基础 1.说明:创建数据库CREATE DATABASE database-name 2.说明:删除数据库drop database dbname3.说明:备份sql server--- 创建 备 ...

  6. sqoop部署及使用

    一.概述 sqoop是hive.hdfs.hbase等与RDMBS(mysql等)之间的沟通桥梁,主要通过JDBC与RDMBS进行交互.有两个版本sqoop1和sqoop2,sqoop1架构简单,使用 ...

  7. C++语言------顺序表实现,用动态数组的方法

    C++ 中常用的一些东西,通过使用动态数组来实现顺序表, 掌握了一下知识点: 1.预处理有三中方法 宏定义,文件包含,条件编译 2.使用同名的变量时,可以在外层使用命名空间 类解决变量名重定义的错误 ...

  8. window如何一键关闭所有进程程序

    1.桌面创建一个快捷方式,点击鼠标右键跳出的菜单=> 新建=>快捷方式 2. 在“请键入对象的位置(T)”下方的文本框中输入: taskkill /F /FI "USERNAME ...

  9. Navicat创建视图与美化SQL

    数据库-->视图-->新建视图,点击视图创建工具,将需要用到的表拖入到右侧,然后再底部填写具体的字段等,示意图如下: 注意:视图的字段别名或者列名是不能重复的,否则创建失败: 当视图语句非 ...

  10. .netcore入门

    开发环境:windows    编辑器: Visual Studio Code 环境安装: .Net Core 1.1 SDK     https://www.microsoft.com/net/co ...