原创C语言程式《变色钻石》
试试看写了一个可以使用菜单功能选项的功能写法,写了一个可以选择颜色然后变更钻石颜色的小程式
程式运用了curses的header,主要是在Unix系统下编译,编译方法是 cc code.c -lcurses
附上源码,大家一起学习
程序代码:
#include<curses.h> #include<stdlib.h> #define RED "\033[31m" #define GREEN "\033[32m" #define YELLOW "\033[33m" #define BLUE "\033[34m" #define MAGENTA "\033[35m" #define CYAN "\033[36m" #define CLEAN "\033[37m" #define CURSAVE "\033[s" #define CURBACK "\033[u" #define CLEAR "\033[2J" char color[7][10]={RED,GREEN,YELLOW,BLUE,MAGENTA,CYAN,CLEAN}; void initial(){ initscr(); cbreak(); nonl(); noecho(); intrflush(stdscr,FALSE); keypad(stdscr,TRUE); refresh(); } void set_Word(char **word); void showColor(int *gate,char *word[]); void closeOther(int *gate,int on); void drawDiamend(int *gate,int point); void killDiamend(); void rainbow(); int main(){ int ch; int gate[8]={0,0,0,0,0,0,0,0}; char *word[8]; int wx; int point; for(wx=0;wx<=8;wx++){ *(word+wx) = (char *)malloc(sizeof(char)*10); } set_Word(word); point = 0; // Point To Red initial(); mvaddstr(4,22,"Please Select Your Favourite Color:\n"); closeOther(gate,0); showColor(gate,word); drawDiamend(gate,0); do{ ch = getch(); switch(ch){ case KEY_UP: if(point < 1){ point = 6; }else{ point--; } closeOther(gate,point); showColor(gate,word); break; case KEY_DOWN: if(point > 5){ point = 1; }else{ point++; } closeOther(gate,point); showColor(gate,word); break; case '\r': killDiamend(); refresh(); drawDiamend(gate,point); break; case 27: killDiamend(); rainbow(); endwin(); exit(1); break; } }while(1); refresh(); } void set_Word(char *word[]){ *word = "RED"; *(word+1) = "GREEN"; *(word+2) = "YELLOW"; *(word+3) = "BLUE"; *(word+4) = "MAGENTA"; *(word+5) = "CYAN"; *(word+6) = "WHITE"; } void showColor(int *gate,char **word){ int x; for(x=0;x<8;x++){ if(gate[x]==1){ attron(A_REVERSE); mvaddstr(5+x,22,word[x]); attroff(A_REVERSE); refresh(); }else{ attrset(A_NORMAL); mvaddstr(5+x,22,word[x]); } } refresh(); } void closeOther(int *gate, int on){ int x; for(x=0;x<8;x++){ *(gate+x) = 0; } *(gate+on) =1; return; } void killDiamend(){ mvaddstr(6,50,"aaaaaaaaaaa"); mvaddstr(7,48,"aaaaaaaaaaaaaaa"); mvaddstr(8,46,"aaaaaaaaaaaaaaaaaaa"); mvaddstr(9,48,"aaaaaaaaaaaaaaa"); mvaddstr(10,50,"aaaaaaaaaaaa"); mvaddstr(11,52,"aaaaaaaa"); mvaddstr(12,54,"aaaa"); refresh(); printf(CLEAN); return; } void drawDiamend(int *gate,int point){ printf(color[point]); mvaddstr(6,50,"***********"); mvaddstr(7,48,"***************"); mvaddstr(8,46,"*******************"); mvaddstr(9,48,"***************"); mvaddstr(10,50,"************"); mvaddstr(11,52,"********"); mvaddstr(12,54,"****"); refresh(); printf(CLEAN); return; } void rainbow(){ printf(color[0]); mvaddstr(6,50,"***********"); refresh(); mvaddstr(7,48,"***************"); printf(color[1]); refresh(); mvaddstr(8,46,"*******************"); printf(color[2]); refresh(); mvaddstr(9,48,"***************"); printf(color[3]); refresh(); mvaddstr(10,50,"************"); printf(color[4]); refresh(); mvaddstr(11,52,"********"); printf(color[5]); refresh(); mvaddstr(12,54,"****"); refresh(); printf(CLEAN); return; } //www. 版權所有 , 盜版不究 :P
[此贴子已经被作者于2016-1-10 18:24编辑过]