当前时区为 UTC + 8 小时



发表新帖 回复这个主题  [ 3 篇帖子 ] 
作者 内容
1 楼 
 文章标题 : [求助]自己写的一个贪食蛇小游戏,编译的时候出现错误……
帖子发表于 : 2009-06-14 11:07 
头像

注册: 2008-07-07 11:16
帖子: 130
地址: 辽宁-大连
送出感谢: 0 次
接收感谢: 0 次
错误信息:
引用:
wecing@DotOne:~/unix/nokiasnake$ gcc -g -Wall -o ns ns2.c
ns2.c: In function ‘newfood’:
ns2.c:48: warning: empty declaration
ns2.c:56: error: parameter ‘movehash’ is initialized
ns2.c:56: warning: braces around scalar initializer
ns2.c:56: warning: (near initialization for ‘movehash’)
ns2.c:56: warning: initialization makes pointer from integer without a cast
ns2.c:56: warning: excess elements in scalar initializer
ns2.c:56: warning: (near initialization for ‘movehash’)
ns2.c:56: warning: braces around scalar initializer
ns2.c:56: warning: (near initialization for ‘movehash’)
ns2.c:56: warning: initialization makes pointer from integer without a cast
ns2.c:56: warning: excess elements in scalar initializer
ns2.c:56: warning: (near initialization for ‘movehash’)
ns2.c:56: warning: excess elements in scalar initializer
ns2.c:56: warning: (near initialization for ‘movehash’)
ns2.c:56: warning: braces around scalar initializer
ns2.c:56: warning: (near initialization for ‘movehash’)
ns2.c:56: warning: excess elements in scalar initializer
ns2.c:56: warning: (near initialization for ‘movehash’)
ns2.c:56: warning: excess elements in scalar initializer
ns2.c:56: warning: (near initialization for ‘movehash’)
ns2.c:56: warning: braces around scalar initializer
ns2.c:56: warning: (near initialization for ‘movehash’)
ns2.c:56: warning: excess elements in scalar initializer
ns2.c:56: warning: (near initialization for ‘movehash’)
ns2.c:56: warning: excess elements in scalar initializer
ns2.c:56: warning: (near initialization for ‘movehash’)
ns2.c:57: error: parameter ‘speed’ is initialized
ns2.c:57: warning: initialization makes pointer from integer without a cast
ns2.c:57: warning: excess elements in scalar initializer
ns2.c:57: warning: (near initialization for ‘speed’)
ns2.c:57: warning: excess elements in scalar initializer
ns2.c:57: warning: (near initialization for ‘speed’)
ns2.c:57: warning: excess elements in scalar initializer
ns2.c:57: warning: (near initialization for ‘speed’)
ns2.c:57: warning: excess elements in scalar initializer
ns2.c:57: warning: (near initialization for ‘speed’)
ns2.c:57: warning: excess elements in scalar initializer
ns2.c:57: warning: (near initialization for ‘speed’)
ns2.c:57: warning: excess elements in scalar initializer
ns2.c:57: warning: (near initialization for ‘speed’)
ns2.c:57: warning: excess elements in scalar initializer
ns2.c:57: warning: (near initialization for ‘speed’)
ns2.c:60: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
ns2.c:75: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
ns2.c:83: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
ns2.c:115: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
ns2.c:145: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
ns2.c:160: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
ns2.c:180: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
ns2.c:215: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
ns2.c:260: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
ns2.c:273: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
ns2.c:288: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
ns2.c:297: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
ns2.c:306: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
ns2.c:317: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
ns2.c:327: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
ns2.c:331: error: old-style parameter declarations in prototyped function definition
ns2.c:331: error: expected ‘{’ at end of input


龌龊地贴上原代码:
代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <curses.h>
#include <signal.h>
#include <fcntl.h>
#include <string.h>

#define  SLENMAX  1000
#define  UP          0
#define  DOWN        1
#define  LEFT        2
#define  RIGHT       3
#define  TRUE        1
#define  FALSE       0
#define  SAVE        0
#define  RESET       1

int  main(int, char**);
int  pcmdline(int, char **);
int  setup(void);
int  onsnake(int, int, int);
void snakemove(int);
void onalarm(void);
void printscore(void);
void randpoint(void);
void newfood(void)
void drawsnake(void);
void settty(void);
void drawbarrier(void);
void pusage(void);
void init(void);
void exitsnake(int);

struct opts
{
     int speedmode;
     int snake_len;
     int score;
} nsopts;

struct point
{
     int x;
     int y;
};

struct point snake[SLENMAX], rp, food;
char         snakechr[SLENMAX];
int          movehash[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int          speed[8] = {80000, 75000, 60000, 50000, 32500, 22500, 20000, 17500};

int main(int argc, char **argv)
{
     init();
     if(pcmdline(argc, argv) == -1)
          exit(0);
     if(setup() == -1)
          exitsnake(0);

     while(1)
          pause();
     
     exitsnake(0);
     return 0;
}

void init(void)
{
     nsopts.speedmode = 2;
     nsopts.snake_len = 10;
     nsopts.score     = 0;
     return;
}

int setup(void)
{
     int i;
     initscr();
     clear();
     drawbarrier();
     cbreak();
     noecho();
     settty(SAVE);
     for(i = 0; i < nsopts.snake_len; i++)
     {
          snakechr[i] = 'x';
          snake[i].x = LINES - 2;
          snake[i].y = nsopts.snake_len - 1;
     }
     snakechr[0] = 'W';
     drawsnake();
     newfood();
     printscore();

     struct itimerval nsnt;
     nsnt.it_value.tv_sec = 1;
     nsnt.it_value.tu_usec = 0;
     nsnt.it_interval.tv_sec = 0;
     nsnt.it_interval.tv_usec = speed[nsopts.speedmode];
     settitimer(ITIMER_REAL, &nsnt, NULL);
     
     signal(SIGALRM, onalarm);
     signal(SIGINT, exitsnake);
     return 0;
}

int pcmdline(int argc, char **argv)
{
     int i;
     while((i = getopt(argc, argv, "l:s:h")) != -1)
          switch(i){
          case 'h':
               pusage();
               return -1;
               break;
          case 's':
               if(optarg[0] == '?' || (!(-1 < (int)atol(optarg) && (int)atol(optarg) < 8)))
               {
                    pusage();
                    return -1;
               } else
                    nsopts.speedmode = (int)atol(optarg);
               break;
          case 'l':
               if(optarg[0] == '?' || (!(0 < (int)atol(optarg) && (int)atol(optarg) < 21)))
               {
                    pusage();
                    return -1;
               } else
                    nsopts.snake_len = (int)atol(optarg);
               break;
          }
     
     return 0;
}

void settty(int mode)
{
     static int ttymode;
     switch(mode){
     case SAVE:
          ttymode = fcntl(0, F_GETFL);
          fcntl(0, F_SETFL, ttymode | O_NDELAY);
          break;
     case RESET:
          fcntl(0, F_SETFL, ttymode);
          break;
     }
     return;
}

void drawbarrier()
{
     int i;
     move(0, 0);
     for(i = 0; i < COLS; i++)
          addch('-');
     move(LINES - 1, 0);
     for(i = 0; i < COLS; i++)
          addch('-');
     for(i = 1; i < LINES - 1; i++)
     {
          move(i, 0);
          addch('|');
          move(i, COLS - 1);
          addch('|');
     }
     refresh();
     return;
}

void onalarm(void)
{
     static int pinited = FALSE, p;
     if(!pinited)
     {
          p = 'l';
          pinited = TRUE;
     }
     
     int c = 'l', pc = EOF;
     while((c = getchar()) != EOF)
          pc = c;
     c = pc;
     if(c == EOF)
          c = p;
     
     switch(c){
     case 'k':
          snakemove(UP);
          break;
     case 'j':
          snakemove(DOWN);
          break;
     case 'h':
          snakemove(LEFT);
          break;
     case 'l':
          snakemove(RIGHT);
          break;
     }
     p = c;

     return;
}

void snakemove(int way)
{
     int i;
     struct itimerval nsnt;

     move(snake[nsopts.snake_len - 1].x, snake[nsopts.snake_len - 1].y);
     addch(' ');

     for(i = nsopts.snake_len - 1; i > 0; i--)
     {
          snake[i].x = snake[i - 1].x;
          snake[i].y = snake[i - 1].y;
     }
     snake[0].x += movehash[way][0];
     snake[0].y += movehash[way][1];
     
     if(snake[0].x == 0 || snake[0].y == 0 ||
        snake[0].x == LINES - 1 || snake[0].y == COLS - 1 ||
        onsnake(snake[0].x, snake[0].y, 1))
          exitsnake(0);
     else
          drawsnake();
     
     if(snake[0].x == food.x && snake[0].y == food.y)
     {
          newfood();
          nsopts.score++;
          nsopts.snake_len++;
          snake[nsopts.snake_len - 1].x = snake[nsopts.snake_len - 2].x;
          snake[nsopts.snake_len - 1].y = snake[nsopts.snake_len - 2].y;
          snakechr[nsopts.snake_len - 1] = 'x';
          printscore();
          if(nsopts.score % 5 == 0 && nsopts.speedmode < 7)
          {
               nsopts.speedmode += 1;
               nsnt.it_value.tv_sec = 0;
               nsnt.it_value.tu_usec = speed[nsopts.speedmode];
               nsnt.it_interval.tv_sec = 0;
               nsnt.it_interval.tv_usec = 0;
               settitimer(ITIMER_REAL, &nsnt, NULL);
          }
     }
     return;
}

void drawsnake(void)
{
     int i;
     for(i = nsopts.snake_len - 1; i >= 0; i--)
     {
          move(snake[i].x, snake[i].y);
          addch(snakechr[i]);
     }
     move(snake[0].x, snake[0].y);
     refresh();
     return;
}

void newfood(void)
{
     do{
          randpoint();
     }while(onsnake(rp.x, rp.y, 0) ||
            rp.x == 0 || rp.y == 0 || rp.x == LINES - 1 || rp.y == COLS - 1);
     move(rp.x, rp.y);
     addch('#');
     move(snake[0].x, snake[0].y);
     food.x = rp.x;
     food.y = rp.y;
     refresh();
     return;
}

int onsnake(int x, int y, int start)
{
     int i;
     for(i = start; i < nsopts.snake_len; i++)
          if(snake[i].x == x && snake[i].y == y)
               return TRUE;
     return FALSE;
}

void randpoint(void)
{
     srand((int)time(0));
     rp.x = rand() % (LINES - 2) + 1;
     srand((int)time(0));
     rp.y = rand() % (COLS - 2) + 1;
     return;
}

void printscore(void)
{
     char strscore[12];
     sprintf(strscore, "SCORE: %4d", nsopts.score);
     move(0, 0);
     addstr(strscore);
     move(snake[0].x, snake[0].y);
     refresh();
     return;
}

void exitsnake(int signum)
{
     usleep(500000);
     endwin();
     settty(RESET);
     printf("SCORE: %d\n", nsopts.score);
     printf("THE END.\n");
     exit(0);
}

void pusage(void)
{
     printf("USAGE: ns [-s speedmode 0-7] [-l inital length 1-20 ]\n");
     printf("    code by wecing\n");
     return;
}


_________________
http://wecing.blogbus.com/->welcome


页首
 用户资料  
 
2 楼 
 文章标题 : Re: [求助]自己写的一个贪食蛇小游戏,编译的时候出现错误……
帖子发表于 : 2009-06-14 12:32 
头像

注册: 2008-07-07 11:16
帖子: 130
地址: 辽宁-大连
送出感谢: 0 次
接收感谢: 0 次
啊,解决了,某个函数原型后面忘了加";"…… :em20


_________________
http://wecing.blogbus.com/->welcome


页首
 用户资料  
 
3 楼 
 文章标题 : Re: [求助]自己写的一个贪食蛇小游戏,编译的时候出现错误……
帖子发表于 : 2009-06-15 7:19 
头像

注册: 2008-10-29 22:16
帖子: 133
送出感谢: 0 次
接收感谢: 0 次
.......................
以后把行号也贴出来...........


_________________
代码:
苏老泉,二十七,始发愤,读书籍。彼既老,犹悔迟。尔小生,宜早思!!


页首
 用户资料  
 
显示帖子 :  排序  
发表新帖 回复这个主题  [ 3 篇帖子 ] 

当前时区为 UTC + 8 小时


在线用户

正在浏览此版面的用户:Sogou [Spider] 和 2 位游客


不能 在这个版面发表主题
不能 在这个版面回复主题
不能 在这个版面编辑帖子
不能 在这个版面删除帖子
不能 在这个版面提交附件

前往 :  
本站点为公益性站点,用于推广开源自由软件,由 DiaHosting VPSBudgetVM VPS 提供服务。
我们认为:软件应可免费取得,软件工具在各种语言环境下皆可使用,且不会有任何功能上的差异;
人们应有定制和修改软件的自由,且方式不受限制,只要他们自认为合适。

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
简体中文语系由 王笑宇 翻译