Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1259737
  • 博文数量: 548
  • 博客积分: 7597
  • 博客等级: 少将
  • 技术积分: 4224
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-15 13:21
个人简介

嵌入式软件工程师&&太极拳

文章分类

全部博文(548)

文章存档

2014年(10)

2013年(76)

2012年(175)

2011年(287)

分类: C/C++

2011-09-19 22:24:51

    1. #include <stdio.h>

    2. #define COL 64
    3. #define ROW 20

    4. #define BGCOLOR ' '
    5. #define FGCOLOR '*'

    6. #define X 10
    7. #define Y 10
    8. #define W 6
    9. #define H 3

    10. #define X_INC 1
    11. #define Y_INC 1

    12. static int color = 0;

    13. void init(int *bg, int row, int col, char color);
    14. void draw(int *bg, int row, int col,
    15.           int x, int y, int w, int h, int color);
    16. void move(int *x, int *y, int w, int h,
    17.           int *x_inc, int *y_inc);
    18. //void show(int *bg, int row, int col);

    19. //void show(int (*arr)[COL], int row);
    20. void show(int arr[][COL], int row);

    21. int main(void)
    22. {
    23.     int background[ROW][COL];
    24.     int x, y, w, h;
    25.     int x_inc, y_inc;

    26.     x = X;
    27.     y = Y;
    28.     w = W;
    29.     h = H;
    30.     x_inc = X_INC;
    31.     y_inc = Y_INC;

    32.     while (1)
    33.     {
    34.         init((int *)background, ROW, COL, BGCOLOR);
    35.         draw((int *)background, ROW, COL,
    36.              x, y, w, h, FGCOLOR);
    37.         //show((int *)background, ROW, COL);
    38.         show(background, ROW);
    39.         move(&x, &y, w, h, &x_inc, &y_inc);

    40.         usleep(100000);
    41.     }

    42.     return 0;
    43. }

    44. void init(int *bg, int row, int col, char color)
    45. {
    46.     int i, j;

    47.     for (i = 0; i < row; ++i)
    48.     {
    49.         for (j = 0; j < col; ++j)
    50.         {
    51.             bg[i * col + j] = color;
    52.         }
    53.     }
    54. }

    55. void draw(int *bg, int row, int col,
    56.           int x, int y, int w, int h, int color)
    57. {
    58.     int i, j;

    59.     for (i = y; i < y + h; ++i)
    60.     {
    61.         for (j = x; j < x + w; ++j)
    62.         {
    63.             bg[i * col + j] = color;
    64.         }
    65.     }
    66. }

    67. void show(int bg[][COL], int row)
    68. {
    69.     int i, j;

    70.     for (i = 0; i < row; ++i)
    71.     {
    72.         for (j = 0; j < COL; ++j)
    73.         {
    74.             if (*(bg[i] + j) == FGCOLOR)
    75.             {
    76.                 printf("\033[3%dm%c\033[0m", color, *(bg[i] + j));
    77.             }
    78.             else
    79.             {
    80.                 printf("%c", *(bg[i] + j));
    81.             }
    82.         }
    83.     }
    84.     fflush(stdout);
    85. }

    86. void move(int *x, int *y, int w, int h,
    87.           int *x_inc, int *y_inc)
    88. {
    89.     if (*x + *x_inc < 0 || *x + w + *x_inc > COL)
    90.     {
    91.         color += 1;
    92.         color %= 8;
    93.         *x_inc = -(*x_inc);
    94.     }
    95.     if (*y + *y_inc < 0 || *y + h + *y_inc > ROW)
    96.     {
    97.         color += 1;
    98.         color %= 8;
    99.         *y_inc = -(*y_inc);
    100.     }

    101.     *x += *x_inc;
    102.     *y += *y_inc;
    103. }

    基本思路:

    1. 初始化二维数组BG 和 FG

    2. 设置移动点 x 和 y  对移动点进行操作:

     (1) “+”法移动

     (2)如果“—”移动超过边界,并进行“—”操作, 就是对内存进行变化,一变到底

    阅读(640) | 评论(0) | 转发(0) |
    给主人留下些什么吧!~~