Skip to content

Commit

Permalink
ghost function fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Kostia Kotliarov committed Aug 30, 2017
1 parent 552bdcc commit bfb5702
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 128 deletions.
8 changes: 4 additions & 4 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,14 @@ int main(void)
drawMap(pacman);
if (ghostOffset % 2)
{
putGhostRed(pacman, &pacman->red);
putGhostRed(pacman, &pacman->yellow);
putGhost(pacman, &pacman->red);
putGhost(pacman, &pacman->yellow);
// ghostOffset = 0;
// }
// else
// {
putGhostRed(pacman, &pacman->blue);
putGhostRed(pacman, &pacman->pink);
putGhost(pacman, &pacman->blue);
putGhost(pacman, &pacman->pink);

}
ghostOffset++;
Expand Down
5 changes: 1 addition & 4 deletions pacman.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,7 @@ void drawMap(t_pacman *pacman);
void putPacman(t_pacman *pacman);
void putScore(t_pacman *pacman);
void setLivesLevel(t_pacman *pacman);
void putGhostRed(t_pacman *pacman, t_ghost *red);
void putGhostBlue(t_pacman *pacman);
void putGhostYellow(t_pacman *pacman);
void putGhostPink(t_pacman *pacman);
void putGhost(t_pacman *pacman, t_ghost *ghost);
void putTextMessage(t_pacman *pacman, char *str);
void intro(t_pacman *pacman, int *introOff);
void setDefaultPosition(t_pacman *pacman);
Expand Down
8 changes: 4 additions & 4 deletions pacman.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
D26517E41F5404CB00F302C2 /* SDL2_image.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8A5D12C61F52BE17001CEFD3 /* SDL2_image.framework */; };
D26517E51F5404CB00F302C2 /* SDL2_ttf.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8A5D12C01F51F0DC001CEFD3 /* SDL2_ttf.framework */; };
D26517E61F5404CB00F302C2 /* SDL2.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D26026231F51614E00D5000A /* SDL2.framework */; };
D26517EA1F540F3900F302C2 /* putRedGhost.c in Sources */ = {isa = PBXBuildFile; fileRef = D26517E91F540F3900F302C2 /* putRedGhost.c */; };
D26517EA1F540F3900F302C2 /* putGhost.c in Sources */ = {isa = PBXBuildFile; fileRef = D26517E91F540F3900F302C2 /* putGhost.c */; };
D26517F21F542A1000F302C2 /* intro.c in Sources */ = {isa = PBXBuildFile; fileRef = D26517F11F542A0F00F302C2 /* intro.c */; };
/* End PBXBuildFile section */

