
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
nodegamesjs
Advanced tools
npm install nodegamesjsFirst you'll have to require the module.
var nodegames = require("nodegamesjs");
Then init nodegames with the following code:
//This will take no time if everything is correctly installed.
//If it's not the case it will install the missing dependecies logging the progress.
nodegames.init();
Then create a game with the following code:
var game = nodegames.newGame(function (game) {
// The callback will be called once the game window has opened and is ready.
// "game" is the game object that you'll use to control the game.
}, width, height)
//Change the above "width" and "height" to the actual width and height
In the following stuff "game" represents the callback argument and as so the game object if you change the argument name you'll have to change the name in the following stuff.
As the default name for your game window is "Untitled" you'll probably want to change it.
game.setWindowName("My game"); //Replace "My game" with the name you want.
Here is the event list:
To use these events:
//Resize event
game.on("resize", function (event) {
//"event" object contains:
var width = event.width; //The new width
var height = event.height; //The new height
console.log("Resized to: " + width + "x" + height);
})
//Close event
game.on("close", function () {
console.log("Game window closed.");
})
//Mousemove event
game.on("mousemove", function (event) {
//"event" object contains
var x = event.x //The new x coordinate of the mouse
var y = event.y //The new y coordinate of the mouse
var direction = event.direction //The direction the mouse is going to (up, down, left, right, upright, upleft, downright, downleft)
var speed = event.speed //The speed the mouse moved
var movement = event.movement //The raw movement values
var movementX = movement.x //The x raw movement value
var movementY = movement.y //The y raw movement value
console.log("Moved mouse to " + x + ", " + y + " in direction: " + direction + " at speed: " + speed);
})
//Keypress event
game.on("keypress", function (event) {
//"event" object contains
var key = event.key; //Which key has been pressed
var shiftKey = event.shiftKey; //Is shift pressed while pressing that key
var ctrlKey = event.ctrlKey; //Is control pressed while pressing that key
console.log("Key pressed: " + key + ", is shift pressed: " + shiftKey + ", is control pressed: " + ctrlKey);
})
//Keyrelease event
game.on("keyrelease", function (event) {
//"event" object contains
var key = event.key; //Which key has been released
var shiftKey = event.shiftKey; //Was shift pressed when the key was still pressed
var ctrlKey = event.ctrlKey; //Was control pressed when the key was still pressed
console.log("Key released: " + key + ", was shift pressed: " + shiftKey + ", was control pressed: " + ctrlKey);
})
//Mouseclick event
game.on("mouseclick", function (event) {
//"event" object contains
var button = event.button; //Mouse button (scrollwheel, left, right, side1, side2)
var x = event.x; //x position of mouse click
var y = event.y; //y position of mouse click
console.log("Mouse button pressed: " + button + " at " + x + ", " + y)
})
//Mousescroll event
game.on("mousescroll", function (event) {
//"event" object contains:
var direction = event.direction; //Direction of scroll (up, down, left, right)
var speed = event.speed; //Speed of scroll
console.log("Scrolling " + direction + " at speed: " + speed)
})
//Imageload event
game.on("imageload", function (id) {
//"id" is the loaded image id
console.log("Image with id: " + id + " loaded.")
})
//Imageunload event
game.on("imageunload", function (id) {
//"id" is the unloaded image id
console.log("Image with id: " + id + " unloaded.")
})
//Error event
game.on("error", function (error) {
//"error" object contains:
var exit_code = error.exit_code; //Always 1 as 1 represents an error.
var message = error.data.message; //Message (what is the error that happened)
var problem = error.data.problem; //Problem (what went wrong)
//For some errors (with the image and sound related errors) there is the image/sound id
var id = error.data.id; //May be null for non image/sound related errors
console.log("Error with exit code: " + exit_code + ", message: " + message + " and problem " + problem)
})
//Framerender event
game.on("framerender", function () {
console.log("Rendered a frame.")
})
//Soundload event
game.on("soundload", function (id) {
//"id" is the loaded sound id
console.log("Sound with id: " + id + " loaded.")
})
//Soundunload event
game.on("soundunload", function (id) {
//"id" is the unloaded sound id
console.log("Sound with id: " + id + " unloaded.")
})
//Soundstop event
game.on("soundstop", function (id) {
//"id" is the sound stopped from playing id
console.log("Sound with id: " + id + " has been stopped from playing.")
})
To draw stuff you can use the following:
//Draw a pixel
//
//Syntax: game.pixel(x:int, y:int, rgb:array
//
//Example:
game.pixel(0, 0, [255, 255, 255]) //White pixel at coordinates 0, 0
//Draw a rectangle
//
//Syntax: game.rect(x:int, y:int, height:int, width:int, rgb:array, rotation:int/null, hollow?:boolean/null)
//
//Examples:
game.rect(0, 0, 10, 10, [255, 255, 255]) //White rectangle at coordinates 0, 0 of width and height 10px, rotation of 0 degrees, not hollow
game.rect(0, 0, 10, 10, [255, 255, 255], 45) //White rectangle at coordinates 0, 0 of width and height 10px, rotation of 45 degrees, not hollow
game.rect(0, 0, 10, 10, [255, 255, 255], 45, true) //White rectangle at coordinates 0, 0 of width and height 10px, rotation of 45 degrees, hollow
game.rect(0, 0, 10, 10, [255, 255, 255], 45, false) //White rectangle at coordinates 0, 0 of width and height 10px, rotation of 45 degrees, not hollow
//Draw a circle
//
//Syntax: game.circle(x:int, y:int, radius:int/float, rgb:array, hollow?:boolean/null)
//
//Exemples:
game.circle(0, 0, 10, [255, 255, 255]) //White circle at coordinates 0, 0 of radius 10px, not hollow
game.circle(0, 0, 10, [255, 255, 255], true) //White circle at coordinates 0, 0 of radius 10px, hollow
game.circle(0, 0, 10, [255, 255, 255], false) //White circle at coordinates 0, 0 of radius 10px, not hollow
//Draw a line
//
//Syntax: game.line(x1:int, y1:int, x2:int, y2:int, rgb:array, rotation:int/null)
//
//Examples:
game.line(0, 0, 10, 0, [255, 255, 255]) //White line from coordinates 0, 0 to 10, 0 with a rotation of 0 degrees
game.line(0, 0, 10, 0, [255, 255, 255], 45) //White line from coordinates 0, 0 to 10, 0 with a rotation of 45 degrees
//Draw text
//
//Syntax: game.text(x:int, y:int, text:string, rgb:array, fontSize:int, fontName:string, rotation:int/null)
//
//Examples:
game.text(0, 0, "Hello world!", [255, 255, 255], 10, "Arial") //"Hello world!" in white at coordinates 0, 0 with font name "Arial" font size 10 and a rotation of 0 degrees.
game.text(0, 0, "Hello world!", [255, 255, 255], 10, "Arial", 45) //"Hello world!" in white at coordinates 0, 0 with font name "Arial" font size 10 and a rotation of 45 degrees.
//Clear window/rectangle
//
//Syntax: game.clear() or game.clear(x:int, y:int, width:int, height:int, rotation:int/null)
//
//Examples:
game.clear() //Clear whole window
game.clear(0, 0, 10, 10) //Clear a rectangle of 10px as width and height at coordinates 0, 0 with for rotation 0 degrees
game.clear(0, 0, 10, 10, 45) //Clear a rectangle of 10px as width and height at coordinates 0, 0 with for rotation 45 degrees
//Draw image
//
//Syntax: game.image(image_id:string, x:int, y:int, width:int, height:int, rotation:int/null)
//
//Examples:
game.image("My_image", 0, 0, 10, 10) //Image loaded with id "My_image" at coordinates 0, 0, with for width and height 10px and for rotation 0 degrees
game.image("My_image", 0, 0, 10, 10, 45) //Image loaded with id "My_image" at coordinates 0, 0, with for width and height 10px and for rotation 45 degrees
After 'drawing' all of your stuff you'll notice nothing is displayed on the window it's because you have to render it with the renderFrame method, add this after 'drawing' all of your stuff:
game.renderFrame()
It is not recommanded to render frames quicker than each 10ms (100fps) as it is the maximum stable fps you can get with nodegames. If you exceed it can lag a lot.
To load/unload an image do this:
game.loadImage(image, id)
Image must be a jpg or png that you can get like this:
var fs = require("fs")
var image = fs.readFileSync("/path/to/image");
The 'problem' of the loadImage method is that it returns a promise so it is not syncronous but the good thing about that is that you can just use .then or await to set a callback or wait for the image to be loaded.
Now to unload an image you can do the following:
game.unloadImage(id)
This should unload the image, this does not return any kind of promise tho. just assume it is 'syncronous' and unloads the image instantly which is almost the case
To load/unload a sound do this:
game.loadSound(sound, id)
Sound must be a wav or mp3 that you can get like this:
var fs = require("fs")
var sound = fs.readFileSync("/path/to/sound");
The 'problem' of the loadSound method is that it returns a promise so it is not syncronous but the good thing about that is that you can just use .then or await to set a callback or wait for the sound to be loaded.
Now to unload a sound you can do the following:
game.unloadSound(id)
This should unload the sound, this does not return any kind of promise tho. just assume it is 'syncronous' and unloads the sound instantly which is almost the case
To play a sound you can do the following:
game.playSound(id)
This should begin playing the sound.
To stop a sound you can do the following:
game.stopSound(id)
This should stop the sound from playing.
To hide the pointer:
game.pointer.hide()
To show the pointer:
game.pointer.show()
To try to lock the pointer
game.pointer.lock()
To unlock the pointer
game.pointer.unlock()
Here is the pointer style list:
game.pointer.setStyle(style); //Replace "style" with the pointer style you want
If you'd like to help you can take a peek at the code, enable/disable devtools in the game window like this:
game.cheats.devtools.enable(); //Enable devtools
game.cheats.devtools.disable(); //Disable devtools
And then press F12 to open devtools.
If you find bugs or have a suggestion you can send them in the issues tab.
FAQs
Nodegames is a node.js game engine inspired by Pygame
The npm package nodegamesjs receives a total of 3 weekly downloads. As such, nodegamesjs popularity was classified as not popular.
We found that nodegamesjs demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.