LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 804|回复: 3

继续问问题 关于涉及一个小动画的程序

[复制链接]
发表于 2006-5-18 08:43:04 | 显示全部楼层 |阅读模式
这个程序主要目的是为了设计一个小球在屏幕上移动,碰撞边界以后弹回。小球用字母o代替,由于需要在横向和纵向设置两个计数器,每个计数器都有两个属性:当前值和间隔。这里用的是ttg和ttm,好了不多说了,程序如下:

#include <curses.h>
#include <signal.h>
#include <sys/time.h>
#include "bounce.h"

struct ppball the_ball;

void set_up();
void wrap_up();
int set_ticker(int);

int main()
{
        int c;
       
        set_up();
       
        while((c = getchar()) != 'Q'){
                if(c == 'f') the_ball.x_ttm--;
                else if(c == 's') the_ball.x_ttm++;
        }
        wrap_up();
}

void set_up()
{
        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_TTM;
        the_ball.x_ttg = the_ball.x_ttm = X_TTM;
        the_ball.y_dir = 1;
        the_ball.x_dir = 1;
       
        the_ball.symbol = DFL_SYMBOL;

        initscr();
        noecho();
        crmode();
        signal(SIGINT,SIG_IGN);
        mvaddch(the_ball.y_pos,the_ball.x_pos,the_ball.symbol);
        refresh();
       
        signal(SIGALRM,ball_move);
        set_ticker(1000/TICKS_PER_SEC);
}

void wrap_up()
{
        set_ticker(0);
        endwin();
}

void ball_move(int signum)
{
        int y_cur,x_cur,moved;
       
        signal(SIGALRM,SIG_IGN);
        y_cur = the_ball.y_pos;
        x_cur = the_ball.x_pos;
        moved = 0;

        if(the_ball.y_ttm > 0&&the_ball.y_ttg-- == 1){
                        the_ball.y_pos += the_ball.y_dir;
                        the_ball.y_ttg += the_ball.y_ttm;
                        moved = 1;
                }
       
        if(the_ball.y_ttm > 0&&the_ball.y_ttg-- == 1){
                        the_ball.x_pos += the_ball.x_dir;
                        the_ball.x_ttg += the_ball.x_ttm;
                        moved = 1;
                }

        if(moved){

                                mvaddch(y_cur,x_cur,BLANK);

                mvaddch(the_ball.y_pos,the_ball.x_pos,the_ball.symbol);
                bounce_or_lose(&the_ball);
                move(LINES-1,COLS-1);
                refresh();
        }
        signal(SIGALRM,ball_move);
}

int bounce_or_lose(struct ppball *bp)
{
        int return_val = 0;
       
        if(bp->y_pos == TOP_ROW){
                bp->y_dir = 1;
                return_val = 1;
        }else if(bp->y_pos == BOT_ROW){
                bp->y_dir = -1;
                return_val = 1;
        }

        if(bp->x_pos == LEFT_EDGE){
                bp->x_dir = 1;
                return_val = 1;
        }else if(bp->x_pos == RIGHT_EDGE){
                bp->x_dir = -1;
                return_val = 1;
        }
        return return_val;
}

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;
        the_ball.y_ttg += the_ball.y_ttm;
       
        return setitimer(ITIMER_REAL,&new_timeset,NULL);
}


/*bounce.h         */
/*some settings for the pro*/

#define BLANK '                '
#define DFL_SYMBOL 'o'
#define TOP_ROW 5
#define BOT_ROW 20
#define LEFT_EDGE 10
#define RIGHT_EDGE 70
#define X_INIT 10
#define Y_INIT 10
#define TICKS_PER_SEC 50
#define X_TTM 5
#define Y_TTM 8

struct ppball{
        int y_pos,x_pos,
            y_ttm,x_ttm,
            y_ttg,x_ttg,
            y_dir,x_dir;
        char symbol;
};

编译的时候,if(moved){

                                mvaddch(y_cur,x_cur,BLANK);

                mvaddch(the_ball.y_pos,the_ball.x_pos,the_ball.symbol);
                bounce_or_lose(&the_ball);
                move(LINES-1,COLS-1);
                refresh();
        }
这一句在mvaddch()这里有问题,请各位指教一下,我错在哪里。谢谢
发表于 2006-5-18 20:18:08 | 显示全部楼层
我没有看楼主的代码, 只是觉得, 为什么要设置两个计时器呢? 一个计时器已经足够了, 然后通过时间间隔和速度分量来计算 x 与 y 的增量. 这种方法的效果不会比楼主的方法差, 甚至会更好一点

P.S. 楼主的问题显然是 #define 语句的位置. 把它们放到前边就好了. 下次贴代码时最好把编译时的出错信息也帖上, 不要只写一句出错了. 代码也要格式化一下, 别人才好阅读, 也方便提问者解决问题
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-5-19 09:39:39 | 显示全部楼层
不好意思,谢谢DODO指出我得不足。问题我已经解决了,实际上只是我在头文件里定义BLANK的时候范了个错误,偏偏肉眼看不出来,搞得郁闷死。还有就是我明明是排好版才发上来的,为何显是的时候所有的缩进都没了呢?

P.S.我觉得其实这里用一个或是两个计时器都是无所谓得了,不过在大规模的程序里可能你的办法会更好点。

再次谢谢DODO的指教,我以后再提问题会注意你所提到的事项。
回复 支持 反对

使用道具 举报

发表于 2006-5-19 21:57:24 | 显示全部楼层
把代码放在
[PHP]

复制代码

[/PHP]
块中就好了
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表