Skip to content

Instantly share code, notes, and snippets.

@thcrack
Created April 17, 2019 05:18
Show Gist options
  • Save thcrack/35e28fade04ce227f27059dd8f059580 to your computer and use it in GitHub Desktop.
Save thcrack/35e28fade04ce227f27059dd8f059580 to your computer and use it in GitHub Desktop.
final int GAME_STOP = 0, GAME_RUN = 1;
int gameState;
int balloonX = 35, balloonY, balloonW = 60, balloonH = 86;
int fireW = 15, fireH = 20;
int cameraOffsetX = 0;
boolean debugMode = false;
PImage bg, balloon, fire;
int hills[] = new int[8];
int hillX[]= new int[8];
final int HILL_W[] = {0, 100, 150, 200};
final int HILL_H[] = {0, 50, 75, 100};
PImage hillImage[] = new PImage[4];
void setup() {
size(800,300);
noCursor(); //隱藏鼠標
bg = loadImage("img/bg.png");
balloon = loadImage("img/balloon.png");
fire = loadImage("img/fire.png");
hillImage[0] = null;
hillImage[1] = loadImage("img/sHill.png");
hillImage[2] = loadImage("img/mHill.png");
hillImage[3] = loadImage("img/lHill.png");
gameState = GAME_RUN;
balloonY = 0;
for(int i = 0; i < hills.length; i++){
hills[i] = floor(random(4));
hillX[i] = i * 200;
}
}
void draw() {
/* ------ Debug Function ------
Please DO NOT edit the code here.
It's for reviewing other requirements when you fail to complete the camera moving requirement.
*/
if (debugMode) {
pushMatrix();
translate(cameraOffsetX, 0);
}
/* ------ End of Debug Function ------ */
switch(gameState) {
case GAME_RUN:
// 顯示背景
image(bg, -cameraOffsetX, 0, width, height);
// 顯示熱氣球
if(balloonY < 0) {
balloonY = 0;
} else if(balloonY + balloonH > height) {
//balloonY = height - balloonH;
gameState=GAME_STOP;
}
image(balloon, balloonX, balloonY, balloonW, balloonH);
balloonY++;
// 顯示山坡
for(int i = 0; i < hills.length; i++){
int hillType = hills[i];
PImage img = hillImage[hillType];
int hillH = HILL_H[hillType], hillW = HILL_W[hillType];
if(img != null){
image(img, hillX[i] + 200 - hillW, height - hillH, hillW, hillH);
}
if(dist(balloonX + balloonW / 2, balloonY + balloonH, hillX[i] + 200 - hillW / 2, height) <= hillW / 2){
gameState = GAME_STOP;
}
hillX[i] -= 2;
if(hillX[i] == -200) hillX[i] = 200 * (hillX.length - 1);
}
// 顯示越過山坡數
// 滑鼠圖示
imageMode(CENTER);
image(fire, mouseX, mouseY, fireW, fireH);
imageMode(CORNER);
break;
case GAME_STOP:
// 點擊鍵盤重啟遊戲
if(keyPressed) {
gameState = GAME_RUN;
balloonY = 0;
for(int i = 0; i < hills.length; i++){
hills[i] = floor(random(4));
hillX[i] = i * 200;
}
}
break;
}
// DO NOT REMOVE OR EDIT THE FOLLOWING 3 LINES
if (debugMode) {
popMatrix();
}
}
void mouseClicked() {
if (gameState == GAME_RUN){
// 點擊滑鼠熱氣球上升 50px
balloonY -= 50;
}
}
void keyPressed() {
// DO NOT REMOVE OR EDIT THE FOLLOWING SWITCH/CASES
switch(key) {
case 'a':
if(cameraOffsetX < 0) {
debugMode = true;
cameraOffsetX += 25;
}
break;
case 'd':
if(cameraOffsetX > -width) {
debugMode = true;
cameraOffsetX -= 25;
}
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment