int ok(map &map, player &pl) { if (pl._v.x < 0) { if (!colision(-1, 0, 0, 32, map, pl)) return (1); } else if (pl._v.x > 0) { if (!colision(32, 0, 0, 32, map, pl)) return (1); } else return (1); return (0); }
void NPC2::logic(Uint8* teclas_presionadas) { SDL_Rect temp = this->rectangulo; rectangulo.x--; int cont=0; if(rectangulo.x<50){ rectangulo.x=0; cont++; } rectangulo.y--; if(cont==350){ if(rectangulo.y<50) rectangulo.y=0; } rectangulo.x--; if(cont==350){ if(rectangulo.x<50) rectangulo.x=0; } //rectangulo.y--; // // if(rectangulo.y<40) // rectangulo.y=0; // // else{ // rectangulo.x--; // } for(list<Personaje*>::iterator i = personajes->begin(); i!=personajes->end(); i++) { if(this==(*i)) continue; if(colision(this->rectangulo, (*i)->rectangulo)) //rectangulo=temp; personajes->erase(i); } }
void Proyectil::logica() { Mix_OpenAudio(22052, MIX_DEFAULT_FORMAT, 1, 4096); int velocity = 3; if(state=="right") { rect.x+=velocity; } if(state=="left") { rect.x-=velocity; } if(state=="up") { rect.y-=velocity; } if(state=="down") { rect.y+=velocity; } for(list<Entidad*>::iterator e=entidades->begin(); e!=entidades->end(); e++) { if((*e)->tipo=="Enemigo") { Enemigo* enemigo = (Enemigo*)(*e); if(colision(rect,enemigo->rect)) { *counter += 1; enemigo->delete_flag = true; this->delete_flag = true; Mix_PlayMusic(aw,1); Mix_CloseAudio(); } } } if(rect.x<0 || rect.x>500 || rect.y<0 || rect.y>250) { this->delete_flag=true; cout<<"Borrando bala"<<endl; } }
void NPC3::logic(Uint8* currentKeyStates) { SDL_Rect temp = this->rectangulo; if(derecha==true) rectangulo.x++; if(rectangulo.x>450) derecha=false; if(derecha==false) rectangulo.x--; if(rectangulo.x<0) derecha=true; for(list<Personaje*>::iterator i = personajes->begin(); i!=personajes->end(); i++) { if(this==(*i)) continue; if(colision(this->rectangulo, (*i)->rectangulo)) rectangulo=temp; } }
void Proyectil::logica() { int velocity = 1; if(state=="right") { rect.x+=velocity; } if(state=="left") { rect.x-=velocity; } if(state=="up") { rect.y-=velocity; } if(state=="down") { rect.y+=velocity; } for(list<Entidad*>::iterator e=entidades->begin(); e!=entidades->end(); e++) { if((*e)->tipo=="Enemigo") { Enemigo* enemigo = (Enemigo*)(*e); if(colision(rect,enemigo->rect)) { enemigo->delete_flag = true; this->delete_flag = true; cout<<"Colision con proyectil"<<endl; } } } }
void PersonajeJugador::logic(Uint8* teclas_presionadas) { Mix_Chunk *Sonido = NULL; Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, 2048 ); Sonido = Mix_LoadWAV("sound.wav"); SDL_Rect temp = this->rectangulo; if( teclas_presionadas[ SDL_SCANCODE_UP ] ) { rectangulo.y-=1; if(orientacion!="up") textura_actual=texturas_up.begin(); orientacion="up"; } if( teclas_presionadas[ SDL_SCANCODE_DOWN ] ) { rectangulo.y+=1; if(orientacion!="down") textura_actual=texturas_down.begin(); orientacion="down"; } if( teclas_presionadas[ SDL_SCANCODE_RIGHT ] ) { rectangulo.x+=1; if(orientacion!="right") textura_actual=texturas_right.begin(); orientacion="right"; } if( teclas_presionadas[ SDL_SCANCODE_LEFT ] ) { rectangulo.x-=1; if(orientacion!="left") textura_actual=texturas_left.begin(); orientacion="left"; } // if( teclas_presionadas[ SDL_SCANCODE_X ] ) // { // SDL_Renderer* renderer2; // SDL_Rect rect_character; // SDL_Texture *poder = IMG_LoadTexture(renderer2, "assets/llamas/01.png"); // //SDL_RenderCopy(renderer2,poder, NULL, &rect_character); // // SDL_RenderCopy(renderer2, poder, NULL, &rect_character); // SDL_RenderPresent(renderer2); //// rectangulo.x-=1; //// if(orientacion!="left") //// textura_actual=texturas_left.begin(); //// orientacion="left"; // } // for(list<Personaje*>::iterator i = personajes->begin(); // i!=personajes->end(); // i++) // { // if(this==(*i)) // continue; // if(colision(this->rectangulo, (*i)->rectangulo)) // { // rectangulo=temp; // c+=1; // break; // } // } // cout<<c; for(list<Personaje*>::iterator i = personajes->begin(); i!=personajes->end(); i++) { if(this==(*i)) continue; if(colision(this->rectangulo, (*i)->rectangulo)&&teclas_presionadas[ SDL_SCANCODE_X ]) { //rectangulo=temp; personajes->erase(i); Mix_PlayChannel( -1, Sonido, 0 ); break; } } }
void PersonajeJugador::logic(Uint8* currentKeyStates) { SDL_Rect temp = this->rectangulo; if(orientacion=="up" && estado=="ataque") { textura_actual=texturas_up.begin(); estado = "pasivo"; } if(orientacion=="down" && estado=="ataque") { textura_actual=texturas_down.begin(); estado = "pasivo"; } if(orientacion=="right" && estado=="ataque") { textura_actual=texturas_right.begin(); estado = "pasivo"; } if(orientacion=="left" && estado=="ataque") { textura_actual=texturas_left.begin(); estado = "pasivo"; } if(rayo_activado) { if(rayo_orientacion=="up") { rayo_rect.y -= 4; } if(rayo_orientacion=="down") { rayo_rect.y += 4; } if(rayo_orientacion=="left") { rayo_rect.x -= 4; } if(rayo_orientacion=="right") { rayo_rect.x += 4; } } if( currentKeyStates[ SDL_SCANCODE_SPACE] ) { estado = "ataque"; if(orientacion=="up") { textura_actual=ataque_arriba.begin(); } if(orientacion=="down") { textura_actual=ataque_abajo.begin(); } if(orientacion=="left") { textura_actual=ataque_izquierda.begin(); } if(orientacion=="right") { textura_actual=ataque_derecha.begin(); } } if( currentKeyStates[ SDL_SCANCODE_W] && rayo2_activado) { rayo2_rect.y -= 2; } if( currentKeyStates[ SDL_SCANCODE_S] && rayo2_activado) { rayo2_rect.y += 2; } if( currentKeyStates[ SDL_SCANCODE_A] && rayo2_activado) { rayo2_rect.x -= 2; } if( currentKeyStates[ SDL_SCANCODE_D] && rayo2_activado) { rayo2_rect.x += 2; } if(rayo_activado || rayo2_activado) rayo_frame_actual++; if(rayo_frame_actual>=rayo_cooldown) { rayo_activado = false; rayo2_activado = false; } if( currentKeyStates[ SDL_SCANCODE_Q] && !rayo_activado && !rayo2_activado) { rayo_orientacion = orientacion; rayo_activado=true; rayo_frame_actual = 0; rayo_rect.x = rectangulo.x; rayo_rect.y = rectangulo.y; estado = "ataque"; if(orientacion=="up") { rayo_rect.y-=rectangulo.h; } if(orientacion=="down") { rayo_rect.y+=rectangulo.h; } if(orientacion=="left") { rayo_rect.x-=rectangulo.w; } if(orientacion=="right") { rayo_rect.x+=rectangulo.w; } }else if( currentKeyStates[ SDL_SCANCODE_E] && !rayo2_activado && !rayo_activado) { rayo2_orientacion = orientacion; rayo2_activado=true; rayo_frame_actual = 0; rayo2_rect.x = rectangulo.x; rayo2_rect.y = rectangulo.y; if(orientacion=="up") { rayo2_rect.y-=rectangulo.h; } if(orientacion=="down") { rayo2_rect.y+=rectangulo.h; } if(orientacion=="left") { rayo2_rect.x-=rectangulo.w; } if(orientacion=="right") { rayo2_rect.x+=rectangulo.w; } }else { if( currentKeyStates[ SDL_SCANCODE_UP ] ) { rectangulo.y-=1; if(orientacion!="up") textura_actual=texturas_up.begin(); orientacion="up"; } if( currentKeyStates[ SDL_SCANCODE_DOWN ] ) { rectangulo.y+=1; if(orientacion!="down") textura_actual=texturas_down.begin(); orientacion="down"; } if( currentKeyStates[ SDL_SCANCODE_RIGHT ] ) { rectangulo.x+=1; if(orientacion!="right") textura_actual=texturas_right.begin(); orientacion="right"; } if( currentKeyStates[ SDL_SCANCODE_LEFT ] ) { rectangulo.x-=1; if(orientacion!="left") textura_actual=texturas_left.begin(); orientacion="left"; } } for(list<Personaje*>::iterator i = personajes->begin();i!=personajes->end();i++) { if(this==(*i)) continue; if(colision(this->rectangulo, (*i)->rectangulo)) { vida--; } if(vida==0) { vivo = false; } if((colision(this->rayo_rect, (*i)->rectangulo) && rayo_activado) || (colision(this->rectangulo, (*i)->rectangulo) && estado=="ataque") || (colision(this->rayo2_rect, (*i)->rectangulo) && rayo2_activado)) { //rectangulo=temp; personajes->erase(i); rayo_activado=false; rayo2_activado=false; break; } } }
void Enemigo::logica() { Mix_OpenAudio(22052,MIX_DEFAULT_FORMAT,2,4096); if(jugador->x>x) { state="right"; } if(jugador->x<x) { state="left"; } if(jugador->y<y) { state="up"; } if(jugador->y>y) { state="down"; } if(state=="right") { x+=velocity; } if(state=="left") { x-=velocity; } if(state=="up") { y-=velocity; } if(state=="down") { y+=velocity; } if(frames%animation_velocity==0) { current_texture++; if(current_texture>=textures[state].size()) current_texture=0; } if(colision(rect,jugador->rect)) { //jugador->delete_flag = true; if(x<jugador->x) jugador->push_orientation="right"; if(x>jugador->x) jugador->push_orientation="left"; if(y<jugador->y) jugador->push_orientation="down"; if(y>jugador->y) jugador->push_orientation="up"; Mix_Music *music2 = Mix_LoadMUS("Hit.wav"); Mix_PlayMusic(music2,1); Mix_CloseAudio(); jugador->push_amount=10; jugador->vidas--; cout<<"Colision con jugador"<<endl; } frames++; }
int main( int argc, char* args[] ) { //Quit flag bool quit = false; //Initialize if( init() == false ) { return 1; } //Load the files if( load_files() == false ) { return 1; } //Render the text up = TTF_RenderText_Solid( font, "Up", textColor ); down = TTF_RenderText_Solid( font, "Down", textColor ); left = TTF_RenderText_Solid( font, "Left", textColor ); right = TTF_RenderText_Solid( font, "Right", textColor ); SDL_Surface *personaje[4]; personaje[0]= load_image("Endless Run/run01.png"); personaje[1]= load_image("Endless Run/run02.png"); personaje[2]= load_image("Endless Run/run03.png"); personaje[3]= load_image("Endless Run/run04.png"); SDL_Surface *ghost[3]; ghost[0]= load_image("Endless Run/volador01.png"); ghost[1]= load_image("Endless Run/volador02.png"); ghost[2]= load_image("Endless Run/volador03.png"); Enemigo bomba(800,250,load_image("Endless Run/bomba01.png")); int bombax = 700; int bombay = 250; SDL_Surface *jump = load_image("Endless Run/jump.png"); SDL_Surface *montanas = load_image("Endless Run/mountains.png"); int i = 0; int frame = 0; int frames = 0; int pospersonajey = 250; int pospersonajex = 0; int posghosty = 280; int posghostx = 400; int flote = 1000; int velocidady = 0; int accy = 2; int altura = 30; int mountainpos = 0; //While the user hasn't quit while( quit == false ) { //While there's events to handle while( SDL_PollEvent( &event ) ) { //If the user has Xed out the window if( event.type == SDL_QUIT ) { //Quit the program quit = true; } } //Apply the background apply_surface( 0, 0, background, screen ); //Get the keystates Uint8 *keystates = SDL_GetKeyState( NULL); pospersonajey+= velocidady; velocidady+=accy; if(pospersonajey>= 250) { pospersonajey = 250; } if(keystates[SDLK_SPACE]) { Mix_PlayChannel( -1, sound, 0 ); Mix_VolumeChunk(sound, 40); if(pospersonajey==250) velocidady= -altura; } apply_surface(mountainpos , 0, montanas, screen); bomba.avanzar(); bomba.imprimir(screen); apply_surface(flote, posghosty, ghost[frames], screen); if(pospersonajey == 250) apply_surface(pospersonajex,pospersonajey,personaje[frame], screen); else apply_surface(pospersonajex,pospersonajey,jump, screen); mountainpos-=5; flote-=8; posghosty-=0.5; bombax -= 8; if(bombax < -bomba->w) { bombax = 700; } int px = pospersonajex; int py = pospersonajey; int pw = personaje[frame]->w; int ph = personaje[frame]->h; int bx = bombax; int by = bombay; int bw = bomba->w; int bh = bomba->h; if(colision(px+pw/6, py+ph/6, pw/4,ph/4, bx+bw/6, by+bh/6, bw/4, bh/4)) { exit(0); } if(mountainpos <-1726) { mountainpos =0; flote = 1000; posghosty = 280; } i++; if(i%20 == 0) { frames++; frame++; if(frame>3) frame = 0; if(frames>2) frames = 0; } //Update the screen if( SDL_Flip( screen ) == -1 ) { return 1; } } //Clean up clean_up(); return 0; }
int main(int argc, char **argv) { SDL_Init(SDL_INIT_VIDEO); SDL_Window *ventana = SDL_CreateWindow("Hi", 200, 200, 864, 510, SDL_WINDOW_SHOWN); SDL_Renderer *render = SDL_CreateRenderer(ventana, -1, SDL_RENDERER_ACCELERATED); //se carga la imagen que contiene todos los graficos SDL_Surface * bmp = SDL_LoadBMP("atlas.bmp"); //se define el blanco como color transparente SDL_SetColorKey(bmp, 1, SDL_MapRGB(bmp->format, 255, 255, 255)); SDL_Texture *textura = SDL_CreateTextureFromSurface(render, bmp); SDL_FreeSurface(bmp); SDL_Event event; if (textura == NULL) { std::cout << "FAILED TO FIND THE IMAGE" << std::endl; } SDL_RenderClear(render); renderFondo(render, textura); //Rectangulo con las coordenadas donde se encuentra el pagaro en la textura SDL_Rect rectangulo_origen; rectangulo_origen.x = 174;//174 azul 6 amarillo rectangulo_origen.y = 982; rectangulo_origen.w = 34; rectangulo_origen.h = 24; //Rectangulo con las coordenadas donde se pintara el pagaro en el render SDL_Rect rectangulo_destino; rectangulo_destino.x = 100; rectangulo_destino.y = 300; rectangulo_destino.w = 34; rectangulo_destino.h = 24; SDL_RenderCopy(render, textura, &rectangulo_origen, &rectangulo_destino); SDL_Rect rectangulo_origen_tubo; rectangulo_origen_tubo.x = 112; rectangulo_origen_tubo.y = 646; rectangulo_origen_tubo.w = 52; rectangulo_origen_tubo.h = 320; SDL_Rect rectangulo_destino_tubo; rectangulo_destino_tubo.x = 800; rectangulo_destino_tubo.y = -150; rectangulo_destino_tubo.w = 52; rectangulo_destino_tubo.h = 320; SDL_RenderCopy(render, textura, &rectangulo_origen_tubo, &rectangulo_destino_tubo); SDL_Rect rectangulo_origen_tubo1; rectangulo_origen_tubo1.x = 168; rectangulo_origen_tubo1.y = 646; rectangulo_origen_tubo1.w = 52; rectangulo_origen_tubo1.h = 320; SDL_Rect rectangulo_destino_tubo1; rectangulo_destino_tubo1.x = 800; rectangulo_destino_tubo1.y = 320; rectangulo_destino_tubo1.w = 52; rectangulo_destino_tubo1.h = 320; SDL_RenderCopy(render, textura, &rectangulo_origen_tubo1, &rectangulo_destino_tubo1); SDL_RenderPresent(render); int done = 1; double angulo = 330; const int FPS = 24; //Cuantos frames por segundo queremos, 60 es el que utilizan los televisores const int DELAY_TIME = 1000.0f / FPS; //1000 ms entre los fps da el numero de milisegundos entre cada frame Uint32 frameStart, frameTime; while (done) { frameStart = SDL_GetTicks(); //validacion del piso if (rectangulo_destino.y < 400) { rectangulo_destino.y += 10; } //validacion del angulo maximo if (angulo < 450) { angulo += 15; } if(rectangulo_destino_tubo.x > 0){ rectangulo_destino_tubo.x -= 2; rectangulo_destino_tubo1.x -= 2; }else{ rectangulo_destino_tubo.x = 864; rectangulo_destino_tubo1.x = 864; } //mientras exista un evento en el pila de eventos while (SDL_PollEvent(&event)) { //salto if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_SPACE) { rectangulo_destino.y -= 100; angulo = 300; } else if (event.type == SDL_QUIT) { done = 0; } } //se pinta actualiza el render SDL_RenderClear(render); renderFondo(render, textura); //funciona como copy pero rota la imagen con el angulo SDL_RenderCopy(render, textura, &rectangulo_origen_tubo, &rectangulo_destino_tubo); SDL_RenderCopy(render, textura, &rectangulo_origen_tubo1, &rectangulo_destino_tubo1); SDL_RenderCopyEx(render, textura, &rectangulo_origen, &rectangulo_destino, angulo, NULL, SDL_FLIP_NONE); SDL_RenderPresent(render); if(colision(&rectangulo_destino,&rectangulo_destino_tubo) ||colision(&rectangulo_destino,&rectangulo_destino_tubo1)) { done = 0; } frameTime = SDL_GetTicks() - frameStart; //Tiempo que ha tardado en presentarse el frame if (frameTime < DELAY_TIME)//Si es menor al tiempo que deber à a ser { SDL_Delay((int)(DELAY_TIME - frameTime));//Espera un tiempo } } angulo = 450; while(rectangulo_destino.y < 400) { frameStart = SDL_GetTicks(); rectangulo_destino.y += 10; //se pinta actualiza el render SDL_RenderClear(render); renderFondo(render, textura); //funciona como copy pero rota la imagen con el angulo SDL_RenderCopy(render, textura, &rectangulo_origen_tubo, &rectangulo_destino_tubo); SDL_RenderCopy(render, textura, &rectangulo_origen_tubo1, &rectangulo_destino_tubo1); SDL_RenderCopyEx(render, textura, &rectangulo_origen, &rectangulo_destino, angulo, NULL, SDL_FLIP_NONE); SDL_RenderPresent(render); frameTime = SDL_GetTicks() - frameStart; //Tiempo que ha tardado en presentarse el frame if (frameTime < DELAY_TIME)//Si es menor al tiempo que deber à a ser { SDL_Delay((int)(DELAY_TIME - frameTime));//Espera un tiempo } } SDL_Delay(5000); /* Free all objects */ SDL_DestroyTexture(textura); SDL_DestroyRenderer(render); SDL_DestroyWindow(ventana); /* Quit program */ SDL_Quit(); return 0; }
void Game::processLogics(float secs) { //Lemos o estado das teclas Uint8* keys = SDL_GetKeyState(NULL); if (gameState == 1){ view = objCamera.mView.z + 1.5; objCamera.Move_Camera(cameraSpeed); cameraSpeed+=speed; rebuildMap(); // Move light //posicao[0] = objCamera.mPos.x; //posicao[1] = objCamera.mPos.y; posicao[2] = objCamera.mPos.z ; glLightfv(GL_LIGHT0, GL_POSITION, posicao); if(GetKeyState(VK_LEFT) & 0x80) { rotate -= 3.0f; if (rotate < 0) rotate = 360.0f; } if(GetKeyState(VK_RIGHT) & 0x80) { rotate += 3.0f; if (rotate > 360) rotate = 0; } // Check collision colision(); // Send info to client sendNetwork(); } if (gameState == -1){ printf("Restart\n"); } if (gameState == 0){ printf("GameOver"); printf("desconectando\n"); tcp->disconnect(); delete(tcp); gameState = 3; } /* printf("POS [%.2f %.2f %.2f] VIEW [%.2f %.2f %.2f] UP [%.2f %.2f %.2f] \n", objCamera.mPos.x, objCamera.mPos.y, objCamera.mPos.z, objCamera.mView.x, objCamera.mView.y, objCamera.mView.z, objCamera.mUp.x, objCamera.mUp.y, objCamera.mUp.z); */ }