📄 wormlet.c
字号:
} arghy[0] = 12; for (arghx[0] = 0; arghx[0] < FIELD_RECT_HEIGHT - ARGH_SIZE; arghx[0]++){ char buf[20]; bool collision; draw_argh(0); collision = worm_argh_collision(&worms[0], 0); if (collision) { collision_count ++; } rb->snprintf(buf, sizeof buf, "collisions: %d", collision_count); rb->lcd_putsxy(0, LCD_HEIGHT -8, buf); rb->lcd_update(); } if (collision_count != ARGH_SIZE * 4) { rb->button_get(true); }}static int testline_in_rect(void) { int testfailed = -1; int rx = 10; int ry = 15; int rw = 20; int rh = 25; /* Test 1 */ int x1 = 12; int y1 = 8; int x2 = 12; int y2 = 42; if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) && !line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) { rb->lcd_drawrect(rx, ry, rw, rh); rb->lcd_drawline(x1, y1, x2, y2); rb->lcd_update(); rb->lcd_putsxy(0, 0, "failed 1"); rb->button_get(true); testfailed = 1; } /* test 2 */ y2 = 20; if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) && !line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) { rb->lcd_drawrect(rx, ry, rw, rh); rb->lcd_drawline(x1, y1, x2, y2); rb->lcd_putsxy(0, 0, "failed 2"); rb->lcd_update(); rb->button_get(true); testfailed = 2; } /* test 3 */ y1 = 30; if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) && !line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) { rb->lcd_drawrect(rx, ry, rw, rh); rb->lcd_drawline(x1, y1, x2, y2); rb->lcd_putsxy(0, 0, "failed 3"); rb->lcd_update(); rb->button_get(true); testfailed = 3; } /* test 4 */ y2 = 45; if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) && !line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) { rb->lcd_drawrect(rx, ry, rw, rh); rb->lcd_drawline(x1, y1, x2, y2); rb->lcd_putsxy(0, 0, "failed 4"); rb->lcd_update(); rb->button_get(true); testfailed = 4; } /* test 5 */ y1 = 50; if (line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) || line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) { rb->lcd_drawrect(rx, ry, rw, rh); rb->lcd_drawline(x1, y1, x2, y2); rb->lcd_putsxy(0, 0, "failed 5"); rb->lcd_update(); rb->button_get(true); testfailed = 5; } /* test 6 */ y1 = 5; y2 = 7; if (line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) || line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) { rb->lcd_drawrect(rx, ry, rw, rh); rb->lcd_drawline(x1, y1, x2, y2); rb->lcd_putsxy(0, 0, "failed 6"); rb->lcd_update(); rb->button_get(true); testfailed = 6; } /* test 7 */ x1 = 8; y1 = 20; x2 = 35; y2 = 20; if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) && !line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) { rb->lcd_drawrect(rx, ry, rw, rh); rb->lcd_drawline(x1, y1, x2, y2); rb->lcd_putsxy(0, 0, "failed 7"); rb->lcd_update(); rb->button_get(true); testfailed = 7; } /* test 8 */ x2 = 12; if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) && !line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) { rb->lcd_drawrect(rx, ry, rw, rh); rb->lcd_drawline(x1, y1, x2, y2); rb->lcd_putsxy(0, 0, "failed 8"); rb->lcd_update(); rb->button_get(true); testfailed = 8; } /* test 9 */ x1 = 25; if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) && !line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) { rb->lcd_drawrect(rx, ry, rw, rh); rb->lcd_drawline(x1, y1, x2, y2); rb->lcd_putsxy(0, 0, "failed 9"); rb->lcd_update(); rb->button_get(true); testfailed = 9; } /* test 10 */ x2 = 37; if (!line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) && !line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) { rb->lcd_drawrect(rx, ry, rw, rh); rb->lcd_drawline(x1, y1, x2, y2); rb->lcd_putsxy(0, 0, "failed 10"); rb->lcd_update(); rb->button_get(true); testfailed = 10; } /* test 11 */ x1 = 42; if (line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) || line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) { rb->lcd_drawrect(rx, ry, rw, rh); rb->lcd_drawline(x1, y1, x2, y2); rb->lcd_putsxy(0, 0, "failed 11"); rb->lcd_update(); rb->button_get(true); testfailed = 11; } /* test 12 */ x1 = 5; x2 = 7; if (line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) || line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh)) { rb->lcd_drawrect(rx, ry, rw, rh); rb->lcd_drawline(x1, y1, x2, y2); rb->lcd_putsxy(0, 0, "failed 12"); rb->lcd_update(); rb->button_get(true); testfailed = 12; } /* test 13 */ rx = 9; ry = 15; rw = FOOD_SIZE; rh = FOOD_SIZE; x1 = 10; y1 = 10; x2 = 10; y2 = 20; if (!(line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) && line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh))) { rb->lcd_drawrect(rx, ry, rw, rh); rb->lcd_drawline(x1, y1, x2, y2); rb->lcd_putsxy(0, 0, "failed 13"); rb->lcd_update(); rb->button_get(true); testfailed = 13; } /* test 14 */ rx = 9; ry = 15; rw = 4; rh = 4; x1 = 10; y1 = 10; x2 = 10; y2 = 19; if (!(line_in_rect(x1, y1, x2, y2, rx, ry, rw, rh) && line_in_rect(x2, y2, x1, y1, rx, ry, rw, rh))) { rb->lcd_drawline(x1, y1, x2, y2); rb->lcd_invertrect(rx, ry, rw, rh); rb->lcd_putsxy(0, 0, "failed 14"); rb->lcd_update(); rb->button_get(true); testfailed = 14; } rb->lcd_clear_display(); return testfailed;}/** * Just a test routine to test wether specific_worm_collision might work properly */static int test_specific_worm_collision(void) { int collisions = 0; int dir; int x = 0; int y = 0; char buf[20]; rb->lcd_clear_display(); init_worm(&worms[0], 10, 20); add_growing(&worms[0], 20 - INITIAL_WORM_LENGTH); for (dir = EAST; dir < EAST + 4; dir++) { int i; set_worm_dir(&worms[0], dir % 4); for (i = 0; i < 5; i++) { if (!(dir % 4 == NORTH && i == 9)) { move_worm(&worms[0]); draw_worm(&worms[0]); } } } for (y = 15; y < 30; y ++){ for (x = 5; x < 20; x++) { if (specific_worm_collision(&worms[0], x, y) != -1) { collisions ++; } rb->lcd_invertpixel(x + FIELD_RECT_X, y + FIELD_RECT_Y); rb->snprintf(buf, sizeof buf, "collisions %d", collisions); rb->lcd_putsxy(0, LCD_HEIGHT - 8, buf); rb->lcd_update(); } } if (collisions != 21) { rb->button_get(true); } return collisions;}static void test_make_argh(void){ int dir; int seed = 0; int hit = 0; int failures = 0; int last_failures = 0; int i, worm_idx; rb->lcd_clear_display(); worm_count = 3; for (worm_idx = 0; worm_idx < worm_count; worm_idx++) { init_worm(&worms[worm_idx], 10 + worm_idx * 20, 20); add_growing(&worms[worm_idx], 40 - INITIAL_WORM_LENGTH); } for (dir = EAST; dir < EAST + 4; dir++) { for (worm_idx = 0; worm_idx < worm_count; worm_idx++) { set_worm_dir(&worms[worm_idx], dir % 4); for (i = 0; i < 10; i++) { if (!(dir % 4 == NORTH && i == 9)) { move_worm(&worms[worm_idx]); draw_worm(&worms[worm_idx]); } } } } rb->lcd_update(); for (seed = 0; hit < 20; seed += 2) { char buf[20]; int x, y; rb->srand(seed); x = rb->rand() % (FIELD_RECT_WIDTH - ARGH_SIZE); y = rb->rand() % (FIELD_RECT_HEIGHT - ARGH_SIZE); for (worm_idx = 0; worm_idx < worm_count; worm_idx++){ if (expensive_worm_in_rect(&worms[worm_idx], x, y, ARGH_SIZE, ARGH_SIZE)) { int tries = 0; rb->srand(seed); tries = make_argh(0); if ((x == arghx[0] && y == arghy[0]) || tries < 2) { failures ++; } rb->snprintf(buf, sizeof buf, "(%d;%d) fail%d try%d", x, y, failures, tries); rb->lcd_putsxy(0, LCD_HEIGHT - 8, buf); rb->lcd_update(); rb->lcd_invertrect(x + FIELD_RECT_X, y+ FIELD_RECT_Y, ARGH_SIZE, ARGH_SIZE); rb->lcd_update(); draw_argh(0); rb->lcd_update(); rb->lcd_invertrect(x + FIELD_RECT_X, y + FIELD_RECT_Y, ARGH_SIZE, ARGH_SIZE); rb->lcd_clearrect(arghx[0] + FIELD_RECT_X, arghy[0] + FIELD_RECT_Y, ARGH_SIZE, ARGH_SIZE); if (failures > last_failures) { rb->button_get(true); } last_failures = failures; hit ++; } } }}static void test_worm_argh_collision_in_moves(void) { int hit_count = 0; int i; rb->lcd_clear_display(); init_worm(&worms[0], 10, 20); arghx[0] = 20; arghy[0] = 18; draw_argh(0); set_worm_dir(&worms[0], EAST); for (i = 0; i < 20; i++) { char buf[20]; move_worm(&worms[0]); draw_worm(&worms[0]); if (worm_argh_collision_in_moves(&worms[0], 0, 5)){ hit_count ++; } rb->snprintf(buf, sizeof buf, "in 5 moves hits: %d", hit_count); rb->lcd_putsxy(0, LCD_HEIGHT - 8, buf); rb->lcd_update(); } if (hit_count != ARGH_SIZE + 5) { rb->button_get(true); } }#endif /* DEBUG_WORMLET */extern bool use_old_rect;/** * Main entry point */enum plugin_status plugin_start(struct plugin_api* api, void* parameter){ bool worm_dead = false; int button; TEST_PLUGIN_API(api); (void)(parameter); rb = api; rb->lcd_setfont(FONT_SYSFIXED);#ifdef DEBUG_WORMLET testline_in_rect(); test_worm_argh_collision_in_moves(); test_make_argh(); test_worm_food_collision(); test_worm_argh_collision(); test_specific_worm_collision();#endif /* Setup screen */ do { char buf[20]; char* ptr; rb->lcd_clear_display(); /* first line players */ rb->snprintf(buf, sizeof buf, "%d Players UP/DN", players); rb->lcd_puts(0, 0, buf); /* second line worms */ rb->snprintf(buf, sizeof buf, "%d Worms L/R", worm_count); rb->lcd_puts(0, 1, buf); /* third line control */ if (players > 1) { if (use_remote) { ptr = "Remote Control F1"; } else { ptr = "No Rem. Control F1"; } } else { if (players > 0) { if (use_remote) { ptr = "2 Key Control F1"; } else { ptr = "4 Key Control F1"; } } else { ptr = "Out Of Control"; } } rb->lcd_puts(0, 2, ptr); rb->lcd_update(); /* user selection */ button = rb->button_get(true); switch (button) { case BUTTON_UP: if (players < 3) { players ++; if (players > worm_count) { worm_count = players; } if (players > 2) { use_remote = true; } } break; case BUTTON_DOWN: if (players > 0) { players --; } break; case BUTTON_LEFT: if (worm_count > 1) { worm_count--; if (worm_count < players) { players = worm_count; } } break; case BUTTON_RIGHT: if (worm_count < MAX_WORMS) { worm_count ++; } break; case BUTTON_F1: use_remote = !use_remote; if (players > 2) { use_remote = true; } break; case SYS_USB_CONNECTED: rb->usb_screen(); return PLUGIN_USB_CONNECTED; } } while (button != BUTTON_PLAY && button != BUTTON_OFF && button != BUTTON_ON); rb->lcd_clear_display(); /* end of setup */ do { /* button state will be overridden if the game quits with the death of the worm. Initializing button to BUTTON_OFF ensures that the user can hit BUTTON_OFF during the game to return to the menu. */ button = BUTTON_OFF; /* start the game */ worm_dead = run(); /* if worm isn't dead the game was quit via BUTTON_OFF -> no need to wait for buttons. */ if (worm_dead) { do { button = rb->button_get(true); } /* BUTTON_ON -> start new game */ /* BUTTON_OFF -> back to game menu */ while (button != BUTTON_OFF && button != BUTTON_ON); } } while (button != BUTTON_OFF); return PLUGIN_OK;}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -