Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.61 KB | None | 0 0
  1. /**
  2. *
  3. */
  4. package isu;
  5.  
  6. /**
  7. * @author James Muffitt
  8. * @date June 16, 2019
  9. * @title Breakout
  10. * @description Simple game very similar to space invaders.
  11. *
  12. */
  13. import java.awt.*;
  14. import java.applet.Applet;
  15.  
  16. public class Breakout extends Applet implements Runnable
  17. {
  18. Dimension d;
  19. Font largefont = new Font("Helvetica", Font.BOLD, 24);
  20. Font smallfont = new Font("Helvetica", Font.BOLD, 14);
  21. FontMetrics fmsmall, fmlarge;
  22. Graphics goff;
  23. Image ii;
  24. Thread thethread;
  25. boolean oneplayer=false;
  26. boolean ingame=false; int player1score;int player2score; int player1lives; int player2lives;
  27. int player1pos;
  28. int player1dpos;
  29. boolean player1fire;
  30. int player2pos;
  31. int player2dpos;
  32. boolean player2fire;
  33. int player1y;
  34. int player2y;
  35. int bullet1xpos;
  36. int bullet1ypos;
  37. int bullet2xpos;
  38. int bullet2ypos;
  39. int[] ballx;
  40. int[] bally;
  41. int[] balldx;
  42. int[] ballampl;
  43. int[] ballpos;
  44. boolean[] ballinstore;
  45. final int numballs=8;
  46. final int storeheight=48;
  47. final int borderwidth=5;
  48. final int ballsize=6;
  49. final int bounceperiod=128;
  50. final int scoreheight=20;
  51. final int screendelay=300;
  52. final int gutterheight=24;
  53. final int turretwidth=17;
  54. final int bulletspeed=4;
  55. final int maxballdelta=48;
  56. final int minampl=32;
  57. int count;
  58. boolean showtitle=true;
  59. Color ballcolor;
  60. Color turret1color;
  61. Color turret2color;
  62. int ballsinplay;
  63. int balldelta;
  64. public String getAppletInfo()
  65. {
  66. return("James Muffitt - ISU");
  67. }
  68. public void init()
  69. {
  70. Graphics g;
  71. int i;
  72. d = size();
  73. g=getGraphics();
  74. g.setFont(smallfont);
  75. fmsmall = g.getFontMetrics();
  76. g.setFont(largefont);
  77. fmlarge = g.getFontMetrics();
  78. ballx=new int[numballs];
  79. bally=new int[numballs];
  80. balldx=new int[numballs];
  81. ballampl=new int[numballs];
  82. ballpos=new int[numballs];
  83. ballinstore=new boolean[numballs];
  84. ballcolor=new Color(255,0,0);
  85. turret1color=new Color(255,192,128);
  86. turret2color=new Color(192,255,128);
  87. GameInit();
  88. }
  89.  
  90. public void GameInit()
  91. {
  92. int i;
  93.  
  94. for (i=0; i<numballs; i++)
  95. {
  96. ballpos[i]=(int)(Math.random()*(float)(bounceperiod));
  97. ballx[i]=borderwidth*2+(int)(Math.random()*(float)(d.width-ballsize-4*borderwidth));
  98. ballampl[i]=storeheight-ballsize;
  99. bally[i]=storeheight+borderwidth-ballsize-
  100. (int)((float)ballampl[i]*Math.sin(3.141592*ballpos[i]/bounceperiod));
  101. if (Math.random()>=0.5)
  102. balldx[i]=1;
  103. else
  104. balldx[i]=-1;
  105. ballinstore[i]=true;
  106. }
  107. player1score=0;
  108. player2score=0;
  109. player1lives=3;
  110. player2lives=3;
  111. player1fire=false;
  112. player2fire=false;
  113. player1dpos=0;
  114. player2dpos=0;
  115. player1y=d.height-scoreheight-borderwidth*2-gutterheight-1;
  116. player2y=player1y;
  117. player1pos=0+borderwidth+32;
  118. player2pos=d.width-borderwidth-32-turretwidth;
  119. bullet1xpos=-1;
  120. bullet1ypos=-1;
  121. bullet2xpos=-1;
  122. bullet2ypos=-1;
  123. ballsinplay=0;
  124. balldelta=6;
  125. }
  126.  
  127.  
  128. public boolean keyDown(Event e, int key)
  129. {
  130. if (ingame)
  131. {
  132. if (!oneplayer)
  133. {
  134. if (key == '1')
  135. player2dpos=-3;
  136. if (key == '3')
  137. player2dpos=3;
  138. if (key == '2')
  139. player2fire=true;
  140. }
  141. if (key == 'z' || key == 'Z' )
  142. player1dpos=-3;
  143. if (key == 'c' || key == 'C' )
  144. player1dpos=3;
  145. if (key == 'x' || key == 'X' )
  146. player1fire=true;
  147. if (key == Event.ESCAPE)
  148. ingame=false;
  149. }
  150. else
  151. {
  152. if (key == '1')
  153. {
  154. oneplayer=true;
  155. GameInit();
  156. ingame=true;
  157. }
  158. if (key == '2')
  159. {
  160. oneplayer=false;
  161. GameInit();
  162. ingame=true;
  163. }
  164. }
  165. return true;
  166. }
  167.  
  168. public boolean keyUp(Event e, int key)
  169. {
  170. if (key == '1' || key == '3')
  171. player2dpos=0;
  172. if (key == 'z' || key == 'Z' || key == 'c' || key == 'C')
  173. player1dpos=0;
  174. return true;
  175. }
  176.  
  177. public void paint(Graphics g)
  178. {
  179. String s;
  180. Graphics gg;
  181.  
  182. if (goff==null && d.width>0 && d.height>0)
  183. {
  184. ii = createImage(d.width, d.height);
  185. goff = ii.getGraphics();
  186. }
  187. if (goff==null || ii==null)
  188. return;
  189.  
  190. goff.setColor(new Color(16,24,64));
  191. goff.fillRect(0, 0, d.width, d.height);
  192. if (ingame)
  193. PlayGame();
  194. else
  195. ShowIntroScreen();
  196. g.drawImage(ii, 0, 0, this);
  197. }
  198.  
  199.  
  200. public void PlayGame()
  201. {
  202. DrawPlayField();
  203. HandleKeys();
  204. DrawTurrets();
  205. CheckBallsInPlay();
  206. DoBalls();
  207. DoBullets();
  208. ShowScore();
  209.  
  210. if (oneplayer)
  211. {
  212. if (player1lives<=0 && player1pos>=d.width)
  213. ingame=false;
  214. }
  215. else
  216. {
  217. if (player1lives<=0 && player2lives<=0 && player1pos>=d.width && player2pos>=d.width)
  218. ingame=false;
  219. }
  220. }
  221.  
  222.  
  223. void HandleKeys()
  224. {
  225. player1pos+=player1dpos;
  226. if (player1pos<=borderwidth)
  227. player1pos=borderwidth;
  228. if (player1pos>=(d.width-borderwidth-turretwidth-1) && player1lives>0)
  229. player1pos=d.width-borderwidth-turretwidth-1;
  230. if (player1fire && bullet1xpos<0 && bullet1ypos<0)
  231. {
  232. bullet1ypos=d.height-scoreheight-gutterheight-ballsize*2-borderwidth*2;
  233. bullet1xpos=player1pos+turretwidth/2;
  234. }
  235. player1fire=false;
  236.  
  237. if (oneplayer)
  238. return;
  239.  
  240. player2pos+=player2dpos;
  241. if (player2pos<=borderwidth)
  242. player2pos=borderwidth;
  243. if (player2pos>=(d.width-borderwidth-turretwidth-1) && player2lives>0)
  244. player2pos=d.width-borderwidth-turretwidth-1;
  245. if (player2fire && bullet2xpos<0 && bullet2ypos<0)
  246. {
  247. bullet2ypos=d.height-scoreheight-gutterheight-ballsize*2-borderwidth*2;
  248. bullet2xpos=player2pos+turretwidth/2;
  249. }
  250. player2fire=false;
  251. }
  252.  
  253. void CheckBallsInPlay()
  254. {
  255. boolean none=true;
  256. int i;
  257.  
  258. for (i=0; i<numballs; i++)
  259. {
  260. if (!ballinstore[i])
  261. none=false;
  262. }
  263. if (none)
  264. {
  265. if (ballsinplay<numballs)
  266. ballsinplay++;
  267. else
  268. if (balldelta<=maxballdelta)
  269. balldelta+=4;
  270.  
  271. for (i=0; i<ballsinplay; i++)
  272. {
  273. ballinstore[i]=false;
  274. ballampl[i]=d.height-4*borderwidth-scoreheight-storeheight-ballsize-gutterheight;
  275. ballpos[i]=bounceperiod/2;
  276. }
  277. }
  278. }
  279.  
  280. void ResetBall(int i)
  281. {
  282. ballinstore[i]=true;
  283. ballampl[i]=storeheight-ballsize;
  284. ballx[i]=borderwidth*2+(int)(Math.random()*(float)(d.width-ballsize-4*borderwidth));
  285. bally[i]=storeheight+borderwidth-ballsize-
  286. (int)((float)ballampl[i]*Math.sin(3.141592*ballpos[i]/bounceperiod));
  287. }
  288.  
  289.  
  290. boolean DrawBullet(int x, int y)
  291. {
  292. int i;
  293. goff.setColor(Color.white);
  294. goff.drawLine(x,y,x,y+ballsize);
  295.  
  296. for (i=0; i<numballs; i++)
  297. {
  298. if ((bally[i]+ballsize)>=y && bally[i]<=(y+ballsize) &&
  299. (ballx[i]+ballsize)>=x && ballx[i]<=x)
  300. {
  301. ResetBall(i);
  302. return true;
  303. }
  304. }
  305. return false;
  306. }
  307.  
  308.  
  309. void DoBullets()
  310. {
  311. if (bullet1xpos>0 && bullet1ypos>0)
  312. {
  313. if (DrawBullet(bullet1xpos,bullet1ypos))
  314. {
  315. player1score+=10;
  316. bullet1xpos=-1;
  317. bullet1ypos=-1;
  318. }
  319. else
  320. {
  321. bullet1ypos-=bulletspeed;
  322. if (bullet1ypos<=(2*borderwidth+storeheight))
  323. {
  324. bullet1ypos=-1;
  325. bullet1xpos=-1;
  326. }
  327. }
  328. }
  329.  
  330. if (oneplayer)
  331. return;
  332.  
  333. if (bullet2xpos>0 && bullet2ypos>0)
  334. {
  335. if (DrawBullet(bullet2xpos,bullet2ypos))
  336. {
  337. player2score+=10;
  338. bullet2xpos=-1;
  339. bullet2ypos=-1;
  340. }
  341. else
  342. {
  343. bullet2ypos-=bulletspeed;
  344. if (bullet2ypos<=(2*borderwidth+storeheight))
  345. {
  346. bullet2ypos=-1;
  347. bullet2xpos=-1;
  348. }
  349. }
  350. }
  351. }
  352.  
  353. boolean DrawTurret(int x, int y)
  354. {
  355. int x1=x;
  356. int y1=y;
  357. int x2=turretwidth+x;
  358. boolean hitball=false;
  359. int i;
  360.  
  361. while (x1<x2)
  362. {
  363. for (i=0; i<numballs; i++)
  364. {
  365. if ((bally[i]+ballsize>=y1) && bally[i]<=y1 && (ballx[i]+ballsize)>=x1 && ballx[i]<=x2)
  366. {
  367. hitball=true;
  368. ResetBall(i);
  369. }
  370. }
  371.  
  372. goff.drawLine(x1,y1,x2,y1);
  373. y1--;
  374. x1++;
  375. x2--;
  376. }
  377. return hitball;
  378. }
  379.  
  380.  
  381. void DrawBall(int x, int y)
  382. {
  383. goff.fillRect(x,y+1,ballsize,ballsize-2);
  384. goff.drawLine(x+1,y,x+ballsize-2,y);
  385. goff.drawLine(x+1,y+ballsize-1,x+ballsize-2,y+ballsize-1);
  386. }
  387.  
  388.  
  389. void DoBalls()
  390. {
  391. int i;
  392.  
  393. goff.setColor(ballcolor);
  394. for (i=0; i<numballs; i++)
  395. {
  396. if (ballinstore[i])
  397. {
  398. bally[i]=storeheight+borderwidth-ballsize-
  399. (int)((float)ballampl[i]*Math.sin(3.141592*ballpos[i]/bounceperiod));
  400. ballpos[i]=(ballpos[i]+1)%bounceperiod;
  401. ballx[i]=ballx[i]+balldx[i];
  402. if (ballx[i]<=borderwidth || ballx[i]>=(d.width-borderwidth-ballsize))
  403. balldx[i]=-balldx[i];
  404. DrawBall(ballx[i],bally[i]);
  405. }
  406. else
  407. {
  408. if (bally[i]<borderwidth*2+storeheight)
  409. bally[i]+=2;
  410. else
  411. {
  412. bally[i]=d.height-gutterheight-2*borderwidth-scoreheight-ballsize-
  413. (int)((float)ballampl[i]*Math.sin(3.141592*ballpos[i]/bounceperiod));
  414. ballpos[i]=(ballpos[i]+1)%bounceperiod;
  415. if (ballpos[i]==0)
  416. {
  417. ballampl[i]-=balldelta;
  418. if (ballampl[i]<minampl)
  419. ballampl[i]=minampl;
  420. }
  421. }
  422. ballx[i]=ballx[i]+balldx[i];
  423. if (ballx[i]<=borderwidth || ballx[i]>=(d.width-borderwidth-ballsize))
  424. balldx[i]=-balldx[i];
  425. DrawBall(ballx[i],bally[i]);
  426. }
  427. }
  428. }
  429.  
  430.  
  431. public void ShowIntroScreen()
  432. {
  433. String s;
  434.  
  435. DrawPlayField();
  436. ShowScore();
  437. DoBalls();
  438.  
  439. goff.setFont(largefont);
  440. goff.setColor(new Color(96,128,255));
  441.  
  442. if (showtitle)
  443. {
  444. s="Breakout";
  445. goff.drawString(s,(d.width-fmlarge.stringWidth(s)) / 2, (d.height-scoreheight-borderwidth)/2 - 30);
  446. goff.setFont(smallfont);
  447. s="A game dedicated to Mr. Gabriele";
  448. goff.drawString(s,(d.width-fmsmall.stringWidth(s))/2,(d.height-scoreheight-borderwidth)/2);
  449. goff.setColor(new Color(255,160,64));
  450. s="(c)2019 by James Muffitt";
  451. goff.drawString(s,(d.width-fmsmall.stringWidth(s))/2,(d.height-scoreheight-borderwidth)/2 + 20);
  452. s="072937824";
  453. goff.drawString(s,(d.width-fmsmall.stringWidth(s))/2,(d.height-scoreheight-borderwidth)/2 + 40);
  454. }
  455. else
  456. {
  457. goff.setFont(smallfont);
  458. goff.setColor(new Color(96,128,255));
  459. s="'1'=1 player game, '2'=2 players";
  460. goff.drawString(s,(d.width-fmsmall.stringWidth(s))/2,(d.height-scoreheight-borderwidth)/2 - 20);
  461. goff.setColor(new Color(255,160,64));
  462. s="Player 1 use 'z', 'x' and 'c'";
  463. goff.drawString(s,(d.width-fmsmall.stringWidth(s))/2,(d.height-scoreheight-borderwidth)/2 + 10);
  464. s="Player 2 use '1, '2' and '3'";
  465. goff.drawString(s,(d.width-fmsmall.stringWidth(s))/2,(d.height-scoreheight-borderwidth)/2 + 30);
  466. }
  467. count--;
  468. if (count<=0)
  469. { count=screendelay; showtitle=!showtitle; }
  470. }
  471.  
  472.  
  473. void DrawTurrets()
  474. {
  475. int delta;
  476. goff.setColor(turret1color);
  477. if (player1lives==0)
  478. {
  479. if (player1y<d.height-scoreheight-borderwidth*2-1)
  480. player1y++;
  481. else if (player1pos<d.width+turretwidth)
  482. player1pos++;
  483. }
  484. if (DrawTurret(player1pos,player1y))
  485. {
  486. player1lives--;
  487. player1y+=2;
  488. }
  489.  
  490. if (oneplayer)
  491. return;
  492.  
  493. goff.setColor(turret2color);
  494. if (player2lives==0)
  495. {
  496. if (player2y<d.height-scoreheight-borderwidth*2-1)
  497. player2y++;
  498. else if (player2pos<d.width+turretwidth)
  499. player2pos++;
  500. }
  501. if (DrawTurret(player2pos,player2y))
  502. {
  503. player2lives--;
  504. player2y+=2;
  505. }
  506. }
  507.  
  508.  
  509. public void DrawPlayField()
  510. {
  511. goff.setColor(Color.white);
  512. goff.fillRect(0,0,d.width,borderwidth);
  513. goff.fillRect(0,0,borderwidth,d.height-scoreheight);
  514. goff.fillRect(d.width-borderwidth,0,borderwidth,d.height-scoreheight);
  515. goff.fillRect(0,0+borderwidth+storeheight,d.width,borderwidth);
  516. goff.fillRect(0,d.height-gutterheight-2*borderwidth-scoreheight,d.width,borderwidth);
  517. goff.fillRect(0,d.height-scoreheight-borderwidth,d.width,borderwidth);
  518. goff.setColor(new Color(128,128,255));
  519. goff.fillRect(borderwidth,d.height-borderwidth-gutterheight-scoreheight,
  520. d.width-borderwidth,gutterheight);
  521. }
  522.  
  523.  
  524. public void ShowScore()
  525. {
  526. String s;
  527. goff.setFont(smallfont);
  528. goff.setColor(Color.white);
  529.  
  530. s="Player 1: "+player1score;
  531. goff.drawString(s,borderwidth,d.height-5);
  532. if (!oneplayer)
  533. {
  534. s="Player 2: "+player2score;
  535. goff.drawString(s,d.width-borderwidth-fmsmall.stringWidth("Player 2: 00000"),d.height-5);}}
  536. public void run(){ long starttime; Graphics g;
  537. Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
  538. g=getGraphics();
  539. while(true){
  540. starttime=System.currentTimeMillis();
  541. try{
  542. paint(g);
  543. starttime += 20;
  544. Thread.sleep(Math.max(0, starttime-System.currentTimeMillis()));}
  545. catch (InterruptedException e){
  546. break;}}}public void start(){
  547. if (thethread == null) {
  548. thethread = new Thread(this);
  549. thethread.start();
  550. }
  551. }
  552.  
  553. public void stop()
  554. {
  555. if (thethread != null) {
  556. thethread.stop();
  557. thethread = null;
  558. }
  559. }
  560. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement