int main() { WORKING_DIR=SDL_GetBasePath(); WORKING_DIR=WORKING_DIR.substr(0,WORKING_DIR.find_last_of("/\\")); WORKING_DIR=WORKING_DIR.substr(0,WORKING_DIR.find_last_of("/\\")); WORKING_DIR=WORKING_DIR.substr(0,WORKING_DIR.find_last_of("/\\")); #ifdef __gnu_linux__ WORKING_DIR+="/"; #elif __WIN32 WORKING_DIR+="\\"; #endif std::cout<< WORKING_DIR<<std::endl; SDL_Window *mainwindow; /* Our window handle */ SDL_GLContext maincontext; /* Our opengl context handle */ Mix_Chunk *pong = NULL; Mix_Chunk *pong2 = NULL; Mix_Chunk *pong3 = NULL; if( SDL_Init( SDL_INIT_VIDEO| SDL_INIT_AUDIO ) < 0 ) { sdldie("SDL could not initialize! SDL Error: %s\n"); } else { //Use OpenGL 3.3 core SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, 3 ); SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, 3 ); SDL_GL_SetAttribute( SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE ); //Create window mainwindow = SDL_CreateWindow( "pong", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, screenWidth, screenHeight, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN |SDL_WINDOW_RESIZABLE ); if( mainwindow == NULL ) { sdldie("Unable to create window"); } else { //Create context maincontext = SDL_GL_CreateContext( mainwindow ); if( maincontext == NULL ){ sdldie("OpenGL context could not be created! SDL Error: %s\n"); } else { //Initialize GLEW glewExperimental = GL_TRUE; GLenum glewError = glewInit(); if( glewError != GLEW_OK ) { std::cout<<"Error initializing GLEW! %s\n"<<glewGetErrorString( glewError ); } //Use Vsync if( SDL_GL_SetSwapInterval( 1 ) < 0 ) { std::cout<<"Warning: Unable to set VSync! SDL Error: %s\n"<<SDL_GetError(); } SDL_DisplayMode current; int should_be_zero = SDL_GetCurrentDisplayMode(0, ¤t); //@HACK:should check for multiple monitors if(should_be_zero != 0) sdldie("Could not get display mode for video display"); screenWidth=(3.0f/4.0f)*current.w; screenHeight=(3.0f/4.0f)*current.h; (*(int*)(&originalScreenHeight))=screenHeight; (*(int*)(&originalScreenWidth))=screenWidth; } } } //Initialize SDL_mixer if( Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, 2048 ) < 0 ) { std::cout<<"SDL_mixer could not initialize! SDL_mixer Error: "<<Mix_GetError(); return 1; } //Load sound effects pong = Mix_LoadWAV(WORKING_DIR_FILE("assets/sounds/pong.wav")); if( pong == NULL ) { std::cout<< "Failed to load scratch sound effect! SDL_mixer Error: "<<Mix_GetError(); return 1; } pong2 = Mix_LoadWAV(WORKING_DIR_FILE("assets/sounds/pong2.wav")); if( pong2 == NULL ) { std::cout<< "Failed to load scratch sound effect! SDL_mixer Error: "<<Mix_GetError(); return 1; } pong3 = Mix_LoadWAV(WORKING_DIR_FILE("assets/sounds/pong3.wav")); if( pong3 == NULL ) { std::cout<< "Failed to load scratch sound effect! SDL_mixer Error: "<<Mix_GetError(); return 1; } glEnable(GL_DEPTH_TEST); glViewport(0, 0, screenWidth, screenHeight); /* Clear our buffer with a red background */ glClearColor ( 1.0, 0.0, 0.0, 1.0 ); glClear ( GL_COLOR_BUFFER_BIT ); /* Swap our back buffer to the front */ SDL_GL_SwapWindow(mainwindow); /* Wait 2 seconds */ SDL_Delay(100); /* Same as above, but green */ glClearColor ( 0.0, 1.0, 0.0, 1.0 ); glClear ( GL_COLOR_BUFFER_BIT ); SDL_GL_SwapWindow(mainwindow); SDL_Delay(100); /* Same as above, but blue */ glClearColor ( 0.0, 0.0, 1.0, 1.0 ); glClear ( GL_COLOR_BUFFER_BIT ); SDL_GL_SwapWindow(mainwindow); SDL_Delay(100); GLfloat vertices[] = { 0.5f, 0.5f, 0.0f, // Top Right 0.5f, -0.5f, 0.0f, // Bottom Right -0.5f, -0.5f, 0.0f, // Bottom Left -0.5f, 0.5f, 0.0f, // Top Left }; GLuint indices[] = { // Note that we start from 0! 0, 1, 3, // First Triangle 1, 2, 3 // Second Triangle }; std::vector<Vertex> vertices2; std::vector<GLuint> indices2; { for(unsigned int i=0;i<(sizeof(vertices)/sizeof(vertices[0]));i+=3){ Vertex aux; aux.position={vertices[i+0],vertices[i+1],vertices[i+2]}; vertices2.push_back(aux); } for(unsigned int i=0;i<(sizeof(indices)/sizeof(indices[0]));i++){ indices2.push_back(indices[i]); } } Sprite sprite(vertices2,indices2); GLfloat quadVertices[] = { // Vertex attributes for a quad that fills the entire screen in Normalized Device Coordinates. // Positions // TexCoords -1.0f, 1.0f, 0.0f, 1.0f, -1.0f, -1.0f, 0.0f, 0.0f, 1.0f, -1.0f, 1.0f, 0.0f, -1.0f, 1.0f, 0.0f, 1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f }; // Setup cube VAO GLuint quadVAO, quadVBO; glGenVertexArrays(1, &quadVAO); glGenBuffers(1, &quadVBO); glBindVertexArray(quadVAO); glBindBuffer(GL_ARRAY_BUFFER, quadVBO); glBufferData(GL_ARRAY_BUFFER, sizeof(quadVertices), quadVertices, GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), (GLvoid*)0); glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), (GLvoid*)(2 * sizeof(GLfloat))); glBindVertexArray(0); unsigned int points_p1=0,points_p2=0; bool first_point_p1=true,first_point_p2=true; GameObject player1(&sprite,{0.0f,2.0f}); GameObject player2(&sprite,{0.0f,2.0f}); GameObject ball(&sprite,{0.0f,0.0f}); Entity roof(&sprite); Entity floor(&sprite); GameObject leftwall(&sprite,{0.0f,0.0f}); GameObject rightwall(&sprite,{0.0f,0.0f}); { player1.entity.setPos({-0.95f,0.0f}); player2.entity.setPos({0.95f,0.0f}); ball.entity.setPos({0.0f,8.0f}); roof.setPos({-1.0f,1.10f}); floor.setPos({-1.0f,-1.10f}); roof.scale({4.0f,1.0f}); floor.scale({4.0f,1.0f}); leftwall.entity.setPos({-1.0f,0.0f}); rightwall.entity.setPos({1.0f,0.0f}); leftwall.entity.scale({0.025f,2.0f}); rightwall.entity.scale({0.025f,2.0f}); player1.entity.scale({0.025f, 0.49f}); player2.entity.scale({0.025f, 0.49f}); ball.entity.scale({0.0625f,0.0625f}); ball.entity.order(SCALE,ROTATE,TRANSLATE); } std::vector<CollisionChecker> collisions; { collisions.push_back(CollisionChecker(&player1.entity,&roof)); collisions.push_back(CollisionChecker(&player1.entity,&floor)); collisions.push_back(CollisionChecker(&player2.entity,&roof)); collisions.push_back(CollisionChecker(&player2.entity,&floor)); } CollisionChecker ball_floor(&ball.entity,&floor); CollisionChecker ball_roof(&ball.entity,&roof); CollisionChecker ball_p1(&ball.entity,&player1.entity); CollisionChecker ball_p2(&ball.entity,&player2.entity); CollisionChecker ball_leftwall(&ball.entity,&leftwall.entity); CollisionChecker ball_rightwall(&ball.entity,&rightwall.entity); SDL_StartTextInput(); bool quit = false; bool started=false; glm::vec2 p1_speed_gain(0.0f,0.0f); glm::vec2 p2_speed_gain(0.0f,0.0f); unsigned int i=0; Uint32 lastFrame=0; Uint32 deltaTime=0; float framerate=0.0f; float dt; framebuffer fb(originalScreenWidth,originalScreenHeight); Shader shader(WORKING_DIR_FILE("assets/shaders/shader.vert"),WORKING_DIR_FILE("assets/shaders/shader.frag")); Shader fb_shader(WORKING_DIR_FILE("assets/shaders/framebuffer_shader.vert"),WORKING_DIR_FILE("assets/shaders/framebuffer_shader.frag")); while(!quit) { SDL_PumpEvents(); Uint32 currentFrame = SDL_GetTicks();//miliseconds deltaTime = currentFrame - lastFrame; lastFrame = currentFrame; dt = deltaTime/1000.0f; framerate += 1000.0f/deltaTime; p1_speed_gain={0.0f,0.0f}; p2_speed_gain={0.0f,0.0f}; { const Uint8 *keystates = SDL_GetKeyboardState( NULL ); quit = keystates[SDL_GetScancodeFromKey(SDLK_q)] || SDL_QuitRequested(); started = started || keystates[SDL_GetScancodeFromKey(SDLK_SPACE)]; if(started){ if(keystates[SDL_GetScancodeFromKey(SDLK_w )]) { player1.move( dt ); p1_speed_gain={ 0.0f , 1.0f }; } if(keystates[SDL_GetScancodeFromKey(SDLK_s )]) { player1.move(-dt ); p1_speed_gain={ 0.0f ,-1.0f }; } if(keystates[SDL_GetScancodeFromKey(SDLK_UP )]) { player2.move( dt ); p2_speed_gain={ 0.0f , 1.0f }; } if(keystates[SDL_GetScancodeFromKey(SDLK_DOWN)]) { player2.move(-dt ); p2_speed_gain={ 0.0f ,-1.0f }; } if(keystates[SDL_GetScancodeFromKey(SDLK_j)]) { player1.entity.rotate(15.0f ); } do_ball_movement(ball,dt); } } { unsigned int size=collisions.size(); for(unsigned int i=0;i<size;i++) { if(collisions[i].checkCollision()) handleCollision(collisions[i]); } } { glm::vec3 pos=ball.entity.position; glm::vec3 oldpos=ball.entity.oldPosition; if(ball_floor.checkCollision()){ handleCollision(ball_floor); ball.speed=glm::normalize(glm::reflect(glm::vec2(pos.x-oldpos.x,pos.y-oldpos.y),glm::vec2(0.0f,1.0f)))*glm::length(ball.speed); Mix_PlayChannel( -1, pong2, 0 ); } if(ball_roof.checkCollision()){ handleCollision(ball_roof); ball.speed=glm::normalize(glm::reflect(glm::vec2(pos.x-oldpos.x,pos.y-oldpos.y),glm::vec2(0.0f,-1.0f)))*glm::length(ball.speed); Mix_PlayChannel( -1, pong2, 0 ); } if(ball_p1.checkCollision()){ handleCollision(ball_p1); ball.speed=glm::normalize(glm::reflect(glm::vec2(pos.x-oldpos.x,pos.y-oldpos.y),glm::vec2(1.0f,0.0f)) + p1_speed_gain) * (glm::length(ball.speed)+glm::length(p1_speed_gain)); Mix_PlayChannel( -1, pong, 0 ); } if(ball_p2.checkCollision()){ handleCollision(ball_p2); ball.speed=glm::normalize(glm::reflect(glm::vec2(pos.x-oldpos.x,pos.y-oldpos.y),glm::vec2(-1.0f,0.0f)) + p2_speed_gain) * (glm::length(ball.speed)+glm::length(p2_speed_gain)); Mix_PlayChannel( -1, pong, 0 ); } if(ball_leftwall.checkCollision()){ points_p2++; ball.entity.setPos({0.0f,8.0f});//scale adjusted due the order it uses... ball.speed={0.0f,0.0f}; Mix_PlayChannel( -1, pong3, 0 ); } if(ball_rightwall.checkCollision()){ points_p1++; ball.entity.setPos({0.0f,8.0f}); ball.speed={0.0f,0.0f}; Mix_PlayChannel( -1, pong3, 0 ); } if(((ball.speed.y/ball.speed.x)>3.0f) || ((ball.speed.y/ball.speed.x)<-3.0f)){ ball.speed.y/=2.0f; ball.speed.x*=4.0f; } } if(i==100){ i=0; framerate/=100.0f; std::cout<<framerate<<std::endl; std::cout<<points_p1<<'-'<<points_p2<<std::endl; framerate=0.0f; } i+=1; shader.Use(); fb.bind(); glViewport(0,0,originalScreenWidth,originalScreenHeight); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glEnable(GL_DEPTH_TEST); glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); glm::mat4 projection;//= glm::ortho(-1.0f,1.0f,-1.0f,1.0f,0.0f,1.0f); GLint projection_uniform=glGetUniformLocation(shader.Program, "projection"); glUniformMatrix4fv(projection_uniform, 1, GL_FALSE, glm::value_ptr(projection)); glm::vec2 position; position=glm::vec2(-3.0f*1.06255f*0.125f,0.0f); if(points_p1>=5 || points_p2>=5){ drawdigit(-1,&shader,position,{0.125f,0.125f},DIGIT_TOP|DIGIT_MIDDLE|DIGIT_TOPLEFT|DIGIT_TOPRIGHT|DIGIT_BOTTOMLEFT); position+=glm::vec2(1.0625f*0.125f,0.0f); drawdigit((points_p1>=5)?1:2,&shader,position,{0.125f,0.125f}); position+=glm::vec2(1.0625f*0.125f,0.0f); position+=glm::vec2(1.0625f*0.125f,0.0f); drawdigit(-1,&shader,position,{0.125f,0.125f},DIGIT_TOPLEFT |DIGIT_BOTTOMLEFT |DIGIT_BOTTOMLEFT_MIDDLE| DIGIT_TOPRIGHT|DIGIT_BOTTOMRIGHT|DIGIT_BOTTOMRIGHT_MIDDLE); position+=glm::vec2(1.0625f*0.125f,0.0f); drawdigit(-1,&shader,position,{0.125f,0.125f},DIGIT_TOPMIDDLE|DIGIT_BOTTOMMIDDLE); position+=glm::vec2(1.0625f*0.125f,0.0f); drawdigit(-1,&shader,position,{0.125f,0.125f},DIGIT_BOTTOMLEFT |DIGIT_TOPLEFT|DIGIT_TOPLEFT_BOTTOMRIGHT| DIGIT_BOTTOMRIGHT|DIGIT_TOPRIGHT); position+=glm::vec2(1.0625f*0.125f,0.0f); drawdigit(5,&shader,position,{0.125f,0.125f}); started=false; } player1.entity.draw(&shader); player2.entity.draw(&shader); if(started) ball.entity.draw(&shader); roof.draw(&shader); floor.draw(&shader); leftwall.entity.draw(&shader); rightwall.entity.draw(&shader); unsigned int aux=points_p2; position=glm::vec2(0.25f,0.5f); //NOTE: when one of the points hits 20 I should put a you win screen first_point_p2=points_p2? false:true; while((aux/10) || (aux%10) || first_point_p2){ drawdigit(aux%10,&shader,position,{0.125f,0.125f}); position.x-=1.5f*0.125f; aux=aux/10; first_point_p2=false;//endless loop if I dont } aux=points_p1; position={-0.25f,0.5f}; first_point_p1=points_p1? false:true; while((aux/10) || (aux%10) || first_point_p1){ drawdigit(aux%10,&shader,position,{0.125f,0.125f}); position.x-=1.5f*0.125f; aux=aux/10; first_point_p1=false; } fb.unbind(); SDL_GetWindowSize(mainwindow,&screenWidth,&screenHeight); glViewport(0,0,screenWidth,screenHeight); glClearColor(0.0f, 0.0f, 0.1f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); glDisable(GL_DEPTH_TEST); fb_shader.Use(); glm::mat4 screenscaler; float aspectRatio=(float)screenWidth/(float)screenHeight; float inverseAspectRatio=(float)screenHeight/(float)screenWidth; if(aspectRatio>1.0f) screenscaler = glm::perspective(radians(59.2f),aspectRatio,0.1f,1.0f); else screenscaler = glm::perspective(radians(59.2f),inverseAspectRatio,0.1f,1.0f); GLint model_uniform=glGetUniformLocation(fb_shader.Program, "model"); glUniformMatrix4fv(model_uniform, 1, GL_FALSE, glm::value_ptr(screenscaler)); glBindVertexArray(quadVAO);//should scale the scale to the % of resolution glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, fb.texture); glDrawArrays(GL_TRIANGLES, 0, 6); glBindTexture(GL_TEXTURE_2D,0); glBindVertexArray(0); SDL_Delay(1); SDL_GL_SwapWindow(mainwindow); if(points_p1>=5 || points_p2>=5){ points_p1=0; points_p2=0; ball.speed={0.0f,0.0f}; SDL_Delay(3000); } } DESTRUCTOR(fb); DESTRUCTOR(shader); DESTRUCTOR(fb_shader); Mix_FreeChunk(pong); Mix_FreeChunk(pong2); Mix_FreeChunk(pong3); SDL_GL_DeleteContext(maincontext); SDL_DestroyWindow(mainwindow); SDL_Quit(); return 0; }
void pve500_state::pve500(machine_config &config) { /* Main CPU */ TMPZ84C015(config, m_maincpu, 12_MHz_XTAL / 2); // TMPZ84C015BF-6 m_maincpu->set_addrmap(AS_PROGRAM, &pve500_state::maincpu_prg); m_maincpu->set_addrmap(AS_IO, &pve500_state::maincpu_io); m_maincpu->set_daisy_config(maincpu_daisy_chain); m_maincpu->out_dtra_callback().set(FUNC(pve500_state::GPI_w)); m_maincpu->out_dtrb_callback().set(m_buzzer, FUNC(beep_device::set_state)).invert(); m_maincpu->out_txda_callback().set("recorder", FUNC(rs232_port_device::write_txd)); m_maincpu->out_txdb_callback().set("player1", FUNC(rs232_port_device::write_txd)); z80ctc_device& ctc(Z80CTC(config, "external_ctc", 12_MHz_XTAL / 2)); ctc.intr_callback().set_inputline(m_maincpu, INPUT_LINE_IRQ0); z80sio0_device& sio(Z80SIO0(config, "external_sio", 12_MHz_XTAL / 2)); sio.out_int_callback().set_inputline(m_maincpu, INPUT_LINE_IRQ0); sio.out_txda_callback().set("player2", FUNC(rs232_port_device::write_txd)); sio.out_txdb_callback().set("edl_inout", FUNC(rs232_port_device::write_txd)); /* Secondary CPU */ TMPZ84C015(config, m_subcpu, 12_MHz_XTAL / 2); /* TMPZ84C015BF-6 */ m_subcpu->set_addrmap(AS_PROGRAM, &pve500_state::subcpu_prg); m_subcpu->set_addrmap(AS_IO, &pve500_state::subcpu_io); m_subcpu->out_dtra_callback().set(FUNC(pve500_state::cxdio_reset_w)); m_subcpu->out_dtrb_callback().set(FUNC(pve500_state::external_monitor_w)); m_subcpu->out_txda_callback().set("switcher", FUNC(rs232_port_device::write_txd)); m_subcpu->out_txdb_callback().set("serial_mixer", FUNC(rs232_port_device::write_txd)); // PIO callbacks m_subcpu->in_pa_callback().set(FUNC(pve500_state::eeprom_r)); m_subcpu->out_pa_callback().set(FUNC(pve500_state::eeprom_w)); // ICG3: I/O Expander CXD1095(config, m_cxdio, 0); m_cxdio->out_porta_cb().set(FUNC(pve500_state::io_sc_w)); m_cxdio->out_portb_cb().set(FUNC(pve500_state::io_le_w)); m_cxdio->in_portc_cb().set(FUNC(pve500_state::io_ky_r)); m_cxdio->out_portd_cb().set(FUNC(pve500_state::io_ld_w)); m_cxdio->out_porte_cb().set(FUNC(pve500_state::io_sel_w)); /* Search Dial MCUs */ MB88201(config, "dial_mcu_left", 4_MHz_XTAL).set_disable(); /* PLAYER DIAL MCU */ MB88201(config, "dial_mcu_right", 4_MHz_XTAL).set_disable(); /* RECORDER DIAL MCU */ /* Serial EEPROM (128 bytes, 8-bit data organization) */ /* The EEPROM stores the setup data */ EEPROM_MSM16911_8BIT(config, "eeprom"); /* FIX-ME: These are actually RS422 ports (except EDL IN/OUT which is indeed an RS232 port)*/ rs232_port_device &recorder(RS232_PORT(config, "recorder", default_rs232_devices, nullptr)); recorder.rxd_handler().set(m_maincpu, FUNC(tmpz84c015_device::rxa_w)); rs232_port_device &player1(RS232_PORT(config, "player1", default_rs232_devices, nullptr)); player1.rxd_handler().set(m_maincpu, FUNC(tmpz84c015_device::rxb_w)); rs232_port_device &player2(RS232_PORT(config, "player2", default_rs232_devices, nullptr)); player2.rxd_handler().set("external_sio", FUNC(z80dart_device::rxa_w)); rs232_port_device &edl_inout(RS232_PORT(config, "edl_inout", default_rs232_devices, nullptr)); edl_inout.rxd_handler().set("external_sio", FUNC(z80dart_device::rxb_w)); rs232_port_device &switcher(RS232_PORT(config, "switcher", default_rs232_devices, nullptr)); switcher.rxd_handler().set(m_subcpu, FUNC(tmpz84c015_device::rxa_w)); rs232_port_device &serial_mixer(RS232_PORT(config, "serial_mixer", default_rs232_devices, nullptr)); serial_mixer.rxd_handler().set(m_subcpu, FUNC(tmpz84c015_device::rxb_w)); clock_device &clk1(CLOCK(config, "clk1", 12_MHz_XTAL / 20)); clk1.signal_handler().set(m_maincpu, FUNC(tmpz84c015_device::rxca_w)); clk1.signal_handler().append(m_maincpu, FUNC(tmpz84c015_device::txca_w)); clk1.signal_handler().append(m_maincpu, FUNC(tmpz84c015_device::rxcb_w)); clk1.signal_handler().append(m_maincpu, FUNC(tmpz84c015_device::txcb_w)); clk1.signal_handler().append(m_subcpu, FUNC(tmpz84c015_device::rxca_w)); clk1.signal_handler().append(m_subcpu, FUNC(tmpz84c015_device::txca_w)); clk1.signal_handler().append(m_subcpu, FUNC(tmpz84c015_device::rxcb_w)); clk1.signal_handler().append(m_subcpu, FUNC(tmpz84c015_device::txcb_w)); /* ICF5: 2kbytes of RAM shared between the two CPUs (dual-port RAM)*/ mb8421_device &mb8421(MB8421(config, "mb8421")); mb8421.intl_callback().set(FUNC(pve500_state::mb8421_intl)); mb8421.intr_callback().set(FUNC(pve500_state::mb8421_intr)); /* video hardware */ config.set_default_layout(layout_pve500); /* audio hardware */ SPEAKER(config, "mono").front_center(); BEEP(config, "buzzer", 12_MHz_XTAL / 3200).add_route(ALL_OUTPUTS, "mono", 0.05); // 3.75 kHz CLK2 coming out of IC D4 (frequency divider circuitry) }
void player2() { do { cout<<endl; textcolor(RED); clreol(); cout<<" PLAYER 2's MOVE"<<endl<<endl; cout<<" ENTER THE COORDINATES WHERE YOU WANT TO PUT YOUR 'O' "<<endl; cout<<" x="; cin>>x; if(x==-1) menu(); cout<<endl; cout<<" y="; cin>>y; if((x<0)||(x>2)||(y<0)||(y>2)) //check for valid coordinates { cout<<endl; textcolor(BLUE); clreol(); cout<<" *.ENTER THE CORRECT COORDINATES!!!!"<<endl<<endl; } }while((x<0)||(x>2)||(y<0)||(y>2)); if(tic[x][y]==' ') //check for vacant space at entered coordinates { tic[x][y]='O'; cout<<endl<<endl; ++sum; textbackground(GREEN); textcolor(WHITE); clrscr(); display(); //calling function display } else { textcolor(BLUE); clreol(); cout<<"\t\tTHIS POSITION IS ALREADY FILLED."<<endl; textcolor(BLUE); clreol(); cout<<"\t\tCHOOSE SOME OTHER COORDINATES"<<endl; player2(); //user function call } d=check(); //check function call if(d==0) { if(sum==9) draw(); else user(); //user function call } else { cout<<endl; player2win(); } }
int main() { //window properties sf::RenderWindow pong(sf::VideoMode(RENDERWIDTH, RENDERHEIGHT, 32), "GameName"); pong.setMouseCursorVisible(false); pong.setFramerateLimit(60); //music sf::Music bgm; bgm.openFromFile("musica.wav"); bgm.setPitch(1.5); bgm.setLoop(true); bgm.play(); //sound sf::SoundBuffer buffer1; buffer1.loadFromFile("/usr/lib/libreoffice/share/gallery/sounds/beam.wav"); sf::Sound bounce; bounce.setBuffer(buffer1); sf::SoundBuffer buffer2; buffer2.loadFromFile("/usr/lib/libreoffice/share/gallery/sounds/beam2.wav"); sf::Sound point; point.setBuffer(buffer2); sf::SoundBuffer buffer3; buffer3.loadFromFile("/usr/lib/libreoffice/share/gallery/sounds/beam.wav"); //perfecti hit!!!!! sf::Sound perfecthit; perfecthit.setBuffer(buffer3); //player 1 properties int p1Len = 80; sf::RectangleShape player1(sf::Vector2f(15, p1Len)); player1.setFillColor(sf::Color(0, 0, 255)); player1.setPosition(0, RENDERHEIGHT / 2 - player1.getSize().y / 2); int player1Score = 0; //player 2 properties int p2Len = 80; sf::RectangleShape player2(sf::Vector2f(15, p2Len)); player2.setFillColor(sf::Color(0, 255, 0)); player2.setPosition(RENDERWIDTH - player2.getSize().x, RENDERHEIGHT / 2 - player2.getSize().y / 2); int player2Score = 0; //ball properties sf::CircleShape ball(7, 25); ball.setFillColor(sf::Color(255,255,255)); ball.setPosition(RENDERWIDTH / 2 - ball.getRadius(), RENDERHEIGHT / 2 - ball.getRadius()); float BALLSPEED = 2; float ballVelX = -BALLSPEED, ballVelY = -BALLSPEED; float ballX = RENDERWIDTH / 2 - ball.getRadius(), ballY = RENDERHEIGHT / 2 - ball.getRadius(); float ballDiameter = ball.getRadius() * 2; //BONUS properties /* sf::CircleShape bonus(10, 25); ball.setFillColor(sf::Color(0,255,0)); ball.setPosition(RENDERWIDTH / 2 , RENDERHEIGHT / rand ()%10+1); float BONUSSPEED = 2; float bonusVelX = -BONUSSPEED, bonusVelY = -BONUSSPEED; float bonusX = RENDERWIDTH / 2 - bonus.getRadius(), bonusY = RENDERHEIGHT / 2 - bonus.getRadius(); float bonusDiameter = bonus.getRadius() * 2; */ //score text sf::Font font; font.loadFromFile("/usr/share/cups/fonts/FreeMonoOblique.ttf"); font.loadFromFile("/usr/share/fonts/truetype/ttf-liberation/LiberationSerif-Bold.ttf"); sf::Text score1("0", font, 80); score1.setPosition(RENDERWIDTH / 4, 0); sf::Text score2("0", font, 80); score2.setPosition(3 * RENDERWIDTH / 4, 0); //game loop while(pong.isOpen()) { sf::Event event; while(pong.pollEvent(event)) { if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) pong.close(); } //player 1 movement if(sf::Keyboard::isKeyPressed(sf::Keyboard::W)){ player1.move(0, -10);} else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S)){ player1.move(0, 10); } //MOVIMIENTO GOLPE PLAYER1 if (player1.getPosition().x <= 10){ if(sf::Keyboard::isKeyPressed(sf::Keyboard::D)){ player1.move(10, 0); }} //player 2 movement if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) player2.move(0, -10); else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) player2.move(0, 10); //MOVIMIENTO GOLPE PLAYER2 if (player2.getPosition().x >= RENDERWIDTH-player2.getSize().x-10){ if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){ player2.move(-10, 0); }} //player 1 and wall collision if(player1.getPosition().y <= 0) player1.setPosition(0, 0); if(player1.getPosition().y >= RENDERHEIGHT - player1.getSize().y) player1.setPosition(0, RENDERHEIGHT - player1.getSize().y); //PLAYER1 AND WALL BACK COLLISION if(player1.getPosition().x != 0) player1.move(-1,0); //PLAYER2 AND WALL BACK COLLISION if(player2.getPosition().x != RENDERWIDTH-player2.getSize().x) player2.move(1,0); //player 2 and wall collision if(player2.getPosition().y <= 0) player2.setPosition(RENDERWIDTH - player2.getSize().x, 0); if(player2.getPosition().y >= RENDERHEIGHT - player2.getSize().y) player2.setPosition(RENDERWIDTH - player2.getSize().x, RENDERHEIGHT - player2.getSize().y); //ball and wall collision if(ball.getPosition().y <= 0 || ball.getPosition().y >= RENDERHEIGHT - ballDiameter) { ballVelY *= -1; bounce.play(); } //ball and player 1 collision if (ball.getPosition().x <= player1.getPosition().x + player1.getSize().x) { if ((ball.getPosition().y + ballDiameter >= player1.getPosition().y && ball.getPosition().y + ballDiameter <= player1.getPosition().y + player1.getSize().y) || ball.getPosition().y <= player1.getPosition().y + player1.getSize().y && ball.getPosition().y >= player1.getPosition().y){ if (player1.getPosition().x > 14){ ballVelX = (ballVelX - 2) * -1; ball.setFillColor(sf::Color(255,0,0)); perfecthit.play(); } else if (player1.getPosition().x <= 14){ ballVelX = (ballVelX - 1) * -1; ball.setFillColor(sf::Color(0,0,255)); bounce.play(); } } else { ball.setFillColor(sf::Color(255,255,255)); point.play(); player2Score += 1; ballX = RENDERWIDTH / 2 - ball.getRadius(); if (BALLSPEED < 8){ BALLSPEED += 0.2; } ballVelX = BALLSPEED; score2.setString(convertInt(player2Score)); score2.setPosition(3 * RENDERWIDTH / 4 - score2.getLocalBounds().width, 0); if (p2Len > 40) p2Len -= 10; player2.setSize(sf::Vector2f(15, p2Len)); if (p1Len < 100) p1Len += 10; player1.setSize(sf::Vector2f(15, p1Len)); } } //ball and player 2 collision if (ball.getPosition().x + ballDiameter >= player2.getPosition().x) { if ((ball.getPosition().y + ballDiameter >= player2.getPosition().y && ball.getPosition().y + ballDiameter <= player2.getPosition().y + player2.getSize().y) || ball.getPosition().y <= player2.getPosition().y + player2.getSize().y && ball.getPosition().y >= player2.getPosition().y) { if (player2.getPosition().x < (RENDERWIDTH-9-player2.getSize().x)){ ballVelX = (ballVelX + 2) * -1; ball.setFillColor(sf::Color(255,0,0)); perfecthit.play(); } else if (player2.getPosition().x >= (RENDERWIDTH-9-player2.getSize().x)){ ballVelX = (ballVelX + 1) * -1; ball.setFillColor(sf::Color(0,255,0)); bounce.play(); } } else { ball.setFillColor(sf::Color(255,255,255)); point.play(); player1Score += 1; ballX = RENDERWIDTH / 2 - ball.getRadius(); if (BALLSPEED < 8) BALLSPEED += 0.5; ballVelX = -BALLSPEED; score1.setString(convertInt(player1Score)); if (p1Len > 40) p1Len -= 10; player1.setSize(sf::Vector2f(15, p1Len)); if (p2Len < 100) p2Len += 10; player2.setSize(sf::Vector2f(15, p2Len)); } } //ball position update ballX += ballVelX; ballY += ballVelY; ball.setPosition(ballX, ballY); //render updates pong.clear(); pong.draw(score1); pong.draw(score2); pong.draw(player1); pong.draw(player2); pong.draw(ball); pong.display(); } return 0; }
int main(){ ////////////////////GAME PROPERTIES/////////////////////////////////// //window properties sf::RenderWindow pong(sf::VideoMode(RENDERWIDTH, RENDERHEIGHT, 32), "GameName"); pong.setMouseCursorVisible(false); pong.setFramerateLimit(60); //music sf::Music bgm; bgm.openFromFile("musica.wav"); bgm.setPitch(1.5); bgm.setLoop(true); bgm.play(); //sound sf::SoundBuffer buffer1; buffer1.loadFromFile("/usr/lib/libreoffice/share/gallery/sounds/beam.wav"); sf::Sound bounce; bounce.setBuffer(buffer1); sf::SoundBuffer buffer2; buffer2.loadFromFile("/usr/lib/libreoffice/share/gallery/sounds/beam2.wav"); sf::Sound point; point.setBuffer(buffer2); sf::SoundBuffer buffer3; buffer3.loadFromFile("/usr/lib/libreoffice/share/gallery/sounds/beam.wav"); //perfecti hit!!!!! sf::Sound perfecthit; perfecthit.setBuffer(buffer3); //player 1 properties int p1Len = 80; sf::RectangleShape player1(sf::Vector2f(15, p1Len)); player1.setFillColor(sf::Color(0, 0, 255)); player1.setPosition(0, RENDERHEIGHT / 2 - player1.getSize().y / 2); int player1Score = 0; //player 2 properties int p2Len = 80; sf::RectangleShape player2(sf::Vector2f(15, p2Len)); player2.setFillColor(sf::Color(0, 255, 0)); player2.setPosition(RENDERWIDTH - player2.getSize().x, RENDERHEIGHT / 2 - player2.getSize().y / 2); int player2Score = 0; //ball properties sf::CircleShape ball(7, 25); ball.setFillColor(sf::Color(255,255,255)); ball.setPosition(RENDERWIDTH / 2 - ball.getRadius(), RENDERHEIGHT / 2 - ball.getRadius()); float BALLSPEED = 2; float ballVelX = -BALLSPEED, ballVelY = -BALLSPEED; float ballX = RENDERWIDTH / 2 - ball.getRadius(), ballY = RENDERHEIGHT / 2 - ball.getRadius(); float ballDiameter = ball.getRadius() * 2; sf::Font font; font.loadFromFile("/usr/share/cups/fonts/FreeMonoOblique.ttf"); font.loadFromFile("/usr/share/fonts/truetype/ttf-liberation/LiberationSerif-Bold.ttf"); sf::Text score1("0", font, 80); score1.setPosition(RENDERWIDTH / 4, 0); sf::Text score2("0", font, 80); score2.setPosition(3 * RENDERWIDTH / 4, 0); ///////////////FINISH PROPERTIES////////////////////////////////////////////////////// CvCapture* capture =0; capture = cvCaptureFromCAM(1); if(!capture){ printf("Capture failure\n"); return -1; } IplImage* frame=0; frame = cvQueryFrame(capture); if(!frame) return -1; //create a blank image and assigned to 'imgTracking' which has the same size of original video imgTracking=cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U, 3); cvZero(imgTracking); //covert the image, 'imgTracking' to black cvNamedWindow("Video"); cvNamedWindow("Ball"); //iterate through each frames of the video while(true){ frame = cvQueryFrame(capture); if(!frame) break; frame=cvCloneImage(frame); cvSmooth(frame, frame, CV_GAUSSIAN,3,3); //smooth the original image using Gaussian kernel IplImage* imgHSV = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3); cvCvtColor(frame, imgHSV, CV_BGR2HSV); //Change the color format from BGR to HSV IplImage* imgThresh = GetThresholdedImage(imgHSV, 94, 169, 127, 143, 251, 229); cvSmooth(imgThresh, imgThresh, CV_GAUSSIAN,3,3); //smooth the binary image using Gaussian kernel player100 = trackObject(imgThresh, 255, 0, 0, 1); IplImage* imgHSV2 = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3); cvCvtColor(frame, imgHSV2, CV_BGR2HSV); IplImage* imgThresh2 = GetThresholdedImage(imgHSV2, 22, 64, 152, 50, 134, 256); cvSmooth(imgThresh2, imgThresh2, CV_GAUSSIAN,3,3); //smooth the binary image using Gaussian kernel player200 = trackObject(imgThresh2, 0, 255, 0, 2); // Add the tracking image and the frame cvAdd(frame, imgTracking, frame); cvShowImage("Ball", imgThresh); cvShowImage("Ball2", imgThresh2); cvShowImage("Video", frame); //Clean up used images cvReleaseImage(&imgHSV); cvReleaseImage(&imgHSV2); cvReleaseImage(&imgThresh); cvReleaseImage(&imgThresh2); cvReleaseImage(&frame); //Wait 10mS int c = cvWaitKey(10); //If 'ESC' is pressed, break the loop if((char)c==27 ) break; //////////////////////////////Game//////////////////////////////////// //player 1 movement if(player100==1){ player1.move(0, -10);} else if(player100==-1){ player1.move(0, 10); } //MOVIMIENTO GOLPE PLAYER1 if(player100==0){ player1.move(0,0);} if (player1.getPosition().x <= 10){ if(player100==2){ player1.move(10, 0); }} //player 2 movement if(player200==1) player2.move(0, -10); else if(player200==-1) player2.move(0, 10); if(player200==0){ player2.move(0,0);} //MOVIMIENTO GOLPE PLAYER2 if (player2.getPosition().x >= RENDERWIDTH-player2.getSize().x-10){ if(player200==-2){ player2.move(-10, 0); }} //player 1 and wall collision if(player1.getPosition().y <= 0) player1.setPosition(0, 0); if(player1.getPosition().y >= RENDERHEIGHT - player1.getSize().y) player1.setPosition(0, RENDERHEIGHT - player1.getSize().y); //PLAYER1 AND WALL BACK COLLISION if(player1.getPosition().x != 0) player1.move(-1,0); //PLAYER2 AND WALL BACK COLLISION if(player2.getPosition().x != RENDERWIDTH-player2.getSize().x) player2.move(1,0); //player 2 and wall collision if(player2.getPosition().y <= 0) player2.setPosition(RENDERWIDTH - player2.getSize().x, 0); if(player2.getPosition().y >= RENDERHEIGHT - player2.getSize().y) player2.setPosition(RENDERWIDTH - player2.getSize().x, RENDERHEIGHT - player2.getSize().y); //ball and wall collision if(ball.getPosition().y <= 0 || ball.getPosition().y >= RENDERHEIGHT - ballDiameter) { ballVelY *= -1; bounce.play(); } //ball and player 1 collision if (ball.getPosition().x <= player1.getPosition().x + player1.getSize().x) { if ((ball.getPosition().y + ballDiameter >= player1.getPosition().y && ball.getPosition().y + ballDiameter <= player1.getPosition().y + player1.getSize().y) || ball.getPosition().y <= player1.getPosition().y + player1.getSize().y && ball.getPosition().y >= player1.getPosition().y){ if (player1.getPosition().x > 14){ ballVelX = (ballVelX - 2) * -1; ball.setFillColor(sf::Color(255,0,0)); perfecthit.play(); } else if (player1.getPosition().x <= 14){ ballVelX = (ballVelX - 1) * -1; ball.setFillColor(sf::Color(0,0,255)); bounce.play(); } } else { ball.setFillColor(sf::Color(255,255,255)); point.play(); player2Score += 1; ballX = RENDERWIDTH / 2 - ball.getRadius(); if (BALLSPEED < 8){ BALLSPEED += 0.2; } ballVelX = BALLSPEED; score2.setString(convertInt(player2Score)); score2.setPosition(3 * RENDERWIDTH / 4 - score2.getLocalBounds().width, 0); if (p2Len > 40) p2Len -= 10; player2.setSize(sf::Vector2f(15, p2Len)); if (p1Len < 100) p1Len += 10; player1.setSize(sf::Vector2f(15, p1Len)); } } //ball and player 2 collision if (ball.getPosition().x + ballDiameter >= player2.getPosition().x) { if ((ball.getPosition().y + ballDiameter >= player2.getPosition().y && ball.getPosition().y + ballDiameter <= player2.getPosition().y + player2.getSize().y) || ball.getPosition().y <= player2.getPosition().y + player2.getSize().y && ball.getPosition().y >= player2.getPosition().y) { if (player2.getPosition().x < (RENDERWIDTH-9-player2.getSize().x)){ ballVelX = (ballVelX + 2) * -1; ball.setFillColor(sf::Color(255,0,0)); perfecthit.play(); } else if (player2.getPosition().x >= (RENDERWIDTH-9-player2.getSize().x)){ ballVelX = (ballVelX + 1) * -1; ball.setFillColor(sf::Color(0,255,0)); bounce.play(); } } else { ball.setFillColor(sf::Color(255,255,255)); point.play(); player1Score += 1; ballX = RENDERWIDTH / 2 - ball.getRadius(); if (BALLSPEED < 8) BALLSPEED += 0.5; ballVelX = -BALLSPEED; score1.setString(convertInt(player1Score)); if (p1Len > 40) p1Len -= 10; player1.setSize(sf::Vector2f(15, p1Len)); if (p2Len < 100) p2Len += 10; player2.setSize(sf::Vector2f(15, p2Len)); } } //ball position update ballX += ballVelX; ballY += ballVelY; ball.setPosition(ballX, ballY); //render updates pong.clear(); pong.draw(score1); pong.draw(score2); pong.draw(player1); pong.draw(player2); pong.draw(ball); pong.display(); /////////////////////Finish Game///////////////////////////////// } cvDestroyAllWindows() ; cvReleaseImage(&imgTracking); cvReleaseCapture(&capture); return 0; }
int main() { //window properties sf::RenderWindow pong(sf::VideoMode(RENDERWIDTH, RENDERHEIGHT, 32), "GameName"); //, sf::Style::Fullscreen pong.setMouseCursorVisible(false); pong.setFramerateLimit(24); /* //music sf::Music bgm; bgm.openFromFile("musica.wav"); bgm.setPitch(1.5); bgm.setLoop(true); bgm.play(); */ //sound sf::SoundBuffer buffer1; buffer1.loadFromFile("/usr/lib/libreoffice/share/gallery/sounds/beam.wav"); sf::Sound bounce; bounce.setBuffer(buffer1); sf::SoundBuffer buffer2; buffer2.loadFromFile("/usr/lib/libreoffice/share/gallery/sounds/beam2.wav"); sf::Sound point; point.setBuffer(buffer2); sf::SoundBuffer buffer3; buffer3.loadFromFile("/usr/lib/libreoffice/share/gallery/sounds/beam.wav"); //perfecti hit!!!!! sf::Sound perfecthit; perfecthit.setBuffer(buffer3); //player 1 properties int p1Len = 80; sf::RectangleShape player1(sf::Vector2f(15, p1Len)); player1.setFillColor(sf::Color(0, 0, 255)); player1.setPosition(0, RENDERHEIGHT / 2 - player1.getSize().y / 2); int player1Score = 0; //player 2 properties int p2Len = 80; sf::RectangleShape player2(sf::Vector2f(15, p2Len)); player2.setFillColor(sf::Color(0, 255, 0)); player2.setPosition(RENDERWIDTH - player2.getSize().x, RENDERHEIGHT / 2 - player2.getSize().y / 2); int player2Score = 0; //ball properties sf::CircleShape ball(7, 25); ball.setFillColor(sf::Color(255,255,255)); ball.setPosition(RENDERWIDTH / 2 - ball.getRadius(), RENDERHEIGHT / 2 - ball.getRadius()); float BALLSPEED = 2; float ballVelX = -BALLSPEED, ballVelY = -BALLSPEED; float ballX = RENDERWIDTH / 2 - ball.getRadius(), ballY = RENDERHEIGHT / 2 - ball.getRadius(); float ballDiameter = ball.getRadius() * 2; sf::Font font; font.loadFromFile("/usr/share/cups/fonts/FreeMonoOblique.ttf"); font.loadFromFile("/usr/share/fonts/truetype/ttf-liberation/LiberationSerif-Bold.ttf"); sf::Text score1("0", font, 80); score1.setPosition(RENDERWIDTH / 4, 0); sf::Text score2("0", font, 80); score2.setPosition(3 * RENDERWIDTH / 4, 0); //game loop //while(pong.isOpen()) /////////////////////Declaraciones//////////////////////////////// IplImage* frame=0; IplImage* imgTracking; CvCapture* capture =0; IplImage* imgThresh; IplImage* imgThresh2; sf::Event event; int player100=0; int player200=0; capture = cvCaptureFromCAM(1); for(int i=0;i<1000;++i) { if(!capture){ printf("Capture failure\n"); return -1; } frame = cvRetrieveFrame(capture); if(!frame) return -1; //create a blank image and assigned to 'imgTracking' which has the same size of original video imgTracking=cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U, 3); cvSmooth(frame, frame, CV_GAUSSIAN,3,3); //smooth the original image using Gaussian kernel IplImage* imgHSV = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3); cvCvtColor(frame, imgHSV, CV_BGR2HSV); //Change the color format from BGR to HSV imgThresh = GetThresholdedImage(imgHSV, 24, 0, 224, 46, 89, 256); cvReleaseImage(&imgHSV); delete(imgHSV); cvSmooth(imgThresh, imgThresh, CV_GAUSSIAN,3,3); //smooth the binary image using Gaussian kernel player100 = trackObject(imgThresh, 255, 0, 0, 1); ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// IplImage* imgHSV2 = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3); cvCvtColor(frame, imgHSV2, CV_BGR2HSV); imgThresh2 = GetThresholdedImage(imgHSV2, 24, 0, 224, 46, 89, 256); cvReleaseImage(&imgHSV2); delete(imgHSV2); cvSmooth(imgThresh2, imgThresh2, CV_GAUSSIAN,3,3); //smooth the binary image using Gaussian kernel player200 = trackObject(imgThresh2, 0, 255, 0, 2); /* //player 1 movement if( player100 == 1 ) { player1.move(0, -10);} else if(player100==-1){ player1.move(0, 10); } //MOVIMIENTO GOLPE PLAYER1 if (player1.getPosition().x <= 10){ if(player100==2){ player1.move(10, 0); }} //player 2 movement if(player200==1) player2.move(0, -10); else if(player200==-1) player2.move(0, 10); //MOVIMIENTO GOLPE PLAYER2 if (player2.getPosition().x >= RENDERWIDTH-player2.getSize().x-10){ if(player200==-2){ player2.move(-10, 0); }} //player 1 and wall collision if(player1.getPosition().y <= 0) player1.setPosition(0, 0); if(player1.getPosition().y >= RENDERHEIGHT - player1.getSize().y) player1.setPosition(0, RENDERHEIGHT - player1.getSize().y); //PLAYER1 AND WALL BACK COLLISION if(player1.getPosition().x != 0) player1.move(-1,0); //PLAYER2 AND WALL BACK COLLISION if(player2.getPosition().x != RENDERWIDTH-player2.getSize().x) player2.move(1,0); //player 2 and wall collision if(player2.getPosition().y <= 0) player2.setPosition(RENDERWIDTH - player2.getSize().x, 0); if(player2.getPosition().y >= RENDERHEIGHT - player2.getSize().y) player2.setPosition(RENDERWIDTH - player2.getSize().x, RENDERHEIGHT - player2.getSize().y); //ball and wall collision if(ball.getPosition().y <= 0 || ball.getPosition().y >= RENDERHEIGHT - ballDiameter) { ballVelY *= -1; bounce.play(); } //ball and player 1 collision if (ball.getPosition().x <= player1.getPosition().x + player1.getSize().x) { if ((ball.getPosition().y + ballDiameter >= player1.getPosition().y && ball.getPosition().y + ballDiameter <= player1.getPosition().y + player1.getSize().y) || ball.getPosition().y <= player1.getPosition().y + player1.getSize().y && ball.getPosition().y >= player1.getPosition().y){ if (player1.getPosition().x > 14){ ballVelX = (ballVelX - 2) * -1; ball.setFillColor(sf::Color(255,0,0)); perfecthit.play(); } else if (player1.getPosition().x <= 14){ ballVelX = (ballVelX - 1) * -1; ball.setFillColor(sf::Color(0,0,255)); bounce.play(); } } else { ball.setFillColor(sf::Color(255,255,255)); point.play(); player2Score += 1; ballX = RENDERWIDTH / 2 - ball.getRadius(); if (BALLSPEED < 8){ BALLSPEED += 0.2; } ballVelX = BALLSPEED; score2.setString(convertInt(player2Score)); score2.setPosition(3 * RENDERWIDTH / 4 - score2.getLocalBounds().width, 0); if (p2Len > 40) p2Len -= 10; player2.setSize(sf::Vector2f(15, p2Len)); if (p1Len < 100) p1Len += 10; player1.setSize(sf::Vector2f(15, p1Len)); } }//fin IF //ball and player 2 collision if (ball.getPosition().x + ballDiameter >= player2.getPosition().x) { if ((ball.getPosition().y + ballDiameter >= player2.getPosition().y && ball.getPosition().y + ballDiameter <= player2.getPosition().y + player2.getSize().y) || ball.getPosition().y <= player2.getPosition().y + player2.getSize().y && ball.getPosition().y >= player2.getPosition().y) { if (player2.getPosition().x < (RENDERWIDTH-9-player2.getSize().x)){ ballVelX = (ballVelX + 2) * -1; ball.setFillColor(sf::Color(255,0,0)); perfecthit.play(); } else if (player2.getPosition().x >= (RENDERWIDTH-9-player2.getSize().x)){ ballVelX = (ballVelX + 1) * -1; ball.setFillColor(sf::Color(0,255,0)); bounce.play(); }//fin if elseh } else { ball.setFillColor(sf::Color(255,255,255)); point.play(); player1Score += 1; ballX = RENDERWIDTH / 2 - ball.getRadius(); if (BALLSPEED < 8) BALLSPEED += 0.5; ballVelX = -BALLSPEED; score1.setString(convertInt(player1Score)); if (p1Len > 40) p1Len -= 10; player1.setSize(sf::Vector2f(15, p1Len)); if (p2Len < 10) p2Len += 10; player2.setSize(sf::Vector2f(15, p2Len)); }//fin else }//fin IF */ //ball position update ballX += ballVelX; ballY += ballVelY; ball.setPosition(ballX, ballY); //render updates pong.clear(); pong.draw(score1); pong.draw(score2); pong.draw(player1); pong.draw(player2); pong.draw(ball); pong.display(); }//fin for cvReleaseCapture(&capture); cvReleaseImage(&imgThresh); cvReleaseImage(&imgThresh2); cvReleaseImage(&frame); cvReleaseImage(&imgTracking); //cvReleaseImage(&imgTemp); revisar este puntero delete(moments); return 0; }