pong game using ncurses

bounce2d2.c

  1 /*
  2  * bounce2d 1.0
  3  * bounce a character (default is ‘o‘) around the screen
  4  *  defined by some parameters
  5  * user input: s slow down x component, S: slow y component
  6  *             f speed up x component, F: speed y component
  7  *             Q quit
  8  * blocks on read, but timer tick sends SIGALRM caught by ball_move
  9  * build: cc bounce2d.c set_ticker.c -lcurses -o bounce2d
 10     */
 11 #include <curses.h>
 12 #include <string.h>
 13 #include <signal.h>
 14 #include "bounce.h"
 15
 16 struct ppball the_ball;
 17
 18 /** the main loop **/
 19 int flap_pos = RIGHT_EDGE / 2 - LEFT_EDGE;
 20 int old_pos;
 21 void set_up();
 22 void wrap_up();
 23 void move_flap();
 24 int bounce_or_lose(struct ppball *);
 25
 26 int main()
 27 {
 28     printf("LEFT_EDGE: %d, RIGHT_EDGE: %d\n",LEFT_EDGE, RIGHT_EDGE);
 29     int c;
 30     set_up();
 31     while (((c = getchar())) != ‘Q‘)
 32     {
 33         if (c == ‘f‘) the_ball.x_ttm--;
 34         else if (c == ‘s‘) the_ball.x_ttm++;
 35         else if (c == ‘F‘) the_ball.y_ttm--;
 36         else if (c == ‘S‘) the_ball.y_ttm++;
 37         else if (c == ‘a‘){
 38             if (flap_pos > LEFT_EDGE){
 39                 old_pos = flap_pos;
 40                 flap_pos -= FLAP_SPEED;
 41                 move_flap();
 42             }
 43         }else if (c == ‘d‘){
 44             if (flap_pos + (int)strlen(FLAP) < RIGHT_EDGE){
 45                 old_pos = flap_pos;
 46                 flap_pos += FLAP_SPEED;
 47                 move_flap();
 48             }
 49         }
 50     }
 51     wrap_up();
 52     return 0;
 53 }
 54
 55 void set_up()
 56     /*
 57      * init structure and other stuff
 58      */
 59 {
 60     void ball_move(int);
 61     the_ball.y_pos = Y_INIT;
 62     the_ball.x_pos = X_INIT;
 63     the_ball.y_ttg = the_ball.y_ttm = Y_TIM;
 64     the_ball.x_ttg = the_ball.x_ttm = X_TIM;
 65     the_ball.y_dir = 1;
 66     the_ball.x_dir = 1;
 67     the_ball.symbol = DFL_SYMBOL;
 68     the_ball.x_moved = the_ball.y_moved = false;
 69
 70     initscr();
 71     noecho();
 72     crmode();
 73
 74     signal(SIGINT, SIG_IGN);
 75     mvaddch(the_ball.y_pos, the_ball.x_pos, the_ball.symbol);
 76     move_flap();
 77     signal(SIGALRM, ball_move);
 78     set_ticker(1000 / TICKS_PER_SEC);
 79 }
 80
 81 void wrap_up()
 82 {
 83     set_ticker(0);
 84     endwin();
 85 }
 86
 87 void move_flap()
 88 {
 89     move(BOT_ROW + 1, old_pos);
 90     addstr(FLAP); //FLAP is blank. Here it is used to clear its old existence.
 91
 92     move(BOT_ROW + 1, flap_pos);
 93     standout();
 94     addstr(FLAP);
 95     standend();
 96     refresh();
 97 }
 98
 99 void ball_move(int signum)
100 {
101     int y_cur, x_cur, moved;
102
103     signal(SIGALRM, SIG_IGN);
104     x_cur = the_ball.x_pos;
105     y_cur = the_ball.y_pos;
106     moved = 0;
107
108     if (the_ball.y_ttm > 0 &&  the_ball.y_ttg-- == 1){
109         the_ball.y_pos += the_ball.y_dir;  /* move */
110         the_ball.y_ttg = the_ball.y_ttm;   /* reset */
111         the_ball.y_moved = 1;
112         moved = 1;
113     }
114
115     if (the_ball.x_ttm > 0 && the_ball.x_ttg-- == 1){
116         the_ball.x_pos += the_ball.x_dir;  /* move */
117         the_ball.x_ttg = the_ball.x_ttm;   /* reset */
118         the_ball.x_moved = 1;
119         moved = 1;
120     }
121
122     if (moved){
123         mvaddch(y_cur, x_cur, BLANK);
124         mvaddch(y_cur, x_cur, BLANK);
125         mvaddch(the_ball.y_pos, the_ball.x_pos, the_ball.symbol);
126         if(bounce_or_lose(&the_ball)){
127             signal(SIGALRM, SIG_IGN);
128             move(LINES / 2, COLS / 2);
129             addstr("GAME OVER");
130             refresh();
131             return;
132         }
133         move(LINES-1, COLS-1);
134         if (the_ball.x_moved && the_ball.y_moved){
135             refresh();
136             the_ball.x_moved = the_ball.y_moved = false; /* reset */
137         }
138     }
139     signal(SIGALRM, ball_move);
140 }
141
142 int bounce_or_lose(struct ppball *bp)
143     /*
144      * 1 lose
145      * 0 not lose
146      */
147 {
148     int return_val = 0;
149
150     if (bp->y_pos == TOP_ROW){
151         bp->y_dir = 1;
152     }else if (bp->y_pos == BOT_ROW){
153         bp->y_dir = -1;
154         if (!(bp->x_pos >= flap_pos && bp->x_pos <= (flap_pos + (int)strlen(FLAP)))){
155             return_val = 1;
156         }
157     }
158
159     if (bp->x_pos == LEFT_EDGE){
160         bp->x_dir = 1;
161     }else if (bp->x_pos == RIGHT_EDGE){
162         bp->x_dir = -1;
163     }
164     return return_val;
165 }

bounce.h

 1 #define BLANK ‘ ‘
 2 #define DFL_SYMBOL ‘o‘
 3 #define TOP_ROW 5
 4 #define BOT_ROW 20
 5 #define LEFT_EDGE 10
 6 #define RIGHT_EDGE 70
 7 #define X_INIT 10
 8 #define Y_INIT 10
 9 #define TICKS_PER_SEC 50
10 #define Y_TIM 8
11 #define X_TIM 8
12 #define FLAP "     "
13 //#define FLAP_LEN 21
14 #define FLAP_SPEED 1
15
16 struct ppball {
17     int x_ttg; // x 轴下次重画还要等待多少个计时器
18     int y_ttg; // y 轴下次重画还要等待多少个计时器
19     int x_ttm; // x 轴移动需要等待的信号间隔
20     int y_ttm; // y 轴移动絮叨等待的信号间隔
21     int y_pos;
22     int x_pos;
23     int y_dir;
24     int x_dir;
25     int x_moved;
26     int y_moved;
27     char symbol;
28 };

set_ticker.c

 1 #include    <stdio.h>
 2 #include        <sys/time.h>
 3 #include        <signal.h>
 4 #include    <stdlib.h>
 5
 6 /*
 7  *      set_ticker.c
 8  *          set_ticker( number_of_milliseconds )
 9  *                   arranges for the interval timer to issue
10  *                   SIGALRM‘s at regular intervals
11  *          returns -1 on error, 0 for ok
12  *
13  *      arg in milliseconds, converted into micro seoncds
14  */
15
16
17 set_ticker( n_msecs )
18 {
19         struct itimerval new_timeset;
20         long    n_sec, n_usecs;
21
22         n_sec = n_msecs / 1000 ;
23         n_usecs = ( n_msecs % 1000 ) * 1000L ;
24
25         new_timeset.it_interval.tv_sec  = n_sec;        /* set reload  */
26         new_timeset.it_interval.tv_usec = n_usecs;      /* new ticker value */
27         new_timeset.it_value.tv_sec     = n_sec  ;      /* store this   */
28         new_timeset.it_value.tv_usec    = n_usecs ;     /* and this     */
29
30     return setitimer(ITIMER_REAL, &new_timeset, NULL);
31 }
时间: 2024-08-02 16:05:32

pong game using ncurses的相关文章

Coursera-An Introduction to Interactive Programming in Python (Part 1)-Mini-project #4 —&quot;Pong&quot;

In this project, we will build a version of Pong, one of the first arcade video games (1972). While Pong is not particularly exciting compared to today's video games, Pong is relatively simple to build and provides a nice opportunity to work on the s

Ping pong

Description N(3N20000) ping pong players live along a west-east street(consider the street as a line segment). Each player has a unique skill rank. To improve their skill rank, they often compete with each other. If two players want to compete, they

Non-blocking user input in loop without ncurses.

The title sounds a bit awkward, let my briefly explain what is it all about. In my program, I want to wait for user input, but at the same time, I want my other operations keep continue processing. That I define it as non-blocking user input. I want

POJ 3928 &amp; HDU 2492 Ping pong(树状数组求逆序数)

题目链接: PKU:http://poj.org/problem?id=3928 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=2492 Description N(3<=N<=20000) ping pong players live along a west-east street(consider the street as a line segment). Each player has a unique skill rank. To im

uva 1428 - Ping pong(树状数组)

题目链接:uva 1428 - Ping pong 题目大意:一条大街上住着n个乒乓球爱好者,经常组织比赛.每个人都有一个不同的能力值,每场比赛需要3个人,裁判要住在两个选手之间,并且能力值也要在选手之间,问说最多能举行多少场比赛. 解题思路:预处理出bi和ci分别表示说在1~i中能力值比第i个人小的人和i+1~n中能力值比第i个人小的.处理过程用树状数组维护即可. #include <cstdio> #include <cstring> #include <algorith

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

Ping pong Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3109   Accepted: 1148 Description N(3<=N<=20000) ping pong players live along a west-east street(consider the street as a line segment). Each player has a unique skill rank. To i

Motion sensing game (Ping Pong Game)

Project Demonstration Here is the source code of the project based on OpenCV anc C++. Before you run this code on Linux, you should install the OpenCV library  first. #include<opencv/highgui.h> #include<opencv2/opencv.hpp> #include<string&g

poj3928 Ping pong 树状数组

http://poj.org/problem?id=3928 Ping pong Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2087   Accepted: 798 Description N(3<=N<=20000) ping pong players live along a west-east street(consider the street as a line segment). Each player

Cygwin中使用ncurses库

首先安装Cygwin的完整包,这个包有7GB大小左右 之后使用这个小例子测试是否成功 #include <ncurses.h> #include <string> #include <vector> //#define DEBUG #ifdef DEBUG #include <iostream> #include <cstdio> #endif int main(void) { int x,y; std::vector<std::strin