Page 149 - KEC Khaitan C5 Flipbook
P. 149
player_car.velocityX = -3;
}
if(keyDown("right")){
player_car.velocityX = 3;
}
o The player can navigate using the arrow keys.
o Tip: Adjust the speed to make the game easier or harder.
Ensure the Player Car Stays in Bounds:
player_car.bounceOff(edges);
Step 6: Programming Enemy Car Movement
Randomize Enemy Car Positions:
if(car1.y > 405){
car1.x = randomNumber(400, 0);
car1.y = 0;
}
if(car2.y > 405){
car2.x = randomNumber(400, 0);
car2.y = 0;
}
o This randomizes the position of enemy cars when they reach the bottom of the screen.
o Tip: Increase enemy car speed as the game progresses for more challenge.
Prevent Overlapping:
if(car1.isTouching(car2)){
car1.x = randomNumber(400, 0);
car1.y = 0;
}
Step 7: Adding Game Start Logic
Start the Game When Spacebar is Pressed:
if(keyDown("space")){
car1.velocityY = 10;
Game Development in Sprite Lab 147

