Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Player {
- ArrayList<PVector> body;
- PVector direction;
- PVector nextDirection;
- int cellWidth;
- int cellHeight;
- Player() {
- body = new ArrayList<PVector>();
- body.add(new PVector(0, 0));
- body.add(new PVector(0, 20));
- body.add(new PVector(0, 40));
- body.add(new PVector(0, 60));
- direction = new PVector(0, 20);
- nextDirection = direction;
- cellWidth = 20;
- cellHeight = 20;
- }
- void setDirection(int dirX, int dirY) {
- if (20 * dirX + direction.x == 0 && 20 * dirY + direction.y == 0) return;
- nextDirection = new PVector(20 * dirX, 20 * dirY);
- }
- void move(){
- direction = nextDirection;
- body.remove(0);
- PVector head = body.get(body.size() - 1).copy();
- head.add(direction);
- if (body.contains(head)) println("Crash");
- body.add(head);
- }
- void display(){
- for (int i = 0; i < body.size(); i++) {
- PVector pos = body.get(i);
- ellipse(pos.x, pos.y, cellWidth, cellHeight);
- }
- }
- }
- Player player;
- int counter = 0;
- void setup() {
- size(640, 480);
- player = new Player();
- background(255);
- frameRate(30);
- noFill();
- }
- void draw() {
- background(255);
- if (counter % 10 == 0) {
- player.move();
- }
- counter++;
- pushMatrix();
- translate(width/2, height/2);
- player.display();
- popMatrix();
- }
- void keyPressed() {
- if (key == 'i') {
- player.setDirection(0, -1);
- } else if (key == 'k') {
- player.setDirection(0, 1);
- } else if (key == 'j') {
- player.setDirection(-1, 0);
- } else if (key == 'l') {
- player.setDirection( 1, 0);
- }
- }
Add Comment
Please, Sign In to add comment