Skip to content

Commit 5f9d3fe

Browse files
committed
This is a Covid Fighter game, move your fighter with the help of arrow keys and complete the challenge
1 parent dc82334 commit 5f9d3fe

16 files changed

Lines changed: 449 additions & 40 deletions

File tree

‎Color-game/README.md‎

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1 @@
1-
2-
<p align="center">
3-
<img src="https://hacktoberfest.digitalocean.com/assets/HF-full-logo-b05d5eb32b3f3ecc9b2240526104cf4da3187b8b61963dd9042fdc2536e4a76c.svg">
4-
</p>
5-
6-
7-
A community repository to allow enthusiasts and beginners add their projects for [@DigitalOcean's HacktoberFest](https://hacktoberfest.digitalocean.com)
8-
## About this Repository:
9-
10-
This repository allows any Algos to be added! When adding your own Code, place it in a folder within most suitable category. If there are no folder of that category yet, make a new folder and add it there! Making your PR will get your 1 step closer to your free T-Shirt!
11-
12-
## What is Hacktoberfest?
13-
14-
Hacktoberfest® is open to everyone in our global community. Whether you’re a developer, student learning to code, event host, or company of any size, you can help drive growth of open source and make positive contributions to an ever-growing community. All backgrounds and skill levels are encouraged to complete the challenge.
15-
16-
- Hacktoberfest is a celebration open to everyone in our global community.
17-
- Pull requests can be made in any GitHub-hosted repositories/projects.
18-
- You can sign up anytime between October 1 and October 31.
19-
20-
Sign up [Here](https://hacktoberfest.digitalocean.com)
21-
22-
## Making A Thoughtful Pull Request
23-
24-
Pull requests should include the following:
25-
26-
- A title about the project/program you are including
27-
- Detailed information about the commits
28-
- No low effort pull requests or duplicate programs
29-
- Place your Code in the appropriate folder with a README and information on the code
30-
31-
32-
33-
=======
34-
## Steps to contribute :
35-
1. Fork the repository
36-
2. Add a file or make a change
37-
3. Create a pull request
38-
4. Wait for the pr to get merged
39-
40-
### If you like this repo, drop a ⭐
1+
This is a color game. In this you have to guess the color on the basis of randomly generated color code.

‎Covid Fighter/Assets/back.jpeg‎

23.9 KB
Loading

‎Covid Fighter/Assets/gem.png‎

13.1 KB
Loading

‎Covid Fighter/Assets/gem.svg‎

Lines changed: 45 additions & 0 deletions
Loading

‎Covid Fighter/Assets/gemm.png‎

11.8 KB
Loading

‎Covid Fighter/Assets/superhero.png‎

18.2 KB
Loading

‎Covid Fighter/Assets/v1.png‎

34 KB
Loading

‎Covid Fighter/Assets/v2.png‎

45.1 KB
Loading

‎Covid Fighter/game.js‎

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
function load_images(){
2+
//player,virus,gem
3+
enemy_image = new Image;
4+
enemy_image.src = "Assets/v1.png";
5+
6+
player_img = new Image;
7+
player_img.src = "Assets/superhero.png";
8+
9+
gem_image = new Image;
10+
gem_image.src = "Assets/gemm.png";
11+
12+
13+
}
14+
15+
16+
function init(){
17+
//define the objects that we will have in the game
18+
canvas = document.getElementById("mycanvas");
19+
console.log(canvas);
20+
W = 700;
21+
H = 400;
22+
23+
canvas.width = W;
24+
canvas.height = H;
25+
26+
// create a context
27+
pen = canvas.getContext('2d');
28+
console.log(pen);
29+
game_over = false;
30+
31+
e1 = {
32+
x : 150,
33+
y : 50,
34+
w : 60,
35+
h : 60,
36+
speed : 20,
37+
};
38+
e2 = {
39+
x : 300,
40+
y : 150,
41+
w : 60,
42+
h : 60,
43+
speed : 30,
44+
};
45+
e3 = {
46+
x : 450,
47+
y : 20,
48+
w : 60,
49+
h : 60,
50+
speed : 40,
51+
};
52+
53+
enemy = [e1,e2,e3];
54+
55+
player = {
56+
x : 20,
57+
y : H/2,
58+
w : 60,
59+
h : 60,
60+
speed : 20,
61+
moving : false,
62+
health : 100,
63+
};
64+
65+
gem = {
66+
x : W-100,
67+
y : H/2,
68+
w : 60,
69+
h : 60,
70+
};
71+
//listen to events on the canvas
72+
canvas.addEventListener('mousedown',function(){
73+
console.log("Mouse Pressed");
74+
player.moving = true;
75+
});
76+
canvas.addEventListener('mouseup',function(){
77+
console.log("Mouse Released");
78+
player.moving = false;
79+
});
80+
81+
}
82+
83+
function isOverlap(rect1,rect2){
84+
if (rect1.x < rect2.x + rect2.w &&
85+
rect1.x + rect1.w > rect2.x &&
86+
rect1.y < rect2.y + rect2.h &&
87+
rect1.y + rect1.h > rect2.y) {
88+
return true
89+
}
90+
91+
return false;
92+
93+
}
94+
95+
function draw(){
96+
97+
//clear the canvas area for the old frame
98+
pen.clearRect(0,0,W,H);
99+
100+
pen.fillStyle = "red";
101+
//pen.fillRect(box.x,box.y,box.w,box.h);
102+
//pen.drawImage(enemy_image,box.x,box.y,box.w,box.h);
103+
104+
//draw the player
105+
106+
//draw the gem
107+
pen.drawImage(player_img,player.x,player.y,player.w,player.h);
108+
pen.drawImage(gem_image,gem.x,gem.y,gem.w,gem.h);
109+
110+
111+
for(let i=0;i<enemy.length;i++){
112+
pen.drawImage(enemy_image,enemy[i].x,enemy[i].y,enemy[i].w,enemy[i].h);
113+
}
114+
115+
pen.fillStyle = "white";
116+
pen.fillText("Score "+player.health,10,10);
117+
118+
}
119+
120+
function update(){
121+
122+
//if the player is moving
123+
if(player.moving==true){
124+
player.x += player.speed;
125+
player.health += 20;
126+
}
127+
128+
for(let i=0;i<enemy.length;i++){
129+
if(isOverlap(enemy[i],player)){
130+
player.health -= 50;
131+
if(player.health <0){
132+
console.log(player.health);
133+
game_over = true;
134+
alert("Game Over" + player.health);
135+
}
136+
}
137+
}
138+
139+
140+
141+
//overlap overlap
142+
if(isOverlap(player,gem)){
143+
144+
console.log("You Won");
145+
alert("You Won!");
146+
game_over = true;
147+
return;
148+
}
149+
150+
//move the box downwards
151+
//update each enemy by same logic
152+
for(let i=0;i<enemy.length;i++){
153+
enemy[i].y += enemy[i].speed;
154+
if(enemy[i].y>H-enemy[i].h || enemy[i].y <0){
155+
enemy[i].speed *= -1;
156+
}
157+
}
158+
159+
}
160+
161+
function gameloop(){
162+
if(game_over==true){
163+
clearInterval(f);
164+
}
165+
draw();
166+
update();
167+
console.log("In gameloop");
168+
}
169+
170+
load_images();
171+
init();
172+
var f = setInterval(gameloop,100);

‎Covid Fighter/index.html‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<html>
2+
<head>
3+
<title>Game Challenge-1</title>
4+
<style>
5+
canvas{
6+
background-image: url("Assets/back.jpeg");
7+
border:5px solid black;
8+
9+
}
10+
</style>
11+
12+
</head>
13+
<body>
14+
<canvas id="mycanvas"></canvas>
15+
<script src="game.js"></script>
16+
</body>
17+
18+
19+
</html>

0 commit comments

Comments
 (0)