I am attempting to program a easy area shooter and my purpose proper now’s that when the participant presses down and holds a key, the ship accelerates to a max velocity. I’ve that considerably, the issue is that when beginning the sport I can press the directional/arrow keys on my keyboard about 4 or 5 occasions (in any route) and get the specified consequence earlier than additional key presses haven’t any have an effect on on the rate and the ship simply floats. It is just like the keyboard will get disabled. Listed here are the related components of this system:
void Ship::handleEvent( SDL_Event &e ) {
290
291 const Uint8* keystate = SDL_GetKeyboardState(NULL);
292
293 bool keydown = false;
294
295 if ( e.sort == SDL_KEYDOWN )
296 keydown = true;
297
298 if ( keydown ) {
299
300 // std::cout << std::boolalpha << "keydown: " << keydown << "n" << "keyup: " << keyup << "n";
301
302 // std::cout << holdTime << "n";
303
304 if ( keystate[SDL_SCANCODE_UP] && std::abs(mVelY) <= SHIP_MAX_VELOCITY ) {
305
306 if (mVelY <= SHIP_MAX_VELOCITY) {
307 mVelY -= SHIP_ACCELERATION;
308 route = UP;
309 }
310 }
311
312 if ( keystate[SDL_SCANCODE_DOWN] && std::abs(mVelY) <= SHIP_MAX_VELOCITY ) {
313
314 if (mVelY <= SHIP_MAX_VELOCITY) {
315 mVelY += SHIP_ACCELERATION;
316 route = DOWN;
317 }
318 }
319
320 if ( keystate[SDL_SCANCODE_LEFT] && std::abs(mVelX) <= SHIP_MAX_VELOCITY ) {
321
322 if (mVelX <= SHIP_MAX_VELOCITY) {
323 mVelX -= SHIP_ACCELERATION;
324 route = LEFT;
325 }
326 }
327
328 if ( keystate[SDL_SCANCODE_RIGHT] ) {
329
330 if (mVelX <= SHIP_MAX_VELOCITY) {
331 mVelX += SHIP_ACCELERATION;
332 route = RIGHT;
333 }
334 }
335
336 }
337
338 // std::cout << std::boolalpha << "keydown: " << keydown << "n" << "keyup: " << keyup << "n";
339
340 }
357 void Ship::transfer() {
358
359 mPosX += mVelX;
360 shipCollBox.x = mPosX;
361
362 if ( mPosX < 0 || mPosX + SHIP_WIDTH > LEVEL_WIDTH || checkCollision( shipCollBox, wall1 ) || checkCollision( ship CollBox, wall2) || checkCollision( shipCollBox, wall3 ) ) {
363 mPosX -= mVelX;
364 shipCollBox.x = mPosX;
365 }
366
367 mPosY += mVelY;
368 shipCollBox.y = mPosY;
369 std::cout << "mVelY: " << mVelY << "n" << "mPosY: " << mPosY << "n";
370 // SDL_Delay(100);
371 if ( mPosY < 0 || mPosY + SHIP_HEIGHT > LEVEL_HEIGHT || ( checkCollision( shipCollBox, wall1 ) || checkCollision( shipColl Field, wall2) || checkCollision( shipCollBox, wall3 ) ) ) {
372 mPosY -= mVelY;
373 shipCollBox.y = mPosY;
374 }
375 }
379 void Ship::render( int camX, int camY ) {
380
381 change (route) {
382
383 case UP: shipTextureUp.render( mPosX - camX, mPosY - camY ); break;
384 case DOWN: shipTextureDown.render(mPosX - camX, mPosY - camY ); break;
385 case LEFT: shipTextureLeft.render(mPosX - camX, mPosY - camY ); break;
386 case RIGHT: shipTextureRight.render(mPosX - camX, mPosY - camY ); break;
387 }
388 }
445 int predominant( int argc, char* args[] ) {
446
447 bool give up = false;
448 SDL_Event e;
449 Ship ship;
450 Projectile bullet;
451 SDL_Rect digital camera = { 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT };
452 init();
453 loadMedia();
454
455 whereas( !give up ) {
456
457 whereas ( SDL_PollEvent( &e ) !=0 ) {
458 if( e.sort == SDL_QUIT ) {
459 give up = true;
460 }
461
462 ship.handleEvent( e );
463 bullet.handleEvent( e, ship.getDirection(), ship.getPosX(), ship.getPosY() );
464 }
465
466 bullet.replace();
467 ship.transfer();
468 digital camera.x = ( ship.getPosX() + Ship::SHIP_WIDTH / 2 ) - SCREEN_WIDTH / 4;
469 digital camera.y = ( ship.getPosY() + Ship::SHIP_HEIGHT / 2 ) - SCREEN_HEIGHT / 4;
470
471 if (digital camera.x < 0)
472 digital camera.x = 0;
473 if (digital camera.y < 0)
474 digital camera.y = 0;
475 if (digital camera.x > LEVEL_WIDTH - digital camera.w)
476 digital camera.x = LEVEL_WIDTH - digital camera.w;
477 if (digital camera.y > LEVEL_HEIGHT - digital camera.h)
478 digital camera.y = LEVEL_HEIGHT - digital camera.h;
479
480 // Clear the display
481 SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
482 SDL_RenderClear( gRenderer );
483
484 bullet.render();
485 backgroundTexture.render( 0, 0, &digital camera );
486 ship.render( digital camera.x, digital camera.y );
487
488 // Replace display
489 SDL_RenderPresent( gRenderer );
490 SDL_Delay(10);
491 }
492
493 shut();
494
495 return 0;
496 }
```