///////////////////////运行函数 void initSnake() { int i; for (i = 0; i < SNAKE_MIN_LEN; i++) { mySnake[i].x = 10 - i; mySnake[i].y = 3;//////(10,3)坐标可任意摆放 } fixConsoleSize((TOTAL_WIDTH) * 2, TOTAL_HEIGHT); setCursorVisible(0); setConsoleTitle("Snake"); }
/***************************************************************************** int initConsole(void) Description : Initializes the console window. Parameters : - Returns : TRUE, if everything initialized correctly. FALSE, if not. *****************************************************************************/ int initConsole(void) { if(!setConsoleSize(100, 40)) { // console size could not be set return FALSE; } if(!setConsoleTitle("Game Compilation")) { // console title could not be set return FALSE; } // everything went well return TRUE; }
//主函数 int main() { clock_t start,finish; int isPause = 1; int chance=0; float RewardTime=0; int z; setCursorVisible(0); setConsoleTitle("贪吃蛇--D_Y。 2010.1.11"); //标题 ch=menu(); ch2=menuDouble(); if(ch2=='1') z=0; if(ch2=='2') z=1; initSnake(SNAKE_MIN_LEN,z); drawMap(); drawSnake(z); //PlaySound("d:\\泡泡堂轻松欢快音乐音效WAV.wav",NULL, SND_ASYNC|SND_NODEFAULT|SND_LOOP ); switch(ch) //不同的难度对应不同的奖励食物消失时间 { case '1': RewardTime=2500; case '2': RewardTime=3000; case '3': RewardTime=3500; } while (!isOver[0]&&!isOver[1]) //当两条蛇任意一条死亡则游戏结束 { srand((uint32)time(NULL)); if (!isPause) { moveSnake(z); if (!isFood) drawFood(); while(((point[1]+point[0])%4==0)&&((point[1]+point[0])>3)&&p==1) //规定在得分大于三分时每吃掉四个常规食物产生一个奖励食物 { if (!isReward) drawReward(); start=clock(); //画出食物,记录此刻时间 break; } finish=start+RewardTime ; //结束时间为开始时间加规定的奖励食物出现时间 if((isReward==1)&&(finish<=clock())) { drawBlock(myReward.x, myReward.y, BS_SPACE); //超出规定时间则将奖励食物用背景方框覆盖 myReward.x=-1; myReward.y=-1; isReward=0; } if(((point[1]+point[0])%3==0)&&((point[1]+point[0])>2)&&q==0) { clearBar(); drawBar(); } setTextColor(myColors[4]); //规定出输出分数等信息 gotoTextPos(MAP_BASE_X*2 -14 , MAP_BASE_Y + 2); printf("Player 1"); gotoTextPos(MAP_BASE_X*2 -14 , MAP_BASE_Y + 4); printf("Score: %d.", point[0]); if(z==1) { gotoTextPos(MAP_BASE_X*2 + MAP_WIDTH*2+4 , MAP_BASE_Y + 2); printf("Player 2"); gotoTextPos(MAP_BASE_X*2 + MAP_WIDTH*2+4 , MAP_BASE_Y + 4); printf("Score: %d.", point[1]); } } DELAY(ch); //赋予游戏速度 delayMS(80- snakeLength[0]*2); //游戏速度随着蛇身增长而增加 if (jkHasKey()) //键盘控制 { switch (jkGetKey()) { case JK_UP: if (snakeDir[0] != DIR_DOWN) snakeDir[0] = DIR_UP; break; case 'w': if (snakeDir[1] != DIR_DOWN) snakeDir[1] = DIR_UP; break; case JK_DOWN: if (snakeDir[0] != DIR_UP) snakeDir[0] = DIR_DOWN; break; case 's': if (snakeDir[1] != DIR_UP) snakeDir[1] = DIR_DOWN; break; case JK_LEFT: if (snakeDir[0] != DIR_RIGHT) snakeDir[0] = DIR_LEFT; break; case 'a': if (snakeDir[1] != DIR_RIGHT) snakeDir[1] = DIR_LEFT; break; case JK_RIGHT: if (snakeDir[0] != DIR_LEFT) snakeDir[0] = DIR_RIGHT; case 'd': if (snakeDir[1] != DIR_LEFT) snakeDir[1] = DIR_RIGHT; break; case JK_ENTER: case JK_SPACE: isPause = !isPause; break; case JK_ESC: isOver[0]=1; break; default: break; } } } PlaySound("d:\\超级马里奥兄弟 死掉音.wav",NULL, SND_ASYNC|SND_NODEFAULT ); gotoTextPos(MAP_BASE_X + MAP_WIDTH - 7, MAP_BASE_Y + MAP_HEIGHT + 1); if(isOver[0]==1&&z==0) printf("Game Over!!"); if(isOver[0]==1&&z==1) printf("Player 2 win!!"); if(isOver[1]==1) printf("Player 1 win!!");//游戏结束,在规定处显示分数 getch(); return 0; }
/** \fn int main(int argc, char *argv[]) \brief main routine \param argc number of commandline arguments + 1 \param argv string array containing commandline arguments (argv[0] contains name of executable) \return dummy return code (not used) Main routine for import, programming, and check routines */ int main(int argc, char ** argv) { char *appname; // name of application without path char portname[STRLEN]; // name of communication port int baudrate; // communication baudrate [Baud] uint8_t resetSTM8; // 0=no reset; 1=HW reset via DTR (RS232/USB) or GPIO18 (Raspi); 2=SW reset by sending 0x55+0xAA uint8_t enableBSL; // don't enable ROM bootloader after upload (caution!) uint8_t jumpFlash; // jump to flash after upload uint8_t pauseOnLaunch; // prompt for <return> prior to upload int flashsize; // size of flash (kB) for w/e routines uint8_t versBSL; // BSL version for w/e routines char hexfile[STRLEN]; // name of file to flash HANDLE ptrPort; // handle to communication port char buf[BUFSIZE]; // buffer for hexfiles char image[BUFSIZE]; // memory image buffer uint32_t imageStart; // starting address of image uint32_t numBytes; // number of bytes in image char *ptr=NULL; // pointer to memory int i, j; // generic variables //char Tx[100], Rx[100]; // debug: buffer for tests // initialize global variables g_pauseOnExit = 1; // wait for <return> before terminating g_UARTmode = 0; // 2-wire interface with UART duplex mode verbose = false; // verbose output when requested only // initialize default arguments portname[0] = '\0'; // no default port name baudrate = 230400; // default baudrate resetSTM8 = 0; // don't automatically reset STM8 jumpFlash = 1; // jump to flash after uploade pauseOnLaunch = 1; // prompt for return prior to upload enableBSL = 1; // enable bootloader after upload hexfile[0] = '\0'; // no default hexfile // for debugging only //sprintf(portname, "/dev/tty.usbserial-A4009I0O"); // required for strncpy() portname[STRLEN-1] = '\0'; hexfile[STRLEN-1] = '\0'; // reset console color (needs to be called once for Win32) setConsoleColor(PRM_COLOR_DEFAULT); //////// // parse commandline arguments //////// for (i=1; i<argc; i++) { // debug: print argument //printf("arg %d: '%s'\n", (int) i, argv[i]); // name of communication port if (!strcmp(argv[i], "-p")) strncpy(portname, argv[++i], STRLEN-1); // communication baudrate else if (!strcmp(argv[i], "-b")) sscanf(argv[++i],"%d",&baudrate); // UART mode: 0=duplex, 1=1-wire reply, 2=2-wire reply (default: duplex)\n"); else if (!strcmp(argv[i], "-u")) { sscanf(argv[++i], "%d", &j); g_UARTmode = j; } // name of hexfile else if (!strcmp(argv[i], "-f")) strncpy(hexfile, argv[++i], STRLEN-1); // HW reset STM8 via DTR line (RS232/USB) or GPIO18 (Raspi only) else if (!strcmp(argv[i], "-r")) { sscanf(argv[++i], "%d", &j); resetSTM8 = j; } // don't enable ROM bootloader after upload (caution!) else if (!strcmp(argv[i], "-x")) enableBSL = 0; // don't jump to address after upload else if (!strcmp(argv[i], "-j")) jumpFlash = 0; // don't prompt for <return> prior to upload else if (!strcmp(argv[i], "-Q")) pauseOnLaunch = 0; // don't prompt for <return> prior to exit else if (!strcmp(argv[i], "-q")) g_pauseOnExit = 0; // verbose output else if (!strcmp(argv[i], "-v")) verbose = true; // else print list of commandline arguments and language commands else { if (strrchr(argv[0],'\\')) appname = strrchr(argv[0],'\\')+1; // windows else if (strrchr(argv[0],'/')) appname = strrchr(argv[0],'/')+1; // Posix else appname = argv[0]; printf("\n"); printf("usage: %s [-h] [-p port] [-b rate] [-u mode] [-f file] [-r ch] [-x] [-j] [-Q] [-q] [-v]\n\n", appname); printf(" -h print this help\n"); printf(" -p port name of communication port (default: list all ports and query)\n"); printf(" -b rate communication baudrate in Baud (default: 230400)\n"); printf(" -u mode UART mode: 0=duplex, 1=1-wire reply, 2=2-wire reply (default: duplex)\n"); printf(" -f file name of s19 or intel-hex file to flash (default: none)\n"); #ifdef __ARMEL__ printf(" -r ch reset STM8: 1=DTR line (RS232), 2=send 'Re5eT!' @ 115.2kBaud, 3=GPIO18 pin (Raspi) (default: no reset)\n"); #else printf(" -r ch reset STM8: 1=DTR line (RS232), 2=send 'Re5eT!' @ 115.2kBaud (default: no reset)\n"); #endif printf(" -x don't enable ROM bootloader after upload (default: enable)\n"); printf(" -j don't jump to flash after upload (default: jump to flash)\n"); printf(" -Q don't prompt for <return> prior to upload (default: prompt)\n"); printf(" -q don't prompt for <return> prior to exit (default: prompt)\n"); printf(" -v verbose output\n"); printf("\n"); Exit(0, 0); } } // process commandline arguments //////// // print app name & version, and change console title //////// get_app_name(argv[0], VERSION, buf); printf("\n%s\n", buf); setConsoleTitle(buf); //////// // if no port name is given, list all available ports and query //////// if (strlen(portname) == 0) { printf(" enter comm port name ( "); list_ports(); printf(" ): "); scanf("%s", portname); getchar(); } // if no comm port name // If specified import hexfile - do it early here to be able to report file read errors before others if (strlen(hexfile)>0) { const char *shortname = strrchr(hexfile, '/'); if (!shortname) shortname = hexfile; // convert to memory image, depending on file type const char *dot = strrchr (hexfile, '.'); if (dot && !strcmp(dot, ".s19")) { if (verbose) printf(" Loading Motorola S-record file %s …\n", shortname); load_hexfile(hexfile, buf, BUFSIZE); convert_s19(buf, &imageStart, &numBytes, image); } else if (dot && (!strcmp(dot, ".hex") || !strcmp(dot, ".ihx"))) { if (verbose) printf(" Loading Intel hex file %s …\n", shortname); load_hexfile(hexfile, buf, BUFSIZE); convert_hex(buf, &imageStart, &numBytes, image); } else { if (verbose) printf(" Loading binary file %s …\n", shortname); load_binfile(hexfile, image, &imageStart, &numBytes, BUFSIZE); } } //////// // put STM8 into bootloader mode //////// if (pauseOnLaunch) { printf(" activate STM8 bootloader and press <return>"); fflush(stdout); fflush(stdin); getchar(); } //////// // open port with given properties //////// printf(" open port '%s' with %gkBaud ... ", portname, (float) baudrate / 1000.0); fflush(stdout); if (g_UARTmode == 0) ptrPort = init_port(portname, baudrate, 1000, 8, 2, 1, 0, 0); // use even parity else ptrPort = init_port(portname, baudrate, 1000, 8, 0, 1, 0, 0); // use no parity printf("ok\n"); fflush(stdout); // debug: communication test (echo+1 test-SW on STM8) /* printf("open: %d\n", ptrPort); for (i=0; i<254; i++) { Tx[0] = i; send_port(ptrPort, 1, Tx); receive_port(ptrPort, 1, Rx); printf("%d %d\n", (int) Tx[0], (int) Rx[0]); } printf("done\n"); Exit(1,0); */ //////// // communicate with STM8 bootloader //////// // HW reset STM8 using DTR line (USB/RS232) if (resetSTM8 == 1) { printf(" reset via DTR ... "); pulse_DTR(ptrPort, 10); printf("done\n"); SLEEP(5); // allow BSL to initialize } // SW reset STM8 via command 'Re5eT!' at 115.2kBaud (requires respective STM8 SW) else if (resetSTM8 == 2) { set_baudrate(ptrPort, 115200); // expect STM8 SW to receive at 115.2kBaud printf(" reset via UART command ... "); sprintf(buf, "Re5eT!"); // reset command (same as in STM8 SW!) for (i=0; i<6; i++) { send_port(ptrPort, 1, buf+i); // send reset command bytewise to account for slow handling SLEEP(10); } printf("done\n"); set_baudrate(ptrPort, baudrate); // restore specified baudrate } // HW reset STM8 using GPIO18 pin (only Raspberry Pi!) #ifdef __ARMEL__ else if (resetSTM8 == 3) { printf(" reset via GPIO18 ... "); pulse_GPIO(18, 10); printf("done\n"); SLEEP(5); // allow BSL to initialize } #endif // __ARMEL__ // synchronize baudrate bsl_sync(ptrPort); // get bootloader info for selecting flash w/e routines bsl_getInfo(ptrPort, &flashsize, &versBSL); // select device dependent flash routines for upload if ((flashsize==32) && (versBSL==0x10)) { ptr = (char*) STM8_Routines_E_W_ROUTINEs_32K_ver_1_0_s19; ptr[STM8_Routines_E_W_ROUTINEs_32K_ver_1_0_s19_len]=0; } else if ((flashsize==32) && (versBSL==0x12)) { ptr = (char*) STM8_Routines_E_W_ROUTINEs_32K_ver_1_2_s19; ptr[STM8_Routines_E_W_ROUTINEs_32K_ver_1_2_s19_len]=0; } else if ((flashsize==32) && (versBSL==0x13)) { ptr = (char*) STM8_Routines_E_W_ROUTINEs_32K_ver_1_3_s19; ptr[STM8_Routines_E_W_ROUTINEs_32K_ver_1_3_s19_len]=0; } else if ((flashsize==32) && (versBSL==0x14)) { ptr = (char*) STM8_Routines_E_W_ROUTINEs_32K_ver_1_4_s19; ptr[STM8_Routines_E_W_ROUTINEs_32K_ver_1_4_s19_len]=0; } else if ((flashsize==128) && (versBSL==0x20)) { ptr = (char*) STM8_Routines_E_W_ROUTINEs_128K_ver_2_0_s19; ptr[STM8_Routines_E_W_ROUTINEs_128K_ver_2_0_s19_len]=0; } else if ((flashsize==128) && (versBSL==0x21)) { ptr = (char*) STM8_Routines_E_W_ROUTINEs_128K_ver_2_1_s19; ptr[STM8_Routines_E_W_ROUTINEs_128K_ver_2_1_s19_len]=0; } else if ((flashsize==128) && (versBSL==0x22)) { ptr = (char*) STM8_Routines_E_W_ROUTINEs_128K_ver_2_2_s19; ptr[STM8_Routines_E_W_ROUTINEs_128K_ver_2_2_s19_len]=0; } else if ((flashsize==128) && (versBSL==0x24)) { ptr = (char*) STM8_Routines_E_W_ROUTINEs_128K_ver_2_4_s19; ptr[STM8_Routines_E_W_ROUTINEs_128K_ver_2_4_s19_len]=0; } else if ((flashsize==256) && (versBSL==0x10)) { ptr = (char*) STM8_Routines_E_W_ROUTINEs_256K_ver_1_0_s19; ptr[STM8_Routines_E_W_ROUTINEs_256K_ver_1_0_s19_len]=0; } else { setConsoleColor(PRM_COLOR_RED); fprintf(stderr, "\n\nerror: unsupported device, exit!\n\n"); Exit(1, g_pauseOnExit); } { char ramImage[8192]; uint32_t ramImageStart; uint32_t numRamBytes; convert_s19(ptr, &ramImageStart, &numRamBytes, ramImage); if (verbose) printf("Uploading RAM routines\n"); bsl_memWrite(ptrPort, ramImageStart, numRamBytes, ramImage, 0); } // if specified upload hexfile if (strlen(hexfile)>0) bsl_memWrite(ptrPort, imageStart, numBytes, image, 1); // memory read //imageStart = 0x8000; numBytes = 128*1024; // complete 128kB flash //imageStart = 0x00A0; numBytes = 352; // RAM //bsl_memRead(ptrPort, imageStart, numBytes, image); // enable ROM bootloader after upload (option bytes always on same address) if (enableBSL==1) { printf(" activate bootloader ... "); bsl_memWrite(ptrPort, 0x487E, 2, (char*)"\x55\xAA", 0); printf("done\n"); } // jump to flash start address after upload (reset vector always on same address) if (jumpFlash) bsl_jumpTo(ptrPort, 0x8000); //////// // clean up and exit //////// close_port(&ptrPort); if (verbose) printf("done with program\n"); Exit(0, g_pauseOnExit); // avoid compiler warnings return(0); } // main