Guest User

Untitled

a guest
May 20th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.15 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.geom.*;
  3. import java.awt.event.*;
  4. import java.applet.*;
  5. import javax.swing.*;
  6. import java.io.*;
  7. import javax.imageio.*;
  8. import java.net.*;
  9.  
  10.  
  11.  
  12.  
  13. class rectangle
  14. {
  15. private int xpos, ypos, height, width, speed, ydir, xdir;
  16. private Rectangle c;
  17. public rectangle(int y,int x, int h, int w, int s, int yd)
  18. {
  19. ypos = y;
  20. xpos = x;
  21. height = h;
  22. width = w;
  23. speed = s;
  24. ydir = yd;
  25. c = new Rectangle(xpos, ypos, width, height);
  26. }
  27. public void move(Bullet b)
  28. {
  29. xpos = xpos + speed*xdir;
  30. ypos = ypos + speed*ydir;
  31. c.setFrame(xpos, ypos, width, height);
  32. }
  33.  
  34. public int getX() { return xpos; }
  35. public int getY() { return ypos; }
  36. public int getHeight() { return height; }
  37. public int getWidth() { return width; }
  38. public int getXDir() { return xdir; }
  39. public int getYDir() { return ydir; }
  40.  
  41. public void setDir(int x, int y)
  42. {
  43. xdir = x;
  44. ydir = y;
  45. }
  46. public boolean intersects(double xpos2, double ypos2, double height2, double width2)
  47. {
  48. return c.intersects(xpos2, ypos2, height2, width2);
  49. }
  50. public void Kill()
  51. {
  52. width = width /2;
  53. }
  54. public void paint(Graphics2D g)
  55. {
  56. g.fill(c);
  57. }
  58. }
  59.  
  60.  
  61.  
  62. class Alien
  63. {
  64. private double xpos, ypos, xdir, ydir,score;
  65. private double speed;
  66. private Image alienPic;
  67. private Component parent;
  68. // private Ellipse2D.Double c;
  69. public Alien(double x, double y, double xd, double yd, double s,double S, Image alienPic, Component parent)
  70. {
  71. xpos = x;
  72. ypos = y;
  73. xdir = xd;
  74. ydir = yd;
  75. speed = s;
  76. score = S;
  77. this.alienPic = alienPic;
  78. this.parent = parent;
  79. }
  80.  
  81. public void Speed(double As)
  82. {
  83. speed = As;
  84. }
  85. public void move(Score s, Bullet b)
  86. {
  87. xpos = xpos + speed*xdir;
  88. ypos = ypos + speed*ydir;
  89. c.setFrame(xpos, ypos, width, height);
  90. if(b.intersects(xpos, ypos))
  91. {
  92. ypos = 1;
  93. xpos = 458;
  94. xdir = 0;
  95. ydir = 0;
  96. s.newS();
  97. b.Kill();
  98. }
  99. }
  100. public double getX() { return xpos; }
  101. public double getY() { return ypos; }
  102. public double getXDir() { return xdir; }
  103. public double getYDir() { return ydir; }
  104. public double getSpeed() {return speed; }
  105. public double getS(){return score;}
  106.  
  107. public void setY (double yD)
  108. {
  109. ypos = yD;
  110. }
  111. public void setHW()
  112. {
  113. ypos = -1000;
  114. xpos = -1000;
  115. }
  116. public void setDir(double x, double y)
  117. {
  118. xdir = x;
  119. ydir = y;
  120.  
  121. }
  122. public void shoot(Bullet b)
  123. {
  124. b = new Bullet(100,200 ,10, 10 ,1, 1);
  125. }
  126. public boolean intersects(int x, int y, int w, int h)
  127. {
  128. return c.intersects(x, y, w, h);
  129. }
  130. public void paint(Graphics2D g)
  131. {
  132. g2.drawImage(alienPic,xpos,ypos,null);
  133. }
  134. }
  135.  
  136.  
  137.  
  138. class Bullet
  139. {
  140. private double xpos, ypos, height, width, speed, xdir, ydir;
  141. private Ellipse2D.Double c;
  142. public Bullet(int x, int y, int w, int h, int s, int yd)
  143. {
  144. xpos = x;
  145. ypos = y;
  146. width = w;
  147. height = h;
  148. speed = s;
  149. ydir = yd;
  150. c = new Ellipse2D.Double(xpos, ypos, width, height);
  151. }
  152. public void move()
  153. {
  154. xpos = xpos + speed*xdir;
  155. ypos = ypos + speed*ydir;
  156. c.setFrame(xpos, ypos, width, height);
  157. }
  158.  
  159. public double getX() { return xpos; }
  160. public double getY() { return ypos; }
  161. public double getHeight() { return height; }
  162. public double getWidth() { return width; }
  163. public double getXDir() { return xdir; }
  164. public double getYDir() { return ydir; }
  165.  
  166. public void setDir(int x, int y)
  167. {
  168. xdir = x;
  169. ydir = y;
  170. }
  171. public boolean intersects(double x, double y, double w, double h)
  172. {
  173. return c.intersects(x, y, w, h);
  174. }
  175. public void Kill()
  176. {
  177. width = 0;
  178. height = 0;
  179. }
  180. public void paint(Graphics2D g)
  181. {
  182. g.fill(c);
  183. }
  184. }
  185.  
  186.  
  187.  
  188. class AlienBullet
  189. {
  190. private double xpos, ypos, height, width, speed, xdir, ydir;
  191. private Ellipse2D.Double c;
  192. public AlienBullet(double x, double y, double w, double h, int s, int yd)
  193. {
  194. xpos = x;
  195. ypos = y;
  196. width = w;
  197. height = h;
  198. speed = s;
  199. ydir = yd;
  200. c = new Ellipse2D.Double(xpos, ypos, width, height);
  201.  
  202. }
  203. public void move(rectangle r2, rectangle r3, rectangle r4)
  204. {
  205. xpos = xpos + speed*xdir;
  206. ypos = ypos + speed*ydir;
  207. c.setFrame(xpos, ypos, width, height);
  208.  
  209. if(r2.intersects(xpos, ypos, height,width))
  210. {
  211. width = 0;
  212. height = 0;
  213. ypos = 1;
  214. xpos = 300;
  215. r2.Kill();
  216.  
  217. }
  218. if(r3.intersects(xpos, ypos, height,width))
  219. {
  220. width = 0;
  221. height = 0;
  222. ypos = 1;
  223. xpos = 300;
  224. r3.Kill();
  225. }
  226. if(r4.intersects(xpos, ypos, height,width))
  227. {
  228. width = 0;
  229. height = 0;
  230. ypos = 1;
  231. xpos = 300;
  232. r4.Kill();
  233. }
  234.  
  235.  
  236.  
  237. }
  238.  
  239. public double getX() { return xpos; }
  240. public double getY() { return ypos; }
  241. public double getHeight() { return height; }
  242. public double getWidth() { return width; }
  243. public double getXDir() { return xdir; }
  244. public double getYDir() { return ydir; }
  245.  
  246. public void setDir(int x, int y)
  247. {
  248. xdir = x;
  249. ydir = y;
  250. }
  251. public boolean intersects(double x, double y, double w, double h)
  252. {
  253. return c.intersects(x, y, w, h);
  254. }
  255. public void WH(double x, double y)
  256. {
  257. xpos = x;
  258. ypos = y;
  259. height = 7;
  260. width = 5;
  261.  
  262. }
  263. public void Kill()
  264. {
  265. width = 0;
  266. height = 0;
  267. }
  268. public void paint(Graphics2D g)
  269. {
  270. g.fill(c);
  271. }
  272. }
  273.  
  274.  
  275.  
  276. class Score
  277. {
  278. private int score;
  279. public Score(int s)
  280. {
  281. score = s;
  282. }
  283. public void newS()
  284. {
  285. score = score + 10;
  286. }
  287. public void newSMother()
  288. {
  289. score = score + 30;
  290. }
  291. public int getScore(){return score;}
  292. }
  293.  
  294.  
  295. class Mother
  296. {
  297. private double xpos, ypos, height, width, xdir, ydir,score;
  298. private double speed;
  299. private Ellipse2D.Double c;
  300. public Mother(double x, double y, double w, double h, double xd, double yd, double s,double S)
  301. {
  302. xpos = x;
  303. ypos = y;
  304. width = w;
  305. height = h;
  306. xdir = xd;
  307. ydir = yd;
  308. speed = s;
  309. score = S;
  310. c = new Ellipse2D.Double(xpos, ypos, width, height);
  311. }
  312.  
  313. public void Speed(double As)
  314. {
  315. speed = As;
  316. }
  317. public void move(Score s, Bullet b)
  318. {
  319. xpos = xpos + speed*xdir;
  320. ypos = ypos + speed*ydir;
  321. c.setFrame(xpos, ypos, width, height);
  322. if(b.intersects(xpos, ypos, height,width))
  323. {
  324. width = 0;
  325. height = 0;
  326. ypos = 1;
  327. xpos = 458;
  328. xdir = 0;
  329. ydir = 0;
  330. s.newSMother();
  331. b.Kill();
  332. }
  333. }
  334. public double getX() { return xpos; }
  335. public double getY() { return ypos; }
  336. public double getHeight() { return height; }
  337. public double getWidth() { return width; }
  338. public double getXDir() { return xdir; }
  339. public double getYDir() { return ydir; }
  340. public double getSpeed() {return speed; }
  341. public double getS(){return score;}
  342.  
  343. public void setY (double yD)
  344. {
  345. ypos = yD;
  346. }
  347. public void setHW()
  348. {
  349. height = 0;
  350. width = 0;
  351. ypos = -1000;
  352. xpos = -1000;
  353. }
  354. public void setDir(double x, double y)
  355. {
  356. xdir = x;
  357. ydir = y;
  358. }
  359. public void shoot(Bullet b)
  360. {
  361. b = new Bullet(100,200 ,10, 10 ,1, 1);
  362. }
  363. public boolean intersects(int x, int y, int w, int h)
  364. {
  365. return c.intersects(x, y, w, h);
  366. }
  367. public void paint(Graphics2D g)
  368. {
  369. g.fill(c);
  370. }
  371. }
  372.  
  373.  
  374. public class SpaceInvade extends Applet implements KeyListener{
  375. int roll;
  376. Image img;
  377. private int roll1 = 0;
  378. int roll2 = 0;
  379. int lives = 3;
  380. private Alien a;
  381. private rectangle r;
  382. private rectangle r2;
  383. private rectangle r3;
  384. private rectangle r4;
  385. private rectangle r5;
  386. private rectangle r6;
  387. private Mother m1;
  388. private Score s1;
  389. private Timer t;
  390. private Bullet b;
  391. AlienBullet[]Alienb = new AlienBullet[10];
  392. Alien[]aliens = new Alien [10];
  393. Alien[]aliens2 = new Alien [10];
  394. Alien[]aliens3 = new Alien [10];
  395. Alien[]aliens4 = new Alien [10];
  396. Alien[]aliens5 = new Alien [10];
  397. int XDIR = 1;
  398. public void init() {
  399.  
  400. r = new rectangle(400, 1, 20, 20, 7, 0);
  401. b = new Bullet (-10, -123,10, 10 ,0, 0);
  402. r2 = new rectangle(330,50 , 20, 100, 7, 0);
  403. r3 = new rectangle(330,350 , 20, 100, 7, 0);
  404. r4 = new rectangle(330,650 , 20, 100, 7, 0);
  405. r5 = new rectangle(85,500,30,30,0,0);
  406. m1 = new Mother(100,100, 100, 100, 7, 1,1,0);
  407. s1 = new Score(0);
  408. addKeyListener(this);
  409. setFocusable(true);
  410. int score1 = s1.getScore();
  411. for (int i = 0; i <aliens.length ;i++)
  412. {
  413. aliens[i] = new Alien(40*i,20, 1,0,0.3,score1,getImage(getCodeBase (), "P:\My Pictures\alien"),this);
  414. aliens[i] = new Alien(40*i,20, 1,0,0.3,score1,getImage(getCodeBase (), "P:\My Pictures\alien"),this);
  415. aliens[i] = new Alien(40*i,20, 1,0,0.3,score1,getImage(getCodeBase (), "P:\My Pictures\alien"),this);
  416. aliens[i] = new Alien(40*i,20, 1,0,0.3,score1,getImage(getCodeBase (), "P:\My Pictures\alien"),this);
  417. aliens[i] = new Alien(40*i,20, 1,0,0.3,score1,getImage(getCodeBase (), "P:\My Pictures\alien"),this);
  418. }
  419. for (int b = 0; b <Alienb.length ;b++)
  420. {
  421. Alienb[b] = new AlienBullet(20*b,20,0,0,1,1);
  422. }
  423. ActionListener s = new ActionListener()
  424. {
  425. public void actionPerformed(ActionEvent evt)
  426. {
  427. r.move(b);
  428. r2.move(b);
  429. r3.move(b);
  430. r4.move(b);
  431. int ALIEN = 0;
  432. b.move();
  433. int FONT = 0;
  434.  
  435. roll = (int)(Math.random()*200 + 1);
  436. if (roll <= 3)
  437. {
  438. roll1 = (int)(Math.random()*9 + 1);
  439. if(Alienb[roll1].getHeight() == 0 && Alienb[roll1].getWidth() == 0 && aliens[roll1].getHeight() > 0 && aliens[roll1].getWidth() > 0)
  440. {
  441. Alienb[roll1].WH(aliens[roll1].getX(), aliens[roll1].getY());
  442. Alienb[roll1].WH(aliens2[roll1].getX(), aliens2[roll1].getY());
  443. Alienb[roll1].WH(aliens3[roll1].getX(), aliens3[roll1].getY());
  444. Alienb[roll1].WH(aliens4[roll1].getX(), aliens4[roll1].getY());
  445. Alienb[roll1].WH(aliens5[roll1].getX(), aliens5[roll1].getY());
  446. }
  447. }
  448.  
  449. if(roll2 != 10)
  450. {
  451. roll2 = (int)(Math.random()*20 + 2);
  452. System.out.println(roll2);
  453. }
  454. if (roll2 == 10)
  455. {
  456. roll2 = 1;
  457. r6 = new rectangle(100,500,30,50,2,-1);
  458. r6.move(b);
  459. System.out.println(roll2);
  460. }
  461. for (int i = 0; i < aliens.length ;i++)
  462. {
  463. if (aliens3[i].getY() >= r2.getY())
  464. {
  465. aliens3[i].setHW();
  466. FONT = 1;
  467. }
  468. if (aliens4[i].getY() >= r2.getY())
  469. {
  470. aliens4[i].setHW();
  471. FONT = 1;
  472. }
  473. if (aliens5[i].getY() >= r2.getY())
  474. {
  475. aliens5[i].setHW();
  476. FONT = 1;
  477. }
  478. if (aliens2[i].getY() >= r2.getY())
  479. {
  480. aliens2[i].setHW();
  481. FONT = 1;
  482. }
  483. if (aliens[i].getY() >= r2.getY())
  484. {
  485. aliens[i].setHW();
  486. FONT = 1;
  487. }
  488. if ((aliens3[i].getX() + aliens3[i].getWidth() >= getWidth()) || (aliens3[i].getX() < 0))
  489. {
  490. ALIEN = 1;
  491. }
  492. if ((aliens4[i].getX() + aliens4[i].getWidth() >= getWidth()) || (aliens4[i].getX() < 0))
  493. {
  494. ALIEN = 1;
  495. }
  496. if ((aliens5[i].getX() + aliens5[i].getWidth() >= getWidth()) || (aliens5[i].getX() < 0))
  497. {
  498. ALIEN = 1;
  499. }
  500. if ((aliens2[i].getX() + aliens2[i].getWidth() >= getWidth()) || (aliens2[i].getX() < 0))
  501. {
  502. ALIEN = 1;
  503. }
  504. if ((aliens[i].getX() + aliens[i].getWidth() >= getWidth()) || (aliens[i].getX() < 0))
  505. {
  506. ALIEN = 1;
  507. }
  508.  
  509. if (ALIEN == 1)
  510. {
  511. for (i = 0; i < aliens2.length ;i++)
  512. {
  513. aliens5[i].setY(aliens5[i].getY() + 20);
  514. aliens5[i].setDir(-aliens5[i].getXDir(),aliens5[i].getYDir());
  515. aliens5[i].Speed(aliens5[i].getSpeed() + 0.1);
  516. }
  517. }
  518. if (ALIEN == 1)
  519. {
  520. for (i = 0; i < aliens2.length ;i++)
  521. {
  522. aliens4[i].setY(aliens4[i].getY() + 20);
  523. aliens4[i].setDir(-aliens4[i].getXDir(),aliens4[i].getYDir());
  524. aliens4[i].Speed(aliens4[i].getSpeed() + 0.1);
  525. }
  526. }
  527. if (ALIEN == 1)
  528. {
  529. for (i = 0; i < aliens2.length ;i++)
  530. {
  531. aliens3[i].setY(aliens3[i].getY() + 20);
  532. aliens3[i].setDir(-aliens3[i].getXDir(),aliens3[i].getYDir());
  533. aliens3[i].Speed(aliens3[i].getSpeed() + 0.1);
  534. }
  535. }
  536. if (ALIEN == 1)
  537. {
  538. for (i = 0; i < aliens2.length ;i++)
  539. {
  540. aliens2[i].setY(aliens2[i].getY() + 20);
  541. aliens2[i].setDir(-aliens2[i].getXDir(),aliens2[i].getYDir());
  542. aliens2[i].Speed(aliens2[i].getSpeed() + 0.1);
  543. }
  544. }
  545. if (ALIEN == 1)
  546. {
  547. for (i = 0; i < aliens.length ;i++)
  548. {
  549. aliens[i].setY(aliens[i].getY() + 20);
  550. aliens[i].setDir(-aliens[i].getXDir(),aliens[i].getYDir());
  551. aliens[i].Speed(aliens[i].getSpeed() + 0.1);
  552. }
  553. }
  554. }
  555. for (int i = 0; i < aliens.length;i++)
  556. {
  557. aliens[i].move(s1, b);
  558. aliens2[i].move(s1,b);
  559. aliens3[i].move(s1,b);
  560. aliens4[i].move(s1,b);
  561. aliens5[i].move(s1,b);
  562. }
  563. for (int b = 0; b < Alienb.length;b++)
  564. {
  565. Alienb[b].move(r2,r3,r4);
  566. }
  567.  
  568. repaint();
  569.  
  570. if (b.intersects(r2.getX(), r2.getY(), r2.getWidth(), r2.getHeight()))
  571. {
  572. b.Kill();
  573. r2.Kill();
  574. }
  575. if (b.intersects(r4.getX(), r4.getY(), r4.getWidth(), r4.getHeight()))
  576. {
  577. b.Kill();
  578. r4.Kill();
  579. }
  580. if (b.intersects(r3.getX(), r3.getY(), r3.getWidth(), r3.getHeight()))
  581. {
  582. b.Kill();
  583. r3.Kill();
  584. }
  585.  
  586. if (r.getX() <= 0)
  587. {
  588. r.setDir(0, 0);
  589. }
  590. if (r.getX() >= getWidth() - 30)
  591. {
  592. r.setDir( 0, 0);
  593. }
  594.  
  595. for (int a = 0; a <= Alienb.length-1; a++)
  596. {
  597. if (Alienb[a].intersects(r.getX(), r.getY(), r.getWidth(), r.getHeight()))
  598. {
  599. lives--;
  600. Alienb[a].Kill();
  601. }
  602. }
  603. }
  604. };
  605.  
  606. t=new Timer(10, s);
  607. t.start();
  608. }
  609. public void keyPressed(KeyEvent e)
  610. {
  611. if (e.getKeyCode() == e.VK_LEFT && (r.getX() >= 0))
  612. {
  613. r.setDir(-1,0);
  614. }
  615. if (e.getKeyCode() == e.VK_RIGHT && (r.getX() <= getWidth() - 30))
  616. {
  617. r.setDir(1,0);
  618. }
  619. if ((e.getKeyCode() == e.VK_SPACE) && (b.getY() < 0))
  620. {
  621. b = new Bullet (r.getX(), 390, 5, 7 ,7, 0);
  622. b.setDir (0,-1); //change direction of the paddle to positive
  623. }
  624. }
  625.  
  626. public void keyReleased(KeyEvent e)
  627. {
  628. if (e.getKeyCode() == e.VK_LEFT)
  629. {
  630. r.setDir(0,0); //change direction of the paddle to positive
  631. }
  632. if (e.getKeyCode() == e.VK_RIGHT)
  633. {
  634. r.setDir(0,0); //change direction of the paddle to negative
  635. }
  636. }
  637. public void keyTyped(KeyEvent e) {} //not needed but has to be here
  638.  
  639.  
  640. public void paint(Graphics g) {
  641. Graphics2D g2 = (Graphics2D) g;
  642. setBackground ( Color.red);
  643. for (int i = 0; i < aliens.length ;i++)
  644. {
  645. if (aliens[i].getY() >= r2.getY() || lives == 0)
  646. {
  647. Font font = new Font("Arial",Font.BOLD,45);
  648. g.setFont(font);
  649. FontMetrics fm = g.getFontMetrics();
  650. g.setColor(Color.BLACK);
  651. int y = fm.getHeight();
  652. g.drawString("Game Over",320,225);
  653. g.drawString("Try Again",320,275);
  654. t.stop();
  655. }
  656. Font font = new Font("Helevetica",Font.BOLD,25);
  657. g.setFont(font);
  658. FontMetrics fm = g.getFontMetrics();
  659. g.setColor(Color.BLACK);
  660. int y = fm.getHeight();
  661. g.drawString("Score = " +s1.getScore(),70,500);
  662. g.drawString("Lives = " +lives,300,500);
  663. }
  664.  
  665. r.paint(g2);
  666. b.paint(g2);
  667. r2.paint(g2);
  668. r3.paint(g2);
  669. r4.paint(g2);
  670. m1.paint(g2);
  671. if (roll1 == 1)
  672. {
  673. //g2.setColor(Color.GREEN);
  674. //r6.paint(g2);
  675. }
  676. g2.setColor(Color.BLACK);
  677. for (int i = 0; i < aliens.length;i++)
  678. {
  679.  
  680. aliens[i].paint(g2);
  681. aliens2[i].paint(g2);
  682. aliens3[i].paint(g2);
  683. aliens4[i].paint(g2);
  684. aliens5[i].paint(g2);
  685. }
  686. for (int b = 0; b < Alienb.length;b++)
  687. {
  688. Alienb[b].paint(g2);
  689. }
  690. }
  691. }
Add Comment
Please, Sign In to add comment