Treasure Hunter(尋寶者游戲)KTBYTE(試講2)

image.png

image.png
/** POSITION: -1732;-1373 */
// global variables
PImage bg = loadImage("https://static.ktbyte.com/images/Fun2a/ocean.jpg"); // create variable bg of type PImage, set to: loadImage("https://static.ktbyte.com/images/Fun2a/ocean.jpg")
Sprite ship = new Sprite("https://static.ktbyte.com/images/Fun2a/ship.png", 320, 240, 80, 80); // create variable ship of type Sprite, set to: new Sprite("https://static.ktbyte.com/images/Fun2a/ship.png", 320, 240, 80, 80)
Sprite treasure = new Sprite("https://static.ktbyte.com/images/Fun2a/treasure.png", 320, 240, 50, 50); // create variable treasure of type Sprite, set to: new Sprite("https://static.ktbyte.com/images/Fun2a/treasure.png", 320, 240, 50, 50)
Sprite button = new Sprite(320, 50, 100, 50); // create variable button of type Sprite, set to: new Sprite(320, 50, 100, 50)
Integer startTime = 15; // create variable startTime of type Integer, set to: 15
Integer timer = 0; // create variable timer of type Integer, set to: 0
Integer score = 0; // create variable score of type Integer, set to: 0
Boolean start = false; // create variable start of type Boolean, set to: false
Integer speed = 5; // create variable speed of type Integer, set to: 5


/** POSITION: -664;-1302 */
// draw function
// runs 60 times per second
void draw() {
  image(bg, 320, 240, width, height); // draw image bg at: (320, 240) with width: width, height: height
  ship.display(); //draw ship on screen
  treasure.display(); //draw treasure on screen
  button.display(); //draw button on screen
  text("開始", 320, 50); // display "開始" at (320, 50)
  text("得分:" + score, 80, 50); // display "得分:" + score at (80, 50)
  text("時(shí)間:" + (startTime - timer/60), 550, 50); // display "時(shí)間:" + (startTime - timer/60) at (550, 50)
  if (start == true) {
    moveShip(); // run function moveShip with inputs: null
    if (startTime - timer/60 <= 0) {
      start = false; // set start to: false
    }
    timer = timer + 1; // set timer to: timer + 1
  }
  if (ship.touchingSprite(treasure)) {
    score = score + 1; // set score to: score + 1
    treasure.moveToPoint(random(0, width), random(0, height));
    if (timer > startTime * 60) {
      start = false; // set start to: false
    }
  }
}

/** POSITION: -1093;-1026 */
void moveShip() {
  if (pressed("LEFT")) {
    ship.moveX(-speed);
  }
  if (pressed("RIGHT")) {
    ship.moveX(speed);
  }
  if (pressed("UP")) {
    ship.moveY(-speed);
  }
  if (pressed("DOWN")) {
    ship.moveY(speed);
  }
}

/** POSITION: -1708;-853 */
// mousePressed function
// runs when mouse clicked on canvas
void mousePressed() {
  if (button.touchingPoint(mouseX, mouseY)) {
    start = true; // set start to: true
    score = 0; // set score to: 0
    timer = 0; // set timer to: 0
  }
}

/** POSITION: -1707;-552 */
// setup function
// runs at the beginning
void setup() {
  treasure.moveToPoint(random(0, width), random(0, height));
  button.setColor(0, 255, 0);
  fill(0, 0, 0); // set shapes to color: 0, 0, 0
}


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
禁止轉(zhuǎn)載,如需轉(zhuǎn)載請通過簡信或評論聯(lián)系作者。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容