绘制火箭,跟随鼠标

  1. //9:24
  2. PImage rocket;
  3. void setup(){
  4. size(600,500);
  5. rocket = loadImage("🚀.png");
  6. pixelDensity(2);
  7. imageMode(CENTER);
  8. }
  9. void draw(){
  10. image(rocket,mouseX,mouseY,68/2,132/2);
  11. }

image.png

绘制子弹,触壁反弹

  1. //9:24
  2. PImage rocket;
  3. float x, y, r;
  4. float spx, spy;
  5. void setup() {
  6. size(800, 600);
  7. rocket = loadImage("🚀.png");
  8. pixelDensity(2);
  9. imageMode(CENTER);
  10. //variable of bullet
  11. r=10;
  12. x=width/2;
  13. y=r;
  14. spx = 2;
  15. spy=2;
  16. }
  17. void draw() {
  18. background(#132638);
  19. //rocket
  20. image(rocket, mouseX, mouseY, 34, 66);
  21. //bullet
  22. noStroke();
  23. fill(220, 220, 30);
  24. ellipse(x, y, 2*r, 2*r);
  25. //update bullet position
  26. x+=spx;
  27. y+=spy;
  28. //checkEdge
  29. if(x>=width-r || x<r){
  30. spx = -spx;
  31. }else if(y>=height-r || y<r ){
  32. spy = -spy;
  33. }
  34. if(x<mouseX+34/2 && x>mouseX-34 && y>mouseY-66/2 && y<mouseY+66/2){
  35. background(255,100,100);
  36. noLoop();
  37. }
  38. }

image.png

模块化

  1. //9:24
  2. PImage rocket;
  3. float x, y, r;
  4. float spx, spy;
  5. void setup() {
  6. size(800, 600);
  7. rocket = loadImage("🚀.png");
  8. pixelDensity(2);
  9. imageMode(CENTER);
  10. //variable of bullet
  11. r=10;
  12. x=width/2;
  13. y=r;
  14. spx = 2;
  15. spy=2;
  16. }
  17. void draw() {
  18. background(#132638);
  19. //rocket
  20. image(rocket, mouseX, mouseY, 34, 66);
  21. display();
  22. update();
  23. checkEdge();
  24. if (x<mouseX+34/2 && x>mouseX-34 && y>mouseY-66/2 && y<mouseY+66/2) {
  25. background(255, 100, 100);
  26. noLoop();
  27. }
  28. }
  29. void display() {
  30. //display bullet
  31. noStroke();
  32. fill(220, 220, 30);
  33. ellipse(x, y, 2*r, 2*r);
  34. }
  35. void update() {
  36. //update bullet position
  37. x+=spx;
  38. y+=spy;
  39. }
  40. void checkEdge() {
  41. //check edge
  42. if (x>=width-r || x<r) {
  43. spx = -spx;
  44. } else if (y>=height-r || y<r ) {
  45. spy = -spy;
  46. }
  47. }

面向对象编程

  1. //9:24
  2. PImage rocket;
  3. Bullet [] bullets= new Bullet[10];
  4. void setup() {
  5. size(800, 600);
  6. rocket = loadImage("🚀.png");
  7. pixelDensity(2);
  8. imageMode(CENTER);
  9. for (int i=0; i<bullets.length; i++) {
  10. bullets[i] = new Bullet();
  11. }
  12. }
  13. void draw() {
  14. background(#132638);
  15. //rocket
  16. image(rocket, mouseX, mouseY, 34, 66);
  17. //操作子弹
  18. for (int i=0; i<bullets.length; i++) {
  19. bullets[i].display();
  20. bullets[i].update();
  21. bullets[i].checkEdge();
  22. bullets[i].checkJoin();
  23. }
  24. }
  1. class Bullet {
  2. //子弹的变量
  3. float x, y, r;
  4. float spx, spy;
  5. Bullet() {
  6. //初始化子弹
  7. r=10;
  8. x=random(0,width);
  9. y=r;
  10. spx = 2;
  11. spy=2;
  12. }
  13. void display() {
  14. //display bullet
  15. noStroke();
  16. fill(220, 220, 30);
  17. ellipse(x, y, 2*r, 2*r);
  18. }
  19. void update() {
  20. //update bullet position
  21. x+=spx;
  22. y+=spy;
  23. }
  24. void checkEdge() {
  25. //check edge
  26. if (x>=width-r || x<r) {
  27. spx = -spx;
  28. } else if (y>=height-r || y<r ) {
  29. spy = -spy;
  30. }
  31. }
  32. void checkJoin() {
  33. if (x<mouseX+34/2 && x>mouseX-34 && y>mouseY-66/2 && y<mouseY+66/2) {
  34. background(255, 100, 100);
  35. noLoop();
  36. }
  37. }
  38. }

优化

  1. ...
  2. void draw() {
  3. ...
  4. //操作子弹
  5. for (int i=0; i<bullets.length; i++) {
  6. bullets[i].display();
  7. bullets[i].update();
  8. bullets[i].checkEdge();
  9. }
  10. for (int i=0; i<bullets.length; i++) {
  11. bullets[i].checkJoin();
  12. }
  13. }

加入声音

  1. import processing.sound.*;
  2. PImage rocket;
  3. SoundFile explode;
  4. ...
  5. void setup() {
  6. ...
  7. for (int i=0; i<bullets.length; i++) {
  8. bullets[i] = new Bullet(random(r, width), random(r, height), random(-2, 2), random(-2, 2));
  9. }
  10. explode = new SoundFile(this,"explode.wav");
  11. }
  12. ...
  1. class Bullet {
  2. ...
  3. void checkJoin() {
  4. if (x<mouseX+34/2 && x>mouseX-34 && y>mouseY-66/2 && y<mouseY+66/2) {
  5. background(255, 100, 100);
  6. explode.play();
  7. noLoop();
  8. }
  9. }
  10. }