Home > Software design >  How to change Javascript hitboxes for images that dont take up the full width of the object
How to change Javascript hitboxes for images that dont take up the full width of the object

Time:01-25

I am pretty new to HTML canvas, and I am trying to make a flappy bird clone. I have the bird and the pipes on screen, but the images do not take up the full width of the area I have assigned for the objects on the screen because there are invisible pixels. This is due to me having to remove a background and make it transparent. Does anyone know how to make the hitbox of the pipes take up the same amount of space as the image of the pipes even with the invisible pixels? Thank you.

const canvas = document.getElementById("canvas");
const c = canvas.getContext("2d");
const backgroundImage = document.getElementById("image")
const flappyBirdImage = document.getElementById("flappy-bird-image");
const topPipeImage = document.getElementById("top-pipe-image");
const bottomPipeImage = document.getElementById("bottom-pipe-image")
let controlsKeyDown = {up: false, right: false, down: false, left: false};
let dx = 2;
let dy = 2;

canvas.width = innerWidth;
canvas.height = innerHeight;




class Bird {
    constructor(x, y, width, height) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }
    
    draw() {
        c.beginPath();
        c.fillStyle = 'blue';
        c.strokeStyle = 'lightBlue';
        c.drawImage(flappyBirdImage, this.x, this.y, this.width, this.height)
        c.fill()
        c.stroke();
        c.closePath();
    }
    
}



class Pipe {
    constructor(x, y, height, width, color, image) {
        this.x = x;
        this.y = y;
        this.height = height;
        this.width = width;
        this.color = color;
        this.image = image;
    }
    
    draw() {
        c.beginPath();
        c.drawImage(bottomPipeImage, this.x, this.y, this.width, this.height)
        c.fill();
        
    }

    drawtop() {
        c.beginPath();
        c.drawImage(topPipeImage, this.x, this.y, this.width, this.height);
        c.closePath()
    }
}

const topPipe = new Pipe(400, 200, 100, 200, 'black')

const bottomPipe = new Pipe(500, 200, 100, 200, 'black')

const myBird = new Bird(200, 200, 40, 40)

function birdControlsKeyDown(Event) {
    if(Event.key === 'ArrowUp') {
        controlsKeyDown.up = true;
    }
    if(Event.key === 'ArrowRight') {
        controlsKeyDown.right = true;
    }
    
    if(Event.key === 'ArrowLeft') {
        controlsKeyDown.left = true;
    }
    
    if(Event.key === "ArrowDown") {
        controlsKeyDown.down = true;
    }
}
function birdControlsKeyUp(Event) {
    if(Event.key === 'ArrowUp') {
        controlsKeyDown.up = false;
    }
    
    if(Event.key === 'ArrowRight') {
        controlsKeyDown.right = false;
    }
    
    if(Event.key === 'ArrowLeft') {
        controlsKeyDown.left = false;
    }
    
    if(Event.key === 'ArrowDown') {
        controlsKeyDown.down = false;
    }
}

//Bird  With Edge Collision Detection

function birdCollisionDetection() {
    // Bird Hits Bottom Of Screen
    if(myBird.y   myBird.height >= canvas.height){
        myBird.y -= dy;
    }
    
    // Bird Hits Top Of Screen
    if(myBird.y <= 0) {
        myBird.y  = dy;
    } 
    
    // Bird Hits Left Of Screen
    if(myBird.x<= 0) {
        myBird.x  = dx;
    }
    
    // Bird Hits Right Of Screen
    if(myBird.x   myBird.height >= canvas.width) {
        myBird.x -= dx;
    }
    
    // Bird With Pipe Collision Detection
    
}
function birdWithPipeCollisionDetection(a,b) {
    
    if(a.x   a.width >= b.x && a.x <= b.x   b.width && a.y  <= b.y   b.height && a.y   a.height >= b.y){
        console.log('test');
        a.x -= dx;
        
    }
}
function draw() {
    c.drawImage(backgroundImage, 0,0,canvas.width,canvas.height)
    myBird.draw() 
    topPipe.drawtop()
    bottomPipe.draw()
  
   
    
    // Bird Controls
    
    addEventListener('keydown', birdControlsKeyDown)
    
    addEventListener('keyup', birdControlsKeyUp)
    
    
    if(controlsKeyDown.up) {
        myBird.y -= dy;
    }
    if(controlsKeyDown.right) {
        myBird.x  = dx;
    } 
    
    if(controlsKeyDown.left) {
        myBird.x -= dx;
    }

    if(controlsKeyDown.down) {
        myBird.y  = dy;
    }

    birdCollisionDetection();


    birdWithPipeCollisionDetection(myBird, topPipe);

    birdWithPipeCollisionDetection(myBird, bottomPipe);



    requestAnimationFrame(draw)
}
console.log(myBird)


draw()
html {
    margin: 0;
    padding: 0;
}

body {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
}

canvas {
    overflow: hidden;
}

image {
    background: transparent;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <link rel="icon" type="image/png" href="./Flappy Bird Pictures and Animations/Flappy Bird Icon.png" type="icon">
    <script src="Flappy Bird.js" defer></script>
    <link rel="stylesheet" href="Flappy Bird.css">
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Flappy Bird</title>
</head>
<body>
    <canvas id="canvas"></canvas>
    <img src="./Flappy Bird Pictures and Animations/background image.png" id="image">
    <img src="./Flappy Bird Pictures and Animations/The Flappy Bird.png" id="flappy-bird-image">
    <img src="./Flappy Bird Pictures and Animations/Bottom Pipe.png" id="bottom-pipe-image">
    <img src="./Flappy Bird Pictures and Animations/Top Pipe.png" id="top-pipe-image">
</body>
</html>

CodePudding user response:

will be used @email 4 image the database for sql isn;t up that means the only image property is gif

In order 2 use gif u need a video format camera;;video-src, video-sectional Video-sec works on viaos but.. video-sec=i’mm<i’mm></i’mm>

Javac “example.tutorial.gif” .java
{var video-sec = “example.tutorial.gif”   ‘<i’mm sec’=“example.gif”> example.gif </img> printf-javac-example.tutorial.gif/bin/ .java

The xml document should be used 2then the email to >!DOCTYPE should elude the imminent response to javac akara!2techsupport.nappster.email;

CodePudding user response:

    video_sec = "kermit"   '128';
video_src = video_sec   "{ lkermit will fall from the balcony;;lkermit.email;;kermit-gif-tutorial.email;;(kermit-will-fall-from-the-balcony) }";
video_src == video_src   '128';

def printf (xml, email):
  email='nappster.email';
  xml = "!@DOCTYPE:"   email;
  print (email, xml);
  return;

def return_2_sender(edoc, lisp):
  email2='nappster.email'   1;
  xml2='websocket'   'email2';
  xml2 = xml2   "internet down today";
  
  print (email2, xml2);
  
##*%TIN #%TIN TI-CALC#ENGENIORFER #(TFORE A#T*THE GIF #%*%#TO P#LLAAY #(#U%USE #%T2
##$BIN OR #*#YUSE #$%#%EMAIL ADDRESS (#$*##%(%IN @$($MUSHCLIENT
video_sec == video_sec   "email!2nappster.email";
video_src == video_src   "email!2nappster.email";


print (video_src);

CodePudding user response:

Mushclient is a new_client(data),,the mushclient can’t be used on the internet without a data-pass (which i don’t know) but many people who asked me at tech support you just use a website but the only website available is ‘www.saltstack.com’

Example

   var ip_address = “MOSH”;
   ip_address == ip_address   ‘line’;
   html_5 = ‘[email protected]’;…

   html5 = html5   1;
   ip_address = ip_address * html_5;

def ip_locate (xml5, socket):
   …
  •  Tags:  
  • Related