Advertisement
Guest User

zmush3

a guest
Feb 25th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. // Creating variables
  2. var to4kiX=[],to4kiY=[];
  3. class RotRect{
  4. constructor (x, y, h, w, ang){
  5. this.updatePos(x, y);
  6. this.h = h;
  7. this.w = w;
  8. this.ang = ang;
  9. }
  10.  
  11. updatePos(x, y){
  12. this.x = x;
  13. this.y = y;
  14. }
  15.  
  16. changePos(x, y){
  17. this.x += x;
  18. this.y += y;
  19. }
  20.  
  21. updateAng(ang){
  22. this.ang = ang;
  23. }
  24.  
  25. changeAng(ang){
  26. this.ang += ang;
  27. }
  28.  
  29. draw(){
  30. let x = [], y = [], x1 = [], y1 = [];
  31. for (let i=0; i<4; ++i){
  32. x[i] = Math.cos(this.ang + i*Math.PI/2);
  33. y[i] = Math.sin(this.ang + i*Math.PI/2);
  34. if (i%2 == 1){
  35. x[i] *= this.w/2;
  36. y[i] *= this.w/2;
  37. }else{
  38. x[i] *= this.h/2;
  39. y[i] *= this.h/2;
  40. }
  41. }
  42. context.beginPath();
  43. context.moveTo(this.x + x[3] + x[0], this.y + y[3] + y[0]);
  44. for (let i=0; i<3; ++i){
  45. context.lineTo(this.x + x[i] + x[i+1], this.y + y[i] + y[i+1]);
  46. }
  47. context.fill();
  48. context.closePath();
  49. }
  50. }
  51.  
  52. var rect = new RotRect(400, 550, 100, 50, 0);
  53. var bullets=[];
  54. function update() {
  55. if(isKeyPressed[87]){rect.changePos(3*Math.cos(rect.ang),3*Math.sin(rect.ang));}
  56. if(isKeyPressed[83]){rect.changePos(-3*Math.cos(rect.ang),-3*Math.sin(rect.ang));}
  57. if(isKeyPressed[65]){rect.changeAng(-0.03);}
  58. if(isKeyPressed[68]){rect.changeAng(0.03);}
  59. for(let i=0;i<bullets.length;++i){
  60. bullets[i].changePos(5*Math.cos(bullets[i].ang),5*Math.sin(bullets[i].ang));
  61. }
  62. }
  63.  
  64.  
  65.  
  66. function draw() {
  67. context.fillStyle="red";
  68. rect.draw();
  69. for(let i=0;i<bullets.length;++i){
  70. bullets[i].draw();
  71. }
  72. }
  73.  
  74. function keyup(key) {
  75. // Show the pressed keycode in the console
  76. console.log("Pressed", key);
  77. }
  78. function mouseup() {
  79. bullets.push(new RotRect(rect.x,rect.y,30,10,Math.atan2
  80. (mouseY-rect.y,mouseX-rect.x)));
  81. // Show coordinates of mouse on click
  82. console.log("Mouse clicked at", mouseX, mouseY);
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement