Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 53 additions & 51 deletions Project_Snake_Game/snake-game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,57 +124,59 @@ void Input() {
}
// logic how snake should grow in size and when game stops
void Logic() {

int prevx = tx[0];
int prevy = ty[0];
int prev2x, prev2y;
tx[0] = x;
ty[0] = y;
for (int i = 1; i < nt; i++) {
prev2x = tx[i];
prev2y = ty[i];
tx[i] = prevx;
ty[i] = prevy;
prevx = prev2x;
prevy = prev2y;
}



switch (dir)
{
case UP:
y--;
break;
case DOWN:
y++;
break;
case LEFT:
x--;
break;
case RIGHT:
x++;
break;

default:
break;
}
if (x >= width) x = 0; else if (x < 0) x = width - 1;
if (y >= height) y = 0; else if (y < 0) y = height - 1;

for (int i = 0; i < nt; i++) {
if (tx[i] == x && ty[i] == y) {
lostgame();
}

}

if (x == fx && y == fy) {
score += 1;
nt++;
fx = rand() % width;
fy = rand() % height;
}
if (nt > MAX_SIZE) { // Ensure nt does not exceed the array bounds
lostgame();
return;
}

int prevx = tx[0];
int prevy = ty[0];
int prev2x, prev2y;
tx[0] = x;
ty[0] = y;
for (int i = 1; i < nt; i++) {
prev2x = tx[i];
prev2y = ty[i];
tx[i] = prevx;
ty[i] = prevy;
prevx = prev2x;
prevy = prev2y;
}

switch (dir)
{
case UP:
y--;
break;
case DOWN:
y++;
break;
case LEFT:
x--;
break;
case RIGHT:
x++;
break;

default:
break;
}
if (x >= width) x = 0; else if (x < 0) x = width - 1;
if (y >= height) y = 0; else if (y < 0) y = height - 1;

for (int i = 0; i < nt; i++) {
if (tx[i] == x && ty[i] == y) {
lostgame();
}

}

if (x == fx && y == fy) {
score += 1;
nt++;
fx = rand() % width;
fy = rand() % height;
}
}
//condition when a player wins
void win() {
Expand Down