Skip to content

Commit

Permalink
new algo of position check
Browse files Browse the repository at this point in the history
  • Loading branch information
Kostia Kotliarov committed Aug 30, 2017
1 parent bfb5702 commit 2df0bd4
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 29 deletions.
36 changes: 36 additions & 0 deletions checkPosition.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

#include "pacman.h"

void checkPosition(t_pacman *pacman)
{
extern int map[H][W];

// check if ghost and pacman on same position on map
if ((pacman->red.ghostPos.x == pacman->pac.x && pacman->red.ghostPos.y == pacman->pac.y) ||
(pacman->blue.ghostPos.x == pacman->pac.x && pacman->blue.ghostPos.y == pacman->pac.y) ||
(pacman->yellow.ghostPos.x == pacman->pac.x && pacman->yellow.ghostPos.y == pacman->pac.y) ||
(pacman->pink.ghostPos.x == pacman->pac.x && pacman->pink.ghostPos.y == pacman->pac.y) )
{
pacman->pacmanLives--;
if (pacman->pacmanLives == 0)
{
putTextMessage(pacman, "You lose");
SDL_Delay(1500);
exit(0);
}

// put to map previous value
map[pacman->red.ghostPos.y][pacman->red.ghostPos.x] = pacman->red.ghostMapPreviousValue;
map[pacman->blue.ghostPos.y][pacman->blue.ghostPos.x] = pacman->blue.ghostMapPreviousValue;
map[pacman->pink.ghostPos.y][pacman->pink.ghostPos.x] = pacman->pink.ghostMapPreviousValue;
map[pacman->yellow.ghostPos.y][pacman->yellow.ghostPos.x] = pacman->yellow.ghostMapPreviousValue;

map[pacman->pac.y][pacman->pac.x] = 0;
setDefaultPosition(pacman);

putTextMessage(pacman, "Get Ready!");
SDL_Delay(1500);
sdlRenderClear(pacman);
}

}
19 changes: 13 additions & 6 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ void setDefaultPosition(t_pacman *pacman)
pacman->yellow.ghostPos = (t_pos){10, 10};
pacman->blue.ghostPos = (t_pos){1, 4};
pacman->pink.ghostPos = (t_pos){16, 4};

pacman->red.ghostMapPreviousValue = 0;
pacman->yellow.ghostMapPreviousValue = 0;
pacman->blue.ghostMapPreviousValue = (map[4][1] == 1) ? 1 : 0;
pacman->pink.ghostMapPreviousValue = (map[4][16] == 1) ? 1 : 0;

pacman->pacMove = (t_pos){-1, 0};
}

static void initDefault(t_pacman *pacman, int *stop)
Expand All @@ -42,16 +49,10 @@ static void initDefault(t_pacman *pacman, int *stop)
pacman->pause = 0;
pacman->score = 0;
pacman->eat = 0;
pacman->pacMove = (t_pos){0, 0};

setDefaultPosition(pacman);
pacman->pacmanLives = 3;

pacman->red.ghostMapPreviousValue = 0;
pacman->blue.ghostMapPreviousValue = 1;
pacman->pink.ghostMapPreviousValue = 1;
pacman->yellow.ghostMapPreviousValue = 0;

pacman->red.id = 5;
pacman->blue.id = 6;
pacman->pink.id = 7;
Expand Down Expand Up @@ -119,6 +120,7 @@ int main(void)
}
if (introOff == 0)
intro(pacman, &introOff);

if (pacman->pause == 0)
{
sdlRenderClear(pacman);
Expand All @@ -137,6 +139,11 @@ int main(void)

}
ghostOffset++;

checkPosition(pacman);



SDL_RenderPresent(pacman->sdl.renderer);
SDL_Delay(250);
}
Expand Down
1 change: 1 addition & 0 deletions pacman.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,5 @@ 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);
void checkPosition(t_pacman *pacman);
#endif
4 changes: 4 additions & 0 deletions pacman.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/* Begin PBXBuildFile section */
8A5D12C31F51F2FA001CEFD3 /* putText.c in Sources */ = {isa = PBXBuildFile; fileRef = 8A5D12C21F51F2FA001CEFD3 /* putText.c */; };
8A7BB07B1F56AF2400CC845A /* checkPosition.c in Sources */ = {isa = PBXBuildFile; fileRef = 8A7BB07A1F56AF2400CC845A /* checkPosition.c */; };
D26026211F51612C00D5000A /* main.c in Sources */ = {isa = PBXBuildFile; fileRef = D26026201F51612C00D5000A /* main.c */; };
D26026271F51615D00D5000A /* initSdl.c in Sources */ = {isa = PBXBuildFile; fileRef = D26026251F51615D00D5000A /* initSdl.c */; };
D26026291F51644E00D5000A /* drawMap.c in Sources */ = {isa = PBXBuildFile; fileRef = D26026281F51644E00D5000A /* drawMap.c */; };
Expand Down Expand Up @@ -35,6 +36,7 @@
8A5D12C01F51F0DC001CEFD3 /* SDL2_ttf.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = SDL2_ttf.framework; sourceTree = "<group>"; };
8A5D12C21F51F2FA001CEFD3 /* putText.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = putText.c; sourceTree = "<group>"; };
8A5D12C61F52BE17001CEFD3 /* SDL2_image.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = SDL2_image.framework; sourceTree = "<group>"; };
8A7BB07A1F56AF2400CC845A /* checkPosition.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = checkPosition.c; sourceTree = "<group>"; };
D26026161F5160DF00D5000A /* pacman */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = pacman; sourceTree = BUILT_PRODUCTS_DIR; };
D26026201F51612C00D5000A /* main.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = main.c; sourceTree = "<group>"; };
D26026231F51614E00D5000A /* SDL2.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = SDL2.framework; sourceTree = "<group>"; };
Expand Down Expand Up @@ -63,6 +65,7 @@
D260260D1F5160DF00D5000A = {
isa = PBXGroup;
children = (
8A7BB07A1F56AF2400CC845A /* checkPosition.c */,
D26026281F51644E00D5000A /* drawMap.c */,
D26026251F51615D00D5000A /* initSdl.c */,
D26517F11F542A0F00F302C2 /* intro.c */,
Expand Down Expand Up @@ -153,6 +156,7 @@
D26026211F51612C00D5000A /* main.c in Sources */,
D260262B1F51B0CB00D5000A /* putPacman.c in Sources */,
D26517F21F542A1000F302C2 /* intro.c in Sources */,
8A7BB07B1F56AF2400CC845A /* checkPosition.c in Sources */,
D26026271F51615D00D5000A /* initSdl.c in Sources */,
8A5D12C31F51F2FA001CEFD3 /* putText.c in Sources */,
D26026291F51644E00D5000A /* drawMap.c in Sources */,
Expand Down
Binary file not shown.
23 changes: 0 additions & 23 deletions putGhost.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,35 +75,12 @@ void putGhost(t_pacman *pacman, t_ghost *ghost)
}
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};
}


0 comments on commit 2df0bd4

Please sign in to comment.