Skip to content

Instantly share code, notes, and snippets.

@jdrew1303
Created August 15, 2018 23:43
Show Gist options
  • Save jdrew1303/f59ba647c519f3ba44d37f887ee8c16d to your computer and use it in GitHub Desktop.
Save jdrew1303/f59ba647c519f3ba44d37f887ee8c16d to your computer and use it in GitHub Desktop.
javascript nes emulator

This will be used to train an ai to play nes games (first things first: get it to run).

require('text-encoding');
const fs = require("fs");
const {NES} = require("jsnes");
const sinon = require("sinon");
const SCREEN_WIDTH = 256;
const SCREEN_HEIGHT = 240;
var Canvas = require('canvas')
, Image = Canvas.Image
, canvas = new Canvas(200, 200)
, ctx = canvas.getContext('2d');
canvas.width = SCREEN_WIDTH;
canvas.height = SCREEN_HEIGHT;
// create imageData object
var idata = ctx.createImageData(SCREEN_WIDTH, SCREEN_HEIGHT);
var onFrame = sinon.spy();
var nes = new NES({
onFrame: function(frameBuffer){
// set our buffer as source
idata.data.set(new Uint8ClampedArray(frameBuffer));
// update canvas with new data
ctx.putImageData(idata, 0, 0);
console.log(canvas.toDataURL());
},
onStatusUpdate: console.log
});
fs.readFile(require.resolve( "jsnes/roms/croom/croom.nes" ), {encoding: 'binary'}, function(err, data) {
if (err) return done(err);
nes.loadROM(data);
setInterval(() => {
nes.frame();
console.log(nes.getFPS());
}, 1000 / 60);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment