Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Creating variables
- var to4kiX=[],to4kiY=[];
- class RotRect{
- constructor (x, y, h, w, ang){
- this.updatePos(x, y);
- this.h = h;
- this.w = w;
- this.ang = ang;
- }
- updatePos(x, y){
- this.x = x;
- this.y = y;
- }
- changePos(x, y){
- this.x += x;
- this.y += y;
- }
- updateAng(ang){
- this.ang = ang;
- }
- changeAng(ang){
- this.ang += ang;
- }
- draw(){
- let x = [], y = [], x1 = [], y1 = [];
- for (let i=0; i<4; ++i){
- x[i] = Math.cos(this.ang + i*Math.PI/2);
- y[i] = Math.sin(this.ang + i*Math.PI/2);
- if (i%2 == 1){
- x[i] *= this.w/2;
- y[i] *= this.w/2;
- }else{
- x[i] *= this.h/2;
- y[i] *= this.h/2;
- }
- }
- context.beginPath();
- context.moveTo(this.x + x[3] + x[0], this.y + y[3] + y[0]);
- for (let i=0; i<3; ++i){
- context.lineTo(this.x + x[i] + x[i+1], this.y + y[i] + y[i+1]);
- }
- context.fill();
- context.closePath();
- }
- }
- var rect = new RotRect(400, 550, 100, 50, 0);
- var bullets=[];
- function update() {
- if(isKeyPressed[87]){rect.changePos(3*Math.cos(rect.ang),3*Math.sin(rect.ang));}
- if(isKeyPressed[83]){rect.changePos(-3*Math.cos(rect.ang),-3*Math.sin(rect.ang));}
- if(isKeyPressed[65]){rect.changeAng(-0.03);}
- if(isKeyPressed[68]){rect.changeAng(0.03);}
- for(let i=0;i<bullets.length;++i){
- bullets[i].changePos(5*Math.cos(bullets[i].ang),5*Math.sin(bullets[i].ang));
- }
- }
- function draw() {
- context.fillStyle="red";
- rect.draw();
- for(let i=0;i<bullets.length;++i){
- bullets[i].draw();
- }
- }
- function keyup(key) {
- // Show the pressed keycode in the console
- console.log("Pressed", key);
- }
- function mouseup() {
- bullets.push(new RotRect(rect.x,rect.y,30,10,Math.atan2
- (mouseY-rect.y,mouseX-rect.x)));
- // Show coordinates of mouse on click
- console.log("Mouse clicked at", mouseX, mouseY);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement