Re: Untitled

From Soft Mousedeer, 1 Year ago, written in Plain Text, viewed 2 times. This paste will bite the big one in 1 Second. This paste is a reply to Untitled from Gentle Cheetah - view diff
URL https://paste.paivola.fi/view/dc04bf42 Embed
Download Paste or View Raw
  1. object FlappyBugApp extends App {
  2.  
  3.   val sky        = rectangle(ViewWidth, ViewHeight,  LightBlue)
  4.   val ground     = rectangle(ViewWidth, GroundDepth, SandyBrown)
  5.   val trunk      = rectangle(30, 250, SaddleBrown)
  6.   val foliage    = circle(200, ForestGreen)
  7.   val tree       = trunk.onto(foliage, TopCenter, Center)
  8.   val rootedTree = tree.onto(ground, BottomCenter, new Pos(ViewWidth / 2, 30))
  9.   val scenery    = sky.place(rootedTree, BottomLeft, BottomLeft)
  10.  
  11.  
  12.   val bugPic = Pic("ladybug.png")
  13.  
  14.  
  15.   def rockPic(obstacle: Obstacle) = circle(obstacle.radius * 2, Black)
  16.  
  17.  
  18.   val game = new Game()
  19.  
  20.   val gui = new View(game, "FlappyBug"){
  21.     var background = scenery
  22.     def makePic = {
  23.       for (obs <- game.obstacles){
  24.         this.background = this.background.place(rockPic(obs), obs.pos)
  25.       }
  26.       this.background.place(bugPic, game.bug.pos)    
  27.      
  28.     }
  29.     override def onKeyDown(painettu: Key)={
  30.       if (painettu == Key.Space) game.activateBug()
  31.     }
  32.     override def onTick() ={
  33.       game.timePasses()
  34.       print("time")
  35.       this.background = this.background.shiftLeft(backgroundSpeed)
  36.     }
  37.     override def isDone() =  game.isLost
  38.   }
  39.  
  40.   gui.start()
  41.  
  42. }
  43.  
  44. ---------------------
  45. class Obstacle(val radius: Int) {
  46.   var currentPos = randomLaunchPosition()
  47.  
  48.   def pos = this.currentPos
  49.  
  50.   def approach() = {
  51.     if (this.isActive())
  52.       this.currentPos = this.currentPos.addX(-ObstacleSpeed)
  53.     else
  54.       this.currentPos = randomLaunchPosition()
  55.   }
  56.  
  57.   override def toString = "center at " + this.pos + ", radius " + this.radius
  58.  
  59.   def touches(bug: Bug)={
  60.     this.pos.distance(bug.pos) < this.radius + bug.radius
  61.   }
  62.   def isActive() = {
  63.     this.currentPos.x >= (-1.0*this.radius)
  64.   }
  65.   private def randomLaunchPosition() ={
  66.     val launchX = 1000 + this.radius + Random.nextInt(499)
  67.     val launchY = Random.nextInt(400)
  68.     new Pos(launchX, launchY)
  69.   }
  70. }
  71. ----------------------
  72. //Palaute
  73. The farthest obstacle by its X-coordinate seems to be at (1392.0,75.0)
  74. Thus approaching the obstacles 41.0 times should have them all on screen. Farthest:(982.0,75.0).
  75. The obstacle that was created at the farthest location is now at: (902.0,75.0).
  76. And the pixel at the previous location of that obstacle (982.0,75.0) should be showing the background, not the obstacle.
  77. Reason for failure:
  78. Color(0, 0, 0) was not equal to Color(255, 202, 24), but Color(0, 0, 0) was equal to Black.
  79.  
  80. -----------------------
  81. class Game {
  82.   val bug = new Bug(new Pos(100.0,40.0))
  83.   val obstacles = Vector(new Obstacle(70), new Obstacle(30), new Obstacle(20))
  84.  
  85.   def activateBug(){
  86.     bug.flap(15)
  87.   }
  88.  
  89.   def timePasses(){
  90.     bug.fall()
  91.     for (obs <- this.obstacles){
  92.       obs.approach()
  93.     }    
  94.   }
  95.  
  96.   def isLost = {
  97.     var obsTouchBug = false
  98.     for (obs <- obstacles){
  99.       if (obs.touches(this.bug)){
  100.         obsTouchBug = true
  101.        
  102.       }
  103.     }    
  104.      obsTouchBug==true || !this.bug.isInBounds
  105.   }
  106.  
  107. }
  108.  

Replies to Re: Untitled rss

Title Name Language When
Re: Re: Untitled Voluminous Marmoset text 1 Year ago.

Reply to "Re: Untitled"

Here you can reply to the paste above