typesgine-ascii - Game ASCII engine for terminal
About | technologies | Features | Examples | Functions | License
📓
About
Typesgine is engine for create games ascii into terminal with IO, FPS and render.
🎂
technologies 🐱🏍- Node - Nodejs
- typescript - Super Javascript
Features
Functions
- FrameHandler.Handler
This function is called every frame. It's used to render the game. It must execute the render function that needs to receive an Array [][] / [[]] or a String containing the ASCII to be rendered.
If Array [][] / [[]] dont set the width and height of the screen, the engine will set it automatically.
If String set the width and height of the screen.
/// typesgine.FrameHandler.Handler(callback(engineIo))
const frameHandler = typesgine.FrameHandler.Handler((engineIo) => {
engineIo.render("1234", 24, 16);
});
let Graph = [
["㊗", "2", "㊗", "㊗", "\n"],
["㊗", "㊗", "3", "㊗", "\n"],
["㊗", "4", "㊗", "㊗", "\n"],
];
const frameHandler = typesgine.FrameHandler.Handler((engineIo) => {
engineIo.render(Graph);
});
- InputHandler.Handler
It's used to handle the input. Because this function return a Keypress
/// typesgine.InputHandler.Handler(callback(engineIo, keyPress))
const inputHandler = typesgine.InputHandler.Handler((engineIo, keyPress) => {
console.log("KeyPress: ", keyPress);
if (keyPress === "a") console.log("Moving player");
if (keyPress === "Escape") process.exit();
});
- EngineIo
It's used to handle the input. Because this function return a Keypress
new typesgine.EngineIo({
fps: 60, // Frame per second
//Set the render handler, use peer default is typesgine.RenderTerminal for terminal.
renderHandler: new typesgine.Render(new typesgine.RenderTerminal()),
frameHandler: frameHandler, // - FrameHandler.Handler
keypressHandler: inputHandler, // - InputHandler.Handler
});
Exemple
See Folder Exemple in the root of the project to see how to use the engine.
Simplest example
import typesgine from "typesgine-ascii";
console.clear();
const render = () => {
let final = "";
for (let i = 0; i < 16; i++) {
for (let j = 0; j < 24; j++) {
final += `${Math.round(Math.random() * (0 - 9) + 9)}`;
}
}
return final;
};
const frameHandler = typesgine.FrameHandler.Handler((engineIo) => {
engineIo.render(render(), 24, 16);
});
const inputHandler = typesgine.InputHandler.Handler((engineIo, keyPress) => {
console.log("KeyPress: ", keyPress);
});
new typesgine.EngineIo({
fps: 60,
renderHandler: new typesgine.Render(new typesgine.RenderTerminal()),
frameHandler: frameHandler,
keypressHandler: inputHandler,
});
Simplest example, using Arrays for render
import typesgine from "typesgine-ascii";
console.clear();
const render = () => {
let final = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
];
for (let i = 0; i < 8; i++) {
for (let j = 0; j < 24; j++) {
final[i][j] = Math.round(Math.random() * (0 - 9) + 9);
}
final[i][final[i].length - 1] = "\n";
}
return final;
};
const frameHandler = typesgine.FrameHandler.Handler((engineIo) => {
engineIo.render(render(), 24, 16);
});
const inputHandler = typesgine.InputHandler.Handler((engineIo, keyPress) => {
console.log("KeyPress: ", keyPress);
});
new typesgine.EngineIo({
fps: 60,
renderHandler: new typesgine.Render(new typesgine.RenderTerminal()),
frameHandler: frameHandler,
keypressHandler: inputHandler,
});
License
MIT LICENSE