- object FlappyBugApp extends App {
- val sky = rectangle(ViewWidth, ViewHeight, LightBlue)
- val ground = rectangle(ViewWidth, GroundDepth, SandyBrown)
- val trunk = rectangle(30, 250, SaddleBrown)
- val foliage = circle(200, ForestGreen)
- val tree = trunk.onto(foliage, TopCenter, Center)
- val rootedTree = tree.onto(ground, BottomCenter, new Pos(ViewWidth / 2, 30))
- val scenery = sky.place(rootedTree, BottomLeft, BottomLeft)
- val bugPic = Pic("ladybug.png")
- def rockPic(obstacle: Obstacle) = circle(obstacle.radius * 2, Black)
- val game = new Game()
- val gui = new View(game, "FlappyBug"){
- var background = scenery
- def makePic = {
- for (obs <- game.obstacles){
- this.background = this.background.place(rockPic(obs), obs.pos)
- }
- this.background.place(bugPic, game.bug.pos)
- }
- override def onKeyDown(painettu: Key)={
- if (painettu == Key.Space) game.activateBug()
- }
- override def onTick() ={
- game.timePasses()
- print("time")
- this.background = this.background.shiftLeft(backgroundSpeed)
- }
- override def isDone() = game.isLost
- }
- gui.start()
- }
- ---------------------
- class Obstacle(val radius: Int) {
- var currentPos = randomLaunchPosition()
- def pos = this.currentPos
- def approach() = {
- if (this.isActive())
- this.currentPos = this.currentPos.addX(-ObstacleSpeed)
- else
- this.currentPos = randomLaunchPosition()
- }
- override def toString = "center at " + this.pos + ", radius " + this.radius
- def touches(bug: Bug)={
- this.pos.distance(bug.pos) < this.radius + bug.radius
- }
- def isActive() = {
- this.currentPos.x >= (-1.0*this.radius)
- }
- private def randomLaunchPosition() ={
- val launchX = 1000 + this.radius + Random.nextInt(499)
- val launchY = Random.nextInt(400)
- new Pos(launchX, launchY)
- }
- }
- ----------------------
- //Palaute
- The farthest obstacle by its X-coordinate seems to be at (1392.0,75.0)
- Thus approaching the obstacles 41.0 times should have them all on screen. Farthest:(982.0,75.0).
- The obstacle that was created at the farthest location is now at: (902.0,75.0).
- And the pixel at the previous location of that obstacle (982.0,75.0) should be showing the background, not the obstacle.
- Reason for failure:
- Color(0, 0, 0) was not equal to Color(255, 202, 24), but Color(0, 0, 0) was equal to Black.
- -----------------------
- class Game {
- val bug = new Bug(new Pos(100.0,40.0))
- val obstacles = Vector(new Obstacle(70), new Obstacle(30), new Obstacle(20))
- def activateBug(){
- bug.flap(15)
- }
- def timePasses(){
- bug.fall()
- for (obs <- this.obstacles){
- obs.approach()
- }
- }
- def isLost = {
- var obsTouchBug = false
- for (obs <- obstacles){
- if (obs.touches(this.bug)){
- obsTouchBug = true
- }
- }
- obsTouchBug==true || !this.bug.isInBounds
- }
- }
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
— Expand Paste to full width of browser