Guest User

Untitled

a guest
Nov 18th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. MySpaceship S1;
  2. AllienSpaceship A1, A2, A3;
  3.  
  4. float x = 30;
  5. int counter;
  6.  
  7. void setup() {
  8. size(400, 400);
  9. counter = 0;
  10. S1 = new MySpaceship();
  11. A1 = new AllienSpaceship();
  12. A2 = new AllienSpaceship();
  13. A3 = new AllienSpaceship();
  14. }
  15.  
  16. void draw() {
  17. background(0);
  18.  
  19. textSize(10);
  20. fill(#42FC1F);
  21. text(">>SCORE:" + counter, 10, 20);
  22.  
  23. noStroke();
  24. fill(#42FC1F);
  25. rectMode(CORNER);
  26. rect(0, height - 2, width, 2);
  27.  
  28. S1.display();
  29. S1.control();
  30.  
  31. A1.display();
  32. A1.attack();
  33.  
  34. A2.display();
  35. A2.attack();
  36.  
  37. A3.display();
  38. A3.attack();
  39.  
  40. if (mousePressed == true) {
  41. S1.laser();
  42. if (S1.hit(A1)) {
  43. counter += 1;
  44. A1 = new AllienSpaceship();
  45. }
  46.  
  47. if (S1.hit(A2)) {
  48. counter += 1;
  49. A2 = new AllienSpaceship();
  50. }
  51.  
  52. if (S1.hit(A3)) {
  53. counter += 1;
  54. A3 = new AllienSpaceship();
  55. }
  56. }
  57.  
  58. if (A1.win(S1)) {
  59. background(0);
  60. textSize(10);
  61. fill(#42FC1F);
  62. text(">>SCORE:" + counter, width / 2, height / 2);
  63. noLoop();
  64. }
  65.  
  66. if (A2.win(S1)) {
  67. background(0);
  68. textSize(10);
  69. fill(#42FC1F);
  70. text(">>SCORE:" + counter, width / 2, height / 2);
  71. noLoop();
  72. }
  73.  
  74. if (A3.win(S1)) {
  75. background(0);
  76. textSize(10);
  77. fill(#42FC1F);
  78. text(">>SCORE:" + counter, width / 2, height / 2);
  79. noLoop();
  80. }
  81. }
  82.  
  83. class AllienSpaceship {
  84. float x, y, w, h, speedY;
  85.  
  86. AllienSpaceship() {
  87. w = 30;
  88. h = w;
  89. x = random(50, width - 50);
  90. y = h;
  91. speedY = 1;
  92. }
  93.  
  94. void display() {
  95. rectMode(CENTER);
  96. noStroke();
  97. fill(255);
  98.  
  99. rect(x, y, w, 1.6 * h / 5);
  100. rect(x, y + h / 5, w / 1.6, h / 5);
  101. rect(x, y + 2 * h / 5, w / 1.3, h / 5);
  102. fill(#2C0E20);
  103. rect(x, y, w / 10, h / 4);
  104. }
  105.  
  106. void attack() {
  107. x = x + random(- 5, 5);
  108. y = y + speedY;
  109. }
  110.  
  111. boolean win(MySpaceship other) {
  112. if (y > height) return true;
  113. if (x + w/2 >= other.x - other.w/2 && x - w/2 <= other.x + other.w/2) {
  114. if (y + h/2 >= other.y - other.h/2) {
  115. return true;
  116. }
  117. }
  118. return false;
  119. }
  120. }
  121.  
  122.  
  123. class MySpaceship {
  124. float x, y, w, h, speed;
  125.  
  126. // w = spaceShipWidth; h = spaceShipHeight;
  127. // spaceShip consist of 3 parts.
  128.  
  129. MySpaceship() {
  130. speed = 5;
  131. w = 30;
  132. h = w;
  133. x = width / 2;
  134. y = height - h;
  135. }
  136.  
  137. void display() {
  138. rectMode(CENTER);
  139. noStroke();
  140. fill(#42FC1F);
  141.  
  142. rect(x, y, w, 1.6 * h / 5);
  143. rect(x, y - h / 5, w / 1.6, h / 5);
  144. rect(x, y - 2 * h / 5, w / 10, h / 5);
  145. }
  146.  
  147. void control() {
  148. if (keyPressed) {
  149. if (key == 'd') {
  150. x = x + speed;
  151. } else if (key =='a') {
  152. x = x - speed;
  153. }
  154. }
  155.  
  156. if (x + w / 2 > width) {
  157. x = - w / 2;
  158. } else if (x + w / 2 < 0) {
  159. x = width - w / 2;
  160. }
  161. }
  162.  
  163. void laser() {
  164. fill(#FF21A7);
  165. rectMode(CORNERS);
  166. rect(x, y - h / 1.6, x + w / 20, 0);
  167. }
  168.  
  169. boolean hit(AllienSpaceship other) {
  170. if (x > other.x - other.w / 2 && x < other.x + other.w / 2) return true;
  171. return false;
  172. }
  173. }
Add Comment
Please, Sign In to add comment