Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. //Serial Communication
  2. import processing.serial.*; // import serial lib
  3. Serial myPort; // create instance of serial lib
  4. String sensorValue;
  5.  
  6. Ship myShip; // create a spaceship
  7.  
  8. int xChange, yChange; // variables for the ship
  9. int increment = 1; // variable to change the rate of movement
  10.  
  11. Star[] stars; // array of stars
  12. int starNum = 20; // number of stars
  13.  
  14. void setup() {
  15.  
  16. //serial communication
  17. myPort = new Serial(this, Serial.list()[3], 9600); // instatiate the object
  18. // new serial object (named "myPort") that will run in this" sketch, which is physicallon on port X
  19. // and communicates at 9600 baud
  20.  
  21. // generate a serialEvent() when you get a newline character:
  22. myPort.bufferUntil('\n');
  23.  
  24. size(800, 600);
  25.  
  26. myShip = new Ship(); // instantiate the ship
  27. stars = new Star[starNum]; // new array of stars
  28.  
  29. for (int x=0; x<starNum; x++) {
  30. stars[x] = new Star(); // instatntiate the stars
  31. }
  32. }
  33.  
  34. void draw() {
  35. background(0); // clear the background
  36.  
  37. // directions
  38. String words = "use the arrows to move, 1-9 for ship speed";
  39. fill(255);
  40. text(words, 10, 30);
  41.  
  42. // loop through all the stars
  43. for (int x=0; x< starNum; x++) {
  44. stars[x].update(); // update their position
  45. stars[x].collisionCheck(myShip.xPos, myShip.yPos); // check if colliding with the ship
  46. stars[x].render(); // draw the stars
  47. }
  48.  
  49. myShip.update(xChange, yChange); // update the ship's position & shield size
  50. myShip.render(); // render the ship
  51.  
  52. // reset vars if you want
  53. //yChange = 0;
  54. //xChange = 0;
  55.  
  56. }
  57.  
  58.  
  59. void serialEvent(Serial myPort) {
  60. // get the ASCII string by reading from the port until a newline char is reached
  61. String inString = myPort.readStringUntil('\n');
  62.  
  63. if (inString != null) { // if you have stuff in the string
  64. // trim off any whitespace:
  65. inString = trim(inString);
  66. sensorValue = inString;
  67.  
  68. // split the string at the commas and convert the
  69. // resulting substrings into your array:
  70.  
  71. if (sensorValue.equals("u")) {
  72. yChange = increment * -1;
  73. }
  74. else if (sensorValue.equals("d")) {
  75. yChange = increment;
  76. }
  77. else if (sensorValue.equals("l")) {
  78. xChange = increment * -1;
  79. }
  80. else if (sensorValue.equals("r")) {
  81. xChange = increment;
  82. } else{
  83. increment = int(sensorValue)/128 + 1;
  84. if (xChange != 0){
  85. xChange = abs(xChange)/xChange * increment;
  86. }
  87. if (yChange != 0){
  88. yChange = abs(yChange)/yChange * increment;
  89. }
  90. }
  91. }
  92. myPort.write("A"); //ask for more game data
  93. }
  94.  
  95.  
  96. //************** Star class
  97. class Star {
  98. float xPos, yPos, starSize, speed; // variables
  99. boolean collision; // check for collision
  100.  
  101. // star constructor
  102. Star() { // initial state
  103. speed = random(1, 10);
  104. starSize = random(10, 100);
  105. xPos = random(0, width);
  106. yPos = random(100, width) * -1;
  107. collision = false;
  108. }
  109.  
  110. void update() { // update star position
  111.  
  112. yPos += speed;
  113.  
  114. if (yPos > height+starSize/2) {
  115. yPos = random(100, width) * -1;
  116. speed = random(1, 10);
  117. starSize = random(10, 50);
  118. xPos = random(0, width);
  119. }
  120. }
  121.  
  122. void collisionCheck(int _x, int _y) { // check for a collision
  123.  
  124. int shipX = _x;
  125. int shipY = _y;
  126.  
  127. float dx = shipX - xPos;
  128. float dy = shipY - yPos;
  129. float d = sqrt(sq(dx)+sq(dy)); // distance between star and ship
  130.  
  131. if (d < starSize/2 + 10) { // if distance is less than the radius of the star & ship
  132. collision = !collision; // there's a crash
  133. }
  134. }
  135.  
  136. void render() {
  137. // if there's no collision
  138. if (!collision) {
  139. noStroke();
  140. fill(220, 160, 0);
  141. ellipse(xPos, yPos, starSize, starSize);
  142. } else { // if there is a colliison, supernova
  143. strokeWeight(5);
  144. stroke(255);
  145. fill(220, 100, 0);
  146. ellipse(xPos, yPos, starSize*1.5, starSize*1.5);
  147. collision = !collision; // reset the collison state for the next iteration
  148. }
  149. }
  150. }
  151.  
  152.  
  153.  
  154. //************** Ship class
  155. class Ship {
  156. int xPos;
  157. int yPos;
  158. int shieldSize;
  159.  
  160. Ship() { // ship constructor
  161. xPos = width/2;
  162. yPos = height - 100;
  163. shieldSize = 0;
  164. }
  165.  
  166. void update(int _xDelta, int _yDelta) {
  167. xPos += _xDelta;
  168. yPos += _yDelta;
  169. }
  170.  
  171. void render() {
  172. if (yPos > height-10) {
  173. yPos = height-10;
  174. }
  175. if (yPos < height-200) {
  176. yPos = height-200;
  177. }
  178.  
  179. if (xPos > width-10) {
  180. xPos = width-10;
  181. }
  182. if (xPos < 10) {
  183. xPos = 10;
  184. }
  185.  
  186. noStroke();
  187. fill(180, 10, 200);
  188. rectMode(CENTER);
  189. rect(xPos, yPos, 20, 20);
  190. }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement