Skip to content

Commit

Permalink
added live level
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDigitalBug committed Aug 28, 2017
1 parent b69e846 commit 46e172f
Show file tree
Hide file tree
Showing 12 changed files with 265 additions and 272 deletions.
3 changes: 2 additions & 1 deletion drawMap.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ void drawMap(t_pacman *pacman)
}
rect.y += 30;
}
putText(pacman);
putScore(pacman);
setLivesLevel(pacman);
if (winFlag == 0)
putTextMessage(pacman, "YOU WIN!");
}
Expand Down
6 changes: 0 additions & 6 deletions intro.c

This file was deleted.

16 changes: 13 additions & 3 deletions main.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
int map[22][19]={
int map[22][19] = {
{2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
{2,1,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,2},
{2,4,2,2,1,2,2,2,1,2,1,2,2,2,1,2,2,4,2},
Expand Down Expand Up @@ -37,6 +37,7 @@ static void initDefault(t_pacman *pacman, int *stop)
pacman->ghostYellow = (t_pos){10, 10};
pacman->ghostBlue = (t_pos){1, 4};
pacman->ghostPink = (t_pos){16, 4};
pacman->pacmanLives = 3;

pacman->pacImage = IMG_Load("image/pacman.png");
pacman->pacImageUp = IMG_Load("image/pacmanUp.png");
Expand Down Expand Up @@ -70,12 +71,14 @@ int main(void)
pacman = (t_pacman *)malloc(sizeof(t_pacman));

sdlInit(pacman);

initDefault(pacman, &stop);

putPacman(pacman);
drawMap(pacman);

while (!stop)
{

while (SDL_PollEvent(&(pacman->sdl.e)))
{
if (pacman->sdl.e.type == SDL_QUIT ||
Expand Down Expand Up @@ -104,6 +107,7 @@ int main(void)
pacman->pacMove = (t_pos){-1, 0};
}
}

if (pacman->pause == 0)
{
sdlRenderClear(pacman);
Expand All @@ -121,7 +125,13 @@ int main(void)
}
if (pacman->eat != 0)
pacman->eat--;

if (pacman->pacmanLives == 0)
{
putTextMessage(pacman, "You lose");
SDL_RenderPresent(pacman->sdl.renderer);
SDL_Delay(1500);
exit(0);
}

SDL_RenderPresent(pacman->sdl.renderer);
SDL_Delay(250);
Expand Down
8 changes: 6 additions & 2 deletions pacman.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#define snake_h

# include <stdlib.h>
# include <limits.h>
# include <stdio.h>
# include <unistd.h>
# include <SDL2/SDL.h>
Expand Down Expand Up @@ -42,8 +43,9 @@ typedef struct s_pacman
SDL_Surface *pacImageLeft;
SDL_Surface *pacImageRight;
SDL_Texture *pacTexture;
SDL_Rect pacRect;
SDL_Rect pacRect;

int pacmanLives;


t_pos ghostRed;
Expand Down Expand Up @@ -96,12 +98,14 @@ void drawMap(t_pacman *pacman);
void putPacman(t_pacman *pacman);
void drawCircle(t_pacman *pacman, t_pos center, int radius);

void putText(t_pacman *pacman);
void putScore(t_pacman *pacman);
void setLivesLevel(t_pacman *pacman);
void putGhostRed(t_pacman *pacman);
void putGhostBlue(t_pacman *pacman);
void putGhostYellow(t_pacman *pacman);
void putGhostPink(t_pacman *pacman);
void putTextMessage(t_pacman *pacman, char *str);
void intro(t_pacman *pacman);

//int map[H][W];

Expand Down
4 changes: 0 additions & 4 deletions pacman.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
D26517EA1F540F3900F302C2 /* putRedGhost.c in Sources */ = {isa = PBXBuildFile; fileRef = D26517E91F540F3900F302C2 /* putRedGhost.c */; };
D26517EC1F540F6100F302C2 /* putYellowGhost.c in Sources */ = {isa = PBXBuildFile; fileRef = D26517EB1F540F6100F302C2 /* putYellowGhost.c */; };
D26517EE1F54109900F302C2 /* putBlueGhost.c in Sources */ = {isa = PBXBuildFile; fileRef = D26517ED1F54109900F302C2 /* putBlueGhost.c */; };
D26517F01F54124500F302C2 /* intro.c in Sources */ = {isa = PBXBuildFile; fileRef = D26517EF1F54124500F302C2 /* intro.c */; };
/* End PBXBuildFile section */

/* Begin PBXCopyFilesBuildPhase section */
Expand Down Expand Up @@ -51,7 +50,6 @@
D26517E91F540F3900F302C2 /* putRedGhost.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = putRedGhost.c; sourceTree = "<group>"; };
D26517EB1F540F6100F302C2 /* putYellowGhost.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = putYellowGhost.c; sourceTree = "<group>"; };
D26517ED1F54109900F302C2 /* putBlueGhost.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = putBlueGhost.c; sourceTree = "<group>"; };
D26517EF1F54124500F302C2 /* intro.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = intro.c; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand All @@ -71,7 +69,6 @@
D260260D1F5160DF00D5000A = {
isa = PBXGroup;
children = (
D26517EF1F54124500F302C2 /* intro.c */,
D26517ED1F54109900F302C2 /* putBlueGhost.c */,
D26517E91F540F3900F302C2 /* putRedGhost.c */,
D26517EB1F540F6100F302C2 /* putYellowGhost.c */,
Expand Down Expand Up @@ -166,7 +163,6 @@
8A5D12C51F52A5A2001CEFD3 /* putPinkGhost.c in Sources */,
D260262B1F51B0CB00D5000A /* putPacman.c in Sources */,
D26517EE1F54109900F302C2 /* putBlueGhost.c in Sources */,
D26517F01F54124500F302C2 /* intro.c in Sources */,
D26026271F51615D00D5000A /* initSdl.c in Sources */,
D260262D1F51BAF100D5000A /* drawCircle.c in Sources */,
8A5D12C31F51F2FA001CEFD3 /* putText.c in Sources */,
Expand Down
Binary file not shown.
117 changes: 55 additions & 62 deletions putBlueGhost.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,104 +6,97 @@ void putGhostBlue(t_pacman *pacman)
extern int map[H][W];
static int tmpPos = 0;
int tmp;
int len = 5000;
int len2 = 0;
int lenClosest = INT_MAX;
int lenClosestFarest = 0;

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


map[pacman->ghostBlue.y + 0][pacman->ghostBlue.x + 0] = tmpPos;

if (map[pacman->ghostBlue.y + 1][pacman->ghostBlue.x + 0] == 0 ||
map[pacman->ghostBlue.y + 1][pacman->ghostBlue.x + 0] == 1 ||
map[pacman->ghostBlue.y + 1][pacman->ghostBlue.x + 0] == 3 ||
map[pacman->ghostBlue.y + 1][pacman->ghostBlue.x + 0] == 4)
if (map[pacman->ghostBlue.y + 1][pacman->ghostBlue.x + 0] != 2 &&
map[pacman->ghostBlue.y + 1][pacman->ghostBlue.x + 0] != 5 &&
map[pacman->ghostBlue.y + 1][pacman->ghostBlue.x + 0] != 7 &&
map[pacman->ghostBlue.y + 1][pacman->ghostBlue.x + 0] != 8)
{
if (pacman->eat == 0 && (tmp = sqrt(pow(pacman->ghostBlue.x - pacman->pac.x,2) + pow(pacman->ghostBlue.y + 1 - pacman->pac.y,2))) < len)
if (pacman->eat == 0 && (tmp = sqrt(pow(pacman->ghostBlue.x - pacman->pac.x,2) + pow(pacman->ghostBlue.y + 1 - pacman->pac.y,2))) < lenClosest)
{
lenClosest = tmp;
pacman->ghostBlueMove = (t_pos) {0, 1};
tmpPos = map[pacman->ghostBlue.y + 1][pacman->ghostBlue.x + 0];
}
else if (pacman->eat != 0 && (tmp = sqrt(pow(pacman->ghostBlue.x - pacman->pac.x,2) + pow(pacman->ghostBlue.y + 1 - pacman->pac.y,2))) > lenClosestFarest)
{
len = tmp;
lenClosestFarest = tmp;
pacman->ghostBlueMove = (t_pos) {0, 1};
tmpPos = map[pacman->ghostBlue.y + 1][pacman->ghostBlue.x + 0];
}
else
if (pacman->eat != 0 && (tmp = sqrt(pow(pacman->ghostBlue.x - pacman->pac.x,2) + pow(pacman->ghostBlue.y + 1 - pacman->pac.y,2))) > len2)
{
len2 = tmp;
pacman->ghostBlueMove = (t_pos) {0, 1};
tmpPos = map[pacman->ghostBlue.y + 1][pacman->ghostBlue.x + 0];
}
}

if (map[pacman->ghostBlue.y - 1][pacman->ghostBlue.x + 0] == 0 ||
map[pacman->ghostBlue.y - 1][pacman->ghostBlue.x + 0] == 1 ||
map[pacman->ghostBlue.y - 1][pacman->ghostBlue.x + 0] == 3 ||
map[pacman->ghostBlue.y - 1][pacman->ghostBlue.x + 0] == 4)
if (map[pacman->ghostBlue.y - 1][pacman->ghostBlue.x + 0] != 2 &&
map[pacman->ghostBlue.y - 1][pacman->ghostBlue.x + 0] != 5 &&
map[pacman->ghostBlue.y - 1][pacman->ghostBlue.x + 0] != 7 &&
map[pacman->ghostBlue.y - 1][pacman->ghostBlue.x + 0] != 8)
{
if (pacman->eat == 0 && (tmp = sqrt(pow(pacman->ghostBlue.x - pacman->pac.x,2) + pow(pacman->ghostBlue.y - 1 - pacman->pac.y,2))) < len)
if (pacman->eat == 0 && (tmp = sqrt(pow(pacman->ghostBlue.x - pacman->pac.x,2) + pow(pacman->ghostBlue.y - 1 - pacman->pac.y,2))) < lenClosest)
{
lenClosest = tmp;
pacman->ghostBlueMove = (t_pos) {0, -1};
tmpPos = map[pacman->ghostBlue.y - 1][pacman->ghostBlue.x + 0];
}
else if (pacman->eat != 0 && (tmp = sqrt(pow(pacman->ghostBlue.x - pacman->pac.x,2) + pow(pacman->ghostBlue.y - 1 - pacman->pac.y,2))) > lenClosestFarest)
{
len = tmp;
lenClosestFarest = tmp;
pacman->ghostBlueMove = (t_pos) {0, -1};
tmpPos = map[pacman->ghostBlue.y - 1][pacman->ghostBlue.x + 0];
}
else
if (pacman->eat != 0 && (tmp = sqrt(pow(pacman->ghostBlue.x - pacman->pac.x,2) + pow(pacman->ghostBlue.y - 1 - pacman->pac.y,2))) > len2)
{
len2 = tmp;
pacman->ghostBlueMove = (t_pos) {0, -1};
tmpPos = map[pacman->ghostBlue.y - 1][pacman->ghostBlue.x + 0];
}
}

if (map[pacman->ghostBlue.y + 0][pacman->ghostBlue.x + 1] == 0 ||
map[pacman->ghostBlue.y + 0][pacman->ghostBlue.x + 1] == 1 ||
map[pacman->ghostBlue.y + 0][pacman->ghostBlue.x + 1] == 3 ||
map[pacman->ghostBlue.y + 0][pacman->ghostBlue.x + 1] == 4)
if (map[pacman->ghostBlue.y + 0][pacman->ghostBlue.x + 1] != 2 &&
map[pacman->ghostBlue.y + 0][pacman->ghostBlue.x + 1] != 5 &&
map[pacman->ghostBlue.y + 0][pacman->ghostBlue.x + 1] != 7 &&
map[pacman->ghostBlue.y + 0][pacman->ghostBlue.x + 1] != 8)
{
if (pacman->eat == 0 && (tmp = sqrt(pow(pacman->ghostBlue.x + 1 - pacman->pac.x,2) + pow(pacman->ghostBlue.y - pacman->pac.y,2))) < len)
if (pacman->eat == 0 && (tmp = sqrt(pow(pacman->ghostBlue.x + 1 - pacman->pac.x,2) + pow(pacman->ghostBlue.y - pacman->pac.y,2))) < lenClosest)
{
len = tmp;
lenClosest = tmp;
pacman->ghostBlueMove = (t_pos) {1, 0};
tmpPos = map[pacman->ghostBlue.y + 0][pacman->ghostBlue.x + 1];
}
else if (pacman->eat != 0 && (tmp = sqrt(pow(pacman->ghostBlue.x + 1 - pacman->pac.x,2) + pow(pacman->ghostBlue.y - pacman->pac.y,2))) > lenClosestFarest)
{
lenClosestFarest = tmp;
pacman->ghostBlueMove = (t_pos) {1, 0};
tmpPos = map[pacman->ghostBlue.y + 0][pacman->ghostBlue.x + 1];
}
else
if (pacman->eat != 0 && (tmp = sqrt(pow(pacman->ghostBlue.x + 1 - pacman->pac.x,2) + pow(pacman->ghostBlue.y - pacman->pac.y,2))) > len2)
{
len2 = tmp;
pacman->ghostBlueMove = (t_pos) {1, 0};
tmpPos = map[pacman->ghostBlue.y + 0][pacman->ghostBlue.x + 1];
}
}

if (map[pacman->ghostBlue.y + 0][pacman->ghostBlue.x - 1] == 0 ||
map[pacman->ghostBlue.y + 0][pacman->ghostBlue.x - 1] == 1 ||
map[pacman->ghostBlue.y + 0][pacman->ghostBlue.x - 1] == 3 ||
map[pacman->ghostBlue.y + 0][pacman->ghostBlue.x - 1] == 4)
if (map[pacman->ghostBlue.y + 0][pacman->ghostBlue.x - 1] != 2 &&
map[pacman->ghostBlue.y + 0][pacman->ghostBlue.x - 1] != 5 &&
map[pacman->ghostBlue.y + 0][pacman->ghostBlue.x - 1] != 7 &&
map[pacman->ghostBlue.y + 0][pacman->ghostBlue.x - 1] != 8)
{
if (pacman->eat == 0 && (tmp = sqrt(pow(pacman->ghostBlue.x - 1 - pacman->pac.x,2) + pow(pacman->ghostBlue.y - pacman->pac.y,2))) < len)
if (pacman->eat == 0 && (tmp = sqrt(pow(pacman->ghostBlue.x - 1 - pacman->pac.x,2) + pow(pacman->ghostBlue.y - pacman->pac.y,2))) < lenClosest)
{
lenClosest = tmp;
pacman->ghostBlueMove = (t_pos) {-1, 0};
tmpPos = map[pacman->ghostBlue.y + 0][pacman->ghostBlue.x - 1];
}
else if (pacman->eat != 0 && (tmp = sqrt(pow(pacman->ghostBlue.x - 1 - pacman->pac.x,2) + pow(pacman->ghostBlue.y - pacman->pac.y,2))) > lenClosestFarest)
{
len = tmp;
lenClosestFarest = tmp;
pacman->ghostBlueMove = (t_pos) {-1, 0};
tmpPos = map[pacman->ghostBlue.y + 0][pacman->ghostBlue.x - 1];
}
else
if (pacman->eat != 0 && (tmp = sqrt(pow(pacman->ghostBlue.x - 1 - pacman->pac.x,2) + pow(pacman->ghostBlue.y - pacman->pac.y,2))) > len2)
{
len2 = tmp;
pacman->ghostBlueMove = (t_pos) {-1, 0};
tmpPos = map[pacman->ghostBlue.y + 0][pacman->ghostBlue.x - 1];
}
}
if (tmpPos == 3)
{
putTextMessage(pacman, "You lose");
SDL_RenderPresent(pacman->sdl.renderer);
SDL_Delay(1500);
exit(0);
pacman->pacmanLives--;
map[pacman->pac.y][pacman->pac.x] = 0;
pacman->pac = (t_pos){17, 20};
pacman->pacMove = (t_pos){0, 0};
tmpPos = 0;
}

pacman->ghostBlue.x += pacman->ghostBlueMove.x;
pacman->ghostBlue.y += pacman->ghostBlueMove.y;

map[pacman->ghostBlue.y][pacman->ghostBlue.x] = 6;
pacman->ghostBlueRect = (SDL_Rect){pacman->ghostBlue.x * 30, pacman->ghostBlue.y * 30, 30, 30};
}
Expand Down
8 changes: 4 additions & 4 deletions putPacman.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,25 @@ void putPacman(t_pacman *pacman)
pacman->eat = 28;
// printf("score %d\n\n", pacman->score);
}
else if (map[pacman->pac.y][pacman->pac.x] == 5)
else if (map[pacman->pac.y][pacman->pac.x] == 5 && pacman->eat != 0)
{
pacman->ghostRed = (t_pos){9, 10};
pacman->ghostRedMove = (t_pos){0, 0};
pacman->score += 200;
}
else if (map[pacman->pac.y][pacman->pac.x] == 6)
else if (map[pacman->pac.y][pacman->pac.x] == 6 && pacman->eat != 0)
{
pacman->ghostBlue = (t_pos){1, 4};
pacman->ghostBlueMove = (t_pos){0, 0};
pacman->score += 200;
}
else if (map[pacman->pac.y][pacman->pac.x] == 7)
else if (map[pacman->pac.y][pacman->pac.x] == 7 && pacman->eat != 0)
{
pacman->ghostPink = (t_pos){16, 4};
pacman->ghostPinkMove = (t_pos){0, 0};
pacman->score += 200;
}
else if (map[pacman->pac.y][pacman->pac.x] == 8)
else if (map[pacman->pac.y][pacman->pac.x] == 8 && pacman->eat != 0)
{
pacman->ghostYellow = (t_pos){10, 10};
pacman->ghostYellowMove = (t_pos){0, 0};
Expand Down
Loading

0 comments on commit 46e172f

Please sign in to comment.