void init_game() { g_Round = 0; int i; srand(i); init_snake(&greenSnake, 1, 0, 0); init_snake(&redSnake, 3, g_max_x-1, g_max_y-1); put_snake(&greenSnake, enTile_GreenHeadUp); put_snake(&redSnake, enTile_RedHeadUp); }
int main(int argc,char **argv){ snake cobra; int i; struct winsize w; stat.i=0; if(argc>1) stat.velocity=atoi(argv[1]); else stat.velocity=1; ioctl(0, TIOCGWINSZ, &w); rows=w.ws_row-5; cols=w.ws_col-1; init_snake(&cobra); stat.i=0; while(1){ print_matrix(cobra); sleep(stat.velocity); if(stat.i<2) go_left(cobra); else if(stat.i<4) go_right(cobra); else if(stat.i<10) go_up(cobra); else if(stat.i<18) go_right(cobra); else go_bottom(cobra); stat.i++; } return 0; }
int main(int argc, char *args[]) { int eaten = 0; srand (time(NULL)); printf("Start test...\n"); initEngine(0, 0, boardwidth, boardlength, color(255,255,255), fps, &renderBoard); init_snake(); newapple(); registerKey(&keydown); while(stepEngine() && end == false){ for(int i = 1; i < snake.digit+1; i++){ snake.xpos[i] = snake.xpos[i - 1]-(snake.xdir[i - 1]) * blockwidth; snake.ypos[i] = snake.ypos[i - 1]-(snake.ydir[i - 1]) * blocklength; } if(snake.xdir[head] != snake.xdir[head+1] || snake.ydir[head] != snake.ydir[head+1]){ for(int i = 1; i < snake.digit+1; i++){ snake.xdir[i] = snake.xdir[i - 1]; snake.ydir[i] = snake.ydir[i - 1]; } } snake.xpos[head] = snake.xpos[head] + (snake.xdir[head])*vel; snake.ypos[head] = snake.ypos[head] + (snake.ydir[head])*vel; if(iswall()){ stop(); } if(eatapple()){ newapple(); addtosnake(); eaten++; } // printf("snake.digit:%d\n",snake.digit); printf("head.xpos %d head.ypos: %d\n",snake.xpos[head],snake.ypos[head]); for(int i = 1; i < snake.digit+1; i++){ printf("digit(%d).xpos: %d digit(%d).ypos: %d xdir: %d ydir: %d\n",i,snake.xpos[i],i,snake.ypos[i],snake.xdir[i],snake.ydir[i]); } } printf("Eaten: %d\n", eaten); printf("Closing program...\n"); obliterate(); return 0; }
int snake_start() { restart: score = 0; init_snake(); init_map(); clear(); refresh(); draw_score(); draw_help(); draw_box(0, 1, LINES-2, COLS-2); draw_snake(); draw_map(); gen_food(); if ((pthread_create(&thread_input, NULL, get_input, NULL)) != 0) exit(1); while (1) { usleep(speed); check_input(); if (exit_flag) { pthread_cancel(thread_input); exit_snake(); break; } if (pause_flag) { pthread_cancel(thread_input); getch(); pause_flag = 0; if ((pthread_create(&thread_input, NULL, get_input, NULL)) != 0) exit(1); } move_snake(); if (check_dead()) { pthread_cancel(thread_input); if (snake_dead_exit()) { exit_snake(); break; } goto restart; } if (check_food()) { inc_score(); draw_score(); snake_grow_one(); gen_food(); } }; return 0; }
/** * Stellt den Initialzustand bei Level-Up her. * Schlange wird zurückgesetzt, Spielfeld neu gezeichnet. */ void init_level() { delay_time = START_DELAY - (level-1)*DELAY_REDUCTION; if(delay_time < 60) { delay_time = 60; } startup_size = INITIAL_SIZE; disable_interrupts(); draw_field(); enable_interrupts(); init_snake(); }
//初始化 void START::init(wchar_t *name_snake,wchar_t**name_text,int start,int prompt, const D3DXVECTOR3 &position,const D3DXVECTOR3 &lookat) { init_snake(name_snake); init_camera(position,lookat); init_text(name_text,start,prompt); _init_position = position; _init_target = lookat; //初始化时间 _time_one = _time_one_almost; _time_two = _time_two_almost; _time_three = _time_three_almost; _time_lock = 0; _isrender_start_text = false; _isrender_prompt_text = false; }
int main() { INITSCR(); INITINFOWIN(); INITGAMEWIN(); food Tfood=creat_food(); snake Tsnake = init_snake(); //创建贪吃蛇 draw_food(Tfood,Tsnake); /* 开始游戏 */ while(start_game(Tsnake,Tfood)>0) ; getch(); destory_snake(Tsnake); endwin(); return 0; }
static void test_handle_tunnels(void ** state) { Board b = init_board1(); int nb_snakes = 1; Serpent** snakes = malloc(nb_snakes * sizeof(Serpent*)); Serpent* snake = malloc(sizeof(Serpent)); Point p; p.x = 3; p.y = 5; init_snake(snake, 1, 50, top, p); snakes[0] = snake; add_tunnels(&b, snakes, nb_snakes); snake->tete.x = b.pTunnels[0]->entree.x; snake->tete.y = b.pTunnels[0]->entree.y; b.pTunnels[0]->nNbSorties = 1; handle_tunnels(snake, &b); assert_int_equal(snake->tete.x, b.pTunnels[0]->sorties[0].x); assert_int_equal(snake->tete.y, b.pTunnels[0]->sorties[0].y); free_board(b); free_snakes(snakes, nb_snakes); }
enum plugin_status plugin_start(const void* parameter) { (void)(parameter); /* Lets use the default font */ rb->lcd_setfont(FONT_SYSFIXED); #if LCD_DEPTH > 1 rb->lcd_set_backdrop(NULL); #endif load_all_levels(); if (num_levels == 0) { rb->splash(HZ*2, "Failed loading levels!"); return PLUGIN_OK; } highscore_load(SCORE_FILE, highscores, NUM_SCORES); while(quit==0) { game_init(); if(quit) break; rb->lcd_clear_display(); frames=1; init_snake(); /*Start Game:*/ game(); } highscore_save(SCORE_FILE, highscores, NUM_SCORES); return (quit==1) ? PLUGIN_OK : PLUGIN_USB_CONNECTED; }
static void test_is_cell_free(void ** state) { Board b = init_board1(); int nb_snakes = 1; Serpent** snakes = malloc(nb_snakes * sizeof(Serpent*)); Serpent* snake = malloc(sizeof(Serpent)); Point p; p.x = 3; p.y = 5; init_snake(snake, 1, 50, top, p); snakes[0] = snake; p.x = 10; p.y = 10; assert_int_equal(is_cell_free(p, &b, snakes, nb_snakes), 1); p.x = 0; p.y = 0; assert_int_equal(is_cell_free(p, &b, snakes, nb_snakes), 0); p.x = snake->tete.x; p.y = snake->tete.y; assert_int_equal(is_cell_free(p, &b, snakes, nb_snakes), 0); free_board(b); free_snakes(snakes, nb_snakes); }
void game() { setvbuf(stdin, NULL, _IO_MAGIC | _IONBF, 0); struct termios oldt, newt; tcgetattr( STDIN_FILENO, &oldt ); newt = oldt; newt.c_lflag &= ~( ICANON | ECHO ); tcsetattr( STDIN_FILENO, TCSETS, &newt ); fcntl(STDIN_FILENO, F_SETFL, (void*)O_NONBLOCK); int vga_fd = open("/dev/vga", O_RDONLY); ioctl(vga_fd, SETMODE, (void*)vga_mode_320x200x256); init_snake(); while(avance_snake() != -1) { usleep(100000); thread_input(); ioctl(vga_fd, FLUSH, buffer); } tcsetattr( STDIN_FILENO, TCSETS, &oldt ); }
int main() { t_snake *snake; t_display *display; int dir; display = init_display(); snake = init_snake(); while (snake->alive == true) { display->display_map(); display->display_snake(snake); dir = display->get_event(); snake->move(dir); if (snake->check_collision() == true) snake->alive = false; if (snake->check_food() == true) snake->increase_size(); snake->speed(SLOW); } return (0); }
int main( int argc, char* args[] ) { CSnake * snake; int snake_x, snake_y; unsigned char snake_hit; int k = 1, sb_toggle = 0; Uint32 delay = 10; bool bQuit = false; score = 0; srand((unsigned)time( NULL )); init(); init_items(); init_snake(); init_score(); theMap = new CSnakeMap(mapWidth, mapHeight, map_surface); theMap->AddTiles(map_tex, types_end); init_map(theMap); snake = new CSnake(mapWidth / 2, mapHeight / 2, 0, map_surface, snake_tex); snake->Grow(-1, 0); snake->Grow(-1, 0); snake->Draw(); do_move(snake, RIGHT); place_food(theMap, 1); theMap->Draw(); print_score(); draw_screen(); while (!quitcheck()) { if (bQuit) break; k = getInput(); if (k == QUIT) break; if (k >= LEFT && k <= DOWN) do_move(snake, k); if (collision(snake)) break; if (!checkMap(snake)) break; //theMap->Draw(); draw_screen(); } SDL_Rect r; r.x = 128; r.y = 256, r.w = 512, r.h = 256; SDL_BlitSurface( message, NULL, map_surface, &r ); draw_screen(); SDL_Delay(2000); SDL_FreeSurface(message); delete snake; cleanup(); return 0; }
int main(int argc, char *argv[]) { int ch; initscr(); start_color(); cbreak(); keypad(stdscr, TRUE); noecho(); curs_set(0); init_pair(1, COLOR_CYAN, COLOR_BLACK); attron(COLOR_PAIR(1)); //画边 mvaddch(1, 0, '+'); mvaddch(1, COLS-1, '+'); mvaddch(LINES-1, COLS-1, '+'); mvaddch(LINES-1, 0, '+'); mvhline(1, 1, '-', COLS-1-1); mvhline(LINES-1, 1, '-', COLS-1-1); mvvline(2, 0, '|', LINES-2-1); mvvline(2, COLS-1, '|', LINES-2-1); refresh(); attroff(COLOR_PAIR(1)); // 来条蛇 SNAKE snake; init_snake(&snake); draw_whole_snake(&snake); // 初始豆儿 int *food; food = random_food(&snake); //mvaddch(food[1], food[0], '*'); draw_food(food); int d; int i; now_time=0; while (1) { if(ioctl(0,FIONREAD,&i)<0) { printf("ioctl failed, error=%d\n ",errno); break; } if(!i) // 小蛇自动跑部分 { usleep(time_rate); now_time = now_time + time_rate; if (!(now_time < speed)) { now_time = 0; if (snake_move(&snake, food)) break; } continue; } now_time = 0; ch = getch(); if (ch < 258 || ch > 261) continue; if ( ch+snake.d != KEY_LEFT+KEY_UP ) { d = ch - snake.d; if ( d == 1 || d == -1) continue; } snake.d = ch; if (snake_move(&snake, food)) break; } getch(); endwin(); return 0; }
int main(void) { SCREEN screen; SDL_Rect src, food_place; SDL_Rect all_dst[32]; int quit_screen = TRUE; int key_quit = KEY_NO; /*创建一个窗口*/ creat_screen(&screen); /*初始化蛇的位置*/ init_snake(&screen, &src, all_dst, snake_lenth); begin_time = SDL_GetTicks(); food_place = rand_xy(&food_place); while (quit_screen) { /*移动*/ quit_screen = move_snake(&screen, &src, all_dst, snake_lenth); /*判断是否吃到食物*/ if (getfood(&all_dst[0], &food_place)) { /*刷新一个食物*/ food_place = rand_xy(&food_place); /*长度加1*/ snake_lenth++; /*如果长度大于NEXT, 速度加快,进入下一关*/ if (snake_lenth > NEXT) { /*清除蛇结点*/ clean_snake(&screen, all_dst, snake_lenth); /*速度增加*/ speed -= STEP; /*长度变为初始化长度*/ snake_lenth = INIT_LENTH; } /*如果速度速度达到WIN, 胜利*/ if (speed < WIN) { /*显示胜利*/ game_win(screen.p_screen); while (quit_screen) { quit_screen = key_snake(&key_quit); sleep(10000); } } } /*意外判断, 撞到边界、撞到自己*/ if (check(all_dst)) { /*显示gameover*/ game_over(screen.p_screen); while (quit_screen) { quit_screen = key_snake(&key_quit); usleep(10000); } } appa(&screen, &food_place); SDL_Flip(screen.p_screen); usleep(10000); } SDL_FreeSurface(screen.show_picture); SDL_FreeSurface(screen.food); return 0; }