Skip to content

Commit 87e4d40

Browse files
committed
Basic game
1 parent c6bf033 commit 87e4d40

File tree

5 files changed

+119
-2
lines changed

5 files changed

+119
-2
lines changed

.gitignore

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
bower_components
2-
node_modules
31
.DS_Store
2+
lib

css/style.css

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
body {
2+
margin: 0;
3+
background-color: #111;
4+
display: flex;
5+
justify-content: center;
6+
align-items: center;
7+
}

index.html

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>
5+
Learnathon Platformer
6+
</title>
7+
<link rel='stylesheet' type='text/css' href='css/style.css' />
8+
</head>
9+
<body>
10+
<script type='text/javascript' src='lib/p5.min.js'></script>
11+
<script type='text/javascript' src='lib/p5.play.js'></script>
12+
<script type='text/javascript' src='src/platformer.js'></script>
13+
</body>
14+
</html>

setup.sh

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env bash
2+
3+
mkdir -p lib
4+
wget -P lib 'https://github.com/processing/p5.js/releases/download/0.4.22/p5.min.js'
5+
wget -P lib 'https://raw.githubusercontent.com/molleindustria/p5.play/master/lib/p5.play.js'

src/platformer.js

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
var game = {
2+
blockSize: 30,
3+
objects: []
4+
};
5+
6+
function makeClasses() {
7+
game.Block = function(x, y, w, h) {
8+
this.sprite = createSprite(x, y, w, h);
9+
};
10+
game.Block.prototype = {
11+
tick: function() {},
12+
collideWith: function(target) {}
13+
};
14+
15+
game.Ground = function(x, y, w, h) {
16+
game.Block.call(this, x, y, w, h);
17+
this.sprite.shapeColor = color(80, 180, 100);
18+
//this.sprite.immovable = true;
19+
};
20+
game.Ground.prototype = {
21+
tick: function() {
22+
game.Block.prototype.tick.call(this);
23+
},
24+
collideWith: function(target) {
25+
console.log(target);
26+
game.Block.prototype.collideWith.call(this, target);
27+
target.sprite.velocity.y = 0;
28+
}
29+
};
30+
31+
game.PhysicsObject = function(x, y, w, h) {
32+
game.Block.call(this, x, y, w, h);
33+
this.gravity = createVector(0, 0.2);
34+
};
35+
game.PhysicsObject.prototype = {
36+
tick: function() {
37+
game.Block.prototype.tick.call(this);
38+
this.sprite.velocity.add(this.gravity);
39+
},
40+
collideWith: function(target) {
41+
game.Block.prototype.collideWith.call(this, target);
42+
}
43+
};
44+
45+
game.Player = function(x, y) {
46+
game.PhysicsObject.call(this, x, y, game.blockSize, game.blockSize);
47+
this.sprite.shapeColor = color(255, 0, 0);
48+
this.jump = createVector(0, -15);
49+
};
50+
game.Player.prototype = {
51+
tick: function() {
52+
game.PhysicsObject.prototype.tick.call(this);
53+
if (keyDown('RIGHT_ARROW')) {
54+
this.sprite.velocity.x = 5;
55+
} else if (keyDown('LEFT_ARROW')) {
56+
this.sprite.velocity.x = -5;
57+
} else {
58+
this.sprite.velocity.x = 0;
59+
}
60+
61+
if (keyWentDown('UP_ARROW') && this.sprite.velocity.y == 0) {
62+
this.sprite.velocity.y = this.jump.y;
63+
}
64+
},
65+
collideWith: function(target) {
66+
game.PhysicsObject.prototype.collideWith.call(this, target);
67+
}
68+
};
69+
}
70+
71+
function setup() {
72+
makeClasses();
73+
createCanvas(800, 600);
74+
objects = [new game.Player(400, 300), new game.Ground(200, 400, 300, 30)];
75+
}
76+
77+
function draw() {
78+
background(255, 220, 180);
79+
objects.forEach(function(obj) {
80+
obj.tick();
81+
});
82+
for (var i=0; i<objects.length; i++) {
83+
for (var j=i+1; j<objects.length; j++) {
84+
if (objects[i].sprite.overlap(objects[j].sprite)) {
85+
console.log("touching");
86+
objects[i].collideWith(objects[j]);
87+
objects[j].collideWith(objects[i]);
88+
};
89+
}
90+
}
91+
drawSprites();
92+
}

0 commit comments

Comments
 (0)