The Plane game ( problem)

Monday,  June 17, 2017
When I was working on Pyret the computer fan was constantly running. I would have to debug it. I am using a different computer than I used last time, the computer that I am using has JavaScript  and I think that is the cause of what is happening to the computer. So far when the computer is wrapping the plane. It wouldn't move. So the program is slowing down which means that  the program is causing some issues the computer that I am using. So I will explain what you have to do next just I the last time I used.
We are doing to define the keystroke. We are going to move the plane by using the up and down key. When we press the up the plane would go faster and if we press down the plane would descend even faster. The constant of the plane of how much distance a key represents:

KEY-DISTANCE = 10

Know we define the function we would be alter’s the airplane’s position by that distance depending on which key is pressed:

fun alter-airplane-y-on-key(w, key):
  ask:
    | key == "up"   then: posn(w.x, w.y - KEY-DISTANCE)
    | key == "down" then: posn(w.x, w.y + KEY-DISTANCE)
    | otherwise: w
  end
end

Then we would have to enter what the key would mean


W.big-bang(INIT-POS, [list:
    W.on-tick(move-airplane-xy-on-tick),
    W.on-key(alter-airplane-y-on-key),
    W.to-draw(place-airplane-xy)])


  Would be writing the plane size function
fun is-on-land-or-water(w):
  w.y >= (HEIGHT - BASE-HEIGHT)
end
Then we would tell Pyret to use this predicate to automatically halt the animation by using this function:

 W.big-bang(INIT-POS, [list:
    W.on-tick(move-airplane-xy-on-tick),
    W.on-key(alter-airplane-y-on-key),
    W.to-draw(place-airplane-xy),
    W.stop-when(is-on-land-or-water)])

Then we would put air balloons into the screen in the position that you want or you can do it

random by pyret:
BALLOON-LOC = posn(random(WIDTH), random(HEIGHT))

Not random: BALLOON-LOC = posn(#,#)

Given a position for the balloon, we just need to detect collision:

fun are-overlapping(airplane-posn, balloon-posn):
  distance(airplane-posn, balloon-posn)
    < COLLISION-THRESHOLD
end

What's the distance? you would put 

    fun distance(p1, p2):

      fun square(n): n * n end

      num-sqrt(square(p1.x - p2.x) + square(p1.y - p2.y))

    end


Finally, we have to weave together the two termination conditions:
fun game-ends(w):
  ask:
    | is-on-land-or-water(w)      then: true
    | are-overlapping(w, BALLOON-LOC) then: true
    | otherwise: false
  end
end
and use it instead:
W.big-bang(INIT-POS, [list:
    W.on-tick(move-airplane-xy-on-tick),
    W.on-key(alter-airplane-y-on-key),
    W.to-draw(place-airplane-xy),
    W.stop-when(game-ends)])

Here’s another version:
fun game-ends(w):
  is-on-land-or-water(w) or are-overlapping(w, BALLOON-LOC)
end
     The World State is a structure representing the airplane’s current position and the quantity of fuel left.

 we will use this structure:

    data World:

      | world(p, f)

    end

 On each tick, we expend a world and register one. The progression of time does not expend any fuel, so this code can stay unaltered, other than creating a structure containing the present measure of fuel.
fun move-airplane-xy-on-tick(w :: World):
  world(
    posn(
      move-airplane-wrapping-x-on-tick(w.p.x),
      move-airplane-y-on-tick(w.p.y)),
    w.f)
end
Similarly, the function that responds to keystrokes clearly needs to take into account how much fuel is left in the plane: 
 
 
fun alter-airplane-y-on-key(w, key):
  ask:
    | key == "up"   then:
      if w.f > 0:
        world(posn(w.p.x, w.p.y - KEY-DISTANCE), w.f - 1)
      else:
        w # there's no fuel, so ignore the keystroke
      end
    | key == "down" then:
      world(posn(w.p.x, w.p.y + KEY-DISTANCE), w.f)
    | otherwise: w
  end
end
fun move-airplane-xy-on-tick(w :: World):
  world(
    posn(
      move-airplane-wrapping-x-on-tick(w.p.x),
      move-airplane-y-on-tick(w.p.y)),
    w.f)
end
Similarly, the function that responds to keystrokes clearly needs to take into account how much fuel is left: 
 
fun alter-airplane-y-on-key(w, key):
  ask:
    | key == "up"   then:
      if w.f > 0:
        world(posn(w.p.x, w.p.y - KEY-DISTANCE), w.f - 1)
      else:
        w # there's no fuel, so ignore the keystroke
      end
    | key == "down" then:
      world(posn(w.p.x, w.p.y + KEY-DISTANCE), w.f)
    | otherwise: w
  end
end

 Then Finally we would world state the balloon’s current position, and the quantity of fuel left.
Here is a representation of the world state: 
 
data World:
  | world(p :: Posn, b :: Posn, f :: Number)
end

 










































 






Comments

  1. Great post, Dayana! It is detailed and complete, and will give me what I need to look into the issues with your code. Unfortunately, I won't have time to do that until the end of the week. For the next two days, you could be most helpful working with Eric on the helios elections and testing Duncan's web scraping scripts.

    ReplyDelete

Post a Comment