Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 30 additions & 12 deletions docs/demo/fort/fort.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ let loose = false;
let greenSpots = [];
let redSpots = [];

const defaultDecayInterval = 60;
let decayInterval = defaultDecayInterval;

const defaultPlaceInterval = 60;
let placeInterval = defaultDecayInterval;

function placeGreen() {
if (greenSpots.length > score) {
return;
Expand All @@ -23,7 +29,7 @@ function placeGreen() {
continue placeGreenLoop;
}
}
const randomHealth = randomRange(15, 50);
const randomHealth = randomRange(1, 15);
greencoordinates = { column: random.column, row: random.row, health: randomHealth };
}
greenSpots.push(greencoordinates);
Expand All @@ -49,7 +55,7 @@ function placeRed() {
continue placeRedLoop;
}
}
const randomHealth = randomRange(25, 50);
const randomHealth = randomRange(2, 20);
redcoordinates = { column: random.column, row: random.row, health: randomHealth };
}
redSpots.push(redcoordinates);
Expand Down Expand Up @@ -90,7 +96,7 @@ function init() {
placeRed();
}

function update() {
function update(delta) {
if (loose === true) {
// Crashed!
// Flush the screen.
Expand All @@ -103,6 +109,9 @@ function update() {
greenSpots = [];
redSpots = [];

decayInterval = defaultDecayInterval;
placeInterval = defaultPlaceInterval;

placeGreen();
placeRed();

Expand All @@ -111,18 +120,27 @@ function update() {
return;
}

// Randomly place a counter.
let actions = [
placeGreen,
placeRed,
];
shuffle(actions);
actions[0]();
decayInterval -= (delta * 60);

decreaseHealth();
if (decayInterval <= 0) {
decreaseHealth();
decayInterval = Math.max(5, defaultDecayInterval - score);
}

placeInterval -= (1 + delta);
if (placeInterval <= 0) {
// Randomly place a counter.
let actions = [
placeGreen,
placeRed,
];
shuffle(actions);
actions[0]();
placeInterval = Math.max(5, defaultPlaceInterval - score);
}
}

function draw() {
function draw(delta) {
cls();
for (let i = 0; i < gridMaxX; i += 1) {
for (let j = 0; j < gridMaxY; j += 1) {
Expand Down
30 changes: 22 additions & 8 deletions docs/demo/forward/forward.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Set up variables for the forward game.
let loose = false;
let playercoordinates = {};
let wallTimeout;
const defaultMovementInterval = 60;
let moveInterval = defaultMovementInterval;

const shapes = [];
shapes[0] = [
Expand Down Expand Up @@ -34,7 +35,6 @@ let wall = [];
let wallCoordinates;

function moveWall() {
wallTimeout = setTimeout(moveWall, Math.max(1000 - (score * 100), 250));
if (wallIsActive) {
if (wallCoordinates === -1) {
wallIsActive = false;
Expand All @@ -52,9 +52,6 @@ function moveWall() {

function placePlayer() {
playercoordinates = { column: 0, row: 2 };
if (typeof wallTimeout !== 'number') {
wallTimeout = setTimeout(moveWall, 1000);
}
}

function userActionKeyPress(direction) {
Expand All @@ -69,14 +66,17 @@ function userActionKeyPress(direction) {
playercoordinates.row += 1;
}
break;
default:
// Do nothing.
break;
}
}

function init() {
placePlayer();
}

function update() {
function update(delta) {
if (loose === true) {
// Crashed!
// Flush the screen.
Expand All @@ -85,6 +85,12 @@ function update() {
displayScore(score);
// Reset some variables.
score = 0;
moveInterval = defaultMovementInterval;

wallIsActive = false;
wall = [];
wallCoordinates = 0;

// Set the game up again.
placePlayer();

Expand All @@ -93,13 +99,21 @@ function update() {
return;
}

moveInterval -= (delta * 60);

if (moveInterval <= 0) {
moveWall();
moveInterval = Math.max(5, defaultMovementInterval - score);
}

// Check loose state.
for (let i = 0; i < gridMaxX; i += 1) {
for (let j = 0; j < gridMaxY; j += 1) {
const element = grid[i][j];
if (element.column === wallCoordinates) {
for (let wallpart = 0; wallpart < wall.length; wallpart += 1) {
if (wall[wallpart] === 1 && element.row === wallpart) {
if (element.column === playercoordinates.column
if (element.column === playercoordinates.column
&& element.row === playercoordinates.row) {
loose = true;
return;
Expand All @@ -111,7 +125,7 @@ function update() {
}
}

function draw() {
function draw(delta) {
cls();

for (let i = 0; i < gridMaxX; i += 1) {
Expand Down
2 changes: 1 addition & 1 deletion docs/demo/forward/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ <h1>Four Demo: Forward</h1>
</div>

<script src="forward.js"></script>
<p>An implementation of flappy bird. Use the up and down arrows to move your box up and down and avoid the walls.</p>
<p>An implementation of flappy bird, without the gravity. Use the up and down arrows to move your box up and down and avoid the walls.</p>
</main>
<footer>
<p>Four was created by <a href="https://www.hashbangcode.com">#! code</a> and is licensed under the MIT license.<br>
Expand Down
18 changes: 10 additions & 8 deletions docs/demo/forx/forx.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// Set up variables for the fox game.
let loose = false;
let foxtimeout;
let playercoordinates = {};
let foodcoordinates = {};
let foxcoordinates = {};
const foxMovementInterval = 90;
let moveInterval = foxMovementInterval;

function placeFood() {
foodcoordinates = {};
Expand Down Expand Up @@ -54,7 +55,6 @@ function placePlayer() {
}

function moveFox() {
foxtimeout = setTimeout(moveFox, Math.max(2000 - (score * 100), 250));
let movement = [
function down(fox, player) {
if (fox.row < player.row) {
Expand Down Expand Up @@ -124,9 +124,6 @@ function userActionKeyPress(direction) {
// Do nothing.
break;
}
if (typeof foxtimeout !== 'number') {
foxtimeout = setTimeout(moveFox, 2000);
}
}

function init() {
Expand All @@ -138,16 +135,14 @@ function init() {
function update() {
if (loose === true) {
// Crashed!
// Reset the fox timeout.
clearTimeout(foxtimeout);
foxtimeout = undefined;

// Flush the screen.
cls();
// Display the score.
displayScore(score);
// Reset some variables.
score = 0;
moveInterval = foxMovementInterval;

placePlayer();
placeFood();
Expand All @@ -158,6 +153,13 @@ function update() {
return;
}

moveInterval -= (delta * 60);

if (moveInterval <= 0) {
moveFox();
moveInterval = Math.max(5, foxMovementInterval - (score * 2));
}

if (foxcoordinates.column === playercoordinates.column
&& foxcoordinates.row === playercoordinates.row) {
loose = true;
Expand Down
26 changes: 15 additions & 11 deletions docs/demo/fourdelance/fourdelance.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
let loose = false;
let snake = [];
let foodcoordinates = {};
let snakeMoveTimeout;
let snakeDirection;

const defaultMovementInterval = 60;
let moveInterval = defaultMovementInterval;

const SNAKE_UP = 'up';
const SNAKE_DOWN = 'down';
const SNAKE_LEFT = 'left';
Expand All @@ -30,16 +32,13 @@ function placeFood() {
}

function moveSnake() {
snakeMoveTimeout = setTimeout(moveSnake, Math.max(2000 - (score * 100), 250));

const head = structuredClone(snake[0]);

switch (snakeDirection) {
case SNAKE_LEFT:
if (snake[0].column >= 1) {
head.column -= 1;
}
else {
} else {
loose = true;
}
break;
Expand Down Expand Up @@ -96,9 +95,6 @@ function placeSnake() {

snake.push({ column: random.column, row: random.row });
}
if (typeof snakeMoveTimeout !== 'number') {
snakeMoveTimeout = setTimeout(moveSnake, 100);
}
}

function userActionKeyPress(direction) {
Expand All @@ -122,11 +118,11 @@ function userActionKeyPress(direction) {
}

function init() {
placeSnake();
placeFood();
placeSnake();
placeFood();
}

function update() {
function update(delta) {
if (loose === true) {
// We have a winner!
// flush the screen.
Expand All @@ -137,6 +133,7 @@ function update() {
score = 0;
snake = [];
snakeDirection = undefined;
moveInterval = defaultMovementInterval;

// Reset the game grid.
placeSnake();
Expand All @@ -145,6 +142,13 @@ function update() {
// Turn off the win state.
loose = false;
}

moveInterval -= (delta * 60);

if (moveInterval <= 0) {
moveSnake();
moveInterval = Math.max(5, defaultMovementInterval - score);
}
}

function draw() {
Expand Down
2 changes: 1 addition & 1 deletion docs/demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ <h3>Four De Lance</h3>
<a class="button" href="/four/demo/fourdelance/">See demo</a>

<h3>Forward</h3>
<p>An implementation of flappy bird. Use the up and down arrows to move your box up and down and avoid the walls.</p>
<p>An implementation of flappy bird, without the gravity. Use the up and down arrows to move your box up and down and avoid the walls.</p>
<a class="button" href="/four/demo/forward/">See demo</a>
</main>

Expand Down
31 changes: 19 additions & 12 deletions docs/demo/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,25 @@ function init() {
console.log('init');
}

function update() {
console.log('update');
function update(delta) {
console.log(`update ${delta}`);

if (score === 1) {
displayScore(1234567890);
score = 0;
} else {
score = 1;
}

wait(2000);
}

function draw() {
console.log('draw');
function draw(delta) {
console.log(`draw ${delta}`);

if (score === 1) {
return;
}

const colours = [
'blue',
Expand All @@ -28,19 +41,13 @@ function draw() {
'fuchsia',
];

shuffle(colours);

let colourCount = 0;
for (let i = 0; i < grid.length; i += 1) {
for (let j = 0; j < grid[i].length; j += 1) {
fillBox(grid[i][j], colours[colourCount]);
colourCount += 1;
}
}
wait(2000);

if (score === 1) {
displayScore(1234567890);
score = 0;
} else {
score = 1;
}
}
Loading