Advertisement
vmars316

~paddle-main-NOW.js

Mar 10th, 2016
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.11 KB | None | 0 0
  1. (function () {
  2. // programe.name = ~paddle-main-NOW.js
  3. "use strict";
  4. // http://www.w3schools.com/js/tryit.asp?filename=tryjs_object_function
  5. var red = 153;
  6. var green = 45;
  7. var blue = 45;
  8. var truth01Trips = 0; // max 6
  9. var truth02Trips = 0; // max 4
  10. var truth03Trips = 0; // max 7
  11. var truth04Trips = 0; // max 5
  12. var restartBTN, pauseBTN, playBTN, quitBTN; //position;
  13. var ball, compound, manufacturer, thrower;
  14. var truth01, truth02, truth03, truth04;
  15. var lies01, lies02, lies03, lies04;
  16. var totalScore = 0; var oopsScore = 0; var goodHits = 0; var totalShots = 0;
  17.  
  18. // imports
  19. var CommandEnum = com.dgsprb.quick.CommandEnum;
  20. var Quick = com.dgsprb.quick.Quick;
  21. var GameObject = com.dgsprb.quick.GameObject;
  22. var Rect = com.dgsprb.quick.Rect;
  23. var ImageFactory = com.dgsprb.quick.ImageFactory;
  24. var Scene = com.dgsprb.quick.Scene;
  25. // var Text = com.dgsprb.quick.Text;
  26.  
  27. // static
  28. function main() {
  29. Quick.setAutoScale(false);
  30. Quick.setName("Lies&Cowpies");
  31. Quick.init(function () { return new FirstScene() });
  32. // var position;
  33. // position = restartBtn.pointer.getPosition();
  34. // restartBTN = position.getX(); alert('restartBTN = ' + restartBTN);
  35. }
  36.  
  37. var Background = (function () {
  38. function Background() {
  39. GameObject.call(this);
  40. this.setColor("rgb(" + red + "," + green + "," + blue + ")");
  41. this.setWidth(Quick.getWidth());
  42. this.setHeight(Quick.getHeight());
  43. };
  44. Background.prototype = Object.create(GameObject.prototype);
  45.  
  46. return Background;
  47. })();
  48.  
  49. var RestartBtn = (function () { // RestartBtn class namespace
  50. function RestartBtn() { // RestartBtn class constructor method
  51. GameObject.call(this); // call the constructor from the superclass
  52. this.addTag("restartBtn");
  53. this.setImageId("restartBtn"); // setImageId, a method inherited from Sprite
  54. this.pointer = Quick.getPointer(); // pointer is a member property of RestartBtn
  55. this.setBoundary(new Rect(100, 570, 100, 30)); // a method of Sprite, to set this boundaries
  56. this.setEssential();
  57. this.setBottom(600); this.setLeft(100) ; this.setTop(572);
  58. } RestartBtn.prototype = Object.create(GameObject.prototype);
  59.  
  60. RestartBtn.prototype.update = function () {
  61. if (this.pointer.getPush()) {
  62. // if (GameObject.hasTag("restartBtn")) { // doesnt work
  63. // if (this.hasTag("restartBtn")) { // doesnt work
  64. if (RestartBtn.hasTag("restartBtn")) {
  65. alert('restartBTN = ' + restartBTN);
  66. };
  67. };
  68. }
  69. return RestartBtn; // finally publishes the class to the outer scope
  70. })();
  71.  
  72. var PauseBtn = (function () { // PauseBtn class namespace
  73. function PauseBtn() { // PauseBtn class constructor method
  74. GameObject.call(this); // call the constructor from the superclass
  75. this.addTag("pauseBtn");
  76. this.setImageId("pauseBtn"); // setImageId, a method inherited from Sprite
  77. this.pointer = Quick.getPointer(); // pointer is a member property of PauseBtn
  78. this.setBoundary(new Rect(210, 570, 100, 30)); // a method of Sprite, to set the boundaries of PauseBtn
  79. // this.setSolid();
  80. 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.
  81. this.setBottom(600); this.setLeft(210); this.setTop(572);
  82. }
  83. PauseBtn.prototype = Object.create(GameObject.prototype);
  84.  
  85. PauseBtn.prototype.update = function () {
  86. if (this.pointer.getPush()) {
  87. if (GameObject.hasTag("pauseBtn")) {
  88. alert('pauseBTN = ' + pauseBTN);
  89. var position = this.pointer.getPosition();
  90. alert('pauseBtn position = ' + position.getX());
  91. console.log('pauseBtn position' + position);
  92. console.log('pauseBtn position = ' + position.getX());
  93. };
  94. };
  95. }
  96. return PauseBtn; // finally publishes the class to the outer scope
  97. })();
  98. var PlayBtn = (function () { // PlayBtn class namespace
  99. function PlayBtn() { // PlayBtn class constructor method
  100. GameObject.call(this); // call the constructor from the superclass
  101. this.addTag("playBtn");
  102. this.setImageId("playBtn"); // setImageId, a method inherited from Sprite
  103. this.pointer = Quick.getPointer(); // pointer is a member property of PlayBtn
  104. // getPointer is a static method of Quick
  105. this.setBoundary(new Rect(320, 570, 100, 30)); // a method of Sprite, to set the boundaries of PlayBtn
  106. // this.setSolid();
  107. 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.
  108. this.setBottom(600); this.setLeft(320); this.setTop(572);
  109. }
  110. PlayBtn.prototype = Object.create(GameObject.prototype);
  111.  
  112. PlayBtn.prototype.update = function () {
  113. if (this.pointer.getPush()) {
  114. if (GameObject.hasTag("playBtn")) {
  115. alert('playBtn = ' + playBtn);
  116. };
  117. };
  118. }
  119. return PlayBtn; // finally publishes the class to the outer scope
  120. })();
  121. var QuitBtn = (function () { // QuitBtn class namespace
  122. function QuitBtn() { // QuitBtn class constructor method
  123. GameObject.call(this); // call the constructor from the superclass
  124. this.addTag("pauseBtn");
  125. this.setImageId("pauseBtn"); // setImageId, a method inherited from Sprite
  126. this.pointer = Quick.getPointer(); // pointer is a member property of QuitBtn
  127. // getPointer is a static method of Quick
  128. this.setBoundary(new Rect(430, 570, 100, 30)); // a method of Sprite, to set the boundaries of QuitBtn
  129. // this.setSolid();
  130. 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.
  131. this.setBottom(600); this.setLeft(430); this.setTop(572);
  132. }
  133. QuitBtn.prototype = Object.create(GameObject.prototype);
  134.  
  135. QuitBtn.prototype.update = function () {
  136. if (this.pointer.getPush()) {
  137. // if (GameObject.hasTag("quitBtn")) {
  138. if (this.hasTag("quitBtn")) {
  139. alert('quitBtn = ' + quitBtn);
  140. };
  141. };
  142. }
  143. return QuitBtn; // finally publishes the class to the outer scope
  144. })();
  145.  
  146. var Compound = (function () { // Compound class namespace
  147. function Compound() { // Compound class constructor method
  148. GameObject.call(this); // call the constructor from the superclass
  149. this.setImageId("bgCompound"); // setImageId, a method inherited from Sprite
  150. this.pointer = Quick.getPointer(); // pointer is a member property of Compound
  151. // getPointer is a static method of Quick
  152. this.setBoundary(new Rect(0, 0, Quick.getWidth(), Quick.getHeight())); // a method of Sprite, to set the boundaries of Compound
  153. 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.
  154. this.setBottom(Quick.getHeight() - this.getHeight());
  155. this.setLeft(0); this.setTop(378);
  156. }
  157. Compound.prototype = Object.create(GameObject.prototype);
  158. return Compound; // finally publishes the class to the outer scope
  159. })();
  160. var Manufacturer = (function () { // Manufacturer class namespace
  161. function Manufacturer() { // Manufacturer class constructor method
  162. GameObject.call(this); // call the constructor from the superclass
  163. this.setImageId("manufacturer"); //setImageId, a method inherited from Sprite
  164. this.pointer = Quick.getPointer(); // pointer is a member property of Manufacturer
  165. // getPointer is a static method of Quick
  166. this.setBoundary(new Rect(0, 0, Quick.getWidth(), Quick.getHeight())); // a method of Sprite, to set the boundaries of Manufacturer
  167. 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.
  168. this.setBottom(Quick.getHeight() - this.getHeight());
  169. this.setLeft(618); this.setTop(468);
  170. }
  171. Manufacturer.prototype = Object.create(GameObject.prototype); // this says the Ball class
  172. return Manufacturer; // finally publishes the class to the outer scope
  173. })(); //
  174. 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
  175. function Ball() { // Ball class constructor method - what happens when "new Ball()" is issued
  176. GameObject.call(this); // call the constructor from the superclass - this is done automatically in Java
  177. this.setImageId("cowpieSprite"); // setImageId is a method inherited from Sprite - correct!
  178. this.pointer = Quick.getPointer(); // pointer is a member property of Ball
  179. // 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!
  180. this.setBoundary(new Rect(0, 0, Quick.getWidth(), Quick.getHeight())); // a method of Sprite, to set the boundaries of Ball
  181. // 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.
  182. this.setSolid(); // a method of GameObject , so Ball can collide with other solid objects - correct!
  183. this.setBottom(Quick.getHeight() - this.getHeight());
  184. this.setTop(500);
  185. // this.controller = Quick.getController();
  186. };
  187. Ball.prototype = Object.create(GameObject.prototype); // this says the Ball class inherits from GameObject - reads just like "extends GameObject" in Java
  188. // 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
  189. //
  190. 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
  191. var collision = this.getCollision(GameObject); // a method of Ball Rect to get direction
  192. if (GameObject.hasTag("lies02")) {
  193. lies02.setLeft(0); truth02.setSpeedX(0); lies02.setVisible(false);
  194. truth02.setVisible(true); truth02.setSpeedX(4);
  195. Quick.play("pingSound"); // calls a static method from Quick class
  196. goodHits ++ ; totalScore = ((goodHits * 3) - (oopsScore * 2)); // each goodHit = 3 points , each oopsHits = -2 points
  197. this.expire();
  198. };
  199. if (GameObject.hasTag("truth02")) { // "returns true if object contains the given tag"
  200. // truth02.setVisible(true); truth02.setSpeedX(3);
  201. Quick.play("pingSound"); // calls a static method from Quick class
  202. Quick.play("pongSound"); // calls a static method from Quick class
  203. oopsScore ++ ; totalScore = ((goodHits * 3) - (oopsScore * 2));
  204. this.expire();
  205. };
  206. document.getElementById('totalScore').innerHTML = totalScore;
  207. document.getElementById('oopsScore').innerHTML = oopsScore;
  208. document.getElementById('goodHits').innerHTML = goodHits;
  209. document.getElementById('totalShots').innerHTML = totalShots;
  210. }; // endof Ball.prototype.onCollision
  211. // override
  212. return Ball; // finally publishes the class to the outer scope
  213. })();
  214.  
  215. var Truth = (function () {
  216. function Truth() {
  217. GameObject.call(this);
  218. this.setImageId("truth02Sprite");
  219. this.setBoundary(new Rect(0, 0, Quick.getWidth(), Quick.getHeight()));
  220. // this.setEssential();
  221. this.setSolid();
  222. this.setLeft(0);
  223. this.setTop(120);
  224. this.setSpeedX(4);
  225. };
  226. Truth.prototype = Object.create(GameObject.prototype);
  227.  
  228. Truth.prototype.offBoundary = function (GameObject) {
  229. if (this.hasTag("truth02")) {
  230. truth02Trips += 1; // increment by 1
  231. if(truth02Trips > 2) { truth02Trips = 0;
  232. truth02.setLeft(0); truth02.setVisible(false); truth02.setSpeedX(0);
  233. };
  234. Quick.play("pingSound"); // calls a static method from Quick class
  235. if(truth02.getVisible() )
  236. { this.bounceX(); // for the horizontal axis
  237. this.setImage(ImageFactory.mirror(this.getImage()));
  238. };
  239. if(!truth02.getVisible() ) {
  240. lies02.setLeft(0); lies02.setVisible(true); lies02.setSpeedX(4);
  241. };
  242. };
  243. }
  244. return Truth;
  245.  
  246. })();
  247. //
  248. var Lies = (function () {
  249. function Lies() {
  250. GameObject.call(this);
  251. this.setImageId("lies02Sprite");
  252. this.setBoundary(new Rect(0, 0, Quick.getWidth(), Quick.getHeight()));
  253. // this.setEssential();
  254. this.setSolid();
  255. this.setLeft(0);
  256. this.setTop(120);
  257. this.setSpeedX(4);
  258. };
  259. Lies.prototype = Object.create(GameObject.prototype);
  260. Lies.prototype.offBoundary = function (GameObject) {
  261. this.bounceX(); // for the horizontal axis
  262. this.setImage(ImageFactory.mirror(this.getImage()));
  263. };
  264. return Lies;
  265. })();
  266. //
  267. var FirstScene = (function () {
  268. function FirstScene() {
  269. Scene.call(this);
  270. var position;
  271. this.add(new Background());
  272. var compound = new Compound();
  273. this.add(compound);
  274. manufacturer = new Manufacturer();
  275. this.add(manufacturer);
  276.  
  277. var restartBtn = new RestartBtn();
  278. this.add(restartBtn);
  279. restartBtn.addTag("restartBtn");
  280.  
  281. var pauseBtn = new PauseBtn();
  282. this.add(pauseBtn);
  283. pauseBtn.addTag("pauseBtn");
  284. position = pauseBtn.pointer.getPosition();
  285. pauseBTN = position.getX();
  286.  
  287. var playBtn = new PlayBtn();
  288. this.add(playBtn);
  289. playBtn.addTag("playBtn");
  290. position = playBtn.pointer.getPosition();
  291. playBTN = position.getX();
  292.  
  293. var quitBtn = new QuitBtn();
  294. this.add(quitBtn);
  295. quitBtn.addTag("quitBtn");
  296. position = quitBtn.pointer.getPosition();
  297. quitBTN = position.getX();
  298.  
  299. truth02 = new Truth();
  300. this.add(truth02);
  301. truth02.addTag("truth02");
  302. truth02.setImageId("truth02Sprite");
  303. truth02.setLeft(0);
  304. truth02.setTop(120);
  305. truth02.setSpeedX(9);
  306. lies02 = new Lies();
  307. this.add(lies02);
  308. lies02.addTag("lies02");
  309. lies02.setImageId("lies02Sprite");
  310. lies02.setVisible(false);
  311. lies02.setLeft(0) // (-64);
  312. lies02.setTop(120);
  313. lies02.setSpeedX(0);
  314. //
  315. var ball = new Ball();
  316. thrower = new Thrower();
  317. this.add(thrower);
  318. // var cursorPoint = new Cursor();
  319. // this.add(cursorPoint);
  320. // var spark = new Spark();
  321. };
  322. FirstScene.prototype = Object.create(Scene.prototype);
  323.  
  324. // override
  325. FirstScene.prototype.getNext = function () {
  326. alert('FirstScene.prototype.getNext = function ()');
  327. return new FirstScene();
  328. };
  329.  
  330. return FirstScene;
  331.  
  332. })();
  333. //
  334. var Thrower = (function () {
  335. function Thrower() {
  336. GameObject.call(this);
  337. this.addTag("thrower");
  338. this.controller = Quick.getController();
  339. this.setBoundary(new Rect(0, 0, Quick.getWidth(), Quick.getHeight()));
  340. this.pointer = Quick.getPointer();
  341. // this.setSolid();
  342. this.setImageId("throwerSprite");
  343. this.setEssential();
  344. this.setCenterX(Quick.getWidth() / 2);
  345. this.setBottom(Quick.getHeight() - this.getHeight());
  346. this.setTop(508);
  347. };
  348. Thrower.prototype = Object.create(GameObject.prototype);
  349.  
  350. Thrower.prototype.fire = function () {
  351. var ball = new Ball(); // create a brand new ball to be thrown
  352. var scene = this.getScene(); // ask for the scene the avatar is
  353. scene.add(ball); // add the ball to the same scene I am
  354. if (ball) {ball.setX(this.getX()) }; // within your Thrower update method
  355. totalShots ++ ;
  356. document.getElementById('totalScore').innerHTML = totalScore;
  357. document.getElementById('oopsScore').innerHTML = oopsScore;
  358. document.getElementById('goodHits').innerHTML = goodHits;
  359. document.getElementById('totalShots').innerHTML = totalShots;
  360. ball.setSpeedY(-9);
  361. };
  362.  
  363. Thrower.prototype.update = function () {
  364. if (this.controller.keyDown(CommandEnum.LEFT) && this.getLeft() > 0) {
  365. this.moveX(-8);
  366. }
  367. if (this.controller.keyDown(CommandEnum.RIGHT) && this.getRight() < Quick.getWidth()) {
  368. this.moveX(8);
  369. }
  370. if (this.controller.keyPush(CommandEnum.UP) || this.controller.keyPush(CommandEnum.A))
  371. { // added UP to A (space bar, fire button) because it is a more traditional fire key
  372. this.fire(); // call the method we defined above
  373. }
  374.  
  375. }
  376. Thrower.prototype.offBoundary = function (GameObject) {
  377. this.setCenterX(Quick.getWidth() / 2);
  378. };
  379.  
  380. return Thrower;
  381. })();
  382.  
  383. //}
  384.  
  385. main();
  386.  
  387. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement