[ad_1]
I’m making an attempt to program a easy house 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 instances (in any path) and get the specified outcome earlier than additional key presses haven’t any impact on the speed and the ship simply floats.
It’s just like the keyboard will get disabled. Here are the related elements 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.kind == 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 path = 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 path = 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 path = LEFT;
325 }
326 }
327
328 if ( keystate[SDL_SCANCODE_RIGHT] ) {
329
330 if (mVelX <= SHIP_MAX_VELOCITY) {
331 mVelX += SHIP_ACCELERATION;
332 path = 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 Box, wall2) || checkCollision( shipCollBox, wall3 ) ) ) {
372 mPosY -= mVelY;
373 shipCollBox.y = mPosY;
374 }
375 }
379 void Ship::render( int camX, int camY ) {
380
381 swap (path) {
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 fundamental( int argc, char* args[] ) {
446
447 bool stop = false;
448 SDL_Event e;
449 Ship ship;
450 Projectile bullet;
451 SDL_Rect digicam = { 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT };
452 init();
453 loadMedia();
454
455 whereas( !stop ) {
456
457 whereas ( SDL_PollEvent( &e ) !=0 ) {
458 if( e.kind == SDL_QUIT ) {
459 stop = 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 digicam.x = ( ship.getPosX() + Ship::SHIP_WIDTH / 2 ) - SCREEN_WIDTH / 4;
469 digicam.y = ( ship.getPosY() + Ship::SHIP_HEIGHT / 2 ) - SCREEN_HEIGHT / 4;
470
471 if (digicam.x < 0)
472 digicam.x = 0;
473 if (digicam.y < 0)
474 digicam.y = 0;
475 if (digicam.x > LEVEL_WIDTH - digicam.w)
476 digicam.x = LEVEL_WIDTH - digicam.w;
477 if (digicam.y > LEVEL_HEIGHT - digicam.h)
478 digicam.y = LEVEL_HEIGHT - digicam.h;
479
480 // Clear the display screen
481 SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
482 SDL_RenderClear( gRenderer );
483
484 bullet.render();
485 backgroundTexture.render( 0, 0, &digicam );
486 ship.render( digicam.x, digicam.y );
487
488 // Update display screen
489 SDL_RenderPresent( gRenderer );
490 SDL_Delay(10);
491 }
492
493 shut();
494
495 return 0;
496 }
```
[ad_2]