void startGame(void) { oledClearScreen(1); if (game_running == 0) { gameInitBuffer(); snake_init(); } }
int main() { help_info(); snake_init(); show_food(); drow_wall(); game_info(); while(judge()){ snake_move(); } return 0; }
void setup() { initscr(); clear(); noecho(); crmode(); curs_set(0); snake_init(); food_create(); snake_show_or_hide(SNAKE_BODY); }
void snake_run(){ _delay_ms(200); snake_init(snake); snake_print(snake); uint16_t delay = 15000 / (s_difficulty * 1.0); while (1){ if(timer_read(TIMER_3) > delay){ timer_reset(TIMER_3); if(joystick_x_value() > THRESHOLD && (direction != left)){ direction = right; } else if(joystick_x_value() < -THRESHOLD && (direction != right)){ direction = left; } else if(joystick_y_value() > THRESHOLD && (direction != down)){ direction = up; } else if(joystick_y_value() < -THRESHOLD && (direction != up)){ direction = down; } if(button_right_read()){ snake_add_size(snake); } snake_move(direction, snake); //if we hit border if((snake[0][pos_x] > 15) || (snake[0][pos_x] < 0)){ snake_game_over(); } if((snake[0][pos_y] > 7) || (snake[0][pos_y] < 0)){ snake_game_over(); } //if snake head position == apple position if((snake[0][pos_x] == apple[0]) && (snake[0][pos_y] == apple[1]) && (apple[2])){ //apple bool value = 0, add snake size apple[2] = 0; snake_points++; snake_add_size(snake); } if(!(apple[2])){ snake_make_apple(); } if(snake_self_collide(snake)){ snake_game_over(); } snake_print(snake); oled_write_screen(); } //leave game if(button_left_read()){ return; } } }
static void snake_draw(struct rtgui_widget *widget) { struct rtgui_dc *dc; struct rtgui_rect rect; rt_uint32_t i; dc = rtgui_dc_begin_drawing(widget); if (dc == RT_NULL) { rt_kprintf("dc == RT_NULL\r\n"); return; } /* get room size, run once frist. */ if ((room_size_x == 0) || (room_size_y == 0)) { rt_size_t tmp; rtgui_widget_get_rect(widget, &rect); rt_kprintf("rect => x1:%d x2:%d, y1:%d y2:%d\r\n", rect.x1, rect.x2, rect.y1, rect.y2); room_size_x = rect.x2 - rect.x1; room_size_y = rect.y2 - rect.y1; memcpy(&room_rect, &rect, sizeof(struct rtgui_rect)); rt_kprintf("room_rect => x1:%d x2:%d, y1:%d y2:%d\r\n", room_rect.x1, room_rect.x2, room_rect.y1, room_rect.y2); lattice_size_x = (room_rect.x2 - room_rect.x1) / LATTICE_SIZE; lattice_size_y = (room_rect.y2 - room_rect.y1) / LATTICE_SIZE; lattice_size_x -= 2; lattice_size_y -= 2; rt_kprintf("lattice_size_x:%d lattice_size_y:%d\r\n", lattice_size_x, lattice_size_y); tmp = (room_rect.x2 - room_rect.x1) - (LATTICE_SIZE * lattice_size_x); lattice_rect.x1 = room_rect.x1 + (tmp / 2); lattice_rect.x2 = lattice_rect.x1 + (LATTICE_SIZE * lattice_size_x); tmp = (room_rect.y2 - room_rect.y1) - (LATTICE_SIZE * lattice_size_y); lattice_rect.y1 = room_rect.y1 + (tmp / 2); lattice_rect.y2 = lattice_rect.y1 + (LATTICE_SIZE * lattice_size_y); rt_kprintf("lattice_rect => x1:%d x2:%d, y1:%d y2:%d\r\n", lattice_rect.x1, lattice_rect.x2, lattice_rect.y1, lattice_rect.y2); /* create snake. */ { point_t start; map = map_init(lattice_size_x, lattice_size_y); if (map != RT_NULL) { start.x = snake_init_pointx; start.y = snake_init_pointy; run_state = SNAKE_DIR_DOWN; if (snake_init(&start, snake_length_init, run_state, map)) { food_num = 1; food_init(map, food_num); } else { map_deinit(map); map = RT_NULL; } } } } RTGUI_DC_BC(dc) = BACKGROUND_COLOR; rtgui_dc_fill_rect(dc, &room_rect); memcpy(&rect, &lattice_rect, sizeof(struct rtgui_rect)); rect.x2 += 1; rect.y2 += 1; RTGUI_DC_FC(dc) = WALL_COLOR; rtgui_dc_draw_rect(dc, &rect); for (i = 1; i < lattice_size_y; i++) { memcpy(&rect, &lattice_rect, sizeof(struct rtgui_rect)); rect.x1 += 1; rect.x2 -= 1; rtgui_dc_draw_horizontal_line(dc, rect.x1, rect.x2, rect.y1 + (LATTICE_SIZE * i)); } for (i = 1; i < lattice_size_x; i++) { memcpy(&rect, &lattice_rect, sizeof(struct rtgui_rect)); rect.y1 += 1; rect.y2 -= 1; rtgui_dc_draw_vertical_line(dc, rect.x1 + (LATTICE_SIZE * i), rect.y1, rect.y2); } /* draw snake. */ { rt_int32_t x, y; rt_bool_t first_node = RT_TRUE; for (y = 0; y < map->height; y++) { for (x = 0; x < map->width; x++) { switch (map->range[y * map->width + x]) { case NORMAL: break; case FOOD: snake_fill_lattice(dc, x, y, FOOD_COLOR); break; case OVER: if (first_node) { first_node = RT_FALSE; second_node.x = x; second_node.y = y; snake_fill_lattice(dc, x, y, SNAKE_HEAD_COLOR); } else { snake_fill_lattice(dc, x, y, SNAKE_COLOR); } break; } } } } rtgui_dc_end_drawing(dc); return; }