Guest User

Untitled

a guest
Jul 21st, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. class Player {
  2. ArrayList<PVector> body;
  3. PVector direction;
  4. PVector nextDirection;
  5. int cellWidth;
  6. int cellHeight;
  7.  
  8. Player() {
  9. body = new ArrayList<PVector>();
  10.  
  11. body.add(new PVector(0, 0));
  12. body.add(new PVector(0, 20));
  13. body.add(new PVector(0, 40));
  14. body.add(new PVector(0, 60));
  15.  
  16. direction = new PVector(0, 20);
  17. nextDirection = direction;
  18.  
  19. cellWidth = 20;
  20. cellHeight = 20;
  21. }
  22.  
  23. void setDirection(int dirX, int dirY) {
  24. if (20 * dirX + direction.x == 0 && 20 * dirY + direction.y == 0) return;
  25. nextDirection = new PVector(20 * dirX, 20 * dirY);
  26. }
  27.  
  28.  
  29. void move(){
  30. direction = nextDirection;
  31. body.remove(0);
  32. PVector head = body.get(body.size() - 1).copy();
  33. head.add(direction);
  34. if (body.contains(head)) println("Crash");
  35. body.add(head);
  36. }
  37.  
  38. void display(){
  39. for (int i = 0; i < body.size(); i++) {
  40. PVector pos = body.get(i);
  41. ellipse(pos.x, pos.y, cellWidth, cellHeight);
  42. }
  43. }
  44. }
  45.  
  46.  
  47. Player player;
  48.  
  49. int counter = 0;
  50.  
  51. void setup() {
  52. size(640, 480);
  53. player = new Player();
  54. background(255);
  55. frameRate(30);
  56. noFill();
  57. }
  58.  
  59. void draw() {
  60. background(255);
  61.  
  62. if (counter % 10 == 0) {
  63. player.move();
  64. }
  65.  
  66. counter++;
  67.  
  68. pushMatrix();
  69.  
  70. translate(width/2, height/2);
  71.  
  72. player.display();
  73.  
  74. popMatrix();
  75. }
  76.  
  77. void keyPressed() {
  78. if (key == 'i') {
  79. player.setDirection(0, -1);
  80. } else if (key == 'k') {
  81. player.setDirection(0, 1);
  82. } else if (key == 'j') {
  83. player.setDirection(-1, 0);
  84. } else if (key == 'l') {
  85. player.setDirection( 1, 0);
  86. }
  87. }
Add Comment
Please, Sign In to add comment