Archery Game Using HTML CSS and JavaScript
Introduction
Hello friends, you all are welcome to our new blog post. Today we have created a beautiful project for you which is a game. The name of this game is Archery Game. You must have played such games a lot, that is why today we have created a beautiful game of our own, which we have made using html, css and javascript. If you are now learning web development, then this project is absolutely right for you because when you make such advanced projects, you get to learn a lot from these projects. When we make such games, we get to see all the errors. We get problems and when we solve them our coding knowledge gets improved If you have a little knowledge of coding then it’s a good thing because in this it is important for you to have some knowledge of coding otherwise you might face some difficulty In this we have used mostly HTML and Javascript We have not used much CSS in this game, so let’s understand some important codes step by step.
HTML (index.html)
This code is of HTML through which we have created a structure for our game. Friends, after seeing the code you might find something different but friends, making this game is quite easy. If you have basic knowledge then you can make a game like this. We have used svg path in the game, so let’s understand the code.
- First of all, for any project, there is its <title></title> title, from which the user will know which project it is
- If you do not link your CSS file to HTML, then the design of the project is not correct, so for linking we have used this tag <link rel=”stylesheet” href=”style.css” />
- <body> This is the most important tag of HTML because within this tag we have to link our code which you will see in the code below
- <svg></svg> We have used all our paths and svg in svg in this game
- <g id=”arrow”> With the help of this element we have created our arrow with which our game will start
- <g id=”target”> We have created this element to aim at our target which you will see in the game
- <c id=”bow” this element is used to create the bow so in the code to create the bow it will need to be entered
- <g class=”arrow-angle”> With this element we have created the angle of the arrow which helps in targeting which you can see in the game
- <g class=”miss” if you are playing the game and if your arrow misses the target then a text message is displayed to you due to the medium of this element, that is why we have used this element
- <span>Draw back an arrow and launch it!</span> We have used this text at the top of the game to give a message to the user.
Archery Game
Draw back an arrow and launch it!
CSS (Style.css)
The code which you are seeing below is the CSS code which is used for designing any website or any other project. If you have good knowledge of CSS then you can make very good projects. We are talking about CSS here because we have used CSS in our game but let me tell you that we have used very little CSS which you can see in the below code because we have done all the work using HTML and Javascript. So let’s understand the CSS code.
- First of all comes the background color of the game, we have used background:#222; black color in the background
- In the body, we have applied margin:20px; 20px margin, due to which we get to see a little gap on all four sides
- We have kept the width and height of all the categories equal to 100% and the position fixed.
- In the save we have kept 0 from top and 0 from left so that we don’t get unnecessary gaps
- All the span tags we have used in the game are white in color and font-family:sans-serif; font family is sans serif and opacity is 3 which you can see in the code
body{
background:#222;
margin:20px;
}
svg{
width:100%;
height:100%;
position:fixed;
top:0;
left:0;
}
span{
color:white;
font-family:sans-serif;
opacity:.3;
}
Javascript (Script.js)
If you have understood the code of HTML and CSS properly then let us now understand the code of Javascript, this code is the most important because if you do anything wrong in this code then you may face problem in running your game.
Friends, using HTML you can create a structure of any project and using CSS you can design that structure, but what will happen by clicking which button, we have to do all this work using Javascript, so now we have made our game, but how will our game’s bow and arrow shoot?
Case It will hit the target We have to do all this work with the help of Javascript, that is why we have completed our game completely with the help of Javascript, so if you make some mistake in the Javascript code, then there might be a problem in running your game, that is why you have to directly paste the given code in your folder.
var svg = document.querySelector("svg");
var cursor = svg.createSVGPoint();
var arrows = document.querySelector(".arrows");
var randomAngle = 0;
// center of target
var target = {
x: 900,
y: 249.5
};
// target intersection line segment
var lineSegment = {
x1: 875,
y1: 280,
x2: 925,
y2: 220
};
// bow rotation point
var pivot = {
x: 100,
y: 250
};
aim({
clientX: 320,
clientY: 300
});
// set up start drag event
window.addEventListener("mousedown", draw);
function draw(e) {
// pull back arrow
randomAngle = (Math.random() * Math.PI * 0.03) - 0.015;
TweenMax.to(".arrow-angle use", 0.3, {
opacity: 1
});
window.addEventListener("mousemove", aim);
window.addEventListener("mouseup", loose);
aim(e);
}
function aim(e) {
// get mouse position in relation to svg position and scale
var point = getMouseSVG(e);
point.x = Math.min(point.x, pivot.x - 7);
point.y = Math.max(point.y, pivot.y + 7);
var dx = point.x - pivot.x;
var dy = point.y - pivot.y;
// Make it more difficult by adding random angle each time
var angle = Math.atan2(dy, dx) + randomAngle;
var bowAngle = angle - Math.PI;
var distance = Math.min(Math.sqrt((dx * dx) + (dy * dy)), 50);
var scale = Math.min(Math.max(distance / 30, 1), 2);
TweenMax.to("#bow", 0.3, {
scaleX: scale,
rotation: bowAngle + "rad",
transformOrigin: "right center"
});
var arrowX = Math.min(pivot.x - ((1 / scale) * distance), 88);
TweenMax.to(".arrow-angle", 0.3, {
rotation: bowAngle + "rad",
svgOrigin: "100 250"
});
TweenMax.to(".arrow-angle use", 0.3, {
x: -distance
});
TweenMax.to("#bow polyline", 0.3, {
attr: {
points: "88,200 " + Math.min(pivot.x - ((1 / scale) * distance), 88) + ",250 88,300"
}
});
var radius = distance * 9;
var offset = {
x: (Math.cos(bowAngle) * radius),
y: (Math.sin(bowAngle) * radius)
};
var arcWidth = offset.x * 3;
TweenMax.to("#arc", 0.3, {
attr: {
d: "M100,250c" + offset.x + "," + offset.y + "," + (arcWidth - offset.x) + "," + (offset.y + 50) + "," + arcWidth + ",50"
},
autoAlpha: distance/60
});
}
function loose() {
// release arrow
window.removeEventListener("mousemove", aim);
window.removeEventListener("mouseup", loose);
TweenMax.to("#bow", 0.4, {
scaleX: 1,
transformOrigin: "right center",
ease: Elastic.easeOut
});
TweenMax.to("#bow polyline", 0.4, {
attr: {
points: "88,200 88,250 88,300"
},
ease: Elastic.easeOut
});
// duplicate arrow
var newArrow = document.createElementNS("http://www.w3.org/2000/svg", "use");
newArrow.setAttributeNS('http://www.w3.org/1999/xlink', 'href', "#arrow");
arrows.appendChild(newArrow);
// animate arrow along path
var path = MorphSVGPlugin.pathDataToBezier("#arc");
TweenMax.to([newArrow], 0.5, {
force3D: true,
bezier: {
type: "cubic",
values: path,
autoRotate: ["x", "y", "rotation"]
},
onUpdate: hitTest,
onUpdateParams: ["{self}"],
onComplete: onMiss,
ease: Linear.easeNone
});
TweenMax.to("#arc", 0.3, {
opacity: 0
});
//hide previous arrow
TweenMax.set(".arrow-angle use", {
opacity: 0
});
}
function hitTest(tween) {
// check for collisions with arrow and target
var arrow = tween.target[0];
var transform = arrow._gsTransform;
var radians = transform.rotation * Math.PI / 180;
var arrowSegment = {
x1: transform.x,
y1: transform.y,
x2: (Math.cos(radians) * 60) + transform.x,
y2: (Math.sin(radians) * 60) + transform.y
}
var intersection = getIntersection(arrowSegment, lineSegment);
if (intersection.segment1 && intersection.segment2) {
tween.pause();
var dx = intersection.x - target.x;
var dy = intersection.y - target.y;
var distance = Math.sqrt((dx * dx) + (dy * dy));
var selector = ".hit";
if (distance = 0 && ua = 0 && ub <= 1
};
}
Disclaimer: The code provided in this post is sourced from GitHub and is distributed under the MIT License. All credits for the original code go to the respective owner. We have shared this code for educational purposes only. Please refer to the original repository for detailed information and any additional usage rights or restrictions.
Output
Build a Quiz Game Using HTML CSS and JavaScript Introduction Hello coders, you might have played various games, but were you aware …
Emoji Catcher Game Using HTML CSS and JavaScript Introduction Hello Coders, Welcome to another new blog. In this article we’ve made a …
Typing Challenge Using HTML CSS and JavaScript Introduction Hello friends, all you developer friends are welcome to our new project. If you …
Breakout Game Using HTML CSS and JavaScript With Source Code Introduction Hello friends, welcome to today’s new blog post. All of you …