123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- var blockSize = 55;
- var numBubbles = 5;
- var board = new Array(numBubbles);
- var component;
- var gameStarted = false;
- var bubbleStep = 5;
- var bubblesClicked = 0;
- var updateTime = 200
- var score = 0;
- var startTime = 0;
- function onUpdate()
- {
- var step = 5;
- score = (new Date() - startTime)/1000;
- // %2.02f
- var p1 = Math.floor(score)
- var p2 = Math.floor(100*(score - p1))
- scoreText.text = "Time: " + p1 + "." + p2 + " ";
- for (var n = 0; n < numBubbles; n++)
- {
- var x = board[n].x + board[n].dx;
- var y = board[n].y + board[n].dy;
- if(x > (background.width - board[n].width) || x < 0)
- {
- board[n].dx = -board[n].dx;
- x = x + 2*board[n].dx;
- }
- if(y > (background.height - board[n].height) || y < 0)
- {
- board[n].dy = -board[n].dy;
- y = y + 2*board[n].dy;
- }
- board[n].x = x;
- board[n].y = y;
- }
- }
- function removeBubble(mx,my)
- {
- if(!gameStarted)
- return;
- for (var n = 0; n < numBubbles; n++)
- {
- if(board[n].visible)
- {
- if( (mx > board[n].x) && (mx < (board[n].x + board[n].width)) &&
- (my > board[n].y) && (my < (board[n].y + board[n].height)) )
- {
- board[n].visible = false;
- ++bubblesClicked;
- //bubblepop.play();
- break;
- }
- }
- }
- if(bubblesClicked >= numBubbles)
- {
- gameOverDialog.show("Congratulations !\n" +
- "You popped all bubbles in\n" +
- score + " seconds !");
- newGameBut.text = " Start "
- gameStarted = false;
- bubblesClicked = 0;
- gameTimer.stop();
- }
- }
- function aboutGame()
- {
- aboutDialog
- aboutDialog.show("Bubble kid by:\n" +
- "Marcelo Barros de Almeida\n" +
- "<marcelobarrosalmeida@gmail.com>");
- }
- function stopGame()
- {
- //Delete blocks from previous game
- for (var i = 0; i < numBubbles; i++)
- {
- if (board[i] != null)
- board[i].destroy();
- }
- newGameBut.text = " Start "
- gameStarted = false;
- bubblesClicked = 0;
- gameTimer.stop();
- }
- function startNewGame()
- {
- if(gameStarted)
- {
- stopGame();
- }
- else
- {
- //Initialize Board
- board = new Array(numBubbles);
- for (var n = 0; n < numBubbles; n++)
- {
- board[n] = null;
- createBlock(n);
- }
- bubblesClicked = 0;
- gameTimer.start();
- gameStarted = true;
- newGameBut.text = " Stop ";
- startTime = new Date();
- score = 0;
- }
- }
- function createBlock(n)
- {
- if (component == null)
- component = Qt.createComponent("Bubble.qml");
- // Note that if Block.qml was not a local file, component.status would be
- // Loading and we should wait for the component's statusChanged() signal to
- // know when the file is downloaded and ready before calling createObject().
- if (component.status == Component.Ready)
- {
- var obj = component.createObject(background);
- if (obj == null) {
- console.log("error creating bubble");
- console.log(component.errorString());
- return false;
- }
- obj.index = n;
- obj.width = blockSize;
- obj.height = blockSize;
- obj.x = Math.random()*(background.width-blockSize);
- obj.y = Math.random()*(background.height-blockSize);
- obj.dx = Math.random() > 0.5 ? bubbleStep : -bubbleStep;
- obj.dy = Math.random() > 0.5 ? bubbleStep : -bubbleStep;
- board[n] = obj;
- }
- else
- {
- console.log("error loading bubble component");
- console.log(component.errorString());
- return false;
- }
- return true;
- }
- function quitGame()
- {
- Qt.quit()
- }
- function setGameSpeed(s)
- {
- var old = gameStarted;
- if(gameStarted)
- startNewGame();
- if(s == 0)
- updateTime = 200;
- else if(s == 1)
- updateTime = 75;
- else if(s == 2)
- updateTime = 25;
- gameTimer.interval = updateTime;
- if(old)
- startNewGame();
- }
- function getUpdateTime()
- {
- return updateTime;
- }
- function setNumBubbles(n)
- {
- var old = gameStarted;
- if(gameStarted)
- startNewGame();
- if(n == 0)
- numBubbles = 5;
- else if(n == 1)
- numBubbles = 10;
- else if(n == 2)
- numBubbles = 20;
- if(old)
- startNewGame();
- }
- function getNumBubbles()
- {
- return numBubbles;
- }
- function getScore()
- {
- if(gameStarted)
- return score;
- else
- return "--";
- }
|