見出し画像

【WoofJS】スペースルンバ(ピカチュウ編)


Play demo



サンプルコード

setBackdropURL("./docs/images/boss-backdrop.jpg")
setBackdropStyle("cover")

var instruction_text = new Text({
  text: "Use the left and right arrow keys to turn and the space bar to move forward",
  color:"WHITE",
  size:30,
  x:0,
  y:0,
  fontFamily:"Arial Black"
})

var instruction_text2 = new Text({
  text: "Collect stars and watch out for planets!",
  color:"WHITE",
  size:30,
  x:0,
  y:-40,
  fontFamily:"Arial Black"
})



after(5,"seconds", ()=> {instruction_text.showing = false;instruction_text2.showing = false})


var gameover = new Image({
  url:"./docs/images/explosion.png",
  width:100,
  height: 100,
  x:0,
  y:0
})
gameover.showing = false


var rocket = new Image({
  url:"./docs/images/rocket.png",
  width:120,
  height:60,
})

rocket.turnLeft(90)

forever(() => {
  if (keysDown.includes('LEFT')) {
    rocket.angle +=5
  }
  if (keysDown.includes('RIGHT')) {
    rocket.angle -=5
  }
  if (keysDown.includes('SPACE')) {
    rocket.move(10)
  }
  
  if (rocket.x >maxX) {rocket.x = minX}
  if (rocket.x <minX) {rocket.x = maxX}
  if (rocket.y >maxY) {rocket.y = minY}
  if (rocket.y <minY) {rocket.y = maxY}
})


var stars =[]
if (gameover.showing === false) {
every(2, 'second', () => {
  var star = new Image({
    url: "./docs/images/pikachu.png",
    width: 120,
    height:  100,
    x: randomX(),
    y: randomY()
  })
  stars.push(star)
})
}


var planets =[]
if (gameover.showing === false) {
  every(10, 'seconds', () => {
    var planet = new Image({
      url: "./docs/images/rr-planet.png",
      width: 0,
      height: 50,
      x: randomX(),
      y: randomY()
  })
  planets.push(planet)
})
}


forever(() => {
  stars.forEach(star => {
    if (star.touching(rocket)) {
      star.delete()
      score++
    }    
  })
})


var score =0
var score_text = new Text({
  text: ()=> "Score:" + score,
  color:"rgb(255,0,0)",
  size:48,
  x: -250,
  y: 290,
  fontFamily:"Impact"
})
  

forever(() => {
  planets.forEach(planet => {planet.width = planet.height;
    if (planet.touching(rocket)) {
      rocket.showing = false
      gameover.showing = true
      new Text({x:0,y:100,color:"red",size:30,text:"GAME OVER"})
      star.forEach(star =>{
        clone.delete()
      }
           )
      }    
  })
})
  

instruction_text.sendToFront()
instruction_text2.sendToFront()
rocket.sendToFront()
gameover.sendToFront()

この記事が気に入ったらサポートをしてみませんか?