Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2ff193e

Browse files
committed
add mario example with camera capture
1 parent 2b3d1eb commit 2ff193e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+2901
-0
lines changed

pyscriptjs/public/mario/css/game.css

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
html, body, ul, li {
2+
margin: 0;
3+
border: 0;
4+
padding: 0;
5+
}
6+
7+
canvas {
8+
display: block;
9+
width: 762;
10+
margin: 0 auto;
11+
background-color: blue;
12+
}
13+
14+
p {
15+
text-align: center;
16+
}
17+
18+
body {
19+
overflow: hidden;
20+
height: 100%;
21+
}
22+
23+
html {
24+
overflow: hidden;
25+
height: 100%;
26+
}
27+
28+
.info {
29+
position: absolute;
30+
top: 0;
31+
left: 0;
32+
}

pyscriptjs/public/mario/js/bcoin.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
(function() {
2+
if (typeof Mario === 'undefined')
3+
window.Mario = {};
4+
5+
var Bcoin = Mario.Bcoin = function(pos) {
6+
Mario.Entity.call(this, {
7+
pos: pos,
8+
sprite: level.bcoinSprite(),
9+
hitbox: [0,0,16,16]
10+
});
11+
}
12+
13+
Mario.Util.inherits(Bcoin, Mario.Entity);
14+
15+
//I'm not sure whether it makes sense to use an array for vel and acc here
16+
//in order to keep with convention, or to just use a single value, since
17+
//it's literally impossible for these to move left or right.
18+
Bcoin.prototype.spawn = function() {
19+
sounds.coin.currentTime = 0.05;
20+
sounds.coin.play();
21+
this.idx = level.items.length;
22+
level.items.push(this);
23+
this.active = true;
24+
this.vel = -12;
25+
this.targetpos = this.pos[1] - 32;
26+
}
27+
28+
Bcoin.prototype.update = function(dt) {
29+
if (!this.active) return;
30+
31+
if (this.vel > 0 && this.pos[1] >= this.targetpos) {
32+
player.coins += 1;
33+
//spawn a score thingy.
34+
delete level.items[this.idx];
35+
}
36+
37+
this.acc = 0.75;
38+
this.vel += this.acc;
39+
this.pos[1] += this.vel;
40+
this.sprite.update(dt);
41+
}
42+
43+
Bcoin.prototype.checkCollisions = function() {;}
44+
45+
})();

pyscriptjs/public/mario/js/block.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
(function() {
2+
if (typeof Mario === 'undefined')
3+
window.Mario = {};
4+
5+
//TODO: clean up the logic for sprite switching.
6+
//TODO: There's a weird bug with the collision logic. Look into it.
7+
8+
var Block = Mario.Block = function(options) {
9+
this.item = options.item;
10+
this.usedSprite = options.usedSprite;
11+
this.bounceSprite = options.bounceSprite;
12+
this.breakable = options.breakable;
13+
14+
Mario.Entity.call(this, {
15+
pos: options.pos,
16+
sprite: options.sprite,
17+
hitbox: [0,0,16,16]
18+
});
19+
20+
this.standing = true;
21+
}
22+
23+
Mario.Util.inherits(Block, Mario.Floor);
24+
25+
Block.prototype.break = function() {
26+
sounds.breakBlock.play();
27+
(new Mario.Rubble()).spawn(this.pos);
28+
var x = this.pos[0] / 16, y = this.pos[1] / 16;
29+
delete level.blocks[y][x];
30+
}
31+
32+
Block.prototype.bonk = function(power) {
33+
sounds.bump.play();
34+
if (power > 0 && this.breakable) {
35+
this.break();
36+
} else if (this.standing){
37+
this.standing = false;
38+
if (this.item) {
39+
this.item.spawn();
40+
this.item = null;
41+
}
42+
this.opos = [];
43+
this.opos[0] = this.pos[0];
44+
this.opos[1] = this.pos[1];
45+
if (this.bounceSprite) {
46+
this.osprite = this.sprite;
47+
this.sprite = this.bounceSprite;
48+
} else {
49+
this.sprite = this.usedSprite;
50+
}
51+
52+
this.vel[1] = -2;
53+
}
54+
}
55+
56+
Block.prototype.update = function(dt, gameTime) {
57+
if (!this.standing) {
58+
if (this.pos[1] < this.opos[1] - 8) {
59+
this.vel[1] = 2;
60+
}
61+
if (this.pos[1] > this.opos[1]) {
62+
this.vel[1] = 0;
63+
this.pos = this.opos;
64+
if (this.osprite) {
65+
this.sprite = this.osprite;
66+
}
67+
this.standing = true;
68+
}
69+
} else {
70+
if (this.sprite === this.usedSprite) {
71+
var x = this.pos[0] / 16, y = this.pos[1] / 16;
72+
level.statics[y][x] = new Mario.Floor(this.pos, this.usedSprite);
73+
delete level.blocks[y][x];
74+
}
75+
}
76+
77+
this.pos[1] += this.vel[1];
78+
this.sprite.update(dt, gameTime);
79+
}
80+
81+
})();