Expand All @@ -42,7 +42,7 @@
D26026261F51615D00D5000A /* pacman.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = pacman.h; sourceTree = "<group>"; };
D26026281F51644E00D5000A /* drawMap.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = drawMap.c; sourceTree = "<group>"; };
D260262A1F51B0CB00D5000A /* putPacman.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = putPacman.c; sourceTree = "<group>"; };
D26517E91F540F3900F302C2 /* putRedGhost.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = putRedGhost.c; sourceTree = "<group>"; };
D26517E91F540F3900F302C2 /* putGhost.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = putGhost.c; sourceTree = "<group>"; };
D26517F11F542A0F00F302C2 /* intro.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = intro.c; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand All @@ -68,7 +68,7 @@
D26517F11F542A0F00F302C2 /* intro.c */,
D26026201F51612C00D5000A /* main.c */,
D260262A1F51B0CB00D5000A /* putPacman.c */,
D26517E91F540F3900F302C2 /* putRedGhost.c */,
D26517E91F540F3900F302C2 /* putGhost.c */,
8A5D12C21F51F2FA001CEFD3 /* putText.c */,
D26026261F51615D00D5000A /* pacman.h */,
D26026221F51614E00D5000A /* Frameworks */,
Expand Down Expand Up @@ -156,7 +156,7 @@
D26026271F51615D00D5000A /* initSdl.c in Sources */,
8A5D12C31F51F2FA001CEFD3 /* putText.c in Sources */,
D26026291F51644E00D5000A /* drawMap.c in Sources */,
D26517EA1F540F3900F302C2 /* putRedGhost.c in Sources */,
D26517EA1F540F3900F302C2 /* putGhost.c in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Binary file not shown.
109 changes: 109 additions & 0 deletions putGhost.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#include "pacman.h"

void putGhost(t_pacman *pacman, t_ghost *ghost)
{

extern int map[H][W];
int lenCurrent;
int lenClosest = INT_MAX;
int lenFarest = 0;
int mapValue;

ghost->ghostMove = (t_pos){0, 0};
map[ghost->ghostPos.y][ghost->ghostPos.x] = ghost->ghostMapPreviousValue;

//check move down
mapValue = map[ghost->ghostPos.y + 1][ghost->ghostPos.x];
if (mapValue == 0 || mapValue == 1 || mapValue == 3 || mapValue == 4)
{
if (pacman->eat == 0 && (lenCurrent = sqrt(pow(ghost->ghostPos.x - pacman->pac.x,2) + pow(ghost->ghostPos.y + 1 - pacman->pac.y,2))) < lenClosest)
{
lenClosest = lenCurrent;
ghost->ghostMove = (t_pos) {0, 1};
ghost->ghostMapPreviousValue = map[ghost->ghostPos.y + 1][ghost->ghostPos.x + 0];
}
else if (pacman->eat != 0 && (lenCurrent = sqrt(pow(ghost->ghostPos.x - pacman->pac.x,2) + pow(ghost->ghostPos.y + 1 - pacman->pac.y,2))) > lenFarest)
{
lenFarest = lenCurrent;
ghost->ghostMove = (t_pos) {0, 1};
ghost->ghostMapPreviousValue = map[ghost->ghostPos.y + 1][ghost->ghostPos.x + 0];
}
}
//check move up
mapValue = map[ghost->ghostPos.y - 1][ghost->ghostPos.x];
if (mapValue == 0 || mapValue == 1 || mapValue == 3 || mapValue == 4)
{
if (pacman->eat == 0 && (lenCurrent = sqrt(pow(ghost->ghostPos.x - pacman->pac.x,2) + pow(ghost->ghostPos.y - 1 - pacman->pac.y,2))) < lenClosest)
{
lenClosest = lenCurrent;
ghost->ghostMove = (t_pos) {0, -1};
ghost->ghostMapPreviousValue = map[ghost->ghostPos.y - 1][ghost->ghostPos.x + 0];
}
else if (pacman->eat != 0 && (lenCurrent = sqrt(pow(ghost->ghostPos.x - pacman->pac.x,2) + pow(ghost->ghostPos.y - 1 - pacman->pac.y,2))) > lenFarest)
{
lenFarest = lenCurrent;
ghost->ghostMove = (t_pos) {0, -1};
ghost->ghostMapPreviousValue = map[ghost->ghostPos.y - 1][ghost->ghostPos.x + 0];
}
}
//check move left
mapValue = map[ghost->ghostPos.y][ghost->ghostPos.x + 1];
if (mapValue == 0 || mapValue == 1 || mapValue == 3 || mapValue == 4)
{
if (pacman->eat == 0 && (lenCurrent = sqrt(pow(ghost->ghostPos.x + 1 - pacman->pac.x,2) + pow(ghost->ghostPos.y - pacman->pac.y,2))) < lenClosest)
{
lenClosest = lenCurrent;
ghost->ghostMove = (t_pos) {1, 0};
ghost->ghostMapPreviousValue = map[ghost->ghostPos.y + 0][ghost->ghostPos.x + 1];
}
else if (pacman->eat != 0 && (lenCurrent = sqrt(pow(ghost->ghostPos.x + 1 - pacman->pac.x,2) + pow(ghost->ghostPos.y - pacman->pac.y,2))) > lenFarest)
{
lenFarest = lenCurrent;
ghost->ghostMove = (t_pos) {1, 0};
ghost->ghostMapPreviousValue = map[ghost->ghostPos.y + 0][ghost->ghostPos.x + 1];
}
}
//check move right
mapValue = map[ghost->ghostPos.y][ghost->ghostPos.x - 1];
if (mapValue == 0 || mapValue == 1 || mapValue == 3 || mapValue == 4)
{
if (pacman->eat == 0 && (lenCurrent = sqrt(pow(ghost->ghostPos.x - 1 - pacman->pac.x,2) + pow(ghost->ghostPos.y - pacman->pac.y,2))) < lenClosest)
{
lenClosest = lenCurrent;
ghost->ghostMove = (t_pos) {-1, 0};
ghost->ghostMapPreviousValue = map[ghost->ghostPos.y + 0][ghost->ghostPos.x - 1];
}
else if (pacman->eat != 0 && (lenCurrent = sqrt(pow(ghost->ghostPos.x - 1 - pacman->pac.x,2) + pow(ghost->ghostPos.y - pacman->pac.y,2))) > lenFarest)
{
lenFarest = lenCurrent;
ghost->ghostMove = (t_pos) {-1, 0};
ghost->ghostMapPreviousValue = map[ghost->ghostPos.y + 0][ghost->ghostPos.x - 1];
}
}

if (ghost->ghostMapPreviousValue == 3)
{
//if U eated by ghost
pacman->pacmanLives--;
if (pacman->pacmanLives == 0)
{
putTextMessage(pacman, "You lose");
SDL_Delay(1500);
exit(0);
}
map[pacman->pac.y][pacman->pac.x] = 0;
setDefaultPosition(pacman);
pacman->pacMove = (t_pos){-1, 0};
putTextMessage(pacman, "GET READY");
sdlRenderClear(pacman);
SDL_Delay(1500);
ghost->ghostMapPreviousValue = 0;
return ;
}
ghost->ghostPos.x += ghost->ghostMove.x;
ghost->ghostPos.y += ghost->ghostMove.y;
map[ghost->ghostPos.y][ghost->ghostPos.x] = ghost->id;
ghost->ghostRect = (SDL_Rect){ghost->ghostPos.x * 30, ghost->ghostPos.y * 30, 30, 30};
}


116 changes: 0 additions & 116 deletions putRedGhost.c

This file was deleted.

0 comments on commit bfb5702

Please sign in to comment.