Set 3 AP computer science programming project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

637 lines
17 KiB

8 years ago
8 years ago
8 years ago
  1. /*
  2. Set 3 programming project
  3. 5-18-16
  4. *****************Tanks***************
  5. https://github.com/jrtechs/Tanks/wiki
  6. */
  7. package tanks;
  8. import java.awt.Color;
  9. import java.awt.Font;
  10. import java.awt.Graphics;
  11. import java.awt.event.ActionEvent;
  12. import java.awt.event.ActionListener;
  13. import java.awt.event.KeyEvent;
  14. import java.awt.event.KeyListener;
  15. import java.util.ArrayList;
  16. import javax.swing.JFrame;
  17. import javax.swing.JPanel;
  18. import javax.swing.Timer;
  19. public class Tanks
  20. {
  21. //fields
  22. private JFrame frame;
  23. private JPanel panel;
  24. private int fheight=650;
  25. private int fwidth=900;
  26. private Timer move;
  27. private KeyListener key;
  28. //game elements
  29. private ArrayList<Bullet> bullets;
  30. private Player p;
  31. private ArrayList<Enemy> enemy;
  32. private Wave wave;
  33. //constructor
  34. public Tanks()
  35. {
  36. frame=new JFrame("Tanks project");
  37. frame.setSize(fwidth,fheight + 150);
  38. //frame.setResizable(false);
  39. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  40. newGame();
  41. key=new KeyListener()
  42. {
  43. @Override
  44. public void keyTyped(KeyEvent e)
  45. {
  46. }
  47. @Override
  48. public void keyPressed(KeyEvent e)
  49. {
  50. if(e.getKeyCode() == KeyEvent.VK_ESCAPE)
  51. {
  52. if(wave.gameMode == 1)
  53. {
  54. wave.setGameMode(3);
  55. }
  56. else if (wave.gameMode == 3)
  57. {
  58. wave.setGameMode(1);
  59. }
  60. }
  61. p.updateDir(e, true);
  62. }
  63. @Override
  64. public void keyReleased(KeyEvent e)
  65. {
  66. p.updateDir(e, false);
  67. }
  68. };
  69. frame.addKeyListener(key);
  70. panel = new JPanel()
  71. {
  72. protected void paintComponent(Graphics g)
  73. {
  74. //Paint Wave
  75. wave.draw(g);
  76. for(Bullet b: bullets)
  77. {
  78. b.draw(g);
  79. }
  80. for(Enemy en: enemy)
  81. {
  82. en.draw(g);
  83. }
  84. g.setColor(Color.BLACK);
  85. g.fillRect(0, fheight, fwidth, 150);
  86. p.draw(g);
  87. //Game Info Section
  88. //Wave
  89. g.setColor(Color.WHITE);
  90. g.setFont(new Font("Arial" , 1, 25));
  91. g.drawString("Wave: " + wave.waveNum, 50, fheight + 50);
  92. //Kills
  93. g.setColor(Color.WHITE);
  94. g.setFont(new Font("Arial" , 1, 25));
  95. g.drawString("Kills: " + wave.kills, 300, fheight + 50);
  96. //Health
  97. g.setColor(Color.WHITE);
  98. g.setFont(new Font("Arial" , 1, 25));
  99. g.drawString("Health: " + p.health, 500, fheight + 50);
  100. //Time
  101. g.setColor(Color.WHITE);
  102. g.setFont(new Font("Arial" , 1, 25));
  103. g.drawString("Time: " + wave.timeCount , 700, fheight + 50);
  104. //Pausing the game
  105. if(wave.gameMode == 3)
  106. {
  107. g.setColor(Color.WHITE);
  108. g.setFont(new Font("Arial" , 1, 40));
  109. g.drawString("PAUSED - PRESS ESC TO CONTINUE" , 75, fheight/2);
  110. }
  111. if(wave.gameMode == 2)
  112. {
  113. g.setColor(Color.RED);
  114. g.setFont(new Font("Arial" , 1, 40));
  115. g.drawString("You Died" , fwidth/2 - 100, fheight/2);
  116. }
  117. }
  118. };
  119. frame.add(panel);
  120. frame.setVisible(true);
  121. //Timer
  122. ActionListener tic = new ActionListener()
  123. {
  124. //Override
  125. public void actionPerformed(ActionEvent e)
  126. {
  127. if(wave.gameMode == 1)
  128. {
  129. p.move();
  130. for(int i = 0; i < bullets.size(); i ++)
  131. {
  132. try
  133. {
  134. bullets.get(i).move();
  135. }
  136. catch(Exception ex)
  137. {
  138. }
  139. }
  140. for(int i = 0; i < enemy.size(); i++)
  141. {
  142. try
  143. {
  144. enemy.get(i).move();
  145. }
  146. catch(Exception ex)
  147. {
  148. }
  149. }
  150. }
  151. panel.repaint();
  152. }
  153. };
  154. move = new Timer(30, tic); //Timer that moves things... Moves the player and enemies?
  155. move.start();
  156. }
  157. void newGame()
  158. {
  159. bullets = new ArrayList<Bullet>();
  160. enemy = new ArrayList<Enemy>();
  161. p = new Player();
  162. wave = new Wave();
  163. }
  164. public static void main(String[] arguments)
  165. {
  166. Tanks game = new Tanks();
  167. }
  168. /*
  169. Sub-classes that require the fields in tanks
  170. */
  171. /*
  172. the player class which extends the living class
  173. a player has a turret and an int kills
  174. a player has a specialized move method
  175. a player has a update direction method which takes in a keyevent
  176. */
  177. private class Player extends Living
  178. {
  179. private Turret t;
  180. private boolean up,down,left,right, rleft, rRight;
  181. public Player()
  182. {
  183. t=new Turret(this);
  184. speed = 5;
  185. x = frame.getWidth()/2;
  186. y = frame.getHeight()/2;
  187. health = 100;
  188. this.imageLocation = "player.png";
  189. super.loadImage();
  190. width = 50;
  191. height = 50;
  192. }
  193. void move()
  194. {
  195. if(rleft)
  196. {
  197. t.rotate(-1);
  198. }
  199. if(rRight)
  200. {
  201. t.rotate(1);
  202. }
  203. if(up==true)
  204. {
  205. super.move(-1);
  206. t.move(-1);
  207. }
  208. if(down==true)
  209. {
  210. t.move(1);
  211. super.move(1);
  212. }
  213. if(left==true)
  214. {
  215. super.direction -=5;
  216. t.rotate(-1);
  217. }
  218. if(right==true)
  219. {
  220. super.direction+=5;
  221. t.rotate(1);
  222. }
  223. if(y<=0)
  224. {
  225. y+=speed;
  226. }
  227. else if(x<=0)
  228. {
  229. x+=speed;
  230. }
  231. else if(x>=fwidth-width)
  232. {
  233. x-=speed;
  234. }
  235. else if(y>=fheight-height)
  236. {
  237. y-=speed;
  238. }
  239. t.x = x;
  240. t.y = y;
  241. }
  242. void updateDir(KeyEvent e, boolean pressed)
  243. {
  244. int id=e.getKeyCode();
  245. if(id== KeyEvent.VK_UP)
  246. {
  247. up=pressed;
  248. }
  249. else if(id==KeyEvent.VK_DOWN)
  250. {
  251. down=pressed;
  252. }
  253. else if(id==KeyEvent.VK_LEFT)
  254. {
  255. left=pressed;
  256. }
  257. else if(id==KeyEvent.VK_RIGHT)
  258. {
  259. right=pressed;
  260. }
  261. else if(id==KeyEvent.VK_A)
  262. {
  263. rleft = pressed;
  264. }
  265. else if(id==KeyEvent.VK_D)
  266. {
  267. rRight = pressed;
  268. }
  269. else if(id==KeyEvent.VK_SPACE)
  270. {
  271. if(pressed)
  272. {
  273. shoot();
  274. }
  275. }
  276. }
  277. void shoot()
  278. {
  279. bullets.add(new Bullet (t));
  280. }
  281. public void draw (Graphics g)
  282. {
  283. super.draw(g);
  284. t.draw(g);
  285. }
  286. }
  287. /*
  288. A zombie extends Enemy
  289. a zombie has a specialized move method which
  290. moves it twards the player
  291. the move method checks to see if it collides with the player
  292. if so it removes itself from the arraylist in tanks and deducts damage
  293. */
  294. private class Zombie extends Enemy
  295. {
  296. //constructor instanciated fields
  297. public Zombie()
  298. {
  299. super();
  300. this.spawn(frame);
  301. width = 30;
  302. height = 30;
  303. health = 10;
  304. isAlive=true;
  305. speed = 3;
  306. imageLocation = "zombie.png";
  307. super.loadImage();
  308. }
  309. //uses super to move player if collision then removes zombie and player
  310. //takes damage
  311. public void move()
  312. {
  313. direction = angleToPlayer(p);
  314. super.move(-1);
  315. if(this.checkCollision(p))
  316. {
  317. enemy.remove(this);
  318. p.takeDamage();
  319. if(!p.isAlive)
  320. {
  321. wave.setGameMode(2);
  322. wave.kills++;
  323. }
  324. }
  325. }
  326. }
  327. /*
  328. a bullet moves at a specified angle
  329. if the bullet collides with a enemy it gives damage
  330. if the bullet goes off the screen it removes itself from the arraylist
  331. */
  332. private class Bullet extends RotationalElement
  333. {
  334. public boolean enemyBullet; // whether it is an enemy or player bullet
  335. public Bullet(RotationalElement e)
  336. {
  337. width = 25;
  338. height = 25;
  339. x = e.x + 12;
  340. y = e.y + 12;
  341. direction = e.direction;
  342. speed = 10;
  343. imageLocation = "bullet.png";
  344. super.loadImage();
  345. }
  346. //Moving the bullet
  347. public void move()
  348. {
  349. super.move(-1);
  350. //Checks if the bullet goes off screen, if so... it get removed
  351. if(x < 0 || x > frame.getWidth())
  352. {
  353. bullets.remove(this);
  354. }
  355. if (y < 0 || y > frame.getHeight())
  356. {
  357. bullets.remove(this);
  358. }
  359. //Checks for collision with enemies, enemy takes damage and byllet is removed
  360. for(int i = 0; i < enemy.size(); i++)
  361. {
  362. if(enemyBullet == false)
  363. {
  364. boolean collided = this.checkCollision(enemy.get(i));
  365. if(collided)
  366. {
  367. enemy.get(i).takeDamage();
  368. bullets.remove(this);
  369. if(!enemy.get(i).isAlive)
  370. {
  371. enemy.remove(enemy.get(i));
  372. wave.kills++;
  373. }
  374. }
  375. }
  376. }
  377. //Checking enemy bullet collision with the player, player takes damage and bullet is removed
  378. if(enemyBullet)
  379. {
  380. boolean collided = this.checkCollision(p);
  381. if(collided)
  382. {
  383. p.takeDamage();
  384. bullets.remove(this);
  385. if(!p.isAlive)
  386. {
  387. wave.setGameMode(2);
  388. }
  389. }
  390. }
  391. }
  392. }
  393. /*
  394. a turret is drawn ontop of the tank
  395. a turret can rotate and shoot bullets,
  396. if rotate is called, put in -1 or 1, if -1, it will
  397. turn left, 1 will cause
  398. it to turn right
  399. */
  400. private class Turret extends RotationalElement
  401. {
  402. public Turret(RotationalElement e)
  403. {
  404. width = 50;
  405. height = 50;
  406. x = e.x;
  407. y = e.y;
  408. direction = e.direction;
  409. speed = 10;
  410. imageLocation = "turret.png";
  411. super.loadImage();
  412. }
  413. public void shoot()
  414. {
  415. //bullets.add(new Bullet (this));
  416. }
  417. public void rotate(int e)
  418. {
  419. super.direction = super.direction + 5*e;
  420. }
  421. }
  422. /*
  423. */
  424. private class Tank extends Enemy
  425. {
  426. Bullet b;
  427. Turret t;
  428. Timer tim;
  429. ActionListener al;
  430. public Tank()
  431. {
  432. super();
  433. enemy.add(this);
  434. this.spawn(frame);
  435. width = 50;
  436. height = 50;
  437. health = 20;
  438. isAlive=true;
  439. speed = 2;
  440. imageLocation = "enemyTank.png";
  441. super.loadImage();
  442. t = new Turret(this);
  443. t.imageLocation = "enemyTurret.png";
  444. t.loadImage();
  445. al = new ActionListener()
  446. {
  447. @Override
  448. public void actionPerformed(ActionEvent e)
  449. {
  450. if(getAlive())
  451. {
  452. b = new Bullet(t);
  453. b.enemyBullet=true;
  454. bullets.add(b);
  455. }
  456. }
  457. };
  458. tim = new Timer(3500,al);
  459. tim.start();
  460. }
  461. public void draw (Graphics g)
  462. {
  463. super.draw(g);
  464. t.draw(g);
  465. }
  466. public void move()
  467. {
  468. if(this.distToPlayer(p)>250)
  469. {
  470. direction = angleToPlayer(p);
  471. super.move(-1);
  472. if(this.checkCollision(p))
  473. {
  474. p.takeDamage();
  475. this.takeDamage();
  476. if(isAlive==false)
  477. {
  478. enemy.remove(this);
  479. }
  480. if(!p.isAlive)
  481. {
  482. wave.setGameMode(2);
  483. }
  484. }
  485. }
  486. t.x=this.x;
  487. t.y=this.y;
  488. t.direction=this.angleToPlayer(p);
  489. }
  490. }
  491. private class Wave extends DrawableElement
  492. {
  493. /*fields time is continous while playing, gameMode(1=playing,
  494. 2=paused, 3=menu. spawntime keeps a countdown until next spawn,
  495. kills keeps track of kills duh.
  496. */
  497. int timeCount, kills, gameMode, waveNum;
  498. Timer spawn, time;
  499. //constuctor
  500. public Wave()
  501. {
  502. timeCount=0;
  503. kills=0;
  504. gameMode=1;
  505. waveNum=1;
  506. imageLocation = "wave.jpg";
  507. super.loadImage();
  508. //actionlistener calls spawn every 2 seconds
  509. ActionListener s = new ActionListener()
  510. {
  511. @Override
  512. public void actionPerformed(ActionEvent e)
  513. {
  514. spawn();
  515. }
  516. };
  517. //action listener increments time
  518. spawn = new Timer(2000,s);
  519. spawn.start();
  520. ActionListener t = new ActionListener()
  521. {
  522. @Override
  523. public void actionPerformed(ActionEvent e)
  524. {
  525. if(gameMode==1)
  526. {
  527. timeCount++;
  528. }
  529. }
  530. };
  531. time = new Timer(1000,t);
  532. time.start();
  533. }
  534. public void setGameMode(int newGameMode)
  535. {
  536. gameMode = newGameMode;
  537. if(gameMode == 2)
  538. {
  539. //Player has died, ending the game
  540. time.stop();
  541. move.stop();
  542. }
  543. }
  544. //spawn method checks if spawn timer ==0 and if so then spawns an
  545. //enemy
  546. public void spawn()
  547. {
  548. Enemy temp = new Zombie();
  549. temp.spawn(frame);
  550. enemy.add(temp);
  551. enemy.add(new Tank());
  552. }
  553. //timer
  554. }
  555. }