pyscriptjs/public/mario/js/coin.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
(function() {
2+
if (typeof Mario === 'undefined')
3+
window.Mario = {};
4+
5+
var Coin = Mario.Coin = function(pos, sprite) {
6+
Mario.Entity.call(this, {
7+
pos: pos,
8+
sprite: sprite,
9+
hitbox: [0,0,16,16]
10+
});
11+
this.idx = level.items.length
12+
}
13+
14+
Mario.Util.inherits(Coin, Mario.Entity);
15+
16+
Coin.prototype.isPlayerCollided = function() {
17+
//the first two elements of the hitbox array are an offset, so let's do this now.
18+
var hpos1 = [this.pos[0] + this.hitbox[0], this.pos[1] + this.hitbox[1]];
19+
var hpos2 = [player.pos[0] + player.hitbox[0], player.pos[1] + player.hitbox[1]];
20+
21+
//if the hitboxes actually overlap
22+
if (!(hpos1[0] > hpos2[0]+player.hitbox[2] || (hpos1[0]+this.hitbox[2] < hpos2[0]))) {
23+
if (!(hpos1[1] > hpos2[1]+player.hitbox[3] || (hpos1[1]+this.hitbox[3] < hpos2[1]))) {
24+
this.collect();
25+
}
26+
}
27+
}
28+
29+
Coin.prototype.render = function(ctx, vX, vY) {
30+
this.sprite.render(ctx, this.pos[0], this.pos[1], vX, vY);
31+
}
32+
33+
//money is not affected by gravity, you see.
34+
Coin.prototype.update = function(dt) {
35+
this.sprite.update(dt);
36+
}
37+
Coin.prototype.checkCollisions = function() {
38+
this.isPlayerCollided();
39+
}
40+
41+
Coin.prototype.collect = function() {
42+
sounds.coin.currentTime = 0.05;
43+
sounds.coin.play();
44+
player.coins += 1;
45+
delete level.items[this.idx]
46+
}
47+
})();

pyscriptjs/public/mario/js/entity.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
(function() {
2+
if (typeof Mario === 'undefined')
3+
window.Mario = {};
4+
5+
var Entity = Mario.Entity = function(options) {
6+
this.vel = [0,0];
7+
this.acc = [0,0];
8+
this.standing = true;
9+
this.pos = options.pos;
10+
this.sprite = options.sprite;
11+
this.hitbox = options.hitbox;
12+
this.left = false;
13+
}
14+
15+
Entity.prototype.render = function(ctx, vX, vY) {
16+
this.sprite.render(ctx, this.pos[0], this.pos[1], vX, vY)
17+
}
18+
19+
Entity.prototype.collideWall = function(wall) {
20+
//the wall will always be a 16x16 block with hitbox = [0,0,16,16].
21+
if (this.pos[0] > wall.pos[0]) {
22+
//from the right
23+
this.pos[0] = wall.pos[0] + wall.hitbox[2] - this.hitbox[0];
24+
this.vel[0] = Math.max(0, this.vel[0]);
25+
this.acc[0] = Math.max(0, this.acc[0]);
26+
} else {
27+
this.pos[0] = wall.pos[0] + wall.hitbox[0] - this.hitbox[2] - this.hitbox[0];
28+
this.vel[0] = Math.min(0, this.vel[0]);
29+
this.acc[0] = Math.min(0, this.acc[0]);
30+
}
31+
}
32+
33+
Entity.prototype.bump = function() {;}
34+
})();
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
(function() {
2+
if (typeof Mario === 'undefined')
3+
window.Mario = {};
4+
5+
var Fireball = Mario.Fireball = function(pos) {
6+
this.hit = 0;
7+
this.standing = false;
8+
9+
Mario.Entity.call(this, {
10+
pos: pos,
11+
sprite: new Mario.Sprite('sprites/items.png', [96, 144], [8,8], 5, [0,1,2,3]),
12+
hitbox: [0,0,8,8]
13+
});
14+
}
15+
16+
Mario.Util.inherits(Fireball, Mario.Entity);
17+
18+
Fireball.prototype.spawn = function(left) {
19+
sounds.fireball.currentTime = 0;
20+
sounds.fireball.play();
21+
if (fireballs[0]) {
22+
this.idx = 1;
23+
fireballs[1] = this;
24+
} else {
25+
this.idx = 0;
26+
fireballs[0] = this;
27+
}
28+
this.vel[0] = (left ? -5 : 5);
29+
this.standing = false;
30+
this.vel[1] = 0;
31+
}
32+
33+
Fireball.prototype.render = function(ctx, vX, vY) {
34+
this.sprite.render(ctx, this.pos[0], this.pos[1], vX, vY);
35+
}
36+
37+
Fireball.prototype.update = function(dt) {
38+
if (this.hit == 1) {
39+
this.sprite.pos = [96, 160];
40+
this.sprite.size = [16,16];
41+
this.sprite.frames = [0,1,2];
42+
this.sprite.speed = 8;
43+
this.hit += 1;
44+
return;
45+
} else if (this.hit == 5) {
46+
delete fireballs[this.idx];
47+
player.fireballs -= 1;
48+
return;
49+
} else if (this.hit) {
50+
this.hit += 1;
51+
return;
52+
}
53+
54+
//In retrospect, the way collision is being handled is RIDICULOUS
55+
//but I don't have to use some horrible kludge for this.
56+
if (this.standing) {
57+
this.standing = false;
58+
this.vel[1] = -4;
59+
}
60+
61+
this.acc[1] = 0.5;
62+
63+
this.vel[1] += this.acc[1];
64+
this.pos[0] += this.vel[0];
65+
this.pos[1] += this.vel[1];
66+
if (this.pos[0] < vX || this.pos[0] > vX + 256) {
67+
this.hit = 1;
68+
}
69+
this.sprite.update(dt);
70+
}
71+
72+
Fireball.prototype.collideWall = function() {
73+
if (!this.hit) this.hit = 1;
74+
}
75+
76+
Fireball.prototype.checkCollisions = function() {
77+
if (this.hit) return;
78+
var h = this.pos[1] % 16 < 8 ? 1 : 2;
79+
var w = this.pos[0] % 16 < 8 ? 1 : 2;
80+
81+
var baseX = Math.floor(this.pos[0] / 16);
82+
var baseY = Math.floor(this.pos[1] / 16);
83+
84+
if (baseY + h > 15) {
85+
delete fireballs[this.idx];
86+
player.fireballs -= 1;
87+
return;
88+
}
89+
90+
for (var i = 0; i < h; i++) {
91+
for (var j = 0; j < w; j++) {
92+
if (level.statics[baseY + i][baseX + j]) {
93+
level.statics[baseY + i][baseX + j].isCollideWith(this);
94+
}
95+
if (level.blocks[baseY + i][baseX + j]) {
96+
level.blocks[baseY + i][baseX + j].isCollideWith(this);
97+
}
98+
}
99+
}
100+
101+
var that = this;
102+
level.enemies.forEach(function(enemy){
103+
if (enemy.flipping || enemy.pos[0] - vX > 336){ //stop checking once we get to far away dudes.
104+
return;
105+
} else {
106+
that.isCollideWith(enemy);
107+
}
108+
});
109+
}
110+
111+
Fireball.prototype.isCollideWith = function(ent) {
112+
//the first two elements of the hitbox array are an offset, so let's do this now.
113+
var hpos1 = [this.pos[0] + this.hitbox[0], this.pos[1] + this.hitbox[1]];
114+
var hpos2 = [ent.pos[0] + ent.hitbox[0], ent.pos[1] + ent.hitbox[1]];
115+
116+
//if the hitboxes actually overlap
117+
if (!(hpos1[0] > hpos2[0]+ent.hitbox[2] || (hpos1[0]+this.hitbox[2] < hpos2[0]))) {
118+
if (!(hpos1[1] > hpos2[1]+ent.hitbox[3] || (hpos1[1]+this.hitbox[3] < hpos2[1]))) {
119+
this.hit = 1;
120+
ent.bump();
121+
}
122+
}
123+
};
124+
125+
Fireball.prototype.bump = function() {;}
126+
})();

0 commit comments

Comments
 (0)