void Lib_Ncurses::fresh() { wclear(this->window); draw_map(); draw_snake(); wrefresh(this->window); }
// While in sleep should still respond to keypresses void playerSnake::only_respond(SDL_Event event, SDL_Surface *surface){ SDL_Rect loc; //If a key was pressed if( event.type == SDL_KEYDOWN ) { //Set the proper consequence switch( event.key.keysym.sym ) { case SDLK_UP: loc = mparts_locations.at(0); loc.y = loc.y - 30; mparts_locations.insert(mparts_locations.begin(), loc); mdirections.insert(mdirections.begin(), UP); mlast_press = UP; break; case SDLK_DOWN: loc = mparts_locations.at(0); loc.y = loc.y + 30; mparts_locations.insert(mparts_locations.begin(), loc); mdirections.insert(mdirections.begin(), DOWN); mlast_press = DOWN; break; case SDLK_LEFT: loc = mparts_locations.at(0); loc.x = loc.x - 30; mparts_locations.insert(mparts_locations.begin(), loc); mdirections.insert(mdirections.begin(), LEFT); mlast_press = LEFT; break; case SDLK_RIGHT: loc = mparts_locations.at(0); loc.x = loc.x + 30; mparts_locations.insert(mparts_locations.begin(), loc); mdirections.insert(mdirections.begin(), RIGHT); mlast_press = RIGHT; break; } } // Cases not button was pressed DO NOTHING else{ } // Remove position of tail, but only if the snake moves if(mlast_press != NONE && mIsGrowing == false){ mparts_locations.erase(mparts_locations.begin()+ mparts_locations.size()-1); mdirections.erase(mdirections.begin()+ mdirections.size()-1); mIsGrowing = true; } // draw the snake draw_snake(surface); }
/* * draws the snake and food to the screen */ void Window::draw_screen() { erase(); draw_game_frame(); draw_snake(); draw_food(); refresh(); }
int MlxLib::redraw(const std::list<Position>& pos, const Position& food) { clear_game(); draw_snake(pos); draw_food(food); repaint(); return (LIB_SUCCESS); }
int play_game(){ draw_border(); update_position(); draw_snake(); usleep(get_speed()); return 1; }
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; }
int start_game(snake psnake,food Tfood) { move_snake(psnake); draw_snake(psnake); //检测按键:当按键为Q/q时,返回-1 if(wait_for_press(psnake)==-1){ print_infowin("Quit Game!"); gameover(gamewin, "Quit Game!"); return -1; } //检测贪吃蛇状态:1.是否吃到食物;2.是否触碰自己或者边界. if(check_snake(psnake,Tfood)<0){ gameover(gamewin,"GAME OVER!!!"); print_infowin("QUIT GMAE!"); return -1; } else return 1; }
void do_frame(int full) { /* Clear it out */ memset(playground, PLAYGROUND_EMPTY, XUNITS * YUNITS); /* Fill the matrix */ draw_border(); draw_snake(); draw_nibble(); /* Only draw the score if we need to */ /* draw_score(); */ /* Draw it to the offscreen buffer */ draw_screen(full); /* And finally, show it on the screen */ show_buffer(); }
void draw() { int i; for(i=lewa_sciana; i<=prawa_sciana; i++) { PCD8544_DrawPixel(i, gorna_sciana,PCD8544_Pixel_Set); PCD8544_DrawPixel(i, dolna_sciana,PCD8544_Pixel_Set); } for(i=gorna_sciana; i<=dolna_sciana; i++) { PCD8544_DrawPixel(lewa_sciana, i,PCD8544_Pixel_Set); PCD8544_DrawPixel(prawa_sciana, i,PCD8544_Pixel_Set); } draw_snake(); draw_apple(); draw_score(); PCD8544_Refresh(); }
void redraw(struct snake * s, struct point f) { clear(); draw_border(); draw_food(f); draw_snake(s); }
// Listens for a move and performs it. // if a new move in a direction is made, go that way, else // move the same direction as you did last. void playerSnake::make_move(SDL_Event event, SDL_Surface* surface){ SDL_Rect loc; //Set the proper consequence switch( event.key.keysym.sym ) { case SDLK_UP: loc = mparts_locations.at(0); loc.y = loc.y - 30; mparts_locations.insert(mparts_locations.begin(), loc); mdirections.insert(mdirections.begin(), UP); mlast_press = UP; break; case SDLK_DOWN: loc = mparts_locations.at(0); loc.y = loc.y + 30; mparts_locations.insert(mparts_locations.begin(), loc); mdirections.insert(mdirections.begin(), DOWN); mlast_press = DOWN; break; case SDLK_LEFT: loc = mparts_locations.at(0); loc.x = loc.x - 30; mparts_locations.insert(mparts_locations.begin(), loc); mdirections.insert(mdirections.begin(), LEFT); mlast_press = LEFT; break; case SDLK_RIGHT: loc = mparts_locations.at(0); loc.x = loc.x + 30; mparts_locations.insert(mparts_locations.begin(), loc); mdirections.insert(mdirections.begin(), RIGHT); mlast_press = RIGHT; break; default: // Cases not button was pressed loc = mparts_locations.at(0); if(mlast_press == NONE) { //do nothing } else if(mlast_press == UP){ loc.y -= 30; mparts_locations.insert(mparts_locations.begin(), loc); mdirections.insert(mdirections.begin(), UP); } else if(mlast_press == DOWN){ loc.y += 30; mparts_locations.insert(mparts_locations.begin(), loc); mdirections.insert(mdirections.begin(), DOWN); } else if(mlast_press == RIGHT){ loc.x += 30; mparts_locations.insert(mparts_locations.begin(), loc); mdirections.insert(mdirections.begin(), RIGHT); } else if(mlast_press == LEFT){ loc.x -= 30; mparts_locations.insert(mparts_locations.begin(), loc); mdirections.insert(mdirections.begin(), LEFT); } break; } // Remove position of tail, but only if the snake moves if(mlast_press != NONE && mIsGrowing == false){ mparts_locations.erase(mparts_locations.begin()+ mparts_locations.size()-1); mdirections.erase(mdirections.begin()+ mdirections.size()-1); mIsGrowing = true; } // draw the snake draw_snake(surface); }
int main(int argc, char *argv[]) { // 创建一个世界 WINDOW *world; // 创建一条蛇 Snake *snake; // 创建食物所在位置坐标 Point food_point; // 初始移动方向为向右 Direction direction = RIGHT; srand(time(NULL)); // curses库初始化 initscr(); /* 初始化屏幕 */ noecho(); /* 禁止输入字符getch读取回显 */ cbreak(); /* 关闭行缓冲 */ timeout(TICKRATE); /* 每隔TICKRATE的时间检测一次窗口读操作 */ keypad(stdscr, TRUE); /* 开启键盘的键区,可以使用方向键 */ curs_set(0); /* 禁止显示鼠标指针 */ refresh(); /* 刷新屏幕,准备重画 */ // 初始化世界 world = create_world(); // 以符号*界定世界窗口的边界 box(world, '|' , '-'); // 将世界窗口显示在终端上 wrefresh(world); // 蛇初始化 snake = snake_create(SNAKE_INITIAL_LENGTH); // 食物位置初始化 food_point = new_food_position(); int ch; // 因为没TICKRATE检测一次,并且设置了nodelay模式,getch不会 // 阻塞,每隔TICKRATE下述循环将被执行一次 while ((ch = getch()) != 'q') { // 每当检测到用户输入,重画世界 wclear(world); box(world, '|' , '-'); // 蛇移动,修改蛇的各个部分移动之后的坐标值 snake_move(snake, direction, WORLD_WIDTH, WORLD_HEIGHT); Point *snake_head = (Point *)snake->head->value; // 蛇如果吃掉了食物,在新位置生成新的食物 if (are_points_equal(food_point, *snake_head)) { food_point = new_food_position(); snake_add_part_to_tail(snake); } // 绘制出蛇以及食物 mvwaddch(world, food_point.y, food_point.x, '$'); draw_snake(world, snake); // 将当前绘制的窗口显示到终端 wrefresh(world); // 在nodelay模式下,getch读取不到字符则返回ERR if(ch != ERR) { // 修正移动方向 direction = direction_for_key(ch); } } ll_free_list(snake); delwin(world); endwin(); return 0; }
int game ( int *c ) { char title[100]; int **body; int i = 0; int dir = *c; int state = 0; snakesize = 7; food_x = 0; food_y = 0; points = 0; *c = KEY_RIGHT; clear(); body = (int **) malloc(sizeof(int *)*snakesize); for ( i = 0; i != snakesize; i++ ) { body[i] = (int *) malloc(sizeof(int)*2); } attron(COLOR_PAIR(PANEL)); for ( i = 0; i != COLS; i++ ) { mvaddch(0, i, ' '); } sprintf(title," ..:: Snakie v 1.0 ::.. "); mvaddstr(0, COLS/2-strlen(title)/2, title); attroff(COLOR_PAIR(PANEL)); refresh(); for ( i = 0; i!= snakesize; i++ ) { body[i][x] = (COLS/2)-i; body[i][y] = (LINES/2); } pos_food(body); draw_food(); draw_snake(body); while ( 1 ) { clear_snake(body); switch(*c) { case KEY_UP: dir = 'U'; break; case KEY_DOWN: dir = 'D'; break; case KEY_RIGHT: dir = 'R'; break; case KEY_LEFT: dir = 'L'; break; } if ( *c == 'q' ) { if ( confirm() == 1 ) { state = 0; break; } } move_snake(dir, &body); if ( (state = check_position(body)) != 0 ) { dead = 1; break; } eat_food(&body); draw_food(); draw_info(); draw_snake(body); usleep( dir == 'L' || dir == 'R' ? speed*0.75 : speed ); } int ret = quit(&body, state); return ret; }
void input(POSITION *testa, char tavola[10][10]) { char now; int a=0, s=0, d=0, w=0, giro=1, size_snake_standard=0, size_snake=0, i, j; if (giro==1) { fiore(tavola); } while(1) { while(!kbhit()) /*finchè non premi un tasto*/ { /*CONTROLLI MANOVRE ERRATE*/ if (giro==2) { if(d==1 && now=='a') { testa=down(testa, tavola); /*aggiorna la lista*/ if(testa->next==NULL) { return; } punt=refreshpnt(0, 0); /*leggo il punteggio*/ draw_snake(testa, tavola); /*aggiorna il serpente*/ print(tavola, punt); d=0; a=0; s=1; w=0; continue; } if(s==1 && now=='w') { testa=destra(testa, tavola); /*aggiorna la lista*/ if(testa->next==NULL) { return; } punt=refreshpnt(0, 0); /*leggo il punteggio*/ draw_snake(testa, tavola); /*aggiorna il serpente*/ print(tavola, punt); d=1; a=0; s=0; w=0; continue; } if(a==1 && now=='d') { testa=up(testa, tavola); /*aggiorna la lista*/ if(testa->next==NULL) { return; } punt=refreshpnt(0, 0); /*leggo il punteggio*/ draw_snake(testa, tavola); /*aggiorna il serpente*/ print(tavola, punt); d=0; a=0; s=0; w=1; continue; } if(w==1 && now=='s') { testa=destra(testa, tavola); /*aggiorna la lista*/ if(testa->next==NULL) { return; } punt=refreshpnt(0, 0); /*leggo il punteggio*/ draw_snake(testa, tavola); /*aggiorna il serpente*/ print(tavola, punt); d=1; a=0; s=0; w=0; continue; } } /*MANOVRE*/ if(now=='d') { testa=destra(testa, tavola); /*aggiorna la lista*/ if(testa->next==NULL) { return; } punt=refreshpnt(0, 0); /*leggo il punteggio*/ draw_snake(testa, tavola); /*aggiorna il serpente*/ print(tavola, punt); d=1; a=0; s=0; w=0; } if(now=='s') { testa=down(testa, tavola); /*aggiorna la lista*/ if(testa->next==NULL) { return; } punt=refreshpnt(0, 0); /*leggo il punteggio*/ draw_snake(testa, tavola); /*aggiorna il serpente*/ print(tavola, punt); d=0; a=0; s=1; w=0; } if(now=='a') { testa=sinistra(testa, tavola); /*aggiorna la lista*/ if(testa->next==NULL) { return; } punt=refreshpnt(0, 0); /*leggo il punteggio*/ draw_snake(testa, tavola); /*aggiorna il serpente*/ print(tavola, punt); d=0; a=1; s=0; w=0; } if(now=='w') { testa=up(testa, tavola); /*aggiorna la lista*/ if(testa->next==NULL) { return; } punt=refreshpnt(0, 0); /*leggo il punteggio*/ draw_snake(testa, tavola); /*aggiorna il serpente*/ print(tavola, punt); d=0; a=0; s=0; w=1; } giro=2; usleep(100500); } now=getchar(); } }