/*
//pacman game
#include <stdio.h>
#include <stdbool.h>
#include <conio.h> // for getch() function (Windows only)
const int BOARD_SIZE = 10;
char board[10][10] = {
{'#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
{'#', '.', '.', '.', '.', '.', '.', '.', '.', '#'},
{'#', '.', '#', '#', '#', '#', '#', '#', '.', '#'},
{'#', '.', '#', '.', '.', '.', '.', '#', '.', '#'},
{'#', '.', '#', '.', '#', '#', '.', '#', '.', '#'},
{'#', '.', '#', '.', '#', '#', '.', '#', '.', '#'},
{'#', '.', '.', '.', '.', '.', '.', '.', '.', '#'},
{'#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
};
// Function to draw the game board
void drawBoard(int pacmanX, int pacmanY, int ghostX[], int ghostY[], int numGhosts) {
system("cls"); // Clear the console (Windows only)
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if (i == pacmanY && j == pacmanX) {
printf("C "); // Draw Pacman
} else {
bool ghostPresent = false;
for (int k = 0; k < numGhosts; k++) {
if (i == ghostY[k] && j == ghostX[k]) {
printf("G "); // Draw Ghost
ghostPresent = true;
break;
}
}
if (!ghostPresent) {
printf("%c ", board[i][j]); // Draw walls and dots
}
}
}
printf("\n");
}
}
bool isWall(int x, int y) {
return board[y][x] == '#';
}
int main() {
int pacmanX = BOARD_SIZE / 2;
int pacmanY = BOARD_SIZE / 2;
const int numGhosts = 3;
int ghostX[numGhosts];
int ghostY[numGhosts];
bool gameOver = false;
// Initialize the positions of each ghost
ghostX[0] = 4;
ghostY[0] = 2;
ghostX[1] = 2;
ghostY[1] = 5;
ghostX[2] = 7;
ghostY[2] = 3;
ghostX[3] = 5;
ghostY[3] = 6;
ghostX[4] = 8;
ghostY[4] = 4;
while (!gameOver) {
drawBoard(pacmanX, pacmanY, ghostX, ghostY, numGhosts);
char input = getch(); // Get user input (Windows only)
// Store the previous positions
int prevPacmanX = pacmanX;
int prevPacmanY = pacmanY;
int prevGhostX[numGhosts];
int prevGhostY[numGhosts];
for (int i = 0; i < numGhosts; i++) {
prevGhostX[i] = ghostX[i];
prevGhostY[i] = ghostY[i];
}
// Move Pacman based on user input
switch (input) {
case 'w':
pacmanY--;
break;
case 'a':
pacmanX--;
break;
case 's':
pacmanY++;
break;
case 'd':
pacmanX++;
break;
case 'q':
gameOver = true;
break;
}
// Check if Pacman hits a wall
if (isWall(pacmanX, pacmanY)) {
pacmanX = prevPacmanX;
pacmanY = prevPacmanY;
}
// Move each Ghost randomly
for (int i = 0; i < numGhosts; i++) {
int randomMove = rand() % 4;
switch (randomMove) {
case 0:
ghostY[i]--;
break;
case 1:
ghostX[i]--;
break;
case 2:
ghostY[i]++;
break;
case 3:
ghostX[i]++;
break;
}
// Check if each Ghost hits a wall
if (isWall(ghostX[i], ghostY[i])) {
ghostX[i] = prevGhostX[i];
ghostY[i] = prevGhostY[i];
}
}
// Check if Pacman and Ghosts collide
for (int i = 0; i < numGhosts; i++) {
if (pacmanX == ghostX[i] && pacmanY == ghostY[i]) {
gameOver = true;
printf("Game Over!\n");
break;
}
}
}
return 0;
}
*/
/*
//Keep away from the zombie
#include <stdio.h>
#include <stdbool.h>
#include <conio.h> // for getch() function (Windows only)
const int BOARD_SIZE = 10;
char board[1000][1000] = {
{'#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
{'#', '.', '.', '.', '.', '.', '.', '.', '.', '#'},
{'#', '.', '#', '#', '#', '#', '#', '#', '.', '#'},
{'#', '.', '#', '.', '.', '.', '.', '#', '.', '#'},
{'#', '.', '#', '.', '#', '#', '.', '#', '.', '#'},
{'#', '.', '#', '.', '#', '#', '.', '#', '.', '#'},
{'#', '.', '.', '.', '.', '.', '.', '.', '.', '#'},
{'#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
};
// Function to draw the game board
void drawBoard(int playerX, int playerY, int zombieX, int zombieY, int bulletX, int bulletY) {
system("cls"); // Clear the console (Windows only)
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if (i == playerY && j == playerX) {
printf("P "); // Draw player
} else if (i == zombieY && j == zombieX) {
printf("Z "); // Draw zombie
} else if (i == bulletY && j == bulletX) {
printf("* "); // Draw bullet
} else {
printf("%c ", board[i][j]); // Draw walls and empty spaces
}
}
printf("\n");
}
}
bool isWall(int x, int y) {
if (x < 0 || x >= BOARD_SIZE || y < 0 || y >= BOARD_SIZE)
return true; // Out of board bounds
return board[y][x] == '#';
}
int main() {
int playerX = BOARD_SIZE / 2;
int playerY = BOARD_SIZE / 2;
int zombieX = 1;
int zombieY = 1;
int bulletX = -1; // Initialize bullet position to be outside the board
int bulletY = -1;
bool gameOver = false;
while (!gameOver) {
drawBoard(playerX, playerY, zombieX, zombieY, bulletX, bulletY);
char input = getch(); // Get user input (Windows only)
// Store the previous player position
int prevPlayerX = playerX;
int prevPlayerY = playerY;
// Move player based on user input
switch (input) {
case 'w':
playerY--;
break;
case 'a':
playerX--;
break;
case 's':
playerY++;
break;
case 'd':
playerX++;
break;
case ' ':
// Shoot bullet
if (bulletX == -1 && bulletY == -1) {
bulletX = playerX;
bulletY = playerY;
}
break;
case 'q':
gameOver = true;
break;
}
// Check if player hits a wall
if (isWall(playerX, playerY)) {
playerX = prevPlayerX;
playerY = prevPlayerY;
}
// Move zombie towards the player
int dx = 0;
int dy = 0;
if (zombieX < playerX) {
dx = 1;
} else if (zombieX > playerX) {
dx = -1;
}
if (zombieY < playerY) {
dy = 1;
} else if (zombieY > playerY) {
dy = -1;
}
// Check if the zombie can move in the intended direction
if (!isWall(zombieX + dx, zombieY)) {
zombieX += dx;
}
if (!isWall(zombieX, zombieY + dy)) {
zombieY += dy;
}
// Move bullet
if (bulletX != -1 && bulletY != -1) {
bulletY--;
if (isWall(bulletX, bulletY)) {
bulletX = -1;
bulletY = -1;
}
}
// Check if player and zombie collide
if (playerX == zombieX && playerY == zombieY) {
gameOver = true;
printf("Game Over!\n");
}
}
return 0;
}
*/
/*
//platformer game
#include <stdio.h>
#include <stdlib.h>
#include <conio.h> // for getch() function (Windows only)
#include <windows.h> // for Sleep function (Windows only)
const int SCREEN_WIDTH = 40;
const int SCREEN_HEIGHT = 20;
const char PLAYER_CHAR = '@';
const char ENEMY_CHAR = '*';
const char PLATFORM_CHAR = '#';
char screen[20][40];
int playerX, playerY;
int isJumping;
int jumpPower;
int jumpTime;
typedef struct {
int x;
int y;
int isAlive;
} Enemy;
Enemy enemies[5]; // Array to store enemy positions
int numEnemies = 5;
void initializeScreen() {
for (int i = 0; i < SCREEN_HEIGHT; i++) {
for (int j = 0; j < SCREEN_WIDTH; j++) {
if (i == SCREEN_HEIGHT - 1)
screen[i][j] = PLATFORM_CHAR; // Create a platform at the bottom
else
screen[i][j] = ' '; // Empty space
}
}
}
void drawScreen() {
system("cls"); // Clear the console (Windows only)
for (int i = 0; i < SCREEN_HEIGHT; i++) {
for (int j = 0; j < SCREEN_WIDTH; j++) {
printf("%c", screen[i][j]);
}
printf("\n");
}
}
void updatePlayerPosition(char input) {
switch (input) {
case 'a':
if (playerX > 0)
playerX--;
break;
case 'd':
if (playerX < SCREEN_WIDTH - 1)
playerX++;
break;
case 'w':
if (!isJumping && playerY == SCREEN_HEIGHT - 2) { // Only allow jumping from the ground
isJumping = 1;
jumpPower = 5; // Adjust the jump power as desired
jumpTime = 0;
}
break;
}
}
void updateJump() {
if (isJumping) {
if (jumpTime < jumpPower) {
playerY--;
jumpTime++;
} else {
isJumping = 0;
jumpTime = 0;
}
} else {
if (playerY < SCREEN_HEIGHT - 2) {
playerY++;
}
}
}
void updateEnemies() {
for (int i = 0; i < numEnemies; i++) {
// Update enemy positions
enemies[i].x++;
// Check if the enemy reaches the end of the screen, reset its position
if (enemies[i].x >= SCREEN_WIDTH)
enemies[i].x = 0;
// Check if the enemy collides with the player
if (enemies[i].x == playerX && enemies[i].y == playerY) {
// Handle collision, such as game over or player damage
// Here, we check if the player is jumping on the enemy
if (isJumping && jumpTime < jumpPower) {
enemies[i].isAlive = 0; // Enemy is defeated
playerY--; // Bounce the player up
} else {
// Handle collision, such as game over or player damage
printf("Game Over\n");
Sleep(2000); // Delay for 2 seconds
exit(0); // Exit the game
}
}
}
}
int main() {
initializeScreen();
playerX = SCREEN_WIDTH / 2;
playerY = SCREEN_HEIGHT - 2;
isJumping = 0;
jumpPower = 0;
jumpTime = 0;
for (int i = 0; i < numEnemies; i++) {
enemies[i].x = rand() % SCREEN_WIDTH;
enemies[i].y = SCREEN_HEIGHT - 3; // Set y position above the ground
enemies[i].isAlive = 1; // Set enemy as alive
}
while (1) {
drawScreen();
char input;
// Check if a key is pressed
if (_kbhit()) {
input = _getch(); // Get user input (Windows only)
updatePlayerPosition(input);
} else {
input = ' '; // No key pressed, set input to a default value
}
// Update the jump
updateJump();
// Update the enemies
updateEnemies();
// Update the screen with the player's position
for (int i = 0; i < SCREEN_HEIGHT; i++) {
for (int j = 0; j < SCREEN_WIDTH; j++) {
if (i == playerY && j == playerX)
printf("%c", PLAYER_CHAR);
else {
int isEnemy = 0;
for (int k = 0; k < numEnemies; k++) {
if (enemies[k].x == j && enemies[k].y == i && enemies[k].isAlive) {
printf("%c", ENEMY_CHAR);
isEnemy = 1;
break;
}
}
if (!isEnemy && i == SCREEN_HEIGHT - 1)
printf("%c", PLATFORM_CHAR);
else
printf(" ");
}
}
printf("\n");
}
Sleep(100); // Delay for 100 milliseconds
}
return 0;
}
*/