Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. // Creating variables
  2.  
  3. function dist(x1, y1, x2, y2){
  4. return Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
  5. }
  6.  
  7. function orient(x1, y1, x2, y2, x3, y3){
  8. return x2*y3 + x1*y2 + y1*x3 - y1*x2 - y2*x3 - y3*x1;
  9. }
  10.  
  11.  
  12. function areIntersecting(x1, y1, x2, y2, x3, y3, x4, y4){
  13. let o1 = orient(x1, y1, x3, y3, x4, y4);
  14. let o2 = orient(x2, y2, x3, y3, x4, y4);
  15. let o3 = orient(x3, y3, x1, y1, x2, y2);
  16. let o4 = orient(x4, y4, x1, y1, x2, y2);
  17. if (o1==0 || o2==0 || o3==0 || o4==0){return 0;}
  18. return ((o1>0)^(o2>0)) && ((o3>0)^(o4>0));
  19. }
  20.  
  21. class Player{
  22. constructor(x, y, sx, sy, dx, dy, color, ind){
  23. this.x = [x, x];
  24. this.y = [y, y];
  25. this.sx = sx;
  26. this.sy = sy;
  27. this.dx = dx;
  28. this.dy = dy;
  29. this.color = color;
  30. this.ind = ind;
  31. }
  32.  
  33. turn(targetX, targetY){
  34. let speed = 2.5;
  35. this.x.push(this.x[this.x.length-1]);
  36. this.y.push(this.y[this.y.length-1]);
  37. let lx = this.x[this.x.length-1];
  38. let ly = this.y[this.y.length-1];
  39. this.dx = (targetX-lx)/dist(lx, ly, targetX, targetY)*speed;
  40. this.dy = (targetY-ly)/dist(lx, ly, targetX, targetY)*speed;
  41. }
  42.  
  43. update(){
  44. this.x[this.x.length-1] += this.dx;
  45. this.y[this.y.length-1] += this.dy;
  46. let lx = this.x[this.x.length-1], ly = this.y[this.y.length-1];
  47. let blx = this.x[this.x.length-2], bly = this.y[this.y.length-2];
  48. if (lx<0 || lx>800 || ly<0 || ly>600){
  49. gameover();
  50. }
  51. for (let i=0; i<players.length; ++i){
  52. for (let j=0; j<players[i].x.length-1; ++j){
  53. if (areIntersecting(blx, bly, lx, ly, players[i].x[j], players[i].y[j], players[i].x[j+1], players[i].y[j+1])){
  54. if (i==this.ind && j>=players[i].x.length-3){}
  55. else{
  56. gameover();
  57. }
  58. }
  59. }
  60. }
  61. }
  62.  
  63. draw(){
  64. context.strokeStyle = this.color;
  65. context.beginPath();
  66. context.moveTo(this.x[0], this.y[0]);
  67. for (let i=1; i<this.x.length; ++i){
  68. context.lineTo(this.x[i], this.y[i]);
  69. }
  70. context.stroke();
  71. context.fillStyle = this.color;
  72. context.fillRect(this.x[this.x.length-1], this.y[this.y.length-1], this.sx, this.sy);
  73. }
  74. };
  75.  
  76. function gameover(){
  77. gmover = true;
  78. console.log("asdf");
  79. }
  80.  
  81. var gmover = false;
  82.  
  83. var players = [new Player(100, 100, 15, 15, 2.5, 0, 'blue', 0), new Player(700, 500, 15, 15, -2.5, 0, 'red', 1)];
  84.  
  85. function update() {
  86. if (gmover){return;}
  87. for (let i=0; i<players.length; ++i){
  88. players[i].update();
  89. }
  90. }
  91.  
  92. function draw() {
  93. for (let i=0; i<players.length; ++i){
  94. players[i].draw();
  95. }
  96. // This is how you draw a rectangle
  97. }
  98.  
  99. function keyup(key) {
  100. if (key==65){players[0].turn(mouseX, mouseY);}
  101. if (key==68){players[1].turn(mouseX, mouseY);}
  102. // Show the pressed keycode in the console
  103. console.log("Pressed", key);
  104. }
  105. function mouseup() {
  106. // Show coordinates of mouse on click
  107. console.log("Mouse clicked at", mouseX, mouseY);
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement