Permalink
Browse files

Added ghosts with primitive chase AI

Added a set of four semi-transparent ghost snakes that chase Pacsnake's position with random mistakes.

? The ghost snakes themselves were pretty easy to put in - they're just another kind of snake that moves through the space. Thinking about how they would choose their movement involved me reading an in-depth article about Pacman ghost AI from here:

http://gameinternals.com/post/2072558330/understanding-pac-man-ghost-behavior

And eventually deciding (I think?) that it's overkill to try to replicate the actual AIs from Pacman. But that's not settled yet and I think I'll need to write some stuff about that.

? One issue that actually playing around in the level with the ghosts is that I'm much less clear on the dynamics between Pacsnake and the ghosts. Does Pacsnake eat them by eating a powerpill and changing the mode? Or do they dies on contact with Pacsnake's body? (and each other's?) in which case the 'power' of the power pellet is about being longer and thus potentially more dangerous to the ghosts (but then also to yourself).

? The fact the ghost snakes are semitransparent (and known as ghosts) makes it seem impossible that they could crash into the Pacsnake's body. But then on the other hand in Pacman he can EAT them and they can KILL him, so they clearly have SOME kind of tangibility and connection to the physical realm right? They can't even pass through walls. Are those things EVEN GHOSTS AT ALL? So there's some decision making involved in that. If everyone can collide with everyone's body I think it will lead to utter carnage? But that may be funny... it may be that pulling the gameplay _away_ from Pacman original is a good solution to the issues around Ghost AI? (Although I may be shooting myself in the foot massively? Although I guess ghosts would just respawn in their little closet so it would be fine? Actually it seems complicated.)
  • Loading branch information...
pippinbarr committed Nov 15, 2017
1 parent eea920f commit 74bb4b5917b5366e8ec91fadab8f7c507263b44e
Showing with 96 additions and 23 deletions.
  1. +1 −1 snakes/Missssssile.js
  2. +20 −18 snakes/Msss.js
  3. +1 −1 snakes/Papersss.js
  4. +72 −1 snakes/Snake.js
  5. +1 −1 snakes/SnakeBaseGame.js
  6. +1 −1 snakes/Sssensssible.js
@@ -40,7 +40,7 @@ BasicGame.Missssssile.prototype.tick = function () {
};
BasicGame.Missssssile.prototype.addMissile = function () {
var missile = new Snake(this,this.WALL_LEFT + 1 + Math.floor(Math.random() * (this.WALL_RIGHT - 1 - this.WALL_LEFT)),this.WALL_TOP + 1);
var missile = new Snake(this.game,this.WALL_LEFT + 1 + Math.floor(Math.random() * (this.WALL_RIGHT - 1 - this.WALL_LEFT)),this.WALL_TOP + 1);
missile.target = this.apples.getRandom();
while (!missile.target.alive) {
missile.target = this.apples.getRandom();
@@ -23,7 +23,7 @@ BasicGame.Msss.prototype.create = function () {
[0,0,1,2,2,2,2,1,2,2,2,1,1,2,2,2,1,2,2,2,2,1,0,0],
[0,0,1,1,2,1,1,1,1,1,0,1,1,0,1,1,1,1,1,2,1,1,0,0],
[1,1,1,1,2,2,2,1,0,0,0,0,0,0,0,0,1,2,2,2,1,1,1,1],
[0,0,0,0,0,1,2,1,0,1,1,0,0,1,1,0,1,2,1,0,0,0,0,0],
[0,0,0,0,0,1,2,1,0,1,0,1,1,0,1,0,1,2,1,0,0,0,0,0],
[1,1,1,1,1,1,2,0,0,1,0,0,0,0,1,0,0,2,1,1,1,1,1,1],
[0,0,0,0,0,1,2,1,0,1,1,1,1,1,1,0,1,2,1,0,0,0,0,0],
[1,1,1,1,0,1,2,1,0,0,0,0,0,0,0,0,1,2,1,0,1,1,1,1],
@@ -51,14 +51,23 @@ BasicGame.Msss.prototype.create = function () {
// this.snake.y = (this.WALL_BOTTOM)*GRID_SIZE;
this.ghost = null;
// this.createGhost();
this.createGhosts();
// Name the state for resetting purposes
this.stateName = "Msss";
};
BasicGame.Msss.prototype.createGhosts = function () {
this.ghosts = this.game.add.group();
for (var i = 0; i < 4; i++) {
var ghost = new Snake(this.game,this.WALL_LEFT + 9 + i,this.WALL_TOP + 12);
ghost.bodyPiecesToAdd = ghost.NEW_BODY_PIECES_PER_APPLE;
ghost.target = this.snake.head;
ghost.alpha = 0.5;
this.ghosts.add(ghost);
}
};
BasicGame.Msss.prototype.createApples = function () {
this.apples = this.game.add.group();
this.miniApples = this.game.add.group();
@@ -85,15 +94,14 @@ BasicGame.Msss.prototype.tick = function () {
BasicGame.SnakeBaseGame.prototype.tick.call(this);
// Wrap the snake around the edges, as per Pacman Physical Reality
if (this.snake.head.x >= this.game.width) {
this.snake.head.x = 0;
}
else if (this.snake.head.x < 0) {
this.snake.head.x = this.game.width - GRID_SIZE;
}
this.snake.wrap();
// this.ghost.chase();
// this.ghost.move();
this.ghosts.forEach(function (ghost) {
ghost.chaseMaze(this.map);
ghost.grow();
ghost.move();
ghost.wrap();
},this);
};
BasicGame.Msss.prototype.checkAppleCollision = function () {
@@ -112,12 +120,6 @@ BasicGame.Msss.prototype.checkAppleCollision = function () {
};
BasicGame.Msss.prototype.createGhost = function () {
this.ghost = new Snake(this,0,this.WALL_TOP + 10);
this.immigrant.target = this.snake.head;
this.game.add.sprite(this.ghost);
};
BasicGame.Msss.prototype.gameOver = function () {
};
@@ -88,7 +88,7 @@ BasicGame.Papersss.prototype.checkAppleCollision = function () {
};
BasicGame.Papersss.prototype.createImmigrant = function () {
this.immigrant = new Snake(this,0,this.WALL_TOP + 10);
this.immigrant = new Snake(this.game,0,this.WALL_TOP + 10);
this.immigrantApple = this.game.add.sprite((this.WALL_RIGHT+1)*GRID_SIZE,(this.WALL_TOP + 10)*GRID_SIZE,'apple');
this.immigrant.target = this.immigrantApple;
this.game.add.sprite(this.immigrant);
@@ -2,6 +2,7 @@
Snake = function (game,x,y) {
Phaser.Group.call(this, game);
this.game = game;
this.SNAKE_START_LENGTH = 4;
this.NEW_BODY_PIECES_PER_APPLE = 3;
this.SNAKE_FLICKER_SPEED = 0.2;
@@ -117,7 +118,7 @@ Snake.prototype.checkBodyCollision = function () {
return false;
};
Snake.prototype.chase = function () {
Snake.prototype.chaseLinear = function () {
// If we have reached the target, we stop
if (this.target.position.equals(this.head.position)) {
this.stop();
@@ -166,6 +167,76 @@ Snake.prototype.chase = function () {
}
};
Snake.prototype.chaseMaze = function (map) {
// If we have reached the target, we stop
if (this.target.position.equals(this.head.position)) {
this.stop();
return;
}
var mapX = this.head.x / GRID_SIZE;
var mapY = this.head.y / GRID_SIZE;
var openMoves = [];
if (mapX - 1 >= 0) {
if (map[mapY][mapX - 1] != 1 && this.next.x <= 0) {
openMoves.push(new Phaser.Point(-GRID_SIZE,0));
}
}
if (mapX + 1 >= 0) {
if (map[mapY][mapX + 1] != 1 && this.next.x >= 0) {
openMoves.push(new Phaser.Point(GRID_SIZE,0));
}
}
if (mapY - 1 >= 0) {
if (map[mapY - 1][mapX] != 1 && this.next.y <= 0) {
openMoves.push(new Phaser.Point(0,-GRID_SIZE));
}
}
if (mapY + 1 >= 0) {
if (map[mapY + 1][mapX] != 1 && this.next.y >= 0) {
openMoves.push(new Phaser.Point(0,GRID_SIZE));
}
}
var next = null;
openMoves.forEach(function (m) {
if (this.head.x < this.target.x && m.x > 0) {
next = m;
}
else if (this.head.x > this.target.x && m.x < 0) {
next = m;
}
else if (this.head.y < this.target.y && m.y > 0) {
next = m;
}
else if (this.head.y > this.target.y && m.y < 0) {
next = m;
}
},this);
if (openMoves.length == 0) {
// console.log("OH NO! HOW THE HELL HAS THIS HAPPENED???");
// console.log(this.next);
// console.log(this.head.x,this.head.y);
}
else if (next != null && Math.random() < 0.75) {
this.next = next;
}
else {
this.next = openMoves[Math.floor(Math.random() * openMoves.length)];
}
};
Snake.prototype.wrap = function () {
if (this.head.x >= this.game.width) {
this.head.x = 0;
}
else if (this.head.x < 0) {
this.head.x = this.game.width - GRID_SIZE;
}
};
Snake.prototype.die = function () {
this.hitSFX.play();
this.dead = true;
@@ -129,7 +129,7 @@ BasicGame.SnakeBaseGame.prototype = {
},
createSnake: function () {
this.snake = new Snake(this,this.SNAKE_START_X,this.SNAKE_START_Y);
this.snake = new Snake(this.game,this.SNAKE_START_X,this.SNAKE_START_Y);
},
createApple: function () {
@@ -84,7 +84,7 @@ BasicGame.Sssensssible.prototype.createSnake = function() {
this.SNAKE_TWO_START_X = this.SNAKE_START_X;
this.SNAKE_TWO_START_Y = this.WALL_BOTTOM - this.SNAKE_START_Y + this.WALL_TOP + 1;
this.snakeTwo = new Snake(this,this.SNAKE_TWO_START_X,this.SNAKE_TWO_START_Y);
this.snakeTwo = new Snake(this.game,this.SNAKE_TWO_START_X,this.SNAKE_TWO_START_Y);
};

0 comments on commit 74bb4b5

Please sign in to comment.