Guest User

Untitled

a guest
Dec 13th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.70 KB | None | 0 0
  1. //global variables
  2. import peasy.PeasyCam;
  3. import peasy.*;
  4. PVector pos;//position of the ship
  5. float dir = 0;//direction ship is facing
  6. float speed = 0;//speed of ship
  7. PVector vel;
  8. PVector acc;
  9. PVector sDir;
  10. PeasyCam cam;
  11. //====================================================
  12. boolean leftBool, rightBool, thrustBool;
  13. float speedLimit = 4;
  14. //global variables for tracers
  15. int maxSize = 30;
  16. float startSize = 30;
  17. PVector [] post = new PVector [maxSize];
  18. PVector [] velt = new PVector [maxSize];
  19. PVector [] acct = new PVector [maxSize];
  20. float [] sizes = new float [maxSize];
  21. float shrinkScale = 0.99;
  22. float velScale = 2;
  23. int state = 0;
  24. //-----------------------------------------------------------------------------
  25. //global variables for asteroid
  26. Asteroid main;
  27. ArrayList<Asteroid> asteroids = new ArrayList<Asteroid>();
  28. PShape model;
  29. PVector center;
  30. PImage texture;
  31. //global variables for star map
  32. int maxStarSize = 50;//number of stars
  33. int Smult = 800;
  34. float starSize = 1;
  35. PVector [] Spos2 = new PVector [maxStarSize];
  36. PVector mid;
  37. //global variables for sounds
  38. import ddf.minim.*;//needed to play the music
  39. import ddf.minim.analysis.*;//needed to access the BeatDetect Class
  40. Minim minim;//the object access all of the other minim goodies
  41. AudioPlayer song;//the player to play a specific song, multiple players would be needed to play multiple songs
  42. BeatDetect beat;//the class that will detect the changes in volume associated with beats
  43. //-------------------------------------void setup-------------------
  44. void setup() {
  45. noStroke();
  46. size(800, 800, P3D);
  47. textSize(30);
  48. textAlign(CENTER, CENTER);
  49. frameRate(120);
  50. pos = new PVector(width/2, height/2);
  51. vel = new PVector();
  52. acc = new PVector();
  53. sDir = new PVector(0, 1, 0);
  54. cam = new PeasyCam(this, width/2., height/2., 0., 745);
  55. mid = new PVector(width/2,height/2,Smult/2);
  56. for(int i=1; i < maxStarSize; i++){
  57. Spos2[i] = (PVector.random3D());
  58. Spos2[i].mult(Smult);
  59. Spos2[i].add(mid);
  60. //(pos2[i].x) += (width/2);
  61. //(pos2[i].y) += (height/2);
  62. //(pos2[i].z) += (mult/2);
  63. }
  64. initT();
  65. cam.setActive(false);
  66. }
  67. //---------------------------------void init--------------------------
  68. void initT() {
  69. for (int i = 0; i < maxSize; i++) {
  70. post[i] = new PVector(-1000, -1000);
  71. velt[i] = PVector.random2D();
  72. velt[i].mult(velScale);
  73. acct[i] = new PVector(0, 0);
  74. sizes[i] = startSize;
  75. }
  76. center = new PVector(width/2, height/2);
  77. texture = loadImage("texture.jpg");
  78. model = loadShape("asteroid.obj");
  79. model.setTexture(texture);
  80.  
  81.  
  82. main = new Asteroid(asteroids, 20., model, center, new PVector());
  83. asteroids.add(main);
  84. // asteroids.add(new Asteroid(asteroids, 20., model, new PVector(random(0,width),random(0,height)),new PVector()));
  85. }
  86.  
  87.  
  88. //-----------------------------void key pressed--------------------------
  89. void keyPressed() {
  90. if (keyCode == LEFT) {//turning left
  91. leftBool = true;
  92. }
  93. if (keyCode == RIGHT) {//turning right
  94. rightBool = true;
  95. }
  96. if (keyCode == UP) {//going forward
  97. thrustBool = true;
  98. }
  99. }
  100. //-----------------------------void key released--------------------------
  101. void keyReleased() {
  102. if (keyCode == LEFT) {
  103. leftBool = false;
  104. }
  105. if (keyCode == RIGHT) {
  106. rightBool = false;
  107. }
  108. if (keyCode == UP) {
  109. thrustBool = false;
  110. }
  111. }
  112. //---------------------------------void draw hud-----------------------
  113. void drawHUD() {
  114. float speed = 0;
  115. cam.beginHUD();
  116. fill(0, 128);
  117. rect(0, 0, 800, 80);
  118. fill(255);
  119. text("speed:" +speed , 70, 30);
  120. if(keyPressed && key == UP){
  121. speed++;
  122. }
  123.  
  124. cam.endHUD();
  125. }
  126. //---------------------------------void pre game------------------------
  127. void preGame() {
  128. background(150, 100, 50);
  129. text("", width/2, height/2);
  130. text("Press space to continue", width/2, height/2+200);
  131. if (keyPressed && key == ' ') {
  132. state = 1;
  133. }
  134. }
  135. //-------------------------------------void ship driving------------------------
  136. void shipDriving() {
  137. //pos.x += cos(dir)*(speed);//current location + the next"step"
  138. //pos.y += sin(dir)*(speed);
  139.  
  140. vel.add(acc);
  141. pos.add(vel);
  142. acc.set(0, 0, 0);
  143. fill(0);//colour of the ship
  144. pushMatrix();
  145. translate(pos.x, pos.y);//moving the ship to the new position
  146. //rotate(dir);//rotate the ship to face the desired direction
  147. rotate(sDir.heading());
  148. stroke(0);//shade of colour for stroke
  149. box(30, 10, 10);//drawing the ship, which is a box
  150. popMatrix();
  151. if (leftBool) {
  152. //dir -= 0.05;//rotate the ship to the left
  153. sDir.rotate(-0.05);
  154. }
  155. if (rightBool) {
  156. //dir += 0.05; //rotate the ship to the right
  157. sDir.rotate(0.1);
  158. }
  159. if (thrustBool) {
  160. //speed += 0.1;
  161. acc = sDir.copy().mult(0.1);
  162. } else {
  163. vel.mult(0.99);
  164. //speed *= 0.99;//redue the speed over time
  165. }
  166. vel.limit(speedLimit);
  167. //speed = constrain(speed, 0, speedLimit); //limit the speed
  168. }
  169. //-------------------------------void off edge----------------
  170. void offEdge() {
  171. if (pos.x > width) {
  172. pos.x = 0;
  173. } else if (pos.y > height) {
  174. pos.y = 0;
  175. } else if (pos.y < 0) {
  176. pos.y = height;
  177. } else if (pos.x < 0) {
  178. pos.x = width;
  179. }
  180. }
  181. //--------------------------void tracers------------------
  182. void tracers() {
  183. for (int i = maxSize-1; i > 0; i--) {
  184. post[i] = post[i-1];
  185. sizes[i] = sizes[i-1]*shrinkScale;//shrinks tracers as they go through the array
  186. velt[i] = velt[i-1];
  187. }
  188. if (thrustBool) {
  189. post[0] = new PVector(pos.x, pos.y);
  190. velt[0] = PVector.random2D();
  191. velt[0].mult(velScale);
  192. sizes[0] = startSize;
  193. } else {//if not pressing thrust button, place tracers off screen
  194. post[0] = new PVector(-1000, -1000);
  195. }
  196. for (int i = 0; i < maxSize; i++) {
  197. post[i].add(velt[i]);
  198. pushMatrix();
  199. translate(cos(dir)*speed, sin(dir)*speed);
  200. ellipse(post[i].x, post[i].y, sizes[i]/3, sizes[i]/3);
  201. fill(255,0,0);
  202. popMatrix();
  203. }
  204. }
  205. //------------------------------------void game------------
  206. void game() {
  207. background(255);
  208. shipDriving();
  209. tracers();
  210. offEdge();
  211. drawHUD();
  212. bullets();
  213. for (int i = 0; i<asteroids.size(); i++) {
  214. asteroids.get(i).update();
  215. }
  216. for(int i=1; i < maxStarSize; i++){
  217. pushMatrix();
  218. //fill(255.-(float)i/maxStarSize*255,255,255);
  219. fill(255,0,0);
  220. translate(Spos2[i].x,Spos2[i].y,Spos2[i].z);
  221. sphere(random(starSize, starSize*2));
  222. popMatrix();
  223. }
  224. //background(0);
  225. }
  226.  
  227. //---------------------------void end game----------------------
  228. void endGame() {
  229. }
  230.  
  231. /*void mousePressed(){
  232. PVector mouse = new PVector(mouseX, mouseY,0);
  233. asteroids.add(new Asteroid(main, mouse));
  234.  
  235. }
  236. */
  237. //-----------------------------void draw---------------------------
  238. void draw() {
  239. background(255, 0, 0);
  240. //is state = 0, preagame
  241. if (state == 0) {
  242. preGame();
  243. }
  244. //if state = 1, game
  245. else if (state == 1) {
  246.  
  247. game();
  248. }
  249. //if state = 2, endgame
  250. else if (state == 2) {
  251. endGame();
  252. }
  253. }
Add Comment
Please, Sign In to add comment