Advertisement
Guest User

RotatingSnakeGame

a guest
Mar 29th, 2017
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. ArrayList<LineClass> Lineslist = new ArrayList<LineClass>();
  2. int locX, locY;
  3. int speed = 2;
  4. boolean kup, kdown, kright, kleft;
  5. PGraphics pg;
  6. float rSpeed = 0.001;
  7. float r2 = 0;
  8. int highScore = 0;
  9. boolean spinDir;
  10. int prizeX, prizeY;
  11. long timer; // global
  12. int interval = 4000;
  13.  
  14. void setup() {
  15. size(400, 500, P2D);
  16. Lineslist = new ArrayList();
  17. locX = width/2;
  18. locY = height/2;
  19. kup = true;
  20. pg = createGraphics(220, 340);
  21. imageMode(CENTER);
  22. prizeX = 20;
  23. prizeY = 30;
  24. timer = millis();
  25. }
  26.  
  27. void newLine() {
  28. Lineslist.add(new LineClass(locX, locY));
  29. }
  30.  
  31. void draw() {
  32. background(1);
  33. newLine();
  34. if (kup) {
  35. locY -= speed;
  36. }
  37. if (kdown) {
  38. locY += speed;
  39. }
  40. if (kright) {
  41. locX += speed;
  42. }
  43. if (kleft) {
  44. locX -= speed;
  45. }
  46. // asteriods rules
  47. if (locX < 0) {
  48. locX = 220;
  49. rSpeed += .001;
  50. }
  51. if (locX > 220) {
  52. locX = 0;
  53. rSpeed += .001;
  54. }
  55. if (locY < 0) {
  56. locY = 340;
  57. rSpeed += .001;
  58. }
  59. if (locY > 340) {
  60. locY = 0;
  61. rSpeed += .001;
  62. }
  63. int prizeSize = 12;
  64. if (locX >= prizeX && locX <= prizeX + prizeSize && locY >= prizeY && locY <= prizeY + prizeSize) {
  65. prizeX =int(random(20, 210));
  66. prizeY =int(random(20, 320));
  67. timer = millis();
  68. for (int i = (Lineslist.size())/2; i >= 0; i--) {
  69. Lineslist.remove(i);
  70. }
  71. }
  72.  
  73. for (int i = Lineslist.size()-1; i >= 0; i--) { // loop trhough all the tail segments
  74. LineClass dLine = Lineslist.get(i);
  75. dLine.display();
  76. if (dLine.x == locX && dLine.y == locY) {
  77. if (Lineslist.size() > highScore) {
  78. highScore = Lineslist.size();
  79. }
  80. gameover();
  81. break;
  82. }
  83. }
  84.  
  85. if (millis() - timer > interval) { // in draw
  86. timer = millis();
  87. prizeX =int(random(20, 210));
  88. prizeY =int(random(20, 320));
  89. }
  90. pg.beginDraw();
  91. pg.background(255);
  92. pg.fill(220);
  93. pg.stroke(220);
  94. pg.ellipse(110, 30, 20, 20);
  95. pg.fill(0, 0, 255);
  96. pg.stroke(0, 0, 255);
  97. pg.rect(prizeX, prizeY, prizeSize, prizeSize);
  98. pg.rect(locX, locY, speed, speed); // head of snake
  99. //pg.endDraw();
  100. for (int i = Lineslist.size()-1; i >= 0; i--) { // loop trhough all the tail segments
  101. LineClass dLine = Lineslist.get(i);
  102. dLine.display();
  103. if (dLine.x == locX && dLine.y == locY) {
  104. if (Lineslist.size() > highScore) {
  105. highScore = Lineslist.size();
  106. }
  107. gameover();
  108. break;
  109. }
  110. }
  111. pg.endDraw();
  112. if (spinDir) {
  113. r2 += rSpeed;
  114. } else {
  115. r2 -= rSpeed;
  116. }
  117. pushMatrix();
  118. translate(width/2, height/2);
  119. rotate(r2);
  120. image(pg, 0, 0);
  121. popMatrix();
  122. fill(200);
  123. text("highscore: " + highScore, 20, height-20);
  124. }
  125. void gameover() {
  126. for (int i = Lineslist.size()-1; i >= 0; i--) {
  127. Lineslist.remove(i);
  128. }
  129. locX = width/2;
  130. locY = height/2;
  131. pg.clear();
  132. setKeys();
  133. kup = true;
  134. rSpeed = .001;
  135. r2 = 0;
  136. if (random(2) > 1) {
  137. spinDir = true;
  138. } else {
  139. spinDir = false;
  140. }
  141. timer = millis();
  142. }
  143.  
  144.  
  145.  
  146. void keyPressed() {
  147.  
  148. if (keyCode == UP && kdown == false) {
  149. // the "kdown == false" is to keep you from backing into the line accidentally
  150. setKeys();
  151. kup = true;
  152. }
  153. if (keyCode == DOWN && kup == false) {
  154. setKeys();
  155. kdown = true;
  156. }
  157. if (keyCode == RIGHT && kleft == false) {
  158. setKeys();
  159. kright = true;
  160. }
  161.  
  162. if (keyCode == LEFT && kright == false) {
  163. setKeys();
  164. kleft = true;
  165. }
  166. }
  167. void setKeys() {
  168. kup = false;
  169. kdown = false;
  170. kright = false;
  171. kleft = false;
  172. }
  173.  
  174.  
  175. class LineClass {
  176. float x, y; // declare the variables used in the class
  177. LineClass(float _x, float _y) {
  178. x = _x; // assign incoming temporary values to the variables you set above.
  179. y = _y;
  180. }
  181. void display() {
  182. pg.beginDraw();
  183. pg.fill(1);
  184. pg.stroke(1);
  185. pg.rect(x, y, 2, 2);
  186. //pg.endDraw();
  187. }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement