catparade/cat.html

277 lines
5.6 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<style>
canvas {
border:1px solid #d3d3d3;
background-color: #f1f1f1;
}
</style>
</head>
<body onload="startGame()">
<script>
var myGamePiece;
var things = [];
var myScore;
var GRAVITY = 0.5;
var GRIDSIZE = 80;
var THINGSIZE = 64;
var GRIDW = 5;
var GRIDH = 5;
function getgridthing(gridx, gridy) {
var i;
for (i = 0; i < things.length; i += 1) {
if ((things[i].gridx == gridx) && (things[i].gridy == gridy)) {
return things[i];
}
}
return null;
}
function getthingxy(x, y) {
var i;
for (i = 0; i < things.length; i += 1) {
if ((x >= things[i].x) && (x <= things[i].x + THINGSIZE-1) &&
(y >= things[i].y) && (y <= things[i].y + THINGSIZE-1)) {
return things[i];
}
}
return null;
}
function startGame() {
var x,y;
/*
myGamePiece = new thing(30, 30, "red", 10, 120);
myGamePiece.gravity = 0.05;
myScore = new thing("30px", "Consolas", "black", 280, 40, "text");
*/
for (y = 0; y < GRIDH; y++) {
for (x = 0; x < GRIDW; x++) {
things.push(new thing(x, y, getrandomname()));
}
}
myGameArea.start();
}
var myGameArea = {
canvas : document.createElement("canvas"),
start : function() {
this.canvas.width = 480;
this.canvas.height = 640;
this.context = this.canvas.getContext("2d");
document.body.insertBefore(this.canvas, document.body.childNodes[0]);
this.frameNo = 0;
this.interval = setInterval(updateGameArea, 20);
this.canvas.addEventListener('click', this.handleclick, false);
},
clear : function() {
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
},
draw : function() {
for (y = 0; y < GRIDH*GRIDSIZE; y += GRIDSIZE) {
for (x = 0; x < GRIDW*GRIDSIZE; x += GRIDSIZE) {
this.context.rect(x, y, GRIDSIZE-1, GRIDSIZE-1);
}
}
this.context.stroke();
},
handleclick : function(event) {
// did you click on an object?
var clickedthing = getthingxy(event.pageX,event.pageY);
if (clickedthing) {
clickedthing.kill();
}
}
}
function getrandomcolour() {
var letters = '0123456789ABCDEF';
var col = '#';
for (var i = 0; i < 6; i++ ) {
col += letters[Math.floor(Math.random() * 16)];
}
return col;
}
function getrandomname() {
var letters = '0123456789';
var name = "cat-";
for (var i = 0; i < 3; i++ ) {
name += letters[Math.floor(Math.random() * 10)];
}
return name;
}
function thing(gridx, gridy, name) {
this.name = name;
this.width = THINGSIZE;
this.height = THINGSIZE;
this.color = getrandomcolour();
this.gridx = gridx;
if (gridy == "top") {
highest = 999;
// find highest
for (i = 0; i < things.length; i += 1) {
if ((things[i].gridx == gridx) &&
(things[i].gridy < highest)) {
highest = things[i].gridy;
}
}
highest--;
gridy = highest;
//console.log("adding thing at gridy = " + gridy);
} else {
this.gridy = gridy;
}
this.yspeed = 0;
this.state = "stop";
this.x = gridx * GRIDSIZE;
this.y = gridy * GRIDSIZE;
this.draw = function() {
var yoff;
ctx = myGameArea.context;
ctx.fillStyle = this.color;
ctx.fillRect(this.x, this.y, this.width, this.height);
ctx.fillStyle = "black";
ctx.fillText(this.name, this.x + 10, this.y + (THINGSIZE/2));
ctx.fillText(this.state, this.x + 10, this.y + (THINGSIZE/2) + 10);
}
this.snaptogrid = function() {
if (this.y % GRIDSIZE != 0) {
this.y = this.gridy * GRIDSIZE;
}
}
this.getstoppedbelowthing = function() {
var bt;
bt = getgridthing(this.gridx, this.gridy + 1);
if (bt && bt.state != "fall") {
return bt;
}
return null;
}
this.kill = function() {
// add a new cat above us
things.push(new thing(this.gridx, "top", getrandomname()));
// kill ourselves
var idx = things.indexOf(this);
things.splice(idx, 1);
}
this.move = function() {
// not at bottom and nothing fixed below us?
var dofall = false;
var belowthing = null,atbottom = false;
if ((this.gridy >= GRIDH-1)) {
atbottom = true;
}
if (!atbottom && !this.getstoppedbelowthing()) {
// accelerate
this.yspeed += GRAVITY;
// move
this.y += this.yspeed;
// don't go below bottom of screen
this.hitBottom();
// calc new gridx / gridy
this.gridx = Math.floor(this.x / GRIDSIZE);
this.gridy = Math.floor(this.y / GRIDSIZE);
this.state = "fall";
}
// hit something?
if (atbottom || this.getstoppedbelowthing()) {
// something below us.
// stop
this.yspeed = 0;
// snap to grid
this.snaptogrid();
this.state = "stop";
}
}
this.hitBottom = function() {
var rockbottom = GRIDSIZE * GRIDH;
if (this.y > rockbottom) {
this.y = rockbottom;
this.yspeed = 0;
}
}
}
function updateGameArea() {
var x, height, gap, minHeight, maxHeight, minGap, maxGap;
myGameArea.clear();
myGameArea.draw();
myGameArea.frameNo += 1;
/*
if (myGameArea.frameNo == 1 || everyinterval(150)) {
x = myGameArea.canvas.width;
minHeight = 20;
maxHeight = 200;
height = Math.floor(Math.random()*(maxHeight-minHeight+1)+minHeight);
minGap = 50;
maxGap = 200;
gap = Math.floor(Math.random()*(maxGap-minGap+1)+minGap);
things.push(new thing(10, height, "green", x, 0));
things.push(new thing(10, x - height - gap, "green", x, height + gap));
}
*/
for (i = 0; i < things.length; i += 1) {
things[i].draw();
things[i].move();
}
//myScore.text="SCORE: " + myGameArea.frameNo;
//myScore.draw();
//myGamePiece.move();
//myGamePiece.update();
}
function everyinterval(n) {
if ((myGameArea.frameNo / n) % 1 == 0) {return true;}
return false;
}
//<button onmousedown="accelerate(-0.2)" onmouseup="accelerate(0.05)">ACCELERATE</button>
</script>
<br>
<p>Test game</p>
</body>
</html>