Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. float x=50 ;
  2. float y =0 ;
  3. float dx = 6 ;
  4. float dy = 5 ;
  5. float w = 150 ; //圖片寬度、高度
  6. PImage img; //宣告圖片物件
  7.  
  8. //挑戰題:
  9. //1、將顯示動作更改成水平左右移動,邊界反彈
  10. //2、將顯示動作增加垂直上下移動,邊界反彈
  11.  
  12. void setup(){
  13. size(1024, 800);
  14. //width為畫面的寬度,height為畫面的高度
  15. background(100,100,50) ;
  16. }
  17.  
  18. int idx = 0;
  19. void draw() {
  20. if(idx %2 == 0)
  21. img = loadImage("dog.png"); //載入圖片
  22. else
  23. img = loadImage("d.png");
  24. idx++;
  25. background(100,100,50) ;
  26. image(img, x, y, w, w); //顯示圖片
  27. println(y, dy) ;
  28. y += dy; //更改位置
  29. if(y + w >= height)
  30. dy = -5;
  31. if(y < 0)
  32. dy = 5;
  33.  
  34. x += dx; //更改位置
  35.  
  36. /*if(x + w >= width)
  37. dx = -6;
  38. if(x < 0)
  39. dx = 6;*/
  40. dx = x+w >= width ? -6:6;
  41.  
  42. delay(300);
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement