In javascript. The assignment is to finish a browser based game in which a truck
ID: 3939412 • Letter: I
Question
In javascript. The assignment is to finish a browser based game in which a truck drives around catching falling kiwis, I'm having difficulty getting started with this funciton
Here are the requirements for process_frame()
This function is called by the game every single time the screen refreshes (60 times a second at best). It should do a few things:
if the user is pressing the "b" key, it should call brake_truck()
if the user is pressing the left arrow, it should call translate_truck_left()
if the user is pressing the right arrow, it should call translate_truck_right()
if the user is not pressing any of those keys, it should call coast_truck()
the function should call compute_truck_position() every time regardless of what key is pressed
the function should randomly decide whether to make a new kiwi start falling using the user's setting for the probability
Explanation / Answer
kiwi_caught = 0;
function process_frame() {
if (is_down('b')){
brake_truck();
}
else if (is_down('left')) {
translate_truck_left();
}
else if (is_down('right')) {
translate_truck_right();
}
else {
coast_truck();
}
compute_truck_position();
if (Math.random()*100 < kiwi_rate) {
create_kiwi();
}
}
function translate_truck_right() {
if (get_truck_velocity() < 20) {
set_truck_velocity(get_truck_velocity() + 2.2);
}
}
function translate_truck_left() {
if (get_truck_velocity() > -20) {
set_truck_velocity(get_truck_velocity() - 2.2);
}
}
function coast_truck() {
if (get_truck_velocity() != 0) {
if (get_truck_velocity()>0) {
set_truck_velocity(get_truck_velocity()*.96);
}
else if (get_truck_velocity()<0) {
set_truck_velocity(get_truck_velocity()*.96);
}
}
}
function brake_truck() {
if (get_truck_velocity()>0){
set_truck_velocity(get_truck_velocity()*.8);
}
else if (get_truck_velocity() < 0){
set_truck_velocity(get_truck_velocity()*.8);
}
}
function compute_truck_position() {
newLeft = get_truck_left() + get_truck_velocity();
if (newLeft < 0) {
set_truck_velocity(-.5*get_truck_velocity());
}
else if (newLeft > window.innerWidth-250) {
set_truck_velocity(-.5*get_truck_velocity());
}
newLeft = get_truck_left() + get_truck_velocity();
set_truck_left(newLeft);
}
// MUST BE IN BED OF TRUCK
function find_collisions(kiwi) {
if (get_kiwi_y(kiwi)>window.innerHeight-5){
if(get_kiwi_x(kiwi)>get_truck_left()){
if(get_kiwi_x(kiwi)<get_truck_left()+112){
delete_kiwi(kiwi);
document.getElementById("kiwi-count").innerHTML = ++kiwi_caught
}
}
}
}
function game_over() {
alert("Congrats!","You caught " + kiwi_caught + " kiwi! Press the button again to start a new game!","New Game");
document.getElementById("kiwi-count").innerHTML = 0;
kiwi_caught = 0;
}
// just ignore this (but don't delete it):
check_latest = 2;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.