void move_player(struct Player *p) { int fall_speed = player_fall_speed(p); int speed = player_speed(p); p->vx = 0; p->vy += GRAVITY; if(p->vy >= fall_speed) { p->vy = fall_speed; } if(input.left == 1) { p->vx -= speed; } if(input.right == 1) { p->vx += speed; } if(input.jump == 1) { if(p->on_ground) { p->vy = -9; } } input.jump = 0; check_against_map(p); center_camera(p); }
int main(int argc, char **argv){ awake_the_weaver(); // Initializing Weaver API camera *cam = new_camera(0.0, 0.0, 100.0, 100.0); circle *star = new_circle(50.0, 50.0, 20.0); circle *planet = new_circle(90.0, 90.0, 5.0); rectangle *trash = new_rectangle(75.0, 50.0, 1.25, 2.5); rectangle *ufo = new_rectangle(25.0, 50.0, 1.5, 1.5); film_fullcircle(cam, star, YELLOW); film_fullcircle(cam, planet, BLUE); film_rectangle(cam, trash, GREEN); film_fullrectangle(cam, ufo, WHITE); int camera_moved = 0; // Main loop for(;;){ get_input(); if(keyboard[ESC]){ break; } if(keyboard[UP] || keyboard[DOWN] || keyboard[LEFT] || keyboard[RIGHT] || keyboard[MINUS] || keyboard[PLUS] || keyboard[ENTER]){ film_fullcircle(cam, star, BLACK); film_fullcircle(cam, planet, BLACK); film_rectangle(cam, trash, BLACK); film_fullrectangle(cam, ufo, BLACK); camera_moved = 1; } if(keyboard[UP]) cam -> y -= 1.0; if(keyboard[DOWN]) cam -> y += 1.0; if(keyboard[LEFT]) cam -> x -= 1.0; if(keyboard[RIGHT]) cam -> x += 1.0; if(keyboard[PLUS]) zoom_camera(cam, 1.01); if(keyboard[MINUS]) zoom_camera(cam, 0.99); if(keyboard[ENTER]) center_camera(cam, 50.0, 50.0); film_fullcircle(cam, planet, BLACK); rotate_circle(planet, 50.0, 50.0, 0.01); film_fullcircle(cam, planet, BLUE); if(camera_moved){ film_fullcircle(cam, star, YELLOW); film_rectangle(cam, trash, GREEN); film_fullrectangle(cam, ufo, WHITE); camera_moved = 0; } weaver_rest(10000000); } may_the_weaver_sleep(); return 0; }
// Map class functions Map::Map() { player.box.x = 0; player.box.y = 0; player.image_pointer = image_loader.get_image("../images/character/character.png"); player.box.w = (*player.image_pointer).w; player.box.h = (*player.image_pointer).h; center_camera(); }
/* use m,n keys to change camera focus */ int change_camera_focus(int input_character) { switch (input_character) { case K_NEXT_PLAYER: camera_focus++; break; case K_PREV_PLAYER: camera_focus--; break; default: return 0; } if (camera_focus > Players.count-1) camera_focus=0; if (camera_focus < 0) camera_focus=Players.count; struct player *c_player = dyn_arr_get(&Players, camera_focus); center_camera(c_player->pos); return 1; }
void Map::move_player(int rel_x, int rel_y) { player.box.x += rel_x; player.box.y += rel_y; center_camera(); }
void render_shot(struct shot *shot, int s_id) { struct player *shoot_pl = dyn_arr_get(&Players, s_id); center_camera(shoot_pl->pos); clear(); render_map(); render_tanks(); debug_d(1, "RenderShotX", shoot_pl->pos.x); debug_d(1, "RenderShotY", shoot_pl->pos.y); debug_d(1, "RenderShot Angle", shot->angle); debug_d(1, "RenderShot Power", shot->power); int input_ch; struct f_pair init_v = initial_v(shot); struct f_pair acc = acceleration(); /* position (x,y) must be either double or float */ struct f_pair init_pos = map_pos_to_float(shoot_pl->pos); timeout(SHOOT_TIMEOUT); float t=1; /* this part is duplicated, because it's initial */ struct f_pair b_pos = shot_pos(init_pos, init_v, acc, t); struct map_position map_pos = round_to_map_pos(b_pos); draw_bullet(dx, dy, map_pos.x, map_pos.y); refresh(); /* end */ input_ch = getch(); if (input_ch != ERR) quit_key(input_ch); while (loc_player->state) { /* remove drew bullet */ draw_blank_bullet(dx, dy, map_pos.x, map_pos.y); debug_d(1, "BulletX", map_pos.x); debug_d(1, "BulletY", map_pos.y); t+=(float)SHOOT_TIMEOUT/100; b_pos = shot_pos(init_pos, init_v, acc, t); map_pos = round_to_map_pos(b_pos); /* draw a new one */ switch (draw_bullet(dx, dy, map_pos.x, map_pos.y)) { case SCR_OK: break; case SCR_UP: case SCR_DOWN: case SCR_LEFT: case SCR_RIGHT: center_camera(map_pos); clear(); render_map(); render_tanks(); draw_bullet(dx, dy, map_pos.x, map_pos.y); break; default: debug_s(5, "ScreenMove(shot)", "Wrong ScrMove value"); } refresh(); if (map_pos.x > map_data->length || map_pos.x < 0) break; if (t > g_impact_t) { b_pos = shot_pos(init_pos, init_v, acc, g_impact_t); map_pos = round_to_map_pos(b_pos); center_camera(map_pos); clear(); render_map(); render_tanks(); draw_bullet_explosion(dx, dy, map_pos.x, map_pos.y); refresh(); break; } /* let player see the change */ input_ch = getch(); if (input_ch != ERR) quit_key(input_ch); } input_ch = getch(); // let players input_ch = getch(); // see the explosion struct player *c_player = dyn_arr_get(&Players, camera_focus); center_camera(c_player->pos); /* SCR_ALL is already in screen update queue by center_camera*/ timeout(DEFAULT_TIMEOUT); //back to original }
uint32_t Game::run() { //This is a main game //Quit flag bool quit = false; //The frame rate regulator Timer fps; ih_init(); music_init(); world_init(); //SOME SORT OF MENU SHOULD BE HERE //Constructor calls etc Player *player = new Player(); center_camera((player->get_x() + PLAYER_WIDTH/2), (player->get_y() + PLAYER_HEIGTH/2)); show_message(); //BOX FOR TEST world_generate(); //main loop while(quit == false) { //Start the frame timer fps.start(); //While there's events to handle while( SDL_PollEvent( &event ) ) { handle_events(player); //If the user has Xed out the window if( event.type == SDL_QUIT ) { //Quit the program quit = true; } } music_play(); //MOVES player->move(); player->center(); //CLS ih_clear_screen(); //DRAWING //hide cursor SDL_ShowCursor(0); apply_cursor(); player->draw(); start_over(); while(get_curr()) { get_curr()->draw(); traverse(); } ih_update(); //Cap the frame rate if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND ) { SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() ); } } delete player; world_finit(); music_finit(); ih_cleanup(); return 0; }