Advertisement
Meowmeowcat

Space Invaders Review

Mar 9th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.66 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package spaceinvadersreview;
  7.  
  8. import java.io.*;
  9. import java.awt.*;
  10. import java.awt.geom.*;
  11. import java.awt.event.*;
  12. import javax.swing.*;
  13.  
  14. public class SpaceInvadersReview extends JFrame implements Runnable {
  15. static final int WINDOW_WIDTH = 600;
  16. static final int WINDOW_HEIGHT = 800;
  17. final int XBORDER = 40;
  18. final int YBORDER = 40;
  19. final int YTITLE = 25;
  20. boolean animateFirstTime = true;
  21. int xsize = -1;
  22. int ysize = -1;
  23. Image image;
  24. Graphics2D g;
  25. //Variables for the aliens.
  26. int numAliens = 10;
  27. int alienXPos[] = new int[numAliens];
  28. int alienYPos[] = new int[numAliens];
  29. int alienValue[] = new int[numAliens];
  30. int alienDownSpeed;
  31. int alienSideSpeed;
  32. //Variables for the cannon.
  33. int cannonXPos;
  34. int cannonYPos;
  35. //Variables for the cannonballs.
  36. int currentCannonBall;
  37. int numCannonBalls = 8;
  38. int cannonBallXPos[] = new int[numCannonBalls];
  39. int cannonBallYPos[] = new int[numCannonBalls];
  40. boolean cannonBallActive[] = new boolean[numCannonBalls];
  41. //Other variables.
  42. boolean gameOver;
  43. int score;
  44. int highScore;
  45. int timeCount;
  46.  
  47. static SpaceInvadersReview frame;
  48. public static void main(String[] args) {
  49. frame = new SpaceInvadersReview();
  50. frame.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
  51. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  52. frame.setVisible(true);
  53. }
  54.  
  55. public SpaceInvadersReview() {
  56. addMouseListener(new MouseAdapter() {
  57. public void mousePressed(MouseEvent e) {
  58. if (e.BUTTON1 == e.getButton()) {
  59. //left button
  60.  
  61. //Don't fire cannonballs when game over.
  62. if (gameOver)
  63. return;
  64.  
  65. // location of the cursor.
  66. int xpos = e.getX();
  67. int ypos = e.getY();
  68. //Fire a cannonball.
  69. cannonBallActive[currentCannonBall] = true;
  70. cannonBallXPos[currentCannonBall] = cannonXPos;
  71. cannonBallYPos[currentCannonBall] = cannonYPos;
  72. currentCannonBall++;
  73. //Make sure we don't go off the end of the array.
  74. if (currentCannonBall >= numCannonBalls)
  75. currentCannonBall = 0;
  76. }
  77. if (e.BUTTON3 == e.getButton()) {
  78. //right button
  79. reset();
  80. }
  81. repaint();
  82. }
  83. });
  84.  
  85. addMouseMotionListener(new MouseMotionAdapter() {
  86. public void mouseDragged(MouseEvent e) {
  87. repaint();
  88. }
  89. });
  90.  
  91. addMouseMotionListener(new MouseMotionAdapter() {
  92. public void mouseMoved(MouseEvent e) {
  93. //Freeze the cannon when game over.
  94. if (gameOver)
  95. return;
  96. //Move the cannon with the mouse.
  97. cannonXPos = e.getX() - getX(0);
  98.  
  99. repaint();
  100. }
  101. });
  102.  
  103. addKeyListener(new KeyAdapter() {
  104.  
  105. public void keyPressed(KeyEvent e) {
  106. if (e.VK_UP == e.getKeyCode()) {
  107. } else if (e.VK_DOWN == e.getKeyCode()) {
  108. } else if (e.VK_LEFT == e.getKeyCode()) {
  109. } else if (e.VK_RIGHT == e.getKeyCode()) {
  110. }
  111. repaint();
  112. }
  113. });
  114. init();
  115. start();
  116. }
  117. Thread relaxer;
  118. ////////////////////////////////////////////////////////////////////////////
  119. public void init() {
  120. requestFocus();
  121. }
  122. ////////////////////////////////////////////////////////////////////////////
  123. public void destroy() {
  124. }
  125.  
  126.  
  127.  
  128. ////////////////////////////////////////////////////////////////////////////
  129. public void paint(Graphics gOld) {
  130. if (image == null || xsize != getSize().width || ysize != getSize().height) {
  131. xsize = getSize().width;
  132. ysize = getSize().height;
  133. image = createImage(xsize, ysize);
  134. g = (Graphics2D) image.getGraphics();
  135. g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
  136. RenderingHints.VALUE_ANTIALIAS_ON);
  137. }
  138. //fill background
  139. g.setColor(Color.cyan);
  140. g.fillRect(0, 0, xsize, ysize);
  141.  
  142. int x[] = {getX(0), getX(getWidth2()), getX(getWidth2()), getX(0), getX(0)};
  143. int y[] = {getY(0), getY(0), getY(getHeight2()), getY(getHeight2()), getY(0)};
  144. //fill border
  145. g.setColor(Color.white);
  146. g.fillPolygon(x, y, 4);
  147. // draw border
  148. g.setColor(Color.red);
  149. g.drawPolyline(x, y, 5);
  150.  
  151. if (animateFirstTime) {
  152. gOld.drawImage(image, 0, 0, null);
  153. return;
  154. }
  155. //Display the active cannonballs.
  156. for (int i=0;i<numCannonBalls;i++)
  157. {
  158. if (cannonBallActive[i])
  159. {
  160. g.setColor(Color.black);
  161. drawCannonBall(getX(cannonBallXPos[i]),getYNormal(cannonBallYPos[i]),0,1,1);
  162. }
  163. }
  164. //Display the cannon.
  165. g.setColor(Color.red);
  166. drawCannon(getX(cannonXPos),getYNormal(cannonYPos),0,1,1);
  167. //Display the aliens.
  168. for (int i=0;i<numAliens;i++)
  169. {
  170. g.setColor(Color.green);
  171. drawAlien(getX(alienXPos[i]),getYNormal(alienYPos[i]),0,1,1,alienValue[i]);
  172. }
  173.  
  174. //Display the score and high score.
  175. g.setColor(Color.black);
  176. g.setFont(new Font("Arial",Font.PLAIN,16));
  177. g.drawString("Score: " + score, 40, 50);
  178. g.setColor(Color.black);
  179. g.setFont(new Font("Arial",Font.PLAIN,16));
  180. g.drawString("High Score: " + highScore, 440, 50);
  181.  
  182. //Display game over when the game is over.
  183. if (gameOver)
  184. {
  185. g.setColor(Color.black);
  186. g.setFont(new Font("Arial",Font.PLAIN,32));
  187. g.drawString("Game Over", 50, 100);
  188. }
  189.  
  190. gOld.drawImage(image, 0, 0, null);
  191. }
  192.  
  193. ////////////////////////////////////////////////////////////////////////////
  194.  
  195. public void drawAlien(int xpos,int ypos,double rot,
  196. double xscale,double yscale,int value)
  197. {
  198. g.translate(xpos,ypos);
  199. g.rotate(rot * Math.PI/180.0);
  200. g.scale( xscale , yscale );
  201.  
  202. int xval[] = {10,-10,-10,-5,-10,0,10,5,10};
  203. int yval[] = {-20,-20,-5,-5,10,0,10,-5,-5};
  204. g.fillPolygon(xval,yval,xval.length);
  205.  
  206. g.setColor(Color.black);
  207. g.setFont(new Font("Arial",Font.PLAIN,16));
  208. g.drawString("" + value, 0, 0);
  209.  
  210. g.scale( 1.0/xscale,1.0/yscale );
  211. g.rotate(-rot * Math.PI/180.0);
  212. g.translate(-xpos,-ypos);
  213. }
  214. ////////////////////////////////////////////////////////////////////////////
  215. public void drawCannon(int xpos,int ypos,double rot,
  216. double xscale,double yscale)
  217. {
  218. g.translate(xpos,ypos);
  219. g.rotate(rot * Math.PI/180.0);
  220. g.scale( xscale , yscale );
  221.  
  222. int xvals[] = {0,10,10,-10,-10,0};
  223. int yvals[] = {-15,-10,10,10,-10,-15};
  224. g.fillPolygon(xvals,yvals,xvals.length);
  225.  
  226. g.scale( 1.0/xscale,1.0/yscale );
  227. g.rotate(-rot * Math.PI/180.0);
  228. g.translate(-xpos,-ypos);
  229. }
  230.  
  231. ////////////////////////////////////////////////////////////////////////////
  232. public void drawCannonBall(int xpos,int ypos,double rot,double xscale,double yscale)
  233. {
  234. g.translate(xpos,ypos);
  235. g.rotate(rot * Math.PI/180.0);
  236. g.scale( xscale , yscale );
  237.  
  238.  
  239. g.fillOval(-10,-10,20,20);
  240.  
  241. g.scale( 1.0/xscale,1.0/yscale );
  242. g.rotate(-rot * Math.PI/180.0);
  243. g.translate(-xpos,-ypos);
  244. }
  245.  
  246. ////////////////////////////////////////////////////////////////////////////
  247. // needed for implement runnable
  248. public void run() {
  249. while (true) {
  250. animate();
  251. repaint();
  252. double seconds = .01; //time that 1 frame takes.
  253. int miliseconds = (int) (1000.0 * seconds);
  254. try {
  255. Thread.sleep(miliseconds);
  256. } catch (InterruptedException e) {
  257. }
  258. }
  259. }
  260. /////////////////////////////////////////////////////////////////////////
  261. public void reset() {
  262.  
  263. score = 0;
  264. gameOver = false;
  265. //Initialize the values of the cannonballs.
  266. currentCannonBall = 0;
  267. for (int i=0;i<numCannonBalls;i++)
  268. cannonBallActive[i] = false;
  269. //Initialize the values of the cannon.
  270. cannonYPos = 0;
  271. //Initialize the values of the aliens.
  272. alienSideSpeed = 2;
  273. alienDownSpeed = 1;
  274. for (int i=0;i<numAliens;i++) {
  275. alienXPos[i] = (int)(Math.random()*getWidth2()/2+getWidth2()/4);
  276. alienYPos[i] = (int)(Math.random()*getHeight2()/2+getHeight2()/2);
  277. alienValue[i] = (int)(Math.random()*4+2);
  278. }
  279.  
  280. timeCount = 0;
  281. }
  282. /////////////////////////////////////////////////////////////////////////
  283. public void animate() {
  284. if (animateFirstTime) {
  285. animateFirstTime = false;
  286. if (xsize != getSize().width || ysize != getSize().height) {
  287. xsize = getSize().width;
  288. ysize = getSize().height;
  289. }
  290.  
  291. reset();
  292. //Position the cannon at the center when the program starts.
  293. cannonXPos = getWidth2()/2;
  294. highScore = 0;
  295. }
  296.  
  297. //Freeze the game when game over.
  298. if (gameOver)
  299. return;
  300.  
  301. //Move the cannonballs up and check to see if a cannonball has hit an alien.
  302. for (int j=0;j<numCannonBalls;j++)
  303. {
  304. if (cannonBallActive[j])
  305. {
  306. for (int i=0;i<numAliens;i++) {
  307.  
  308. if (alienXPos[i] > cannonBallXPos[j] - 10 &&
  309. alienXPos[i] < cannonBallXPos[j] + 10 &&
  310. alienYPos[i] > cannonBallYPos[j] - 10 &&
  311. alienYPos[i] < cannonBallYPos[j] + 10)
  312. {
  313. score+=alienValue[i];
  314. if (score > highScore)
  315. highScore = score;
  316. alienXPos[i] = (int)(Math.random()*getWidth2()/2+getWidth2()/4);
  317. alienYPos[i] = getHeight2();
  318. alienValue[i] = (int)(Math.random()*4+2);
  319. cannonBallActive[j] = false;
  320. }
  321. }
  322. if (cannonBallYPos[j] > getHeight2())
  323. cannonBallActive[j] = false;
  324. cannonBallYPos[j] += 5;
  325. }
  326. }
  327.  
  328. //Move the aliens sideways and determine if we need to switch the direction of the aliens.
  329. boolean switchDir = false;
  330. for (int i=0;i<numAliens;i++) {
  331. alienXPos[i] += alienSideSpeed;
  332. if (alienXPos[i] > getWidth2() || alienXPos[i] < 0)
  333. switchDir = true;
  334. }
  335. if (switchDir)
  336. alienSideSpeed = -alienSideSpeed;
  337.  
  338. //Move the aliens down.
  339. if (timeCount % 3 == 2)
  340. {
  341. for (int i=0;i<numAliens;i++)
  342. {
  343. if (alienYPos[i] <= 0)
  344. gameOver = true;
  345. alienYPos[i] -= alienDownSpeed;
  346. }
  347. }
  348.  
  349. timeCount++;
  350. }
  351.  
  352. ////////////////////////////////////////////////////////////////////////////
  353. public void start() {
  354. if (relaxer == null) {
  355. relaxer = new Thread(this);
  356. relaxer.start();
  357. }
  358. }
  359. ////////////////////////////////////////////////////////////////////////////
  360. public void stop() {
  361. if (relaxer.isAlive()) {
  362. relaxer.stop();
  363. }
  364. relaxer = null;
  365. }
  366. /////////////////////////////////////////////////////////////////////////
  367. public int getX(int x) {
  368. return (x + XBORDER);
  369. }
  370.  
  371. public int getY(int y) {
  372. return (y + YBORDER + YTITLE);
  373. }
  374.  
  375. public int getYNormal(int y) {
  376. return (-y + YBORDER + YTITLE + getHeight2());
  377. }
  378.  
  379.  
  380. public int getWidth2() {
  381. return (xsize - getX(0) - XBORDER);
  382. }
  383.  
  384. public int getHeight2() {
  385. return (ysize - getY(0) - YBORDER);
  386. }
  387. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement