/* main */ int main () { /* initialize the corner info */ while (1) { /* loop is executed forever */ int range; /* range to target */ int hit; /* check for targets in range */ int dir, sc; /* scan direction */ int d; /* last damage check */ new_corner (); /* start at a random corner */ d = damage (); /* get current damage */ /* scan around sc. */ sc = plot_course (500, 500); dir = sc - 46; hit = 0; do { dir += 2; if (dir >= sc + 45) /* at end of 90 degrees range... */ { if (!hit) /* move if we hit */ break; dir = sc - 45; /* ... or restart */ } range = scan (dir, 1); /* look at a direction */ /* keep firing while in range, tolerating several hits */ while (range <= 700 && range > 0 && damage () <= d + 15) { hit = 1; cannon (dir, range); /* fire! */ range = scan (dir, 1); /* check target again */ } if (hit) { dir = MAX (dir - 10, sc - 45); /* back up scan after attack */ hit = 0; } } while (d == damage ()); } }
main () { int direction; direction = 90; while (1) { if (speed () == 0) drive (direction, 100); if ((direction == 90 && loc_y () > 575) || (direction == 270 && loc_y () < 425) || (direction == 180 && loc_x () < 150) || (direction == 0 && loc_x () > 850)) { drive (direction, 0); direction = (direction + 90) % 360; } else { int range; if (range = scan (0, 10)) cannon (0, range); else if (range = scan (30, 10)) cannon (30, range); else if (range = scan (60, 10)) cannon (60, range); else if (range = scan (90, 10)) cannon (90, range); else if (range = scan (120, 10)) cannon (120, range); else if (range = scan (150, 10)) cannon (150, range); else if (range = scan (180, 10)) cannon (180, range); else if (range = scan (210, 10)) cannon (210, range); else if (range = scan (240, 10)) cannon (240, range); else if (range = scan (270, 10)) cannon (270, range); else if (range = scan (300, 10)) cannon (300, range); else if (range = scan (330, 10)) cannon (330, range); } } }
int testScanAndShoot() { struct robot walle = { "Wall-E", true, 100, 100, 0, 0, 0, 0, 0, 0, 0, { { 0, 0, 0 }, { 0, 0, 0 } } }; struct robot eve = { "Eve", true, 500, 500, 0, 0, 0, 0, 0, 0, 0, { { 0, 0, 0 }, { 0, 0, 0 } } }; struct robot masiar = { "Bubi", true, 300, 300, 0, 0, 0, 0, 0, 0, 0, { { 0, 0, 0 }, { 0, 0, 0 } } }; struct robot* robogang[3]; robogang[0] = &walle; robogang[1] = &eve; robogang[2] = &masiar; all_robots = robogang; max_robots = 3; int distance = scan(&walle, 315, 10); printf("Closest robot is at %dm from Walle\n", distance); int result = cannon(&walle, 315, distance); printf("Walle shots the closest robot! Its damage is now %d\n", masiar.damage); result = cannon(&walle, 315, distance - 10); printf("Walle shots again at the closest robot! Its damage is now %d\n", masiar.damage); result = cannon(&walle, 315, distance - 20); printf("Walle shots again at the closest robot! Its damage is still %d\n", masiar.damage); int i; for (i = 0; i < 320; i++) { cycle(); } result = cannon(&walle, 315, distance - 20); printf("Walle shots again at the closest robot! Its damage is now %d\n", masiar.damage); distance = scan(&walle, 305, 9); printf("Walle seems to be alone. The scan returns %dm\n", distance); }
int main(int argc, char *argv[]) { Matrix projectionMatrix; Matrix modelMatrix; Matrix viewMatrix; init(); ShaderProgram program(RESOURCE_FOLDER "vertex_textured.glsl", RESOURCE_FOLDER "fragment_textured.glsl"); glUseProgram(program.programID); float lastFrameTicks = 0.0f; // float vertices[] = {-0.5, -0.5, 0.5, -0.5, 0.5, 0.5, -0.5, -0.5, 0.5, 0.5, -0.5, 0.5}; // float texCoords[] = {0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0}; // float vertices[] = {-0.09, -0.09, 0.09, -0.09, 0.09, 0.09, -0.09, -0.09, 0.09, 0.09, -0.09, 0.09}; Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, 4096 ); SDL_Event event; bool done = false; /* Sprites courtesy of http://gooperblooper22.deviantart.com/art/Space-Invaders-Sprite-Sheet-135338373 */ GLuint spriteSheetTexture = LoadTexture("space_invaders_sprite_sheet_by_gooperblooper22.png"); GLuint white = LoadTexture("white.png"); GLuint textTexture = LoadTexture("font1.png"); Mix_Music *bgm = Mix_LoadMUS("bgm.mp3"); Mix_Chunk *missile = Mix_LoadWAV("157439__nengisuls__misslie-1.wav"); bool pressed = false; int score = 0; int currentIndex = 0; int state; const int numFrames = 2; float animation[] = {(74.0f/617.0f),(107.0f/617.0f)}; float animationElapsed = 0.0f; float framesPerSecond = 0.0004f; std::vector<SheetSprite> aliens; while (!done) { while (SDL_PollEvent(&event)) { if (event.type == SDL_QUIT || event.type == SDL_WINDOWEVENT_CLOSE) { done = true; } } program.setProjectionMatrix(projectionMatrix); program.setModelMatrix(modelMatrix); program.setViewMatrix(viewMatrix); float ticks = (float)SDL_GetTicks()/1000; float elapsed = ticks - lastFrameTicks; lastFrameTicks = ticks; animationElapsed += 1/elapsed; if(animationElapsed > 1.0/framesPerSecond) { currentIndex++; animationElapsed = 0.0; if(currentIndex > numFrames-1) { currentIndex = 0; } } glClear(GL_COLOR_BUFFER_BIT); score = ticks; SheetSprite menu(spriteSheetTexture, 160.0f/617.0f, 1.0f/2035.0f, 250.0f/617.0f, 171.0f/2035.0f, 0.5); Entity text; Player cannon(spriteSheetTexture, 277.0f/617.0f, 227.0f/2035.0f, 26.0f/617.0f, 17.0f/2035.0f, 0.05); SheetSprite alien(spriteSheetTexture, animation[currentIndex], (225.0f/2035.0f), 22.0f/617.0f, 16.0f/2035.0f, 0.05); const Uint8 *keys = SDL_GetKeyboardState(NULL); switch (state) { case STATE_MAIN_MENU: menu.update(lastFrameTicks, elapsed, projectionMatrix, viewMatrix, program, true); menu.draw(program); if (!Mix_PlayingMusic()) { Mix_PlayMusic(bgm, -1); } if (keys[SDL_SCANCODE_RETURN]) { state++; } break; case STATE_GAME_LEVEL: text.DrawText(program, textTexture, "Score:" + std::to_string(score), 0.075, 0, -0.23, 0.9); cannon.update(lastFrameTicks, elapsed, projectionMatrix, viewMatrix, program, white, alien); cannon.draw(program); alien.update(lastFrameTicks, elapsed, projectionMatrix, viewMatrix, program, false); alien.draw(program); if (keys[SDL_SCANCODE_SPACE]) { Mix_PlayChannel( -1, missile, 0); Mix_VolumeChunk(missile, 15); } if (keys[SDL_SCANCODE_ESCAPE] && (state == 1)) { pressed = true; state++; } break; case STATE_PAUSE: if (Mix_PlayingMusic()) { Mix_PausedMusic(); } text.DrawText(program, textTexture, "Paused", 0.1, 0, -0.23, 0); if (keys[SDL_SCANCODE_ESCAPE] && pressed) { pressed = false; state--; } break; } SDL_GL_SwapWindow(displayWindow); } Mix_FreeMusic(bgm); Mix_FreeChunk(missile); Mix_Quit(); cleanup(); return 0; }