Advertisement
Guest User

coolkid33

a guest
Dec 16th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.06 KB | None | 0 0
  1.  
  2. /*Description: An applet that runs a space shooter game (Method: Enemy)
  3. *Version: 1.0
  4. *Date: December 13, 2018
  5. *Author: Sean Feigis
  6. */
  7.  
  8. import java.awt.Graphics;
  9. import java.awt.Image;
  10. import java.awt.Rectangle;
  11. import java.awt.Color;
  12. import java.util.Random;
  13.  
  14. import javax.swing.ImageIcon;
  15.  
  16. public class Enemy {
  17.  
  18. // Variables
  19. Image img = new ImageIcon(this.getClass().getResource("/enemy.png")).getImage();
  20. private Graphics graph;
  21. int y_pos; // y position of the enemy
  22. int x_pos; // x position of the enemy
  23. Rectangle recEnemy = new Rectangle(x_pos, y_pos, 50, 22); // Rectangle for collision of the enemy
  24.  
  25. // Constructors
  26. Enemy(int x, int y) {
  27. x_pos = x;
  28. y_pos = y;
  29.  
  30. }// end constructor
  31.  
  32. // METHODS:
  33.  
  34. // GetX - returns the value of X
  35. public int getx() {
  36. return x_pos; // returns x_pos
  37. }// end method getx
  38.  
  39. // Input: None
  40. // Returns the value of x_pos
  41. // Output: returns x_pos
  42.  
  43. // SetX - Changes the value of X to move to left or right
  44. public void xleft() {
  45. x_pos -= 2; // change the value of x_pos to x_pos -2
  46. recEnemy.setLocation(x_pos, y_pos); // adjusts the value of the rectangle to match the new x value
  47. }// end method xleft
  48.  
  49. // Input: None
  50. // sets the value of x_pos to -=2 and adjusts the position of the rectangle
  51. // Output: None
  52.  
  53. public void xright() {
  54. x_pos += 2; // change the value of x_pos to x_pos +2
  55. recEnemy.setLocation(x_pos, y_pos); // adjusts the value of the rectangle to match the new x value
  56. }// end method xright
  57.  
  58. // Input: None
  59. // sets the value of x_pos to +=2 and adjusts the position of the rectangle
  60. // Output: None
  61.  
  62. // Generate shots
  63.  
  64. public Shots shoot() {
  65. return new Shots(x_pos, (y_pos + 10)); // create new shot object
  66. }// end method shoot
  67.  
  68. // Input: None
  69. // Create a shot object that inherits the x and y pos of the spacecraft
  70. // Output: new shot
  71.  
  72. // Draw the polygon shaped spacecraft
  73.  
  74. public void drawEnemy(Graphics g) {
  75. g.drawImage(img, x_pos, y_pos, 50, 22, null); //draw the image of the spaceship in the correct position
  76.  
  77.  
  78. //Code used to draw the old model, currently defunct
  79. //g.setColor(Color.red); // sets the colour of the shot to red
  80. //int[] x_poly = { x_pos, x_pos - 10, x_pos, x_pos + 10 };
  81. //int[] y_poly = { y_pos, y_pos - 15, y_pos - 10, y_pos - 15 }; // creates the shape of the shot
  82. //g.fillPolygon(x_poly, y_poly, 4);
  83.  
  84. //g.drawRect(recEnemy.x, recEnemy.y, 50, 22); //draws the rectangle for collison testing
  85.  
  86.  
  87. }// end method drawEnemy
  88. }// end method Enemy
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96. SHOTS
  97.  
  98.  
  99.  
  100. /*Description: An applet that runs a space shooter game (Method: Enemy)
  101. *Version: 1.0
  102. *Date: December 13, 2018
  103. *Author: Sean Feigis
  104. */
  105.  
  106. import java.awt.Graphics;
  107. import java.awt.Image;
  108. import java.awt.Rectangle;
  109. import java.awt.Color;
  110. import java.util.Random;
  111.  
  112. import javax.swing.ImageIcon;
  113.  
  114. public class Enemy {
  115.  
  116. // Variables
  117. Image img = new ImageIcon(this.getClass().getResource("/enemy.png")).getImage();
  118. private Graphics graph;
  119. int y_pos; // y position of the enemy
  120. int x_pos; // x position of the enemy
  121. Rectangle recEnemy = new Rectangle(x_pos, y_pos, 50, 22); // Rectangle for collision of the enemy
  122.  
  123. // Constructors
  124. Enemy(int x, int y) {
  125. x_pos = x;
  126. y_pos = y;
  127.  
  128. }// end constructor
  129.  
  130. // METHODS:
  131.  
  132. // GetX - returns the value of X
  133. public int getx() {
  134. return x_pos; // returns x_pos
  135. }// end method getx
  136.  
  137. // Input: None
  138. // Returns the value of x_pos
  139. // Output: returns x_pos
  140.  
  141. // SetX - Changes the value of X to move to left or right
  142. public void xleft() {
  143. x_pos -= 2; // change the value of x_pos to x_pos -2
  144. recEnemy.setLocation(x_pos, y_pos); // adjusts the value of the rectangle to match the new x value
  145. }// end method xleft
  146.  
  147. // Input: None
  148. // sets the value of x_pos to -=2 and adjusts the position of the rectangle
  149. // Output: None
  150.  
  151. public void xright() {
  152. x_pos += 2; // change the value of x_pos to x_pos +2
  153. recEnemy.setLocation(x_pos, y_pos); // adjusts the value of the rectangle to match the new x value
  154. }// end method xright
  155.  
  156. // Input: None
  157. // sets the value of x_pos to +=2 and adjusts the position of the rectangle
  158. // Output: None
  159.  
  160. // Generate shots
  161.  
  162. public Shots shoot() {
  163. return new Shots(x_pos, (y_pos + 10)); // create new shot object
  164. }// end method shoot
  165.  
  166. // Input: None
  167. // Create a shot object that inherits the x and y pos of the spacecraft
  168. // Output: new shot
  169.  
  170. // Draw the polygon shaped spacecraft
  171.  
  172. public void drawEnemy(Graphics g) {
  173. g.drawImage(img, x_pos, y_pos, 50, 22, null); //draw the image of the spaceship in the correct position
  174.  
  175.  
  176. //Code used to draw the old model, currently defunct
  177. //g.setColor(Color.red); // sets the colour of the shot to red
  178. //int[] x_poly = { x_pos, x_pos - 10, x_pos, x_pos + 10 };
  179. //int[] y_poly = { y_pos, y_pos - 15, y_pos - 10, y_pos - 15 }; // creates the shape of the shot
  180. //g.fillPolygon(x_poly, y_poly, 4);
  181.  
  182. //g.drawRect(recEnemy.x, recEnemy.y, 50, 22); //draws the rectangle for collison testing
  183.  
  184.  
  185. }// end method drawEnemy
  186. }// end method Enemy
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement