Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function () {
- "use strict";
- // http://www.w3schools.com/js/tryit.asp?filename=tryjs_object_function
- var red = 153;
- var green = 45;
- var blue = 45;
- var truth01Trips = 0; // max 6
- var truth02Trips = 0; // max 4
- var truth03Trips = 0; // max 7
- var truth04Trips = 0; // max 5
- var ball, compound, manufacturer, thrower;
- var truth01, truth02, truth03, truth04;
- var lies01, lies02, lies03, lies04;
- var Cursor;
- var totalScore = 0; var oopsScore = 0; var goodHits = 0; var totalShots = 0;
- // imports
- var CommandEnum = com.dgsprb.quick.CommandEnum;
- var Quick = com.dgsprb.quick.Quick;
- var GameObject = com.dgsprb.quick.GameObject;
- var Rect = com.dgsprb.quick.Rect;
- var ImageFactory = com.dgsprb.quick.ImageFactory;
- var Scene = com.dgsprb.quick.Scene;
- // var Text = com.dgsprb.quick.Text;
- // static
- function main() {
- Quick.setAutoScale(false);
- Quick.setName("Lies&Cowpies");
- Quick.init(function () { return new FirstScene() });
- }
- var Background = (function () {
- function Background() {
- GameObject.call(this);
- this.setColor("rgb(" + red + "," + green + "," + blue + ")");
- this.setWidth(Quick.getWidth());
- this.setHeight(Quick.getHeight());
- };
- Background.prototype = Object.create(GameObject.prototype);
- return Background;
- })();
- var RestartBtn = (function () { // RestartBtn class namespace
- function RestartBtn() { // RestartBtn class constructor method
- GameObject.call(this); // call the constructor from the superclass
- this.setImageId("restartBtn"); // setImageId, a method inherited from Sprite
- this.pointer = Quick.getPointer(); // pointer is a member property of RestartBtn
- this.setBoundary(new Rect(100, 570, 100, 30)); // a method of Sprite, to set this boundaries
- this.setSolid();
- this.setEssential();
- this.setBottom(600); this.setLeft(100) ; this.setTop(572);
- }
- RestartBtn.prototype = Object.create(GameObject.prototype);
- return RestartBtn; // finally publishes the class to the outer scope
- })();
- var PauseBtn = (function () { // PauseBtn class namespace
- function PauseBtn() { // PauseBtn class constructor method
- GameObject.call(this); // call the constructor from the superclass
- this.setImageId("pauseBtn"); // setImageId, a method inherited from Sprite
- this.pointer = Quick.getPointer(); // pointer is a member property of PauseBtn
- this.setBoundary(new Rect(210, 570, 100, 30)); // a method of Sprite, to set the boundaries of PauseBtn
- this.setSolid();
- this.setEssential(); // a method of GameObject, sets the PauseBtn object as essential to its Scene, that if this object expires, the scene will expire too.
- this.setBottom(600); this.setLeft(210); this.setTop(572);
- }
- PauseBtn.prototype = Object.create(GameObject.prototype);
- return PauseBtn; // finally publishes the class to the outer scope
- })();
- var PlayBtn = (function () { // PlayBtn class namespace
- function PlayBtn() { // PlayBtn class constructor method
- GameObject.call(this); // call the constructor from the superclass
- this.setImageId("playBtn"); // setImageId, a method inherited from Sprite
- this.pointer = Quick.getPointer(); // pointer is a member property of PlayBtn
- // getPointer is a static method of Quick
- this.setBoundary(new Rect(320, 570, 100, 30)); // a method of Sprite, to set the boundaries of PlayBtn
- this.setSolid();
- this.setEssential(); // a method of GameObject, sets the PlayBtn object as essential to its Scene, that if this object expires, the scene will expire too.
- this.setBottom(600); this.setLeft(320); this.setTop(572);
- }
- PlayBtn.prototype = Object.create(GameObject.prototype);
- return PlayBtn; // finally publishes the class to the outer scope
- })();
- var QuitBtn = (function () { // QuitBtn class namespace
- function QuitBtn() { // QuitBtn class constructor method
- GameObject.call(this); // call the constructor from the superclass
- this.setImageId("quitBtn"); // setImageId, a method inherited from Sprite
- this.pointer = Quick.getPointer(); // pointer is a member property of QuitBtn
- // getPointer is a static method of Quick
- this.setBoundary(new Rect(430, 570, 100, 30)); // a method of Sprite, to set the boundaries of QuitBtn
- this.setSolid();
- this.setEssential(); // a method of GameObject, sets the QuitBtn object as essential to its Scene, that if this object expires, the scene will expire too.
- this.setBottom(600); this.setLeft(430); this.setTop(572);
- }
- QuitBtn.prototype = Object.create(GameObject.prototype);
- return QuitBtn; // finally publishes the class to the outer scope
- })();
- var Compound = (function () { // Compound class namespace
- function Compound() { // Compound class constructor method
- GameObject.call(this); // call the constructor from the superclass
- this.setImageId("bgCompound"); // setImageId, a method inherited from Sprite
- this.pointer = Quick.getPointer(); // pointer is a member property of Compound
- // getPointer is a static method of Quick
- this.setBoundary(new Rect(0, 0, Quick.getWidth(), Quick.getHeight())); // a method of Sprite, to set the boundaries of Compound
- this.setEssential(); // a method of GameObject, sets the Compound object as essential to its Scene, that if this object expires, the scene will expire too.
- this.setBottom(Quick.getHeight() - this.getHeight());
- this.setLeft(0); this.setTop(378);
- }
- Compound.prototype = Object.create(GameObject.prototype);
- return Compound; // finally publishes the class to the outer scope
- })();
- var Manufacturer = (function () { // Manufacturer class namespace
- function Manufacturer() { // Manufacturer class constructor method
- GameObject.call(this); // call the constructor from the superclass
- this.setImageId("manufacturer"); //setImageId, a method inherited from Sprite
- this.pointer = Quick.getPointer(); // pointer is a member property of Manufacturer
- // getPointer is a static method of Quick
- this.setBoundary(new Rect(0, 0, Quick.getWidth(), Quick.getHeight())); // a method of Sprite, to set the boundaries of Manufacturer
- this.setEssential(); // a method of GameObject, sets the Manufacturer object as essential to its Scene, that if this object expires, the scene will expire too.
- this.setBottom(Quick.getHeight() - this.getHeight());
- this.setLeft(618); this.setTop(468);
- }
- Manufacturer.prototype = Object.create(GameObject.prototype); // this says the Ball class
- return Manufacturer; // finally publishes the class to the outer scope
- })(); //
- var Ball = (function () { // Ball class namespace - this reads just like "class Ball" in Java and encompasses the class definition until the last line of this excerpt
- function Ball() { // Ball class constructor method - what happens when "new Ball()" is issued
- GameObject.call(this); // call the constructor from the superclass - this is done automatically in Java
- this.setImageId("cowpieSprite"); // setImageId is a method inherited from Sprite - correct!
- this.pointer = Quick.getPointer(); // pointer is a member property of Ball
- // getPointer is a static method of Quick, I guess a static method is just a method of Quick, which is a static class whose public members are accessible without creating an instance of that class. - you nailed it!
- this.setBoundary(new Rect(0, 0, Quick.getWidth(), Quick.getHeight())); // a method of Sprite, to set the boundaries of Ball
- // this.setEssential(); // a method of GameObject, sets the Ball object as essential to its Scene, Essential to its Scene -means that if this object expires, the scene will expire too.
- this.setSolid(); // a method of GameObject , so Ball can collide with other solid objects - correct!
- this.setBottom(Quick.getHeight() - this.getHeight());
- this.setTop(500);
- // this.controller = Quick.getController();
- };
- Ball.prototype = Object.create(GameObject.prototype); // this says the Ball class inherits from GameObject - reads just like "extends GameObject" in Java
- // override - this comment means the following method overrides (rewrites) the method with the same name defined in the super class (in this case, GameObject) and though it's not necessary to put that comment there it can be useful for code readers
- //
- Ball.prototype.onCollision = function (gameObject) { // still no instance of Ball here, just class method definition - no instance of this class is created until "new Ball()" is issued
- var collision = this.getCollision(gameObject); // a method of Ball Rect to get direction
- if (gameObject.hasTag("lies02")) {
- lies02.setLeft(0); truth02.setSpeedX(0); lies02.setVisible(false);
- truth02.setVisible(true); truth02.setSpeedX(4);
- Quick.play("pingSound"); // calls a static method from Quick class
- goodHits ++ ; totalScore = ((goodHits * 3) - (oopsScore * 2)); // each goodHit = 3 points , each oopsHits = -2 points
- this.expire();
- };
- if (gameObject.hasTag("truth02")) { // "returns true if object contains the given tag"
- // truth02.setVisible(true); truth02.setSpeedX(3);
- Quick.play("pingSound"); // calls a static method from Quick class
- Quick.play("pongSound"); // calls a static method from Quick class
- oopsScore ++ ; totalScore = ((goodHits * 3) - (oopsScore * 2));
- this.expire();
- };
- document.getElementById('totalScore').innerHTML = totalScore;
- document.getElementById('oopsScore').innerHTML = oopsScore;
- document.getElementById('goodHits').innerHTML = goodHits;
- document.getElementById('totalShots').innerHTML = totalShots;
- }; // endof Ball.prototype.onCollision
- // override
- return Ball; // finally publishes the class to the outer scope
- })();
- /*
- Function UpdateScores () {
- // document.getElementById('totalShots').innerHTML = totalScore + oopsScore +goodHits +totalShots ;
- alert('Function UpdateScores');
- };
- */
- var Truth = (function () {
- function Truth() {
- GameObject.call(this);
- this.setImageId("truth02Sprite");
- this.setBoundary(new Rect(0, 0, Quick.getWidth(), Quick.getHeight()));
- // this.setEssential();
- this.setSolid();
- this.setLeft(0);
- this.setTop(120);
- this.setSpeedX(4);
- };
- Truth.prototype = Object.create(GameObject.prototype);
- Truth.prototype.offBoundary = function (gameObject) {
- if (this.hasTag("truth02")) {
- truth02Trips += 1; // increment by 1
- if(truth02Trips > 2) { truth02Trips = 0;
- truth02.setLeft(0); truth02.setVisible(false); truth02.setSpeedX(0);
- };
- Quick.play("pingSound"); // calls a static method from Quick class
- if(truth02.getVisible() )
- { this.bounceX(); // for the horizontal axis
- this.setImage(ImageFactory.mirror(this.getImage()));
- };
- if(!truth02.getVisible() ) {
- lies02.setLeft(0); lies02.setVisible(true); lies02.setSpeedX(4);
- };
- };
- }
- return Truth;
- })();
- //
- var Lies = (function () {
- function Lies() {
- GameObject.call(this);
- this.setImageId("lies02Sprite");
- this.setBoundary(new Rect(0, 0, Quick.getWidth(), Quick.getHeight()));
- // this.setEssential();
- this.setSolid();
- this.setLeft(0);
- this.setTop(120);
- this.setSpeedX(4);
- };
- Lies.prototype = Object.create(GameObject.prototype);
- Lies.prototype.offBoundary = function (gameObject) {
- this.bounceX(); // for the horizontal axis
- this.setImage(ImageFactory.mirror(this.getImage()));
- };
- return Lies;
- })();
- //
- var FirstScene = (function () {
- function FirstScene() {
- Scene.call(this);
- this.add(new Background());
- var compound = new Compound();
- this.add(compound);
- manufacturer = new Manufacturer();
- this.add(manufacturer);
- var restartBtn = new RestartBtn();
- this.add(restartBtn);
- restartBtn.addTag("restartBtn");
- var pauseBtn = new PauseBtn();
- this.add(pauseBtn);
- pauseBtn.addTag("pauseBtn");
- var playBtn = new PlayBtn();
- this.add(playBtn);
- playBtn.addTag("playBtn");
- var quitBtn = new QuitBtn();
- this.add(quitBtn);
- quitBtn.addTag("quitBtn");
- manufacturer = new Manufacturer();
- this.add(manufacturer);
- truth02 = new Truth();
- this.add(truth02);
- truth02.addTag("truth02");
- truth02.setImageId("truth02Sprite");
- truth02.setLeft(0);
- truth02.setTop(120);
- truth02.setSpeedX(9);
- lies02 = new Lies();
- this.add(lies02);
- lies02.addTag("lies02");
- lies02.setImageId("lies02Sprite");
- lies02.setVisible(false);
- lies02.setLeft(0) // (-64);
- lies02.setTop(120);
- lies02.setSpeedX(0);
- //
- var ball = new Ball();
- thrower = new Thrower();
- this.add(thrower);
- var cursorPoint = new Cursor();
- this.add(cursorPoint);
- var spark = new Spark();
- };
- FirstScene.prototype = Object.create(Scene.prototype);
- // override
- FirstScene.prototype.getNext = function () {
- return new FirstScene();
- };
- return FirstScene;
- })();
- //
- var Cursor = (function () {
- function Cursor() {
- GameObject.call(this);
- this.addTag("pointerSprite");
- this.controller = Quick.getController();
- this.setBoundary(new Rect(0, 600, Quick.getWidth(), 30)); // a method of Sprite,set Boundary
- this.pointer = Quick.getPointer();
- this.setImageId("pointerSprite");
- this.setEssential();
- this.setBottom(600); this.setLeft(50); this.setTop(570);
- };
- Cursor.prototype = Object.create(GameObject.prototype);
- Cursor.prototype.fire = function () {
- var spark = new Spark(); // create a brand new spark to be thrown
- var scene = this.getScene(); // ask for the scene the avatar is
- scene.add(spark); // add the spark to the same scene I am
- if (spark) {spark.setX(this.getX()) };
- spark.setSpeedY(1);
- };
- Cursor.prototype.update = function () {
- var mousePos = this.pointer.getPosition();
- this.setPosition(mousePos);
- if (this.controller.keyPush(CommandEnum.DOWN) || this.controller.keyPush(CommandEnum.A))
- {
- this.fire(); // call the method we defined above
- };
- this.setCenterY(587);
- };
- Cursor.prototype.offBoundary = function (gameObject) {
- this.setCenterY(587);
- };
- return Cursor;
- })();
- var Spark = (function () { // Spark class namespace
- function Spark() { // Spark class constructor method
- GameObject.call(this); // call the constructor from the superclass
- this.setImageId("sparkSprite"); // setImageId,a method inherited from Sprite
- this.pointer = Quick.getPointer(); // pointer is a member property of Spark
- this.setBoundary(new Rect(0, 600, Quick.getWidth(), 30)); // a method of Sprite,set
- this.setSolid(); // a method of GameObject , so Spark can collide with other solid objects
- this.setBottom(600); this.setLeft(50); this.setTop(565);
- this.controller = Quick.getController();
- };
- Spark.prototype = Object.create(GameObject.prototype);
- Spark.prototype.onCollision = function (gameObject) {
- var collision = this.getCollision(gameObject); // get direction at collision
- if (gameObject.hasTag("restartBtn")) { // returns true if object contains given tag
- Quick.play("pongSound"); // calls a static method from Quick class
- // alert('restartBtn clicked');
- };
- if (gameObject.hasTag("pauseBtn")) { // returns true if object contains given tag
- Quick.play("pongSound"); // calls a static method from Quick class
- };
- if (gameObject.hasTag("playBtn")) { // returns true if object contains given tag
- Quick.play("pongSound"); // calls a static method from Quick class
- };
- if (gameObject.hasTag("quitBtn")) { // returns true if object contains given tag
- Quick.play("pongSound"); // calls a static method from Quick class
- };
- };
- return Spark; // finally publishes the class to the outer scope
- })();
- //
- var Thrower = (function () {
- function Thrower() {
- GameObject.call(this);
- this.addTag("thrower");
- this.controller = Quick.getController();
- this.setBoundary(new Rect(0, 0, Quick.getWidth(), Quick.getHeight()));
- this.pointer = Quick.getPointer();
- // this.setSolid();
- this.setImageId("throwerSprite");
- this.setEssential();
- this.setCenterX(Quick.getWidth() / 2);
- this.setBottom(Quick.getHeight() - this.getHeight());
- this.setTop(508);
- };
- Thrower.prototype = Object.create(GameObject.prototype);
- Thrower.prototype.fire = function () {
- var ball = new Ball(); // create a brand new ball to be thrown
- var scene = this.getScene(); // ask for the scene the avatar is
- scene.add(ball); // add the ball to the same scene I am
- if (ball) {ball.setX(this.getX()) }; // within your Thrower update method
- totalShots ++ ;
- document.getElementById('totalScore').innerHTML = totalScore;
- document.getElementById('oopsScore').innerHTML = oopsScore;
- document.getElementById('goodHits').innerHTML = goodHits;
- document.getElementById('totalShots').innerHTML = totalShots;
- ball.setSpeedY(-9);
- };
- Thrower.prototype.update = function () {
- if (this.controller.keyDown(CommandEnum.LEFT) && this.getLeft() > 0) {
- this.moveX(-8);
- }
- if (this.controller.keyDown(CommandEnum.RIGHT) && this.getRight() < Quick.getWidth()) {
- this.moveX(8);
- }
- if (this.controller.keyPush(CommandEnum.UP) || this.controller.keyPush(CommandEnum.A))
- { // added UP to A (space bar, fire button) because it is a more traditional fire key
- this.fire(); // call the method we defined above
- }
- }
- Thrower.prototype.offBoundary = function (gameObject) {
- this.setCenterX(Quick.getWidth() / 2);
- };
- return Thrower;
- })();
- //}
- main();
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement