Skip to content

Commit

Permalink
more improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
valentin-stamate committed Aug 6, 2019
1 parent 518276c commit ddcafe5
Show file tree
Hide file tree
Showing 9 changed files with 293 additions and 67 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
# <p align="center">PacMan</p>

<p align="center">
<img width="400" src="https://i.postimg.cc/4NVj019y/Capture.png">
<img width="400" src="https://i.postimg.cc/bvYx7yWB/Capture.png">
</p>

### About

The game was made using [Processing](https://processing.org).

The rules are pretty straight forward : eat the food , don't be the food.
I succeed adding the A* algorithm, I was using Gandalf to help me.

### What's New?
* Tricky, the one who is always drunk
* Pinky, it's not the smart type
* A nice looking path
* Ghost interaction with Pacman
* Weak Mode for ghosts
* Auto return to home when a ghost is eaten by Pacman
* Few bugs fixed

### Credits
* [CodeBullet](https://www.youtube.com/channel/UC0e3QhIYukixgh5VVpKHH9Q)
* [GeeksForGeeks](https://www.geeksforgeeks.org)
* [Wikipedia](https://en.wikipedia.org/wiki/Pac-Man)
26 changes: 22 additions & 4 deletions Sketch/Destroyer.pde
Original file line number Diff line number Diff line change
@@ -1,18 +1,36 @@
class Destroyer extends Ghost {
Destroyer(int x, int y){
super(x, y);
super.currentCell = array.get(y / sc).get(x / sc);
}

public void search() {
// best search
if(super.x % sc == 0 && super.y % sc == 0){
super.i = (int)super.y / sc;
super.j = (int)super.x / sc;
AStar(array.get(super.i).get(super.j), array.get(pacman.j).get(pacman.i));
super.searchingList = path;
}

super.update();
super.currentCell = array.get(super.i).get(super.j);
// when is weak and can be af
if( (super.isWeak || super.isRecovering ) && super.isAffectedBy ){
// pincky style
super.searchingList.remove( super.currentCell );

AStar(super.currentCell, super.cellToFollow);
super.searchingList = path;

if(super.searchingList.size() <= 2 ){
if(super.isWeak)
super.cellToFollow = super.getRandomCell();
else if(super.isRecovering)
super.cellToFollow = super.getRandomCellFromHome();
}

} else {
AStar( super.currentCell, pacman.currentCell );
super.searchingList = path;
}
}
super.update();
}
}
109 changes: 107 additions & 2 deletions Sketch/Ghost.pde
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,36 @@ import java.util.ArrayList;
import java.util.List;

class Ghost {
private float x, y;
public float x, y;
private List<Cell> searchingList;
private int dirX = 1, dirY = 0;
private float speed = 2.5;
private int i, j;
private int r, g, b;
private Cell cellToFollow;
private List<Cell> homeCells;
private Cell currentCell;

public boolean isWeak = false;
public boolean isRecovering = false;

public boolean isAffectedBy = false;

// the weak time is controled by pacman.isInvincible
private int recoveringCountDowm = 720;// 12 sec

Ghost(int x, int y){
this.x = x;
this.y = y;
this.searchingList = new ArrayList<Cell>();
this.cellToFollow = this.getRandomCell();
this.homeCells = new ArrayList<Cell>();

for(int i = 13; i <= 15; i++){
for(int j = 11; j <= 16; j ++){
this.homeCells.add( array.get(i).get(j) );
}
}
}

private void update(){
Expand Down Expand Up @@ -49,6 +68,9 @@ class Ghost {
this.dirY = 0;
}

if(a[this.i + this.dirY][this.j + this.dirX] == '1'){
this.dirX = 0;this.dirY = 0;
}
try{
Cell c = array.get(this.i + this.dirY).get(this.j + this.dirX);
} catch(Exception e){
Expand All @@ -57,6 +79,26 @@ class Ghost {

}

// for a bug, if not i think there is a 50% chance
if(this.x % 2.5 == 0 && this.y % 2.5 == 0){
if( (this.isWeak || this.isRecovering) && this.isAffectedBy){// TODO
this.speed = 1.25;
} else{
this.speed = 2.5;
}
}

if(this.isRecovering){
if(this.recoveringCountDowm == 0){
println("stops recovering");
this.recoveringCountDowm = 720;
this.isRecovering = false;
this.isAffectedBy = false;
} else {
this.recoveringCountDowm --;
}
}

this.x += this.dirX * this.speed;
this.y += this.dirY * this.speed;

Expand All @@ -71,13 +113,76 @@ class Ghost {
private void show(){
noStroke();
fill(this.r, this.g, this.b);
if(this.isWeak)
fill(27, 112, 247);
if(this.isRecovering)
fill(27, 112, 247, 100);
circle(this.x, this.y, sc + 4);
}

public void makeWeak(){
if(!this.isWeak && this.isAffectedBy){
println("ghost weak");
this.cellToFollow = this.getRandomCell();
this.isWeak = true;
}
}

public void makeNormal(){
if(this.isWeak){
println("ghost normal");
this.isWeak = false;
this.isAffectedBy = false;
}
}

public void retreat(){
if(!this.isRecovering && this.isAffectedBy){
println("ghost retreat");
this.isRecovering = true;
this.isWeak = false;
this.cellToFollow = this.getRandomCellFromHome();
}
}

public void drawPath(){
List<PVector> vectorPath = new ArrayList<PVector>();
for(Cell c : this.searchingList){
c.show(200, 50, 30);
vectorPath.add( new PVector(c.j * sc, c.i * sc) );
}
strokeWeight(3);
stroke(this.r, this.g, this.b);

if(this.isWeak)
stroke(27, 112, 247);
if(this.isRecovering)
stroke(27, 112, 247, 100);

for(int i = 0; i < vectorPath.size() - 1; i ++){
PVector current = vectorPath.get(i);
PVector next = vectorPath.get(i + 1);
if( i == vectorPath.size() - 2 ){
next = new PVector(this.x, this.y);
}
line(current.x , current.y , next.x , next.y );
}
}

private Cell getRandomCell(){

int i = 0, j = 0;
while(a[i][j] == '1'){
i = (int)random(0, 31);
j = (int)random(0, 28);
}
Cell c = array.get(i).get(j);

return c;
}

private Cell getRandomCellFromHome(){
int n = (int)random(0, this.homeCells.size());
return this.homeCells.get(n);
}

}
6 changes: 3 additions & 3 deletions Sketch/Matrix.pde
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ char a[][] = { //<>//
{'1', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '1', '1', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '1'},
{'1', '2', '1', '1', '1', '1', '2', '1', '1', '1', '1', '1', '2', '1', '1', '2', '1', '1', '1', '1', '1', '2', '1', '1', '1', '1', '2', '1'},
{'1', '2', '1', '1', '1', '1', '2', '1', '1', '1', '1', '1', '2', '1', '1', '2', '1', '1', '1', '1', '1', '2', '1', '1', '1', '1', '2', '1'},
{'1', '2', '1', '1', '1', '1', '2', '1', '1', '1', '1', '1', '2', '1', '1', '2', '1', '1', '1', '1', '1', '2', '1', '1', '1', '1', '2', '1'},
{'1', '3', '1', '1', '1', '1', '2', '1', '1', '1', '1', '1', '2', '1', '1', '2', '1', '1', '1', '1', '1', '2', '1', '1', '1', '1', '3', '1'},
{'1', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '1'},
{'1', '2', '1', '1', '1', '1', '2', '1', '1', '2', '1', '1', '1', '1', '1', '1', '1', '1', '2', '1', '1', '2', '1', '1', '1', '1', '2', '1'},
{'1', '2', '1', '1', '1', '1', '2', '1', '1', '2', '1', '1', '1', '1', '1', '1', '1', '1', '2', '1', '1', '2', '1', '1', '1', '1', '2', '1'},
Expand All @@ -23,7 +23,7 @@ char a[][] = { //<>//
{'1', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '1', '1', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '1'},
{'1', '2', '1', '1', '1', '1', '2', '1', '1', '1', '1', '1', '2', '1', '1', '2', '1', '1', '1', '1', '1', '2', '1', '1', '1', '1', '2', '1'},
{'1', '2', '1', '1', '1', '1', '2', '1', '1', '1', '1', '1', '2', '1', '1', '2', '1', '1', '1', '1', '1', '2', '1', '1', '1', '1', '2', '1'},
{'1', '2', '2', '2', '1', '1', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '1', '1', '2', '2', '2', '1'},
{'1', '3', '2', '2', '1', '1', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', '1', '1', '2', '2', '3', '1'},
{'1', '1', '1', '2', '1', '1', '2', '1', '1', '2', '1', '1', '1', '1', '1', '1', '1', '1', '2', '1', '1', '2', '1', '1', '2', '1', '1', '1'},
{'1', '1', '1', '2', '1', '1', '2', '1', '1', '2', '1', '1', '1', '1', '1', '1', '1', '1', '2', '1', '1', '2', '1', '1', '2', '1', '1', '1'},
{'1', '2', '2', '2', '2', '2', '2', '1', '1', '2', '2', '2', '2', '1', '1', '2', '2', '2', '2', '1', '1', '2', '2', '2', '2', '2', '2', '1'},
Expand All @@ -44,5 +44,5 @@ char a[][] = { //<>//
//'0' means free,
//'1' means wall,
//'2' means food
//
//'3' means bonus
//
86 changes: 60 additions & 26 deletions Sketch/PacMan.pde
Original file line number Diff line number Diff line change
@@ -1,60 +1,82 @@
class PacManPlayer{
class PacMan{
public float x = 12 * sc, y = 23 * sc;
private int dirLineX = 12, dirLineY = 23;
private int dirX = -1, dirY = 0;
private int newDirX, newDirY;
private float speed = 2.5; // math stuff ;) , 2.5 * 8 = sc
private int oldX = 12, oldY = 23;
private float speed = 2.5; // math stuff , 2.5 * 8 = sc

public int i, j;
public Cell oldPosition = array.get(23).get(12);
public boolean isInvincible = false;
private int countDown = 600;
public Cell currentCell = array.get(23).get(12);

public void update(){
if( a[ this.oldY ][ this.oldX ] != '-' )
a[ this.oldY ][ this.oldX ] = 'X';

if(frameCount % 100 == 0){
// update the old position every 1 second for Tricky
this.oldPosition = array.get(j).get(i);
// update the old position every 1.40 second for Tricky
this.oldPosition = this.currentCell;
}

if( this.x % 20 == 0 && this.y % 20 == 0 ){

this.i = (int)this.x / sc;
this.j = (int)this.y / sc;
this.j = (int)this.x / sc;
this.i = (int)this.y / sc;

// for food
if(a[this.i][this.j] != '-')
a[this.i][this.j] = '0';

if( a[ this.j ][ this.i ] == '-' && this.i == 0){
if( a[ this.i ][ this.j ] == '-' && this.j == 0){
this.x = 27 * sc;
this.i = 27;
} else if( a[ this.j ][ this.i ] == '-' && this.i == 27){
this.j = 27;
} else if( a[ this.i ][ this.j ] == '-' && this.j == 27){
this.x = 0;
this.i = 0;
this.j = 0;
}

int newPositionX = this.i + this.newDirX;
int newPositionY = this.j + this.newDirY;
int newPositionX = this.j + this.newDirX;
int newPositionY = this.i + this.newDirY;

int nextPositionX = this.i + this.dirX;
int nextPositionY = this.j + this.dirY;
int nextPositionX = this.j + this.dirX;
int nextPositionY = this.i + this.dirY;

if( a[ nextPositionY ][ nextPositionX ] == '1'){
this.dirX = 0;
this.dirY = 0;
}

if( a[ newPositionY ][ newPositionX ] != '1' ){
this.dirX = this.newDirX;
this.dirY = this.newDirY;
// for a little bug when teleport
try{
if( a[ newPositionY ][ newPositionX ] != '1' ){
this.dirX = this.newDirX;
this.dirY = this.newDirY;

this.j = newPositionX;
this.i = newPositionY;
}
}
catch(Exception e){}

this.currentCell = array.get(this.i).get(this.j);

if( this.checkBonus() ){
println("pacman isInvincible");
this.isInvincible = true;
this.countDown = 600;
}
if( a[ this.j ][ this.i ] != '-')
a[ this.j ][ this.i ] = 'X';
if( a[ this.oldY ][ this.oldX ] != '-' )
a[ this.oldY ][ this.oldX ] = '0';
this.oldX = this.i;
this.oldY = this.j;

}

if(this.isInvincible){
if(this.countDown != 0 ){
this.countDown --;
} else {
println("pacman not invincible");
this.isInvincible = false;
}
}

this.x += this.dirX * this.speed;
this.y += this.dirY * this.speed;

Expand All @@ -72,4 +94,16 @@ class PacManPlayer{
this.newDirY = dirY;
}

private boolean checkBonus(){
if(a[this.i][this.j] == '3'){
// i don't know what i am doing
destroyer.isAffectedBy = true;
tricky.isAffectedBy = true;
pinky.isAffectedBy = true;

return true;
}
return false;
}

}
Loading

0 comments on commit ddcafe5

Please sign in to comment.