Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ArrayList<LineClass> Lineslist = new ArrayList<LineClass>();
- int locX, locY;
- int speed = 2;
- boolean kup, kdown, kright, kleft;
- PGraphics pg;
- float rSpeed = 0.001;
- float r2 = 0;
- int highScore = 0;
- boolean spinDir;
- int prizeX, prizeY;
- long timer; // global
- int interval = 4000;
- void setup() {
- size(400, 500, P2D);
- Lineslist = new ArrayList();
- locX = width/2;
- locY = height/2;
- kup = true;
- pg = createGraphics(220, 340);
- imageMode(CENTER);
- prizeX = 20;
- prizeY = 30;
- timer = millis();
- }
- void newLine() {
- Lineslist.add(new LineClass(locX, locY));
- }
- void draw() {
- background(1);
- newLine();
- if (kup) {
- locY -= speed;
- }
- if (kdown) {
- locY += speed;
- }
- if (kright) {
- locX += speed;
- }
- if (kleft) {
- locX -= speed;
- }
- // asteriods rules
- if (locX < 0) {
- locX = 220;
- rSpeed += .001;
- }
- if (locX > 220) {
- locX = 0;
- rSpeed += .001;
- }
- if (locY < 0) {
- locY = 340;
- rSpeed += .001;
- }
- if (locY > 340) {
- locY = 0;
- rSpeed += .001;
- }
- int prizeSize = 12;
- if (locX >= prizeX && locX <= prizeX + prizeSize && locY >= prizeY && locY <= prizeY + prizeSize) {
- prizeX =int(random(20, 210));
- prizeY =int(random(20, 320));
- timer = millis();
- for (int i = (Lineslist.size())/2; i >= 0; i--) {
- Lineslist.remove(i);
- }
- }
- for (int i = Lineslist.size()-1; i >= 0; i--) { // loop trhough all the tail segments
- LineClass dLine = Lineslist.get(i);
- dLine.display();
- if (dLine.x == locX && dLine.y == locY) {
- if (Lineslist.size() > highScore) {
- highScore = Lineslist.size();
- }
- gameover();
- break;
- }
- }
- if (millis() - timer > interval) { // in draw
- timer = millis();
- prizeX =int(random(20, 210));
- prizeY =int(random(20, 320));
- }
- pg.beginDraw();
- pg.background(255);
- pg.fill(220);
- pg.stroke(220);
- pg.ellipse(110, 30, 20, 20);
- pg.fill(0, 0, 255);
- pg.stroke(0, 0, 255);
- pg.rect(prizeX, prizeY, prizeSize, prizeSize);
- pg.rect(locX, locY, speed, speed); // head of snake
- //pg.endDraw();
- for (int i = Lineslist.size()-1; i >= 0; i--) { // loop trhough all the tail segments
- LineClass dLine = Lineslist.get(i);
- dLine.display();
- if (dLine.x == locX && dLine.y == locY) {
- if (Lineslist.size() > highScore) {
- highScore = Lineslist.size();
- }
- gameover();
- break;
- }
- }
- pg.endDraw();
- if (spinDir) {
- r2 += rSpeed;
- } else {
- r2 -= rSpeed;
- }
- pushMatrix();
- translate(width/2, height/2);
- rotate(r2);
- image(pg, 0, 0);
- popMatrix();
- fill(200);
- text("highscore: " + highScore, 20, height-20);
- }
- void gameover() {
- for (int i = Lineslist.size()-1; i >= 0; i--) {
- Lineslist.remove(i);
- }
- locX = width/2;
- locY = height/2;
- pg.clear();
- setKeys();
- kup = true;
- rSpeed = .001;
- r2 = 0;
- if (random(2) > 1) {
- spinDir = true;
- } else {
- spinDir = false;
- }
- timer = millis();
- }
- void keyPressed() {
- if (keyCode == UP && kdown == false) {
- // the "kdown == false" is to keep you from backing into the line accidentally
- setKeys();
- kup = true;
- }
- if (keyCode == DOWN && kup == false) {
- setKeys();
- kdown = true;
- }
- if (keyCode == RIGHT && kleft == false) {
- setKeys();
- kright = true;
- }
- if (keyCode == LEFT && kright == false) {
- setKeys();
- kleft = true;
- }
- }
- void setKeys() {
- kup = false;
- kdown = false;
- kright = false;
- kleft = false;
- }
- class LineClass {
- float x, y; // declare the variables used in the class
- LineClass(float _x, float _y) {
- x = _x; // assign incoming temporary values to the variables you set above.
- y = _y;
- }
- void display() {
- pg.beginDraw();
- pg.fill(1);
- pg.stroke(1);
- pg.rect(x, y, 2, 2);
- //pg.endDraw();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement