Guest User

Untitled

a guest
Jun 21st, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.30 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.Color;
  3. import java.awt.Font;
  4. import java.awt.Graphics;
  5. import java.awt.Point;
  6. import java.awt.Rectangle;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import java.awt.event.MouseAdapter;
  10. import java.awt.event.MouseEvent;
  11. import java.awt.event.MouseListener;
  12. import java.awt.event.MouseMotionAdapter;
  13. import java.awt.event.MouseMotionListener;
  14. import java.awt.geom.Line2D;
  15.  
  16.  
  17. public class ad extends JPanel {
  18. public int spawnRate = 2000;
  19. public int maximumAmountOfMobs = 200;
  20. public int[] mobKind;
  21. public int mobMovementSpeed = 10;
  22. public int mobTimmy = 0;
  23. public int mobSize = 30;
  24. public int health = 100;
  25. public int money = 500;
  26. public int shopSize = 11;
  27. public int sizeBetween = 10;
  28. public int fade = shopSize+1;
  29. public int heldTower = 0;
  30. public int towerID[];
  31. public int air = 99;
  32.  
  33. public Rectangle[] mobs;
  34. public Rectangle[] tower;
  35. public Rectangle[] shop = new Rectangle[shopSize];
  36. public Rectangle end;
  37. public Line2D shot;
  38. public Point mse;
  39.  
  40. public Timer spawn;
  41. public Timer mobMovement;
  42.  
  43. public boolean gameOver = false;
  44. public boolean inGame = true;
  45. public boolean holdingTower = false;
  46.  
  47. public void defineObjects() {
  48. mobKind = new int[maximumAmountOfMobs];
  49. mobs = new Rectangle[maximumAmountOfMobs];
  50. for (int i=0;i<mobs.length;i++) {
  51. mobs[i] = new Rectangle(-5000, -5000, 0, 0);
  52. mobKind[i] = 0;
  53. }
  54.  
  55. for(int i=0;i<shop.length;i++) {
  56. shop[i] = new Rectangle((100)+i*32 + (sizeBetween*i), 12, 32, 32);
  57. }
  58.  
  59. end = new Rectangle(0, aa.height/2-100/2, 30, 100);
  60.  
  61. tower = new Rectangle[200];
  62. towerID = new int[200];
  63. for(int i=0;i<tower.length;i++) {
  64. tower[i] = new Rectangle(-5000, -5000, 0, 0);
  65. towerID[i] = air;
  66. }
  67. }
  68.  
  69. public ad(ab f, ac bp) {
  70. defineObjects();
  71.  
  72. f.addMouseListener(new MouseAdapter() {
  73. public void mouseClicked(MouseEvent e) {
  74. if(e.getButton() == MouseEvent.BUTTON1) {
  75. if(holdingTower) {
  76. if(shop[0].contains(mse)) {
  77. heldTower = mobTimmy;
  78. holdingTower = true;
  79. }
  80. } else {
  81. for(int i=0;i<tower.length;i++) {
  82. if(towerID[i] == air) {
  83. tower[i] = new Rectangle(mse.x-16, mse.y-16, 32, 32);
  84. towerID[i] = heldTower;
  85. }
  86. }
  87. }
  88. }
  89. }
  90. });
  91.  
  92. f.addMouseMotionListener(new MouseMotionAdapter() {
  93.  
  94. public void mouseMoved(MouseEvent e) {
  95. mse = new Point(e.getX(), e.getY()-28);
  96. boolean nope = true;
  97.  
  98. for(int i=0;i<shop.length;i++) {
  99. if(shop[i].contains(mse)) {
  100. fade = i;
  101. nope = false;
  102. }
  103. }
  104.  
  105. if(nope){
  106. fade = shopSize+1;
  107. }
  108.  
  109. repaint();
  110. }
  111.  
  112. });
  113.  
  114. ActionListener spawnListener = new ActionListener() {
  115. public void actionPerformed(ActionEvent e) {
  116. if(inGame) {
  117. for(int i=0;i<maximumAmountOfMobs;i++) {
  118. if(mobs[i].x <= 0) {
  119. mobs[i] = new Rectangle(aa.width, aa.height/2-mobSize/2-15, mobSize, mobSize);
  120. i += maximumAmountOfMobs+1;
  121. }
  122. }
  123. }
  124. repaint();
  125. }
  126. };
  127. spawn = new Timer(spawnRate, spawnListener);
  128. spawn.start();
  129.  
  130. ActionListener mobMovementListener = new ActionListener() {
  131. public void actionPerformed(ActionEvent e) {
  132. if(inGame) {
  133. for(int i=0;i<maximumAmountOfMobs;i++) {
  134. if(end.contains(mobs[i])) {
  135. health -= 1;
  136. mobs[i] = new Rectangle(-5000, -5000, 0, 0);
  137. checkForGameOver ();
  138. } else {
  139. mobs[i].x -= 1;
  140. }
  141. }
  142. }
  143. repaint();
  144. }
  145. };
  146. mobMovement = new Timer(mobMovementSpeed, mobMovementListener);
  147. mobMovement.start();
  148. }
  149.  
  150. public void checkForGameOver () {
  151. if(health == 0) {
  152. gameOver = true;
  153. inGame = false;
  154. }
  155. }
  156.  
  157. public void paintComponent(Graphics g) {
  158. super.paintComponent(g);
  159.  
  160. if(aa.f.bp.imagesLoaded) {
  161. for(int i=0;i<maximumAmountOfMobs;i++) {
  162. g.drawImage(aa.f.bp.ene[mobKind[i]], mobs[i].x, mobs[i].y, mobs[i].width, mobs[i].height, null);
  163. }
  164. }
  165. //Panel
  166. g.setColor(Color.DARK_GRAY);
  167. g.fill3DRect(-4, -4, aa.width+4,60, true);
  168.  
  169. //Health
  170. g.setFont(new Font("Arial", Font.BOLD, 16));
  171. g.setColor(Color.WHITE);
  172. g.drawImage(aa.f.bp.style[0], 10, 10, 16, 16, null);
  173. g.drawString(health + "", 10+16+3, 23);
  174.  
  175. //Money
  176. g.drawImage(aa.f.bp.style[1], 10, 10+16+5, 16, 16, null);
  177. g.drawString("$" + money, 10+16+3, 23+16+5);
  178.  
  179. //Towers
  180. for(int i=0;i<tower.length;i++) {
  181. if(towerID[i] != air)
  182. g.drawImage(aa.f.bp.tower[towerID[i]], tower[0].x, tower[0].y, tower[0].width, tower[0].height, null);
  183. }
  184.  
  185. //Hand held
  186. if(holdingTower) {
  187. g.drawImage(aa.f.bp.tower[heldTower], mse.x - 32/2, mse.y - 32/2, 32, 32, null);
  188. }
  189.  
  190. //Panel Components
  191. for(int i=0;i<shop.length;i++) {
  192. g.setColor(Color.GRAY);
  193. g.fillRect(shop[i].x, shop[i].y, shop[i].width, shop[i].height);
  194. g.setColor(Color.WHITE);
  195. g.drawRect(shop[i].x-2, shop[i].y-2, shop[i].width+3, shop[i].height+3);
  196.  
  197. if(i == fade) {
  198. g.drawImage(aa.f.bp.style[2], shop[i].x, shop[i].y, shop[i].width, shop[i].height, null);
  199. }
  200.  
  201. }
  202. g.drawImage(aa.f.bp.tower[0], 100, 12, 32, 32, null);
  203.  
  204. if(gameOver) {
  205. g.setColor(Color.RED);
  206. g.fillRect(0, 0, aa.width+0, aa.height+10);
  207. g.setColor(Color.WHITE);
  208. g.drawString("GAME OVER! :(", 10, 20);
  209. }
  210. }
  211. }
Add Comment
Please, Sign In to add comment