fork download
  1. class Pacman {
  2. float radius = 40;
  3. float x = radius;
  4. float y = 250;
  5.  
  6. float vel = 2.75;//velocity (speed)
  7. float vx;//velocity x (x-speed)
  8. float vy;//velocity y (y-speed)
  9.  
  10. float mouth = 0;//mouth angle (theta)//θ
  11. float openClose = 0.05;//angle delta//Δθ//open close speed
  12.  
  13. float start;
  14. float end;
  15. float sigs;//sign start
  16. float sige;//sign end
  17.  
  18. Pacman() {
  19. setRight();
  20. }
  21. void setRight() {
  22. vx = vel;
  23. vy = 0;
  24. start = 0;
  25. end = TWO_PI;
  26. sigs = 1;
  27. sige = -1;
  28. }
  29. void setLeft() {
  30. vx = -vel;
  31. vy = 0;
  32. start = -5*(PI/6);
  33. end = 5*(PI/6);
  34. sigs = -1;
  35. sige = 1;
  36. }
  37. void setUp() {
  38. vx = 0;
  39. vy = -vel;
  40. start = -PI/2;
  41. end = TWO_PI + -PI/2;
  42. sigs = 1;
  43. sige = -1;
  44. }
  45. void setDown() {
  46. vx = 0;
  47. vy = vel;
  48. start = PI/2;
  49. end = TWO_PI + PI/2;
  50. sigs = 1;
  51. sige = -1;
  52. }
  53. void keyPressed() {
  54. if (key == CODED) {
  55. switch(keyCode) {
  56. case RIGHT:
  57. setRight();
  58. break;
  59.  
  60. case LEFT:
  61. setLeft();
  62. break;
  63.  
  64. case UP:
  65. setUp();
  66. break;
  67.  
  68. case DOWN:
  69. setDown();
  70. break;
  71. }
  72. }
  73. }
  74. void mouth(){
  75. mouth += openClose;
  76. if (mouth > PI/6 || mouth < 0) {
  77. openClose = openClose * -1;
  78. }
  79. }
  80. void draw() {
  81. //draw
  82. fill(255, 255, 0);
  83. arc(x, y, radius, radius, start+sigs*mouth, end+sige*mouth);
  84.  
  85. //update
  86. mouth();
  87. warp();
  88. raid();
  89. move();
  90. }
  91. void move() {
  92. x += vx;
  93. y += vy;
  94. }
  95. void warp() {
  96. if (x<-radius) {
  97. x=width+radius;
  98. }
  99. if (x>width+radius) {
  100. x=-radius;
  101. }
  102. if (y>height+radius) {
  103. y=-radius;
  104. }
  105. if (y<-radius) {
  106. y=height+radius;
  107. }
  108. }
  109. void raid() {
  110. if (x > width - radius) {
  111. if (y!=height/2) {
  112. x = width-radius;
  113. } else {
  114. y=height/2;
  115. if (x>width+radius) {
  116. x=-radius;
  117. }
  118. }
  119. }
  120. }
  121. }
  122.  
  123. Pacman pacman;
  124.  
  125. void setup() {
  126. size(1020, 500);
  127. smooth();
  128. ellipseMode(RADIUS);
  129. pacman = new Pacman();
  130. }
  131.  
  132. void draw() {
  133. background(0);
  134. pacman.draw();
  135. }
  136.  
  137. void keyPressed() {
  138. pacman.keyPressed();
  139. }
  140.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty