Advertisement
Guest User

Untitled

a guest
May 28th, 2015
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 29.50 KB | None | 0 0
  1. ////////////////////////////////////////////////////////////////
  2. ////////////////////////////////////////////////////////////////
  3. ///////Dear Marker, this is a little 'table of contents'////////
  4. ////////type thing to help you find the lines that you//////////
  5. ///////////////need to mark. You're welcome.////////////////////
  6. ////////////////////////////////////////////////////////////////
  7. ///////------------------------------------------------/////////
  8. ///////Player circular movment | 107-109 /////////
  9. ///////Player changes direction on key press | 498 /////////
  10. ///////Creation of wormhole | 123-145 /////////
  11. ///////Collision with wormhole | 427-432 /////////
  12. ///////Size changes of wormhole | 124-127 /////////
  13. ///////Stary background creation | 400-402 /////////
  14. ///////Warping of stars | 419-422 /////////
  15. ///////Score display | 407-410 /////////
  16. ///////Parallax of the stars | 586-596 /////////
  17. ///////warp() function | 25-27 /////////
  18. ///////blackHoleAt() function | 29-46 /////////
  19. ///////blackHoleAt() calls | 489-490 /////////
  20. ///////------------------------------------------------/////////
  21. ////////////////////////////////////////////////////////////////
  22. ////////////////////////////////////////////////////////////////
  23.  
  24.  
  25. boolean warp() {
  26. return playerCollision(wHoles.get(0)); //The first element will always be the original wormhole
  27. }
  28.  
  29. void blackHoleAt(float holeX, float holeY) { //also handles blcak hole collisions
  30. BlackHole b = new BlackHole(holeX, holeY);
  31. b.blit();
  32.  
  33. if (playerCollision(b)) {b.onPlayerCollision();}
  34. }
  35.  
  36.  
  37. ////////////////////////////////////////////////////////
  38. //////////////////////MAIN CLASSES//////////////////////
  39. ////////////////////////////////////////////////////////
  40.  
  41.  
  42. class Entity { //A base class for everything that will be drawn on screen
  43. color colour;
  44. color strk;
  45. int strkw;
  46. float size;
  47. float posx;
  48. float posy;
  49.  
  50.  
  51. Entity(float posx_, float posy_, int size_, color colour_, color strk_, int strkw_) {
  52. posx = posx_;
  53. posy = posy_;
  54. size = size_;
  55. colour = colour_;
  56. strk = strk_;
  57. strkw = strkw_;
  58. }
  59.  
  60. void update() {} //a function for overriding
  61. void onPlayerCollision() {}
  62. void collisions() {}
  63.  
  64. void blit() {
  65. fill(colour);
  66. strokeWeight(strkw);
  67. stroke(strk);
  68. ellipse(posx, posy, size, size);
  69. update();
  70. }
  71. }
  72.  
  73.  
  74. class Player extends Entity {
  75. float direction;
  76. float diameter;
  77. float speed;
  78. float jetsAngle;
  79. int score;
  80. int smoke;
  81.  
  82. Entity[] lSmoke;
  83. Entity[] rSmoke;
  84.  
  85. Player(float posx_, float posy_) {
  86. super(posx_, posy_, 20, color(19, 232, 141), color(188, 198, 255), 4);
  87. direction = 0.0;
  88. diameter = 5.0;
  89. speed = 0.03;
  90. score = 0;
  91. smoke = 20;
  92. jetsAngle = 0.6;
  93.  
  94. lSmoke = new Entity[smoke];
  95. rSmoke = new Entity[smoke];
  96.  
  97. lSmoke[0] = new Entity(posx, posy, smoke, color(188, 198, 255, 0), color(255, 255, 255, 100), 1);
  98. rSmoke[0] = new Entity(posx, posy, smoke, color(188, 198, 255, 0), color(255, 255, 255, 100), 1);
  99. for (int i=1; i<smoke; i++) {
  100. lSmoke[i] = new Entity(posx, posy, smoke-i, color(188, 198, 255, (1-(float(i))/smoke)*150), color(255, 255, 255, 0), 1);
  101. rSmoke[i] = new Entity(posx, posy, smoke-i, color(188, 198, 255, (1-(float(i))/smoke)*150), color(255, 255, 255, 0), 1);
  102. }
  103. }
  104.  
  105. void collision() {
  106. if (player.posx < 0) {player.posx = width;}
  107. if (player.posx > width) {player.posx = 0;}
  108. if (player.posy < 0) {player.posy = height;}
  109. if (player.posy > height) {player.posy = 0;}
  110. }
  111.  
  112. void update() {
  113. collision();
  114. posx += diameter * cos(direction);
  115. posy += diameter * sin(direction);
  116. direction += speed;
  117. }
  118.  
  119. void blit() {
  120. float g;
  121. float a;
  122.  
  123. BlackHole b1 = new BlackHole(100.0, 40.0);
  124. BlackHole b2 = new BlackHole(400.0, 500.0);
  125. bHoles.add(b1);
  126. bHoles.add(b2);
  127.  
  128. lSmoke[0].posx = posx+(size/4.0*cos(direction+ HALF_PI+jetsAngle));
  129. lSmoke[0].posy = posy+(size/2* sin(direction+ HALF_PI+jetsAngle));
  130. rSmoke[0].posx = posx+(size/4.0*cos(direction-(HALF_PI+jetsAngle)));
  131. rSmoke[0].posy = posy+(size/2* sin(direction-(HALF_PI+jetsAngle)));
  132. lSmoke[0].blit();
  133. rSmoke[0].blit();
  134. for (int i=smoke-1; i>0; i--) {
  135. lSmoke[i].posx = lSmoke[i-1].posx;
  136. lSmoke[i].posy = lSmoke[i-1].posy;
  137.  
  138. rSmoke[i].posx = rSmoke[i-1].posx;
  139. rSmoke[i].posy = rSmoke[i-1].posy;
  140.  
  141. if (i != 0) { //gravity handel
  142. for (Entity h : bHoles) {
  143. g = h.size*160/pow(dist(lSmoke[i].posx, lSmoke[i].posy, h.posx, h.posy), 2);
  144. a = getAngle(int(lSmoke[i].posx), int(lSmoke[i].posy), int(h.posx), int(h.posy));
  145. lSmoke[i].posx += cos(a)*g;
  146. lSmoke[i].posy += sin(a)*g;
  147.  
  148. g = h.size*160/pow(dist(rSmoke[i].posx, rSmoke[i].posy, h.posx, h.posy), 2);
  149. a = getAngle(int(rSmoke[i].posx), int(rSmoke[i].posy), int(h.posx), int(h.posy));
  150. rSmoke[i].posx += cos(a)*g;
  151. rSmoke[i].posy += sin(a)*g;
  152. }
  153. lSmoke[i].blit();
  154. rSmoke[i].blit();
  155. }
  156. }
  157. bHoles.remove(b1);
  158. bHoles.remove(b2);
  159.  
  160. fill(colour);
  161. strokeWeight(strkw);
  162. stroke(strk);
  163. ellipse(posx, posy, size, size);
  164.  
  165. noStroke();
  166. lSmoke[0].strk = color(255, 255, 255, 0);
  167. rSmoke[0].strk = color(255, 255, 255, 0);
  168. lSmoke[0].blit();
  169. rSmoke[0].blit();
  170. ellipse(posx, posy, size, size);
  171.  
  172. update();
  173. }
  174.  
  175. }
  176.  
  177. class WormHole extends Entity {
  178. float expansion;
  179.  
  180. WormHole(float posx_, float posy_) {
  181. super(posx_, posy_, 0, color(137, 49, 193), color(46, 100, 70), 3);
  182. expansion = 1;
  183. }
  184.  
  185. void onPlayerCollision() {
  186. posx = int(random(width));
  187. posy = int(random(height));
  188. shuffleStars();
  189. player.score += (dificulty+1);
  190. }
  191.  
  192. void update() {
  193. //the first 2 lines are for linear expansion, the second 2 are for sinosoidal expansion
  194.  
  195. //size += expansion;
  196. //if (size > 80 || size == 0) {expansion *= -1;}
  197. size = map(sin(expansion), -1, 1, 0, 80);
  198. expansion += 0.04;
  199.  
  200. }
  201.  
  202. void blit() {
  203. float n = 6.0; //how many variations of the colour you get. higher n means smother gradient.
  204.  
  205. fill(colour, 0);
  206. strokeWeight(strkw);
  207. stroke(strk);
  208. ellipse(posx, posy, size, size);
  209. noStroke();
  210. for (float i=1.0; i<n+1; i++) {
  211. fill(colour, 70);
  212. ellipse(posx, posy, (i/n)*size, (i/n)*size);
  213. }
  214.  
  215. update();
  216. }
  217.  
  218. }
  219.  
  220. class BlackHole extends Entity {
  221. int jiggle1;
  222. int jiggle2;
  223.  
  224. BlackHole(float posx_, float posy_) {
  225. super(posx_, posy_, (dificulty+3)*13, color(0), color(255), 20+dificulty*5); //the effective size of the hole will be size-strkw
  226. jiggle1 = int(random(-5, 6));
  227. jiggle2 = int(random(-5, 6));
  228. }
  229.  
  230. void onPlayerCollision() {state = "dead";}
  231.  
  232. void blit() {
  233. noStroke();
  234. for (float i=size+strkw; i>0; i--) {
  235. fill(255, 255, 255, (1-(i/(size+strkw)))*170.0);
  236. ellipse(posx, posy, i+jiggle1, i+jiggle2);
  237. }
  238. fill(0, 0, 0);
  239. ellipse(posx, posy, size, size);
  240.  
  241. update();
  242. }
  243.  
  244. void update() {
  245. jiggle1 = int(random(-5, 6));
  246. jiggle2 = int(random(-5, 6));
  247. }
  248.  
  249. }
  250.  
  251. class Star extends Entity {
  252. int speedx;
  253. int speedy;
  254. Entity[] tail;
  255.  
  256. Star(float posx_, float posy_) {
  257. super(posx_, posy_, 2, color(255), color(255), 1);
  258. speedx = 0;
  259. speedy = 0;
  260. tail = new Entity[4];
  261.  
  262. for (int i=0; i<tail.length; i++) {
  263. tail[i] = new Entity(posx, posy, 2, color(255), color(255, 255, 255, 0), 1);
  264. }
  265. }
  266.  
  267. void collisions() {
  268. if (posx < 0) {posx = width; posy = int(random(height));}
  269. if (posx > width) {posx = 0; posy = int(random(height));}
  270. if (posy < 0) {posy = height; posx = int(random(width));}
  271. if (posy > height) {posy = 0; posx = int(random(width));}
  272. }
  273.  
  274. void update() {
  275. posx += speedx;
  276. posy += speedy;
  277. }
  278.  
  279. void blit() {
  280. tail[0].posx = posx;
  281. tail[0].posy = posy;
  282. tail[0].blit();
  283. for (int i=tail.length-1; i>0; i--) {
  284. tail[i].posx = tail[i-1].posx;
  285. tail[i].posy = tail[i-1].posy;
  286. tail[i].blit();
  287. }
  288.  
  289. fill(colour);
  290. noStroke();
  291. ellipse(posx, posy, size, size);
  292. update();
  293. collisions();
  294. }
  295. }
  296.  
  297. class Score {
  298. String name;
  299. int score;
  300.  
  301. Score(String name_, int score_) {
  302. name = name_;
  303. score = score_;
  304. }
  305. }
  306.  
  307. class ScoreList {
  308. float posx;
  309. float posy;
  310. float speed;
  311. Score[] scores;
  312.  
  313. ScoreList(float posx_, float posy_, Score[] scores_) {
  314. posx = posx_;
  315. posy = posy_;
  316. scores = scores_;
  317. speed = 0;
  318. }
  319.  
  320. void update() {
  321. posy += speed;
  322.  
  323. if (posy > 10) {posy = 10;}
  324. if (posy < -25*(scores.length)) {posy = -25*(scores.length);}
  325. }
  326.  
  327. void blit() {
  328. textSize(20);
  329. fill(255);
  330. textAlign(LEFT, TOP);
  331. text("NAME:", posx, posy);
  332. text("SCORE:", posx+70, posy);
  333. for (int i=0; i<scores.length; i++) {
  334. text(scores[i].name, posx, posy+(25*(i+1)));
  335. text(scores[i].score, posx+70, posy+(25*(i+1)));
  336. }
  337. update();
  338. }
  339. }
  340.  
  341. //////////////////////////////////////////////
  342. /////////////////Power Ups////////////////////
  343. //////////////////////////////////////////////
  344.  
  345. class PowerUp extends Entity {
  346. char letter;
  347. color letterColour;
  348. int lifeSpan;
  349. int lifeCount;
  350. int effectSpan;
  351. int effectCount;
  352. boolean effective;
  353. float rate;
  354.  
  355. PowerUp(float posx_, float posy_, char letter_, color letterColour_, float rate_) {
  356. super(posx_, posy_, 25, color(255, 255, 255, 0), color(200, 255, 0), 3);
  357. letter = letter_;
  358. letterColour = letterColour_;
  359. lifeSpan = 60*5; //5 seconds
  360. lifeCount = 0;
  361. effectSpan = lifeSpan;
  362. effectCount = 0;
  363. effective = false;
  364. rate = rate_;
  365. }
  366.  
  367. void effect(){}
  368. void defect(){}
  369. void extra(){}
  370.  
  371. void update() {
  372. if (effective) {
  373. effectCount++;
  374. extra();
  375. }
  376. else {
  377. lifeCount++;
  378. }
  379. }
  380.  
  381. void blit() {
  382. if (!effective) {
  383. fill(255, 255, 255, 255/3);
  384. noStroke();
  385. arc(posx, posy, size, size, -HALF_PI, (float(lifeCount)/lifeSpan)*TWO_PI-HALF_PI, PIE);
  386.  
  387. fill(colour);
  388. strokeWeight(strkw);
  389. stroke(strk);
  390. ellipse(posx, posy, size, size);
  391.  
  392. fill(letterColour);
  393. textSize(15);
  394. textAlign(CENTER, CENTER);
  395. text(letter, posx, posy-2);
  396. }
  397.  
  398. update();
  399. }
  400. }
  401.  
  402. class SpeedUp extends PowerUp {
  403. SpeedUp(float posx_, float posy_) {
  404. super(posx_, posy_, 'F', color(100, 100, 220), 1.7);
  405. }
  406.  
  407. void effect() {
  408. player.speed *= rate;
  409. player.diameter *= rate;
  410. }
  411.  
  412. void defect() {
  413. player.speed /= rate;
  414. player.diameter /= rate;
  415. }
  416.  
  417. }
  418.  
  419. class SlowDown extends PowerUp {
  420. SlowDown(float posx_, float posy_) {
  421. super(posx_, posy_, 'S', color(255, 200, 225), 1.7);
  422. }
  423.  
  424. void effect() {
  425. player.speed /= rate;
  426. player.diameter /= rate;
  427. }
  428.  
  429. void defect() {
  430. player.speed *= rate;
  431. player.diameter *= rate;
  432. }
  433.  
  434. }
  435.  
  436. class Reverse extends PowerUp {
  437. Reverse(float posx_, float posy_) {
  438. super(posx_, posy_, 'R', color(242, 162, 0), 1);
  439. }
  440.  
  441. void effect() {
  442. player.speed *= -rate;
  443. player.diameter *= -rate;
  444. }
  445.  
  446. void defect() {
  447. effect();
  448. }
  449.  
  450. }
  451.  
  452. class Grow extends PowerUp {
  453. Grow(float posx_, float posy_) {
  454. super(posx_, posy_, 'G', color(40, 215, 230), 15);
  455. }
  456.  
  457. void effect() {
  458. player.size += rate;
  459. }
  460.  
  461. void defect() {
  462. player.size -= rate;
  463. }
  464.  
  465. }
  466.  
  467. class Shrink extends PowerUp {
  468. Shrink(float posx_, float posy_) {
  469. super(posx_, posy_, 'D', color(100, 40, 230), 15);
  470. }
  471.  
  472. void effect() {
  473. player.size -= rate;
  474. }
  475.  
  476. void defect() {
  477. player.size += rate;
  478. }
  479.  
  480. }
  481.  
  482. class Protect extends PowerUp {
  483. Protect(float posx_, float posy_) {
  484. super(posx_, posy_, 'P', color(0, 210, 0), 20);
  485. }
  486.  
  487. void extra() {
  488. fill(0, 0, 0, 0);
  489.  
  490. for (float i=0; i<rate; i++) { //draws halo around player
  491. stroke(0, 210, 0, 12);
  492. strokeWeight(i);
  493. ellipse(player.posx-(player.diameter * cos(player.direction)), player.posy-(player.diameter * sin(player.direction)), player.size*2, player.size*2);
  494. }
  495.  
  496. while (playerCollision(new BlackHole(100, 40))) {
  497. player.update();
  498. }
  499. while (playerCollision(new BlackHole(400, 500))) {
  500. player.update();
  501. }
  502. for (Entity hole : bHoles) {
  503. while (playerCollision(hole)) {
  504. player.update();
  505. }
  506. }
  507. }
  508. }
  509.  
  510. class ExtraBHole extends PowerUp {
  511. BlackHole hole;
  512.  
  513. ExtraBHole(float posx_, float posy_) {
  514. super(posx_, posy_, 'B', color(160, 160, 160), 15);
  515. hole = new BlackHole(random(width), random(height));
  516. }
  517.  
  518. void effect() {
  519. bHoles.add(hole);
  520. }
  521.  
  522. void defect() {
  523. bHoles.remove(hole);
  524. }
  525. }
  526.  
  527. class ExtraWHole extends PowerUp {
  528. WormHole hole;
  529.  
  530. ExtraWHole(float posx_, float posy_) {
  531. super(posx_, posy_, 'W', wHoles.get(0).colour, 15);
  532. hole = new WormHole(random(width), random(height));
  533. }
  534.  
  535. void effect() {
  536. wHoles.add(hole);
  537. }
  538.  
  539. void defect() {
  540. wHoles.remove(hole);
  541. }
  542. }
  543.  
  544.  
  545. //////////////////////////////////////////////
  546. ////////////////BUTTON CLASSES////////////////
  547. //////////////////////////////////////////////
  548.  
  549. class Button {
  550. int sizex;
  551. int sizey;
  552. String textt;
  553. color colour;
  554. boolean hover;
  555.  
  556. Button(String textt_, int sizex_, int sizey_) {
  557. sizex = sizex_;
  558. sizey = sizey_;
  559. textt = textt_;
  560. }
  561.  
  562. void update(float posx, float posy) {
  563. if (mouseX >= posx-sizex/2 && mouseX <= posx+sizex/2 && mouseY >= posy-sizey/2 && mouseY <= posy+sizey/2) {
  564. colour = color(255, 255, 255, 127);
  565. if (mouseClick) { //Only executes on mouseClicked as opposed to mousePressed
  566. exec();
  567. }
  568. }
  569. else {
  570. colour = color(255, 255, 255, 0);
  571. }
  572. }
  573.  
  574. void blit(float posx, float posy) {
  575. update(posx, posy);
  576. fill(colour);
  577. stroke(255);
  578. strokeWeight(3);
  579. rect(posx-sizex/2, posy-sizey/2, sizex, sizey, 10);
  580.  
  581. fill(255);
  582. textSize(40);
  583. textAlign(CENTER, CENTER);
  584. text(textt, posx, posy-7);
  585. }
  586.  
  587. void exec() {
  588. //This function differes from button to button
  589. };
  590.  
  591. }
  592.  
  593. class AgainButton extends Button {
  594. AgainButton(String textt_) {
  595. super(textt_, 150, 50);
  596. }
  597.  
  598. void exec() { //resets the game
  599. state = "game";
  600. player = new Player(width/2, height/2);
  601. submited = false;
  602. powers = new ArrayList<PowerUp>();
  603. wHoles.clear();
  604. bHoles.clear();
  605. wHoles.add(new WormHole(int(random(width)), int(random(height))));
  606. shuffleStars();
  607. }
  608. }
  609.  
  610. class ExitButton extends Button {
  611. ExitButton() {
  612. super("Exit", 150, 50);
  613. }
  614.  
  615. void exec() {
  616. exit();
  617. }
  618. }
  619.  
  620. class StateButton extends Button {
  621. String newState;
  622. StateButton(String textt_, String state_) {
  623. super(textt_, 150, 50);
  624. newState = state_;
  625. }
  626.  
  627. void exec() {
  628. state = newState;
  629. }
  630. }
  631.  
  632. class ScoreButton extends Button {
  633. ScoreButton() {
  634. super("Scores", 150, 50);
  635. }
  636.  
  637. void exec() {
  638. state = "high";
  639. scores = new ScoreList(width*(2/3.0), 10, quickSort(loadScores()));
  640. }
  641. }
  642.  
  643. class SubmitButton extends Button {
  644. SubmitButton() {
  645. super("Submit", 170, 50);
  646. }
  647.  
  648. void exec() {
  649. if (name.length() != 0) {
  650. submited = true;
  651. appendToFile("scores.txt", name+'|'+str(player.score));
  652. }
  653. }
  654. }
  655.  
  656. class ChDiffButton extends Button {
  657. int change;
  658.  
  659. ChDiffButton(String textt_, int change_) {
  660. super(textt_, 30, 50);
  661. change = change_;
  662. }
  663.  
  664. void exec() {
  665. dificulty += change;
  666. if (dificulty > diffs.length-1) {dificulty = 0;}
  667. else if (dificulty < 0) {dificulty = diffs.length-1;}
  668. }
  669. }
  670.  
  671.  
  672.  
  673. //////////////////////////////////////////////
  674. ////////////////MAIN PROGRAM//////////////////
  675. //////////////////////////////////////////////
  676.  
  677.  
  678. //globals
  679. String state = "menu";
  680. String name = "";
  681. String[] diffs = {"Easy", "Med", "Hard"};
  682. boolean submited = false;
  683. Star[] stars = new Star[100];
  684. int dificulty = 1;
  685. Player player;
  686. ScoreList scores;
  687.  
  688. ArrayList <PowerUp> powers = new ArrayList<PowerUp>();
  689. ArrayList <Entity> bHoles = new ArrayList<Entity>();
  690. ArrayList <Entity> wHoles = new ArrayList<Entity>();
  691.  
  692. ArrayList <PowerUp> referances = new ArrayList<PowerUp>();
  693. referances.add(new SpeedUp (width/3, height-200));
  694. referances.add(new SlowDown (width/3, height-250));
  695. referances.add(new Reverse (width/3, height-300));
  696. referances.add(new Shrink (width/3, height-350));
  697. referances.add(new Grow (width/3, height-400));
  698. referances.add(new Protect (width/3, height-450));
  699. referances.add(new ExtraBHole(width/3, height-500));
  700. referances.add(new ExtraWHole(width/3, height-550));
  701.  
  702. boolean mouseClick;
  703.  
  704. //constant
  705. String instruction = "You are the pilot of a circular space shuttle that is on a\nmission to enter as many wormholes as possible.\nYour shuttle can only fly in circles, but as the pilot, you\ncan change the direction by pressing any key on your\nkeyboard.\nThe wormhole pulsates (increases and decreases in size)\nso timing is important too.\nReaching the wormhole will increase your score and send\nyou off into a different part of space where the wormhole\nis in a different position.\nBeware though! Space is forever hunted by black holes\nwho’s position never change relative to us due to a tear in\nthe space time continuum.\nRemember that when you hit the edge of space,\nyour shuttle will always appear on the opposite edge.";
  706.  
  707. //game functions
  708.  
  709. Score[] loadScores() {
  710. String[] lines = loadStrings("scores.txt");
  711. Score[] scores = new Score[lines.length];
  712.  
  713. for (int i=0; i<lines.length; i++) {
  714. String[] t = split(lines[i], "|");
  715. scores[i] = new Score(t[0], int(t[1]));
  716. }
  717. return scores;
  718. }
  719.  
  720. float getAngle(int fromx, int fromy, int tox, int toy) {
  721. int l1 = tox - fromx; //finding the difrence between the 2 points
  722. int l2 = toy - fromy; //aka fiding the leangth of each line
  723. float a = (atan2(l1, l2));
  724. return a;
  725. }
  726.  
  727. void appendToFile(String file, String line) {
  728. String[] lines = loadStrings(file);
  729. String[] added = new String[lines.length+1];
  730. arrayCopy(lines, added);
  731. added[lines.length] = line;
  732. saveStrings(file, added);
  733. }
  734.  
  735. void createStars() {
  736. for (int i=0; i<stars.length ; i++) {
  737. stars[i] = new Star(int(random(width)), int(random(height)));
  738. }
  739. }
  740.  
  741.  
  742. void blitScore() {
  743. fill(255);
  744. textSize(32);
  745. textAlign(LEFT, BOTTOM);
  746. text("Score: "+nf(player.score, 4), 0, height);
  747. }
  748.  
  749.  
  750. void blitList(Entity[] entities) {
  751. for (Entity entity : entities) {
  752. entity.blit();
  753.  
  754. if (playerCollision(entity)) {
  755. entity.onPlayerCollision();
  756. }
  757. }
  758. }
  759.  
  760. void blitEntity(ArrayList<Entity> entities) {
  761. for (Entity entity : entities) {
  762. entity.blit();
  763.  
  764.  
  765. if (playerCollision(entity)) {
  766. entity.onPlayerCollision();
  767. }
  768. }
  769. }
  770.  
  771. void blitPowers() { //also handles colisions and other power realted stuff. This way I only need to go through the powers array once.
  772. ArrayList newPowers = new ArrayList(powers);
  773.  
  774. for (PowerUp power : powers) {
  775. power.blit();
  776.  
  777. if (!power.effective) {
  778. if (power.lifeCount >= power.lifeSpan) {
  779. newPowers.remove(power);
  780. }
  781. if (playerCollision(power)) { //power collision
  782. power.effective = true;
  783. power.effect();
  784. }
  785. }
  786.  
  787. if (power.effectCount >= power.effectSpan) {
  788. power.defect();
  789. power.effective = false;
  790. newPowers.remove(power);
  791. }
  792. }
  793.  
  794. powers = new ArrayList(newPowers);
  795. }
  796.  
  797. void shuffleStars() { //faster than simply recreating the stars
  798. for (Star star : stars) {
  799. star.posx = int(random(width));
  800. star.posy = int(random(height));
  801. }
  802. }
  803.  
  804. void createPowerUp () {
  805. if (int(random(100)) == 50) {
  806. int choice = int(random(8));
  807. switch (choice) {
  808. case 0:
  809. powers.add(new SpeedUp (random(width), random(height))); break;
  810. case 1:
  811. powers.add(new SlowDown (random(width), random(height))); break;
  812. case 2:
  813. powers.add(new Reverse (random(width), random(height))); break;
  814. case 3:
  815. powers.add(new Shrink (random(width), random(height))); break;
  816. case 4:
  817. powers.add(new Grow (random(width), random(height))); break;
  818. case 5:
  819. powers.add(new Protect (random(width), random(height))); break;
  820. case 6:
  821. powers.add(new ExtraBHole(random(width), random(height))); break;
  822. case 7:
  823. powers.add(new ExtraWHole(random(width), random(height))); break;
  824. }
  825. }
  826. }
  827.  
  828. void checkCollisions() {
  829. //player collisions with the wormhole
  830. if (warp()) {
  831. wHoles.get(0).onPlayerCollision();
  832. }
  833. }
  834. boolean playerCollision(Entity entity) {
  835. return dist(player.posx, player.posy, entity.posx, entity.posy) < abs(player.size)/2+entity.size/2;
  836. }
  837.  
  838. void ensureFile(String name) {
  839. File fi = new File(sketchPath(name));
  840.  
  841. if (!fi.exists()){
  842. createOutput(name);
  843. }
  844. }
  845.  
  846. void blitDiff(float posx, float posy, float sizex, float sizey) {
  847. fill(255, 255, 255, 0);
  848. stroke(255);
  849. strokeWeight(3);
  850. rect(posx-sizex/2, posy-sizey/2, sizex, sizey, 10);
  851.  
  852. fill(255);
  853. textSize(40);
  854. textAlign(CENTER, CENTER);
  855. text(diffs[dificulty], posx, posy-7);
  856. }
  857.  
  858. //difrent screens
  859. void titleScreen() {
  860. textAlign(CENTER, TOP);
  861. textSize(60);
  862. text("Space Adventures!", width/2, height/4);
  863. new AgainButton("Play!").blit (width/2, height/2);
  864. new ScoreButton().blit (width/2, (height/2)+70);
  865. new StateButton("How to", "howt").blit(width/2, (height/2)+140);
  866. new ExitButton().blit (width/2, (height/2)+210);
  867.  
  868. blitDiff (width*(3/4.0)+40, height/2, 110, 50);
  869. new ChDiffButton(">", 1).blit (width*(3/4.0)+120, height/2);
  870. new ChDiffButton("<", -1).blit(width*(3/4.0)-40, height/2);
  871. }
  872.  
  873. void gameLoop() {
  874. player.blit();
  875. createPowerUp();
  876. blitEntity(wHoles);
  877. checkCollisions();
  878. blitPowers();
  879. blitScore();
  880. blitEntity(bHoles);
  881. blackHoleAt(100.0, 40.0);
  882. blackHoleAt(400.0, 500.0);
  883.  
  884. if (keyPressed) {
  885. if (key == ESC) {
  886. key = 0; //this is to stop it from exiting on escape press
  887. state = "paus";
  888. }
  889. if ("wasdWASD".indexOf(str(key)) == -1) { //don't affect the player when shifiting stars
  890. player.speed *= -1;
  891. }
  892. }
  893. }
  894.  
  895. void gameOverScreen () {
  896. textSize(32);
  897. fill(255);
  898. textAlign(CENTER, CENTER);
  899. text("You died with a score of "+str(player.score), width/2, height/3);
  900. textSize(16);
  901. text("Type in a 4 letter name to submit a score, if you wish.", width/2, (height/3)+32);
  902. textSize(50);
  903. text(name, width/3, (height/2)+30);
  904. new AgainButton("Again!").blit(width/2, height*(3/4.0));
  905. new StateButton("Menu", "menu").blit (width/4-30, height*(3/4.0));
  906. new ExitButton().blit (width*(3/4.0)+30, height*(3/4.0));
  907. if (submited) {text("Submitted!", width*(2/3.0), (height/2)+30);}
  908. else {new SubmitButton().blit(width*(2/3.0), (height/2)+30);}
  909.  
  910. if (keyPressed) {
  911. if (key == '\n') {
  912. if (!submited) {new SubmitButton().exec();}
  913. else {new AgainButton("Again!").exec();}
  914. }
  915. else if ("abcdefghijklmnopqrstuvwxyz1234567890-_".indexOf(str(key).toLowerCase()) != -1) {
  916. if (name.length() < 4) {
  917. name += str(key);
  918. }
  919. }
  920. else if (key == BACKSPACE && name.length() != 0) {
  921. name = name.substring(0, name.length()-1);
  922. }
  923. }
  924. }
  925.  
  926. void showHighScores() {
  927. textSize(50);
  928. fill(255);
  929. textAlign(CENTER, CENTER);
  930. text("All scores:", width/4, height/2-50);
  931. new StateButton("Menu", "menu").blit (width/4, height/2+50);
  932. textSize(20);
  933. text("(Use arrow keys or scroll wheel)", width/4+3, height/2-10);
  934. scores.blit();
  935.  
  936. if (keyPressed) {
  937. if (key == CODED) {
  938. if (keyCode == UP) {scores.speed = -2;}
  939. if (keyCode == DOWN) {scores.speed = 2;}
  940. }
  941. }
  942. }
  943.  
  944. void paused() {
  945. textSize(60);
  946. fill(255);
  947. textAlign(CENTER, CENTER);
  948. text("Game Paused", width/2, height/4);
  949. new StateButton("Back!", "game").blit(width/2, height/2);
  950. new AgainButton("New").blit (width/2, height*(3/4.0));
  951. new StateButton("Menu", "menu").blit (width/4-30, height*(3/4.0));
  952. new ExitButton().blit (width*(3/4.0)+30, height*(3/4.0));
  953.  
  954. if (keyPressed) {
  955. if (key == ESC) {
  956. key = 0; //this is to stop it from exiting on escape press
  957. state = "game";
  958. }
  959. }
  960. }
  961.  
  962. void howTo() {
  963. textSize(32);
  964. fill(255);
  965. textAlign(LEFT, CENTER);
  966. text("How to play Space Adventures!:", 10, 38);
  967. textSize(20);
  968. text(instruction, 10, height/2);
  969. new StateButton("Menu", "menu").blit(width/2-100, height-35);
  970. new StateButton("Powers", "powu").blit(width/2+100, height-35);
  971. }
  972.  
  973. void powerUpInstruction() {
  974. textSize(32);
  975. fill(255);
  976. textAlign(LEFT, CENTER);
  977. text("Power up:", 10, 38);
  978. new SpeedUp (width/3, height-200).blit();
  979. new SlowDown (width/3, height-250).blit();
  980. new Reverse (width/3, height-300).blit();
  981. new Shrink (width/3, height-350).blit();
  982. new Grow (width/3, height-400).blit();
  983. new Protect (width/3, height-450).blit();
  984. new ExtraBHole(width/3, height-500).blit();
  985. new ExtraWHole(width/3, height-550).blit();
  986.  
  987. textSize(20);
  988. textAlign(LEFT, CENTER);
  989. fill(255);
  990. text("Wormhole: Adds a new wormhole", width/2-70, height-200);
  991. text("Black Hole: Ass a new black hole", width/2-70, height-250);
  992. text("Protect: Makes you immune to black holes", width/2-70, height-300);
  993. text("Grow: Makes you bigger", width/2-70, height-350);
  994. text("Decrease: Makes you smaller", width/2-70, height-400);
  995. text("Reverse: Reverses your direction", width/2-70, height-450);
  996. text("Slow: Slows you down", width/2-70, height-500);
  997. text("Faster: Speeds you up", width/2-70, height-550);
  998.  
  999. new StateButton("Menu", "menu").blit(width/2-100, height-35);
  1000. new StateButton("<Back", "howt").blit(width/2+100, height-35);
  1001. }
  1002.  
  1003. //processing related functions
  1004. void keyPressed() {
  1005. if ((state == "game" || state == "paus") && key == ESC) {
  1006. key = 0; //this is to stop it from exiting ingame insteaed of pausing.
  1007. }
  1008.  
  1009. if (key == 'w') {for (Star star : stars) {star.speedy = 1;}}
  1010. if (key == 'a') {for (Star star : stars) {star.speedx = 1;}}
  1011. if (key == 's') {for (Star star : stars) {star.speedy = -1;}}
  1012. if (key == 'd') {for (Star star : stars) {star.speedx = -1;}}
  1013. }
  1014.  
  1015. void keyReleased() {
  1016. if (key == 'w') {for (Star star : stars) {star.speedy = 0;}}
  1017. if (key == 'a') {for (Star star : stars) {star.speedx = 0;}}
  1018. if (key == 's') {for (Star star : stars) {star.speedy = 0;}}
  1019. if (key == 'd') {for (Star star : stars) {star.speedx = 0;}}
  1020.  
  1021. if (state == "high" && key == CODED) {
  1022. if (keyCode == UP || keyCode == DOWN) {scores.speed = 0;}
  1023. }
  1024. }
  1025.  
  1026. void mouseWheel(MouseEvent event) {
  1027. if (state == "high") {scores.posy += event.getCount()*7;}
  1028. }
  1029.  
  1030. void mouseClicked() {mouseClick = true;}
  1031.  
  1032. void setup() {
  1033. frame.setTitle("Space Adventures!");
  1034. smooth();
  1035. size(600, 600);
  1036.  
  1037. ensureFile("scores.txt");
  1038.  
  1039. player = new Player (width/2, height/2);
  1040. wHoles.add(new WormHole(int(random(width)), int(random(height))));
  1041.  
  1042. createStars();
  1043. }
  1044.  
  1045. void draw() {
  1046. background (0);
  1047. blitList(stars); //NOTE: You can move the stars in any screen ^_^
  1048.  
  1049. if (state == "game") {gameLoop();}
  1050. else if (state == "dead") {gameOverScreen();}
  1051. else if (state == "menu") {titleScreen();}
  1052. else if (state == "high") {showHighScores();}
  1053. else if (state == "paus") {paused();}
  1054. else if (state == "howt") {howTo();}
  1055. else if (state == "powu") {powerUpInstruction();}
  1056.  
  1057. mouseClick = false;
  1058. keyPressed = false;
  1059. }
  1060.  
  1061.  
  1062. //Thank you to Thilina's blog for this code snippet!
  1063. //https://thilinasameera.wordpress.com/2011/06/01/sorting-algorithms-sample-codes-on-java-c-and-matlab/#_Quick_Sort
  1064. //adapted to suit my Score type. Granted I could have done it myself, but why re-invent the wheel?
  1065.  
  1066. Score[] quickSort(Score[] data) {
  1067. int lenD = data.length;
  1068. Score pivot;
  1069. int ind = lenD/2;
  1070. int i, j = 0, k = 0;
  1071. if (lenD<2) {
  1072. return data;
  1073. } else {
  1074. Score[] L = new Score[lenD];
  1075. Score[] R = new Score[lenD];
  1076. Score[] sorted = new Score[lenD];
  1077. pivot = new Score(data[ind].name, data[ind].score);
  1078. for (i=0; i<lenD; i++) {
  1079. if (i!=ind) {
  1080. if (data[i].score>pivot.score) {
  1081. L[j] = new Score(data[i].name, data[i].score);
  1082. j++;
  1083. } else {
  1084. R[k] =new Score(data[i].name, data[i].score);
  1085. k++;
  1086. }
  1087. }
  1088. }
  1089. Score[] sortedL = new Score[j];
  1090. Score[] sortedR = new Score[k];
  1091. arraycopy(L, 0, sortedL, 0, j);
  1092. arraycopy(R, 0, sortedR, 0, k);
  1093. sortedL = quickSort(sortedL);
  1094. sortedR = quickSort(sortedR);
  1095. arraycopy(sortedL, 0, sorted, 0, j);
  1096. sorted[j] = pivot;
  1097. arraycopy(sortedR, 0, sorted, j+1, k);
  1098. return sorted;
  1099. }
  1100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement