void EnnemiSmurfette::onAvance() { static const int marche_droite[] = { 315, 316, 317, 318, 319, 318, 317, 316 }; static const int marche_gauche[] = { 320, 321, 322, 323, 324, 323, 322, 321 }; static const int SPEED = 1; tombe(); // Marche // if (x - SPEED < xmin || mur_opaque(x - SPEED, y) || (plat(x - SPEED, y) == 0 && plat(x - SPEED, y + 5) == 0)) dir = SENS_DROITE; else if (x + SPEED > offset + 640 || mur_opaque(x + SPEED, y) || (plat(x + SPEED, y) == 0 && plat(x + SPEED, y + 5) == 0)) dir = SENS_GAUCHE; if (dir == SENS_DROITE) { marche(SPEED); pic = pbk_ennemis[anime(marche_droite, 8, 6)]; } else { marche(-SPEED); pic = pbk_ennemis[anime(marche_gauche, 8, 6)]; } if (etape_shoot >= wait_shoot && x > offset + 10 && x < offset + 630) { etape = ss_etape = 0; etat = ETAT_TIRE; onTire(); return; } colFromPic(); }
void EnnemiBisouNuage::onNormal() { static const int anim_gauche [] = { 229, 230, 231, 230 }; static const int anim_droite [] = { 226, 227, 228, 227 }; if (x > (offset + 320)) { dir = SENS_GAUCHE; pic = pbk_ennemis[anime(anim_gauche, 4, 10)]; } else { dir = SENS_DROITE; pic = pbk_ennemis[anime(anim_droite, 4, 10)]; } if (etape_shoot >= wait_shoot && x <= offset + 640) { Sprite * s = new TirNuage(); s->x = x; s->y = y - 17; s->dir = dir; list_tirs_ennemis.ajoute((void*) s); etape_shoot = 0; wait_shoot = 250 + rand() % 250; } colFromPic(); }
void jump(int start_x,int x_end,int start_y,cv::Mat character,cv::Mat back,int width,int height){ cv::Mat blend; cv::Mat img; char c; int walk_end_x = x_end-start_x-329; int top_x =(start_x+x_end)/2; int y = start_y-50; int x = start_x; int up_y=start_y+character.rows; std::cout<<"y="<<y<<",start_y"<<start_y<<std::endl; std::cout<<"x="<<x<<std::endl; img = character; x= 0; std::cout<<"top_x"<<top_x<<std::endl; while(start_x<=x_end){ overlay(img, back, blend, cv::Point(start_x+200,start_y)); cv::Mat anime(blend,cv::Rect(x+start_x,0,width,height)); //std::cout<<"x="<<start_x<<",y="<<start_y<<std::endl; imshow(winname,anime); if(start_y >= 255-img.rows){ start_y=start_y-6; }else if(start_x<x_end){ start_x=start_x+6; //std::cout<<"x_2="<<x+start_x<<std::endl; } c=cv::waitKey(10); if(c==27){ break; } } while(start_x>=x_end){ overlay(img, back, blend, cv::Point(start_x+200,start_y)); cv::Mat anime(blend,cv::Rect(x+start_x,0,width,height)); //std::cout<<"x="<<start_x<<",y="<<start_y<<std::endl; imshow(winname,anime); if(start_y <= y){ start_y=start_y+6; } else if(start_y>=y){ break; } c=cv::waitKey(10); if(c==27){ break; } } }
void walk(int start_x,int end_x,int y,cv::Mat character1,cv::Mat character2,cv::Mat back,int width,int height){ int x=0; char c; cv::Mat blend; cv::Mat img; while(start_x+x<=end_x){ if(x%3==0){ if(!img.data) img = character1; else if(img.data==character1.data) img = character2; else if(img.data==character2.data) img = character1; } // std::cout<<"x="<<x+start_x<<",y="<<y<<std::endl; // std::cout<<"width="<<width<<",height="<<height<<std::endl; overlay(img, back, blend, cv::Point(200+x+start_x,225)); //合成 cv::Mat anime(blend,cv::Rect(start_x+x,0,width,height)); imshow(winname,anime); x=x+4; c=cv::waitKey(10); if(c==27){ break; } } }
void EnnemiToad::onCharge() { speed = TOAD_CHARGE_SPEED; if (x - speed < xmin || mur_opaque(x - speed, y)) { dir = SENS_DROITE; speed = 1; etat = ETAT_AVANCE; wait_for_charge = 0; charge_delay = 50 + rand() % 250; etape = 0; ss_etape = 0; onAvance(); return; } else if (x + speed > offset + 640 || mur_opaque(x + speed, y)) { dir = SENS_GAUCHE; speed = 1; etat = ETAT_AVANCE; wait_for_charge = 0; charge_delay = 50 + rand() % 250; etape = 0; ss_etape = 0; onAvance(); return; } charge_etape += 1; if (charge_etape < 80) { if (dir == SENS_DROITE) { //marche(speed); pic = pbk_ennemis[anime(anim_toad_marche_droite, 4, 3)]; } else { //marche( -speed); pic = pbk_ennemis[anime(anim_toad_marche_gauche, 4, 3)]; } } else { if (dir == SENS_DROITE) { marche(speed); pic = pbk_ennemis[anime(anim_toad_marche_droite, 4, 2)]; } else { marche(-speed); pic = pbk_ennemis[anime(anim_toad_marche_gauche, 4, 2)]; } } colFromPic(); }
int main(int argc, char *argv[]) { int ret = 0,cnt =0; int fd,fds,i,diff =0; unsigned int dist,prevdist = 0,prev_direction; struct sigaction act; fd = open(display, O_RDWR); if (fd < 0) pabort("can't open device"); printf("\n initialising....:"); ioctl(fd, SPI_LED_INITIALISE, 0); transfer(fd); fds = open(sensor, O_RDWR); if (fds < 0) pabort("can't open sensor"); memset (&act, '\0', sizeof(act)); act.sa_sigaction = &terminatehdl; act.sa_flags = SA_SIGINFO; if (sigaction(SIGINT, &act,NULL) < 0) { perror ("sigaction"); return 1; } while((!terminate)&&(cnt < 120)){ dist = read(fds,NULL,0); write(fds,NULL,0); printf("\ndistance is %lu \n",dist); diff = dist - prevdist; if(diff<0) { diff = prevdist - dist; } if(dist < 40) { move_left(fd); } else { move_right(fd); } prevdist = dist; cnt++; } write(fd,NULL,0); /* time to displat some patterns */ transfer_moves(fd); anime(fd); write(fd,NULL,0); usleep(100000); close(fd); close(fds); return 0; }
void EnnemiSnorkyBase1::onAvance() { if (plat(x, y) == 0) { etat = ETAT_TOMBE; dy = 0; lat_grav = 0; onTombe(); return; } wait_for_shoot++; if ((wait_for_shoot > shoot_delay) && ((dir == SENS_DROITE && x > offset + 40) || (dir == SENS_GAUCHE && x < offset + 600))) { wait_for_shoot = 0; shoot_delay = 50 + rand() % 200; etape = 0; ss_etape = 0; etat = ETAT_TIRE; onTire(); return; } if (dir == SENS_GAUCHE) { marche(-SNORKY_BASE_SPEED); pic = pbk_ennemis[anime(anim_snorky_base_marche_gauche, 8, 4)]; if (mur_opaque(x - SNORKY_BASE_SPEED, y) || (x - SNORKY_BASE_SPEED < xmin)) dir = SENS_DROITE; } else { marche(SNORKY_BASE_SPEED); pic = pbk_ennemis[anime(anim_snorky_base_marche_droite, 8, 4)]; if (mur_opaque(x + SNORKY_BASE_SPEED, y) || (x + SNORKY_BASE_SPEED > offset + 640)) dir = SENS_GAUCHE; } colFromPic(); }
void fall(int start_x,int start_y,int fall,cv::Mat character1,cv::Mat character2,cv::Mat back,int width,int height){ int end_x =start_x+character1.cols+fall; int y=0; cv::Mat img = character1; cv::Mat blend; char c; // walk(start_x,end_x,start_y,character1,character2,back,width,height); std::cout<<"end_x="<<end_x<<std::endl; end_x=end_x+200; while(start_y>=y){ std::cout<<"end_x="<<end_x<<std::endl; overlay(img, back, blend, cv::Point(end_x,225+y)); //合成 cv::Mat anime(blend,cv::Rect(end_x-200,0,width,height)); imshow(winname,anime); y=y+4; c=cv::waitKey(10); if(c==27){ break; } } }
void Main() { TextureAsset::Register(U"sample", U"asc_anime_sample.png"); Texture texture(U"asc_anime_sample.png"); asc::Anime anime(texture, 4, 0.1s); asc::AnimeAsset animeAsset(U"sample", { 0.1s, 0.5s, 0.1s, 0.1s }); bool stop = false; bool slow = false; while (System::Update()) { if (MouseL.down()) { stop = !stop; } if (MouseR.down()) { slow = !slow; } ClearPrint(); Print(U"[MouseL] stop : {}"_fmt(stop)); Print(U"[MouseR] slow : {}"_fmt(slow)); if (!stop) { anime.update(System::DeltaTime()); } animeAsset.update((slow ? 0.2 : 1.0) * System::DeltaTime()); anime.mirrored().draw(Arg::topLeft = Point(50, 70)); animeAsset.draw(Arg::center = Cursor::Pos()); } }
void EnnemiToad::onAvance() { // Si plus de plateformes on passe dans l'etat TOMBE // if (plat(x, y) == 0) { etat = ETAT_TOMBE; dy = 0; etape = 0; ss_etape = 0; lat_grav = 0; onSaute(); } //pour marcher if (x - speed < xmin || mur_opaque(x - speed, y)) { dir = SENS_DROITE; speed = 1; } else if (x + speed > offset + 640 || mur_opaque(x + speed, y)) { dir = SENS_GAUCHE; speed = 1; } wait_for_charge++; if (wait_for_charge >= charge_delay) { if ((plat2(tete_turc->x, tete_turc->y) == plat2(x, y)) && (((dir == SENS_DROITE) && (x < tete_turc->x)) || ((dir == SENS_GAUCHE) && (x > tete_turc->x)))) { charge_etape = 0; speed = 0; etape = 0; ss_etape = 0; dy = 0; etat = ETAT_TIRE; sbk_niveau.play(34); onCharge(); return; } else { wait_for_charge = 0; charge_delay = 50 + rand() % 250; etape = 0; ss_etape = 0; etat = ETAT_SAUTE; dy = -7; lat_grav = 0; // Sinon les sauts diffèrent par leur hauteur onSaute(); return; } } if (dir == SENS_DROITE) { marche(speed); pic = pbk_ennemis[anime(anim_toad_marche_droite, 4, 12 - 3 * speed)]; } else { marche(-speed); pic = pbk_ennemis[anime(anim_toad_marche_gauche, 4, 12 - 3 * speed)]; } if (speed < TOAD_SPEED) { etape_speed++; if (etape_speed >= 30) { etape_speed = 0; speed++; } } colFromPic(); }
void FondMonolite::update() { pic = pbk_niveau[anime(monolite_animation, 36, 3)]; //anime36 ss_etape_levitation++; ss_etape_levitation %= 4; if (ss_etape_levitation == 0) { etape_levitation++; if (etape_levitation == 44) { etape_levitation = 0; } } int xtmp; // Fait des allers/retour // /*if ( y <= 96) dy = 1; else if ( y >= 387) dy = -1;*/ // Si un joueur est sur la plateforme, on le déplace // Sprite * joueur; Sprite * ennemis; list_joueurs.start(); while (!list_joueurs.fin()) { joueur = (Sprite*) list_joueurs.info(); xtmp = joueur->x; if (xtmp >= x - 138 && xtmp < x + pic->xSize() - 118 && plat(xtmp, joueur->y) == y - 20) { if (etape_levitation <= 22) { joueur->y += monolite_levitation[etape_levitation]; } else { joueur->y -= monolite_levitation[etape_levitation - 22]; } } // joueur->y += dy; list_joueurs.suivant(); } list_ennemis.start(); while (!list_ennemis.fin()) { ennemis = (Sprite*) list_ennemis.info(); xtmp = ennemis->x; if (xtmp >= x - 138 && xtmp < x + pic->xSize() - 118 && plat(xtmp, ennemis->y) == y - 20) { if (etape_levitation <= 22) { ennemis->y += monolite_levitation[etape_levitation]; } else { ennemis->y -= monolite_levitation[etape_levitation - 22]; } } // joueur->y += dy; list_ennemis.suivant(); } // Déplace la plateforme // //y += dy; if (etape_levitation <= 22) { y += monolite_levitation[etape_levitation]; } else { y -= monolite_levitation[etape_levitation - 22]; } for (int i = x - 118; i < x + pic->xSize() - 138; i++) y_plat[4][i] = y - 20; colFromPic(); /*if ( x < offset-400) a_detruire = true;*/ }
int main() { sf::RenderWindow window(sf::VideoMode(1024, 768), "Projet Simu!"); // create window window.setVerticalSyncEnabled(true); auto wndSize = window.getSize(); sf::View view({0,0, (float)wndSize.x, (float)wndSize.y}); window.setView(view); /****** TESTING ANIMATED SPRITES ******/ sf::Clock clock; /**** single animation ****/ AnimationSteps steps; steps.push_back({0,sf::milliseconds(200)}); steps.push_back({2,sf::milliseconds(200)}); steps.push_back({4,sf::milliseconds(200)}); steps.push_back({6,sf::milliseconds(200)}); steps.push_back({8,sf::milliseconds(200)}); steps.push_back({6,sf::seconds(1)}); steps.push_back({8,sf::seconds(1)}); steps.push_back({4,sf::seconds(1)}); steps.push_back({0,sf::seconds(1)}); steps.push_back({2,sf::seconds(1)}); AnimatedSprite anime("../img/animated.png", steps); anime.move(256,128); /**** multi animation ****/ MultiAnimationSteps multisteps; AnimationSteps rightSteps, leftSteps, upSteps, downSteps; rightSteps.push_back({0,sf::milliseconds(200)}); rightSteps.push_back({1,sf::milliseconds(200)}); rightSteps.push_back({2,sf::milliseconds(200)}); rightSteps.push_back({3,sf::milliseconds(200)}); leftSteps.push_back({4,sf::milliseconds(400)}); leftSteps.push_back({5,sf::milliseconds(600)}); leftSteps.push_back({6,sf::milliseconds(400)}); leftSteps.push_back({7,sf::milliseconds(600)}); upSteps.push_back({8,sf::milliseconds(200)}); upSteps.push_back({9,sf::milliseconds(200)}); downSteps.push_back({10,sf::milliseconds(400)}); downSteps.push_back({11,sf::milliseconds(400)}); multisteps.insert({"right", rightSteps}); multisteps.insert({"left", leftSteps}); multisteps.insert({"up", upSteps}); multisteps.insert({"down", downSteps}); AnimatedSprite multianime("../img/multianimated.png", multisteps); multianime.move(256,256); /**** walking guy ****/ MultiAnimationSteps multiwalk; AnimationSteps rightWalk, leftWalk, upWalk, downWalk; downWalk.push_back({0,sf::milliseconds(200)}); downWalk.push_back({1,sf::milliseconds(200)}); downWalk.push_back({2,sf::milliseconds(200)}); downWalk.push_back({1,sf::milliseconds(200)}); leftWalk.push_back({3,sf::milliseconds(200)}); leftWalk.push_back({4,sf::milliseconds(200)}); leftWalk.push_back({5,sf::milliseconds(200)}); leftWalk.push_back({4,sf::milliseconds(200)}); rightWalk.push_back({6,sf::milliseconds(200)}); rightWalk.push_back({7,sf::milliseconds(200)}); rightWalk.push_back({8,sf::milliseconds(200)}); rightWalk.push_back({7,sf::milliseconds(200)}); upWalk.push_back({9,sf::milliseconds(200)}); upWalk.push_back({10,sf::milliseconds(200)}); upWalk.push_back({11,sf::milliseconds(200)}); upWalk.push_back({10,sf::milliseconds(200)}); multiwalk.insert({"right", rightWalk}); multiwalk.insert({"left", leftWalk}); multiwalk.insert({"up", upWalk}); multiwalk.insert({"down", downWalk}); AnimatedSprite guy("../img/walkingguy.png", multiwalk); guy.move(256,256); /**************************************/ // main loop, TODO: move elsewhere while (window.isOpen()) { /****** MANAGING EVENTS ******/ sf::Event event; while(window.pollEvent(event)) { if (event.type == sf::Event::Closed) window.close(); if (event.type == sf::Event::KeyPressed) { if (event.key.code == sf::Keyboard::Escape) window.close(); } if (event.type == sf::Event::Resized) { view.setSize(event.size.width, event.size.height); window.setView(view); } } /*****************************/ /****** MOVING THE SPRITE LIKE A PLAYER ******/ if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) { guy.move(4,0); guy.chooseAnimation("right"); multianime.chooseAnimation("right"); } if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { guy.move(-4,0); guy.chooseAnimation("left"); multianime.chooseAnimation("left"); } if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) { guy.move(0,4); guy.chooseAnimation("down"); multianime.chooseAnimation("down"); } if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) { guy.move(0,-4); guy.chooseAnimation("up"); multianime.chooseAnimation("up"); } auto pos = guy.getPosition(); if (pos[0] < view.getSize().x/2) pos[0] = view.getSize().x/2; if (pos[0] > 64*32 - view.getSize().x/2) pos[0] = 64*32 - view.getSize().x/2; if (pos[1] < view.getSize().y/2) pos[1] = view.getSize().y/2; if (pos[1] > 64*24 - view.getSize().y/2) pos[1] = 64*24 - view.getSize().y/2; view.setCenter(pos[0], pos[1]); window.setView(view); /*********************************************/ /****** UPDATE TIMES THINGS ******/ auto elapsedTime = clock.restart(); anime.rotate(elapsedTime.asMilliseconds()*M_PI/180/4); anime.updateAnimation(elapsedTime); multianime.updateAnimation(elapsedTime); guy.updateAnimation(elapsedTime); /**********************************/ /****** DISPLAY EVERYTHING ******/ window.clear(); window.draw(anime); window.draw(multianime); window.draw(guy); window.display(); /********************************/ } return 0; }