Advertisement
Guest User

Untitled

a guest
Jun 18th, 2015
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.30 KB | None | 0 0
  1. //AUTHOR: William Ye
  2. //TITLE: Orbital
  3. //DESC: Don't let the balls out
  4.  
  5. int sw = 600;
  6. int sh = 600;
  7. Hub hub;
  8. Paddle player;
  9. Color bCol; //background color
  10. ArrayList<Ball> balls;
  11. ArrayList<Particle> particles;
  12. float spawnTimer;
  13. float spawnDelay; //spawns balls
  14. int quality;
  15. float particleDelay;
  16. Camera camera;
  17. String screen;
  18. ArrayList<Button> menuButtons;
  19. ArrayList<Button> qualityButtons;
  20. float mouseAngle;
  21. Button backButton;
  22. ArrayList<Button> gameOverButtons;
  23. ArrayList<FloatingText> floatingText;
  24. float health;
  25. HealthBar healthBar;
  26. int score;
  27. float accelTimer; //increases speed
  28. PFont font;
  29. float ballConstant;
  30. String version = "v1.3";
  31.  
  32. void setup() {
  33. size(600, 600);
  34. frame.setTitle("Orbital " + version);
  35. reset();
  36. particleDelay = 2; //3 is low, 2 is medium, 1 is high
  37. screen = "menu";
  38.  
  39. //font = createFont("Segoe UI Italic", 32);
  40. //textFont(font);
  41. noStroke();
  42. textAlign(CENTER, CENTER);
  43. rectMode(CENTER);
  44.  
  45. particles = new ArrayList<Particle>();
  46. floatingText = new ArrayList<FloatingText>();
  47.  
  48. menuButtons = new ArrayList<Button>();
  49. createButtons(menuButtons, 5);
  50. menuButtons.get(0).text = "Play";
  51. menuButtons.get(1).text = "Help";
  52. menuButtons.get(2).text = "Quality";
  53. menuButtons.get(3).text = "About";
  54. menuButtons.get(4).text = "Quit";
  55.  
  56. qualityButtons = new ArrayList<Button>();
  57. createButtons(qualityButtons, 3);
  58. qualityButtons.get(0).text = "Low";
  59. qualityButtons.get(1).text = "Medium";
  60. qualityButtons.get(2).text = "High";
  61.  
  62. gameOverButtons = new ArrayList<Button>();
  63. gameOverButtons.add(new Button(PI/2 - PI/4, PI/2));
  64. gameOverButtons.get(0).text = "Again?";
  65. gameOverButtons.add(new Button(PI/2, PI/2 + PI/4));
  66. gameOverButtons.get(1).text = "Menu";
  67.  
  68. healthBar = new HealthBar(sw/2, sh/2);
  69. player = new Paddle(sw/2, sh/2);
  70.  
  71. camera = new Camera();
  72.  
  73. bCol = new Color(0, 0, 0);
  74.  
  75. backButton = new Button(PI/2 - PI/6, PI/2 + PI/6); //PI/6 is just a random measurement, it could be PI/10 or 2 or 92.345
  76. backButton.text = "Back";
  77.  
  78. ballConstant = 0.05;
  79.  
  80. }
  81. // which arraylist to add to? how many buttons do i want?
  82. void createButtons(ArrayList<Button> buttons, float numberOfButtons) {
  83. float sliceAngle = TWO_PI/numberOfButtons;
  84. for (int i = 0; i < numberOfButtons; i++) {
  85. buttons.add(new Button(-PI + i*sliceAngle, -PI + i*sliceAngle + sliceAngle)); //draws slices
  86. }
  87. }
  88.  
  89. void addButton(float start, float stop) {
  90. menuButtons.add(new Button(start, stop));
  91. }
  92.  
  93. void reset() { //stuff that resets on each game
  94. hub = new Hub(sw/2, sh/2);
  95. balls = new ArrayList<Ball>();
  96. spawnDelay = 100;
  97. spawnTimer = spawnDelay;
  98. health = 1.0;
  99. score = 0;
  100. accelTimer = 300;
  101. }
  102.  
  103. void normalizeBackground() {
  104. bCol.r += (30 - bCol.r)/20;
  105. bCol.g += (30 - bCol.g)/20;
  106. bCol.b += (30 - bCol.b)/20;
  107. }
  108.  
  109. class Camera {
  110. float x, y, timer;
  111. Camera() {
  112. x = 0;
  113. y = 0;
  114. }
  115. void update() {
  116. if (timer >= 0) {
  117. timer--;
  118. camera.x = random(-10, 10);
  119. camera.y = random(-10, 10);
  120. } else {
  121. camera.x = 0;
  122. camera.y = 0;
  123. }
  124. }
  125. void shake() {
  126. timer = 30;
  127. }
  128. }
  129.  
  130.  
  131. void draw() {
  132. normalizeBackground();
  133.  
  134. background(bCol.r, bCol.g, bCol.b);
  135.  
  136. camera.update();
  137.  
  138. mouseAngle = atan2(mouseY - sh/2, mouseX - sw/2);
  139.  
  140. //update and draw all the stuffs
  141. player.update(mouseAngle);
  142. player.draw(bCol.r, bCol.g, bCol.b);
  143.  
  144.  
  145. for (int i = balls.size() - 1; i >= 0; i--) {
  146. Ball b = balls.get(i);
  147. b.update();
  148. b.draw();
  149.  
  150. if (b.x > sw + b.radius/2 || b.x < -b.radius/2 || b.y > sh + b.radius/2 || b.y < -b.radius/2) { //OH NO YOU DUN GOOFED
  151. balls.remove(i);
  152. loseHealth();
  153. }
  154.  
  155. if (checkCollision(b)) {
  156. if (screen.equals("play")) {
  157. balls.remove(i);
  158. addExplosion(b.x, b.y);
  159. addFloatingText(b.x, b.y, 30, "+100", 0, 255, 0);
  160. score += 100;
  161. }
  162. }
  163.  
  164. }
  165.  
  166. for (int i = particles.size() - 1; i >= 0; i--) { //draw particles
  167. Particle p = particles.get(i);
  168. p.update();
  169. p.draw();
  170.  
  171. if (p.col.o <= 0) {
  172. particles.remove(i);
  173. }
  174. }
  175.  
  176.  
  177. if (screen.equals("play")) hub.update();
  178. hub.draw();
  179. healthBar.update();
  180. healthBar.draw();
  181.  
  182. if (screen.equals("play")) {
  183. spawnBalls();
  184. }
  185.  
  186. for (int i = floatingText.size()-1; i >= 0; i--) {
  187. FloatingText f = floatingText.get(i);
  188. f.update();
  189. f.draw();
  190. if (f.col.o <= 0) {
  191. floatingText.remove(i);
  192. }
  193. }
  194.  
  195. drawScreens();
  196. }
  197.  
  198. void spawnBalls() {
  199. spawnTimer--;
  200. if (spawnTimer <= 0) {
  201. spawnTimer = spawnDelay;
  202. addBall();
  203. }
  204. accelTimer--;
  205. if (accelTimer <= 0) {
  206. accelTimer = 300;
  207. spawnDelay -= spawnDelay/10; //makes the spawning of the ball faster
  208. }
  209. }
  210.  
  211. boolean checkCollision(Ball b) {
  212. if (dist(b.x, b.y, sw/2, sh/2) >= player.distance/2 - b.radius/2 && dist(b.x, b.y, sw/2, sh/2) <= player.distance/2 + 10) {
  213. //this detects the majority of ball directions
  214. if ((b.direction > player.location - player.size/2 - ballConstant && b.direction < player.location + player.size/2 + ballConstant)) {
  215. return true;
  216. }
  217. //however, some errors occur when the paddle is hovering over the part where -PI switches to PI and vice versa
  218. if (player.location < -PI + player.size/2) {
  219. //absolute value because it's in the very negative spectrum
  220. if (b.direction > PI - (abs(player.location - player.size/2) + PI)) {
  221. return true;
  222. }
  223. }
  224. if (player.location > PI - player.size/2) {
  225. if (b.direction < -PI + (player.location + player.size/2 - PI)) {
  226. return true;
  227. }
  228. }
  229. }
  230. return false;
  231. }
  232.  
  233. void loseHealth() {
  234. health -= 0.34;
  235. if (health > 0) {
  236. bCol.r = 200;
  237. bCol.g = 10;
  238. bCol.b = 10;
  239. camera.shake();
  240. healthBar.col.r = 100;
  241. healthBar.col.g = 0;
  242. healthBar.col.b = 0;
  243. if (health < 0.5 && health > 0) {
  244. addFloatingText(sw/2, sh/2, 40, "Warning! Low health!", 255, 0, 0);
  245. } else {
  246. addFloatingText(sw/2, sh/2, 40, "Don't let the balls out!", 255, 0, 0);
  247. }
  248. } else {
  249. if (screen.equals("play")) {
  250. camera.shake();
  251. addFloatingText(sw/2, sh/2, 40, "All health lost!", 255, 0, 0);
  252. gameOver();
  253. }
  254.  
  255. }
  256. }
  257.  
  258. void gameOver() {
  259. screen = "gameover";
  260. //bigass explosion
  261. for (int i = 0; i < 50; i++) {
  262. float x = random(sw);
  263. float y = random(sh);
  264. addBigExplosion(x, y);
  265. }
  266. }
  267.  
  268. void drawScreens() {
  269. if (screen.equals("menu")) {
  270. titleText("Orbital");
  271. for (Button b : menuButtons) {
  272. b.update();
  273. b.draw();
  274. }
  275. } else if (screen.equals("quality")) {
  276. titleText("Quality");
  277. for (Button b : qualityButtons) {
  278. b.update();
  279. b.draw();
  280. }
  281. } else if (screen.equals("help")) {
  282. titleText("Help");
  283. backButton.update();
  284. backButton.draw();
  285. subText("Use your mouse to stop the balls");
  286. } else if (screen.equals("about")) {
  287. titleText("About");
  288. backButton.update();
  289. backButton.draw();
  290. subText("Developed by William Ye, 10/19 - 10/22/14 \n " + version);
  291. } else if (screen.equals("gameover")) {
  292. fill(0, 0, 0, 150);
  293. rect(sw/2, sh/2, sw, sh);
  294. titleText("Game Over");
  295. subText("Score: " + score);
  296. for (Button b : gameOverButtons) {
  297. b.update();
  298. b.draw();
  299. }
  300. }
  301. }
  302.  
  303. void subText(String title) {
  304. fill(255, 255, 255);
  305. textSize(17); //text sizes, position, and color are hardcoded, tweaked until they look nice
  306. text(title, sw/2, sh/2 - 100);
  307. }
  308.  
  309. void titleText(String title) {
  310. fill(85, 255, 206);
  311. textSize(25);
  312. text(title, sw/2, 70);
  313. }
  314.  
  315. void addFloatingText(float x, float y, float size, String text, float r, float g, float b) {
  316. floatingText.add(new FloatingText(x, y, size, text, r, g, b));
  317. }
  318.  
  319. class FloatingText {
  320. float x, y, size;
  321. String text;
  322. Color col;
  323.  
  324. FloatingText(float x, float y, float size, String text, float r, float g, float b) {
  325. this.x = x;
  326. this.y = y;
  327. this.size = size;
  328. this.text = text;
  329. col = new Color(r, g, b);
  330. }
  331.  
  332. void update() {
  333. col.o -= 3;
  334. y -= 1.5;
  335. }
  336.  
  337. void draw() {
  338. fill(col.r, col.g, col.b, col.o);
  339. textSize(size);
  340. text(text, x, y);
  341. }
  342. }
  343.  
  344. class Button {
  345. float x, y;
  346. Color col;
  347. float start, stop;
  348. float distance;
  349. String text;
  350. float angle;
  351.  
  352. Button(float start, float stop) {
  353. x = sw/2;
  354. y = sh/2;
  355. col = new Color(60, 60, 60, 100);
  356. this.start = start;
  357. this.stop = stop;
  358. angle = (start+stop)/2;
  359. distance = 400;
  360. }
  361.  
  362. void draw() {
  363. fill(col.r, col.g, col.b, col.o);
  364. arc(x, y, distance, distance, start, stop);
  365.  
  366. //draw text
  367. fill(255, 255, 255);
  368. textSize(15);
  369. text(text, x + cos(angle)*distance/3, y + sin(angle)*distance/3);
  370. }
  371.  
  372. void update() {
  373.  
  374. if (selected()) {
  375. col.o = 200;
  376. }
  377. col.o += (100 - col.o)/5; //reverts back to 100
  378. }
  379.  
  380. boolean selected() {
  381. if (mouseAngle > start && mouseAngle < stop && dist(x, y, mouseX, mouseY) < distance/2) { //if the mouse angle is within the button angle
  382. return true;
  383. } else {
  384. return false;
  385. }
  386. }
  387.  
  388. }
  389.  
  390. void addExplosion(float x, float y) {
  391. for (int i = 0; i < (3-particleDelay) * 20 + 20; i++) {
  392. addParticle(x, y, random(10), random(1.5), 255, 50, 50);
  393. }
  394. for (int i = 0; i < (3-particleDelay) * 20 + 20; i++) {
  395. addParticle(x, y, random(10), random(1.5), 255, 255, 0);
  396. }
  397. for (int i = 0; i < (3-particleDelay) * 20 + 20; i++) {
  398. addParticle(x, y, random(10), random(1.5), 255, 100, 0);
  399. }
  400. bCol.r = 60;
  401. bCol.g = 60;
  402. bCol.b = 60;
  403. }
  404.  
  405. void addBigExplosion(float x, float y) {
  406. for (int i = 0; i < (3-particleDelay) * 20 + 20; i++) {
  407. addParticle(x, y, random(50), random(10), 255, 50, 50);
  408. }
  409. for (int i = 0; i < (3-particleDelay) * 20 + 20; i++) {
  410. addParticle(x, y, random(50), random(10), 255, 255, 0);
  411. }
  412. for (int i = 0; i < (3-particleDelay) * 20 + 20; i++) {
  413. addParticle(x, y, random(50), random(10), 255, 100, 0);
  414. }
  415. bCol.r = 60;
  416. bCol.g = 60;
  417. bCol.b = 60;
  418. }
  419.  
  420. void addBall() {
  421. balls.add(new Ball(sw/2, sh/2));
  422. hub.radius = 80;
  423. }
  424.  
  425. class Ball {
  426. float x, y, radius;
  427. float sx, sy;
  428. float direction;
  429. float particleTimer; //spawns particles
  430.  
  431. Ball(float x, float y) {
  432. this.x = x;
  433. this.y = y;
  434. radius = 20;
  435. direction = random(-PI, PI);
  436. sx = cos(direction) * 2;
  437. sy = sin(direction) * 2;
  438. particleTimer = 20;
  439. }
  440.  
  441. void update() {
  442. particleTimer--;
  443. if (particleTimer <= 0) {
  444. particleTimer = particleDelay;
  445. addParticle(random(x-5, x+5), random(y-5, y+5), 10, random(0.5), 255, 255, 255);
  446. }
  447.  
  448. x += sx;
  449. y += sy;
  450. }
  451.  
  452. void draw() {
  453. fill(255, 255, 255);
  454. ellipse(x + camera.x, y + camera.y, radius, radius);
  455. }
  456. }
  457.  
  458. void addParticle(float x, float y, float w, float speed, float r, float g, float b) {
  459. particles.add(new Particle(x, y, w, speed, r, g, b));
  460. }
  461.  
  462. void addParticle(float x, float y, float w, float speed, float direction, float r, float g, float b) {
  463. particles.add(new Particle(x, y, w, speed, direction, r, g, b));
  464. }
  465.  
  466. class Particle {
  467. float x, y, w;
  468. float speed;
  469. float sx, sy;
  470. Color col;
  471. float angle;
  472.  
  473. Particle(float x, float y, float w, float speed, float r, float g, float b) {
  474. this.x = x;
  475. this.y = y;
  476. this.w = w;
  477. this.speed = speed;
  478. angle = random(0, TWO_PI);
  479. sx = cos(angle)*speed;
  480. sy = sin(angle)*speed;
  481. col = new Color(r, g, b);
  482. }
  483.  
  484. Particle(float x, float y, float w, float speed, float direction, float r, float g, float b) {
  485. this.x = x;
  486. this.y = y;
  487. this.w = w;
  488. this.speed = speed;
  489. angle = direction;
  490. sx = cos(angle)*speed;
  491. sy = sin(angle)*speed;
  492. col = new Color(r, g, b);
  493. }
  494.  
  495. void update() {
  496. x += sx; //move around
  497. y += sy;
  498. col.o -= 5; //fade away
  499. }
  500.  
  501. void draw() {
  502. fill(col.r, col.g, col.b, col.o);
  503. rect(x + camera.x, y + camera.y, w, w);
  504. }
  505. }
  506.  
  507. void mouseReleased() { //mainly used for clicking menu buttons
  508. if (screen.equals("menu")) {
  509. for (Button b : menuButtons) {
  510. if (b.selected()) { //if it is selected and the mouse is clicked
  511. if (b.text.equals("Play")) {
  512. screen = "play";
  513. reset();
  514. blackFlash();
  515. } else if (b.text.equals("Quality")) {
  516. screen = "quality";
  517. blackFlash();
  518. } else if (b.text.equals("Quit")) {
  519. exit();
  520. } else if (b.text.equals("About")) {
  521. screen = "about";
  522. blackFlash();
  523. } else if (b.text.equals("Help")) {
  524. screen = "help";
  525. blackFlash();
  526. }
  527. }
  528. }
  529. } else if (screen.equals("quality")) {
  530. for (Button b : qualityButtons) {
  531. if (b.selected()) {
  532. if (b.text.equals("Low")) {
  533. particleDelay = 3; //less particles
  534. }
  535. if (b.text.equals("Medium")) {
  536. particleDelay = 2; //normal particles
  537. }
  538. if (b.text.equals("High")) {
  539. particleDelay = 1; //more particles
  540. }
  541. screen = "menu"; //goes back to menu
  542. blackFlash();
  543. }
  544. }
  545. } else if (screen.equals("help") || screen.equals("about")) {
  546. if (backButton.selected()) {
  547. screen = "menu";
  548. blackFlash();
  549. }
  550. } else if (screen.equals("gameover")) {
  551. for (Button b : gameOverButtons) {
  552. if (b.selected()) {
  553. if (b.text.equals("Again?")) {
  554. screen = "play";
  555. reset();
  556. blackFlash();
  557. }
  558. if (b.text.equals("Menu")) {
  559. screen = "menu";
  560. blackFlash();
  561. }
  562. }
  563. }
  564. }
  565.  
  566. }
  567.  
  568. void blackFlash() {
  569. bCol.r = 0;
  570. bCol.g = 0;
  571. bCol.b = 0;
  572. }
  573.  
  574. class Color {
  575. float r, g, b, o;
  576.  
  577. Color(float r, float g, float b) {
  578. this.r = r;
  579. this.g = g;
  580. this.b = b;
  581. this.o = 255;
  582. }
  583.  
  584. Color(float r, float g, float b, float o) {
  585. this.r = r;
  586. this.g = g;
  587. this.b = b;
  588. this.o = o;
  589. }
  590. }
  591.  
  592.  
  593. class Paddle {
  594. float x, y, size, distance, thickness, location;
  595. Color col;
  596. float particleTimer;
  597.  
  598. Paddle(float x, float y) {
  599. this.x = x;
  600. this.y = y;
  601. size = 0.4;
  602. col = new Color(75, 230, 200);
  603. distance = 500;
  604. thickness = 30;
  605. particleTimer = 0;
  606. }
  607.  
  608. void update(float angle) {
  609. location = angle;
  610. particleTimer--;
  611. if (particleTimer <= 0) {
  612. addParticle(x + cos(location + random(-0.2, 0.2))*distance/2, y + sin(location + random(-0.2, 0.2))*distance/2, random(3), random(1), 75, 230, 200);
  613. particleTimer = particleDelay;
  614. }
  615. }
  616. void draw(float br, float bg, float bb) { //background red, background green, background blue
  617. fill(col.r, col.g, col.b, col.o);
  618. arc(x + camera.x, y + camera.y, distance + thickness, distance + thickness, location - size/2, size/2 + location);
  619. fill(br, bg, bb);//cuts middle section away for ring
  620. arc(x + camera.x, y + camera.y, distance, distance, location - size/2 - 1, size/2 + location + 1);
  621. }
  622. }
  623.  
  624. class HealthBar {
  625. float x, y;
  626. Color col;
  627. float start, stop;
  628. float offsetStartA, offsetStopA; //a stands for actual
  629. float offsetStartV, offsetStopV; //v stands for visual
  630. float offsetTimer;
  631.  
  632. HealthBar(float x, float y) {
  633. this.x = x;
  634. this.y = y;
  635. col = new Color(30, 185, 155);
  636. }
  637.  
  638. void update() {
  639. col.r += (30 - col.r)/20;
  640. col.g += (185 - col.g)/20;
  641. col.b += (155 - col.b)/20;
  642.  
  643. //finds the start and stop value based on health
  644. // offsetStartV and offsetStopV values gives the shifty effect
  645. start = -PI * health + PI/2 + offsetStartV;
  646. stop = PI * health + PI/2 + offsetStopV;
  647.  
  648. offsetTimer--;
  649. if (offsetTimer <= 0) { //every 30 ticks, shift
  650. offsetTimer = 30;
  651. offsetStartA = random(-0.2, 0.2);
  652. offsetStopA = random(-0.2, 0.2);
  653. }
  654.  
  655. offsetStartV += (offsetStartA - offsetStartV)/20; //this makes it look smooth. The visual offset is gradually moved towards the actual offset
  656. offsetStopV += (offsetStopA - offsetStopV)/20;
  657. }
  658.  
  659. void draw() {
  660. fill(col.r, col.g, col.b, col.o);
  661. arc(x + camera.x, y + camera.y, hub.radius - 10, hub.radius-10, start, stop, CHORD); // changes width with health, adds a shift (PI/2) to rotate right, randomly shifts around a bit to give a weird look
  662. }
  663. }
  664.  
  665. class Hub {
  666. float x, y, radius;
  667. Color col;
  668. float particleTimer;
  669. float fireSize;
  670.  
  671. Hub(float x, float y) {
  672. this.x = x;
  673. this.y = y;
  674. radius = 60;
  675. col = new Color(75, 230, 200);
  676. particleTimer = 2;
  677. fireSize = 0;
  678. }
  679.  
  680. void update() {
  681.  
  682. //makes fire bigger
  683. fireSize += (30 - fireSize)/1500;
  684.  
  685. //add fire
  686. particleTimer--;
  687. if (particleTimer <= 0) {
  688. particleTimer = particleDelay;
  689. addParticle(random(x-radius/2, x+radius/2), random(y-radius/2, y+radius/2), random(fireSize), 5, -PI/2, random(50, 100), random(205, 255), random(175, 225));
  690. }
  691. //makes the radius return to 60
  692. radius += (60 - radius)/10;
  693. }
  694.  
  695. void draw() {
  696. fill(col.r, col.g, col.b, col.o);
  697. ellipse(x + camera.x, y + camera.y, radius, radius);
  698. }
  699. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement