在linux环境下,求弹珠台游戏代码。
2014-12-07 21:52
423 查看
http://zhidao.baidu.com/link?url=njqA2CMP_H0URs9n-O3uNWR4GEChkVK9aINGoDHIgy38Q_sDGAs2LGZVi7Penn6HmDJjRoix5vVmAF3QHrUINK #include <curses.h> #include <stdio.h> #include <signal.h> #include <sys/types.h> #include <pthread.h> #include <errno.h> #include <unistd.h> #include <fcntl.h> #include <sys/stat.h> #include <string.h> #include <stdlib.h> #define MIN(a,b) a<b?a:b #define MAX(a,b) a>b?a:b int running ; int prunning ; int quit ; int step[2]; int ballx ; int bally; int barx ; int bary ; int d ; float d1; int fd; char buf[1024]; int mrunning = 1; char *bar = "__________"; char ball ='O'; void pa(); void showmsg(); void newgame(); void paint() { clear(); while(prunning) { clear(); mvaddstr(bary,barx,bar); mvaddch(bally,ballx,ball); refresh(); usleep(100000); } pthread_exit(NULL); } void ballmove() { int ret; unlink("record.txt"); //bally = LINES - 2 ; fd = open("record.txt",O_CREAT|O_RDWR,0666); if(fd < 0) { perror("create"); exit(1); } while(1) { while(running) { memset(buf,0,sizeof(buf)); ballx += d; if(ballx == COLS) { d = -1; ballx= COLS -1; beep(); } if(ballx <0) { d = 1; ballx = 0; beep(); } bally -= d1; /*if(bally == LINES ) { //srand(time(NULL)); //d1 = (rand()%10)/(10.0)+1.0; d1 = 1; bally = LINES - 1; beep(); }*/ if(bally < 0) { //srand(time(NULL)); //d1 = (rand()%10)/(10.0) + 1.0; d1 = -1; bally= 0; beep(); } /*if(LINES - 2 == bally) { d1 = 1; bally = LINES - 2; beep(); }*/ else if(bally == LINES) { char tmp[] = "ball move over\n"; write(fd,tmp,strlen(tmp)); running = 0; prunning = 0; //quit = 0; clear(); mvaddstr(12,15,"Game over ! ,thank you play next time!"); refresh(); mvaddstr(13,15,"Input 'n' restart ,'q' exit the game,'r' return the menu "); refresh(); attron(A_BOLD); mvaddstr(14,17,"please input:"); refresh(); move(14,25); break; } else if( (bally == bary)&&((ballx >=step[0])&&(ballx <= step[1]))) { d1 = 1; bally = LINES - 1; char temp[]="the ball click the bar\n"; write(fd,temp,strlen(temp)); //beep(); } sprintf(buf,"x = %d, y = %d\n",ballx,bally); write(fd,buf,strlen(buf)); usleep(100000); } if(0 == prunning) break; } //close(fd); pthread_exit(NULL); } void barmove() { chtype input; clear(); char buf[100]; keypad(stdscr,TRUE); //bary = LINES - 1; mvaddstr(bary,barx,bar); //quit&&(input = getch())&&input !=ERR while(quit&&(input = getch())&&input !=ERR) { if(!quit) { char tmp[] = "bar move over 1\n"; write(fd,tmp,strlen(tmp)); break; } switch(input) { case KEY_LEFT: barx = MAX(barx-1,0); memset(buf,0,sizeof(buf)); step[0] -= 1; step[1] -= 1; sprintf(buf,"step[0] = %d, step[1]= %d \n",step[0],step[1]); write(fd,buf,strlen(buf)); break; case KEY_RIGHT: memset(buf,0,sizeof(buf)); barx = MIN(barx+1,COLS-1-10); step[0] += 1; step[1] += 1; sprintf(buf,"step[0] = %d, step[1]= %d \n",step[0],step[1]); write(fd,buf,strlen(buf)); break; case 'r': { char tmp[] = "bar move over 2\n"; write(fd,tmp,strlen(tmp)); //prunning = 0; //running = 0; if(running && prunning) { prunning = 0; running = 0; quit = 0; } else if(prunning &&!running)//如果按下暂停 { prunning = 0; running = 0; quit = 0; showmsg(); } else { quit = 0; } showmsg(); break; } case 'p': pa(); break; case 'q': { if(prunning&&running )//如果程序正在运行 { prunning = 0; running = 0; quit = 0; showmsg(); } else if(prunning &&!running)//如果按下暂停 { prunning = 0; running = 0; quit = 0; showmsg(); } else//直接退出 { quit = 0; mrunning = 0; prunning = 0; running = 0; } break; } case 'n': newgame(); break; } } char tmp[] = "bar move over 3\n"; write(fd,tmp,strlen(tmp)); close(fd); pthread_exit(NULL); } void showmsg() { //attron(A_BOLD); clear(); mvaddstr(10,30,"Welcome to the game!"); refresh(); attroff(A_UNDERLINE); mvaddstr(11,25,"Help:"); refresh(); mvaddstr(12,26,"'N':Start a new game."); refresh(); mvaddstr(13,26,"'q':Quit the game."); refresh(); mvaddstr(14,26,"'p':Pause the game."); refresh(); mvaddstr(15,26,"'r':Return the menu."); refresh(); } void newgame() { pthread_t pid,pid2,pid3,pid4; prunning = 1; running = 1; quit = 1; step[0] = 10; step[1] = 19; bally = LINES - 1; bary = LINES - 1 ; barx = 10; ballx = 10; d1 = 1; d = 1; //画图 pthread_create(&pid,NULL,(void *)paint,NULL); //木条滚动 pthread_create(&pid2,NULL,(void *)barmove,NULL); //球滚动 pthread_create(&pid3,NULL,(void *)ballmove,NULL); pthread_join(pid,NULL); pthread_join(pid2,NULL); pthread_join(pid3,NULL); //pthread_create(&pid4,NULL,(void *)pa,NULL); //pthread_join(pid4,NULL); } void pa() { if(running) running = 0; else running = 1; } int main() { pthread_t pid4; char buf[100]; initscr(); crmode(); noecho(); chtype input; //keypad(stdscr,TRUE); showmsg(); //pthread_create(&pid4,NULL,(void *)pa,NULL); while(mrunning&&(input = getch())) { switch(input) { case 'n': { newgame(); break; } case 'q': { mrunning = 0; break; } } } endwin(); return 0; }
相关文章推荐
- 在 linux 下 使用代码(获取/设置)环境变量
- Linux环境下android平台调试native代码, 从java debug 到C++ (NDK DEBUG)
- 【Linux开发技术之工具使用】配置VIM下编程和代码阅读环境
- Linux 环境使用vim搭建php IDE -- 提高代码编写数度数倍!手把手教你打造程序员的上古神器VIM!
- Linux环境下代码阅读与编写的利器----vim+ctags+cscope
- linux下代码阅读环境的快速建立--lxr+glimpse
- linux下阅读代码的环境设置(vim + taglist + ctags + cscope)
- Linux环境下C语言实现贪吃蛇游戏
- linux下自动同步svn代码到生产环境
- Git+GitHub创建分支&提交并贡献代码(Linux环境)
- Linux环境下android平台调试native代码, 从java debug 到C++ (NDK DEBUG)
- linux环境中svn up代码时错误。无法成功提交
- Linux环境下2410开发板串口读写关键代码
- git+github创建分支&提交并贡献代码(linux环境)
- Linux环境下C语言实现报数游戏
- 搭建Linux内核代码阅读环境
- Linux环境下2410开发板串口读写关键代码
- (转)【Linux开发技术之工具使用】配置VIM下编程和代码阅读环境
- 在windows环境下写linux代码
- Linux环境下android平台调试native代码, 从java debug 到C++ (NDK DEBUG)