Space-centipede-master/.vscode/c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "windows-msvc-x64"
}
],
"version": 4
}
Space-centipede-master/bullet.c
#include
#include
#include
#include
#include "bullet.h"
#include "console.h"
#include "globals.h"
#include "player.h"
bool check_hit(int row, int col);
static Bullet *b;
Bullet *init_bullet(char *head, int row, int col, int direction)
{
Bullet *bullet = (Bullet *)malloc(sizeof(Bullet));
//initialize the bullet
bullet->head[0] = head;
bullet->col = col;
bullet->row = row;
bullet->hits = false;
bullet->speed = BULLET_SPEED;
bullet->direction = direction;
return bullet;
}
void update_bullet(Bullet * bullet)
{
b = bullet;
}
Bullet *get_bullet()
{
return b;
}
void bulletRun(void *bullet_t)
{
Bullet *bullet = (Bullet *)bullet_t;
while (!killThreads && !bullet->hits)
{
if (bullet->row > TOP_WALL && bullet->row < BOTTOM)
{
pthread_mutex_lock(&lock_console);
consoleClearImage(bullet->row, bullet->col, BULLET_SIZE, BULLET_SIZE);
bullet->row += bullet->direction;
consoleDrawImage(bullet->row, bullet->col, bullet->head, BULLET_SIZE);
pthread_mutex_unlock(&lock_console);
if(bullet->direction == DOWN)
{
if( bullet->row == player->row && bullet->col >= player->col
&& bullet->col < player->col + PLAYER_WIDHT)
{
bullet->hits = true;
pthread_mutex_lock(&lock_player);
player->dies = true;
pthread_mutex_unlock(&lock_player);
}
}
else if (bullet->direction == UP && !bullet->hits)
{
update_bullet(bullet);
}
}
else
{
pthread_mutex_lock(&lock_console);
consoleClearImage(bullet->row, bullet->col, BULLET_SIZE, BULLET_SIZE);
pthread_mutex_unlock(&lock_console);
bullet->hits = true;
}
sleepTicks(bullet->speed);
}
pthread_exit(NULL);
}
Space-centipede-master/bullet.h
#ifndef MY_BULLET_H
#define MY_BULLET_H
#include
#define BULLET_SPEED 4
#define BULLET_UP_LIMIT 2
#define BULLET_SIZE 1
typedef struct BULLET Bullet;
struct BULLET
{
char *head[BULLET_SIZE];
int col;
int row;
int direction;
pthread_t thread;
int speed;
int hits;
};
void bulletRun();
//creates a bullet give the starting position and the direction to go
Bullet *init_bullet(char *head, int row, int col, int direction);
Bullet *get_bullet();
#endif
Space-centipede-master/comp3430_A2.c
#include
#include "game.h"
#include "console.h"
int main(int argc, char**argv) {
gameRun();
printf("done!\n");
return 0;
}
Space-centipede-master/console.c
#include "console.h"
#include
#include
#include /*for nano sleep */
#include
static int CON_WIDTH, CON_HEIGHT;
static int consoleLock = false;
static int MAX_STR_LEN = 256; /* for strlen checking */
/* Local functions */
static bool checkConsoleSize(int reqHeight, int reqWidth)
{
if ( (reqWidth > COLS) || (reqHeight > LINES) )
{
fprintf(stderr, "\n\n\rSorry, your window is only %ix%i. \n\r%ix%i is required. Sorry.\n\r", COLS, LINES, reqWidth, reqHeight);
return (false);
}
return(true);
}
bool consoleInit(int height, int width, char *image[]) /* assumes image height/width is same as height param */
{
bool status;
initscr();
crmode();
noecho();
clear();
CON_HEIGHT = height; CON_WIDTH = width;
status = checkConsoleSize(CON_HEIGHT, CON_WIDTH);
if (status)
{
consoleDrawImage(0, 0, image, CON_HEIGHT);
consoleRefresh();
}
return(status);
}
void consoleDrawImage(int row, int col, char *image[], int height)
{
int i, length;
int newLeft, newRight, newOffset, newLength;
if (consoleLock) return;
newLeft = col < 0 ? 0 : col;
newOffset = col < 0 ? -col : 0;
for (i = 0; i < height; i++)
{
if (row+i < 0 || row+i >= CON_HEIGHT)
continue;
length = strnlen(image[i], MAX_STR_LEN);
newRight = col+length >= CON_WIDTH ? CON_WIDTH-1 : col+length;
newLength = newRight - newLeft + 1;
if (newOffset >= length || newLength <= 0)
continue;
if (mvaddnstr(row+i, newLeft, image[i]+newOffset, newLength) == ERR)
fprintf(stderr, "ERROR drawing to screen"); /* smarter handling is needed */
}
}
void consoleClearImage(int row, int col, int height, int width)
{
int i, j;
if (consoleLock) return;
if (col+width > CON_WIDTH)
width = CON_WIDTH-col;
if (col < 0)
{
width += col; /* -= -col */
col = 0;
}
if (width < 1 || col >= CON_WIDTH) /* nothing to clear */
return;
for (i = 0; i < height; i++)
{
if (row+i < 0 || row+i >= CON_HEIGHT)
continue;
move(row+i, col);
for (j = 0; j < width; j++)
addch(' ');
}
}
void consoleRefresh(void)
{
if (!consoleLock)
{
move(LINES-1, COLS-1);
refresh();
}
}
void consoleFinish(void)
{
endwin();
}
void putBanner(const char *str)
{
if (consoleLock) return;
int len;
len = strnlen(str,MAX_STR_LEN);
move (CON_HEIGHT/2, (CON_WIDTH-len)/2);
addnstr(str, len);
consoleRefresh();
}
void putString(char *str, int row, int col, int maxlen)
{
if (consoleLock) return;
move(row, col);
addnstr(str, maxlen);
}
/* setup to work in USECS, reduces risk of overflow */
/* 10000 usec = 10 ms, or 100fps */
#define TIMESLICE_USEC 10000
#define TIME_USECS_SIZE 1000000
#define USEC_TO_NSEC 1000
struct timespec getTimeout(int ticks)
{
struct timespec rqtp;
/* work in usecs at first */
rqtp.tv_nsec = TIMESLICE_USEC * ticks;
/* handle usec overflow */
rqtp.tv_sec = rqtp.tv_nsec / TIME_USECS_SIZE;
rqtp.tv_nsec %= TIME_USECS_SIZE;
rqtp.tv_nsec *= USEC_TO_NSEC; /*convert to nsecs */
return rqtp;
}
void sleepTicks(int ticks)
{
if (ticks <= 0)
return;
struct timespec rqtp = getTimeout(ticks);
nanosleep(&rqtp, NULL);
}
#define FINAL_PAUSE 2
void finalKeypress()
{
flushinp();
sleepTicks(FINAL_PAUSE);
move(LINES-1, COLS-1);
getch(); /* wait for user to press a character, blocking. */
}
void disableConsole(int disabled)
{
consoleLock = disabled;
}
Space-centipede-master/console.h
#ifndef CONSOLE_H
#define CONSOLE_H
#include
/**************** DRAWING **************************/
/* directions in terms of deltas in x / y dimension */
#define LEFT -1
#define RIGHT 1
#define UP -1
#define DOWN 1
#define SCR_LEFT 0
#define SCR_TOP 0
/* Initialize curses, draw initial gamescreen. Refreshes console to terminal.
Also stores the requested dimensions of the consoe and tests the terminal for the
given dimensions.*/
extern bool consoleInit(int reqHeight, int reqWidth, char *image[]);
/* Draws 2d `image' of `height' rows, at curses coordinates `(row, col)'.
Note: parts of the `image' falling on negative rows are not drawn; each
row drawn is clipped on the left and right side of the game console (note
that `col' may be negative, indicating `image' starts to the left of the
screen and will thus only be partially drawn. Useful for objects that are
half off the screen */
extern void consoleDrawImage(int row, int col, char *image[], int height);
/* Clears a 2d `width'x`height' rectangle with spaces. Upper left hand
corner is curses coordinate `(row,col)'. */
extern void consoleClearImage(int row, int col, int width, int height);
/* Moves cursor to bottom right corner and refreshes. If this is not done,
the curses internal buffer (that you have been drawing to) is not dumped
to screen. */
extern void consoleRefresh(void);
/* turns off all updates. Can be used to prevent the screen refresh from working, e.g., at game end while threads are all catching up.*/
extern void disableConsole(int disabled);
/* Terminates curses cleanly. */
extern void consoleFinish(void);
/* Puts the given banner in the center of the screen */
void putBanner(const char *);
/* Draws the given string at the given location */
void putString(char *, int row, int col, int maxlen);
/* Sleeps the given number of 20ms ticks */
void sleepTicks(int ticks);
/* clears the input buffer and then waits for one more key */
void finalKeypress();
/* gets a timespec that represents the time of one tick */
struct timespec getTimeout(int ticks);
#endif /* CONSOLE_H */
Space-centipede-master/enemy.c
#include
#include
#include
#include
#include
#include
#include "enemy.h"
#include "console.h"
#include "bullet.h"
#include "globals.h"
char* ENEMY_HEAD[ENEMY_ANIM_TILES][ENEMY_HEIGHT] =
{
{"@@", "><"},
{"QQ", "=="},
};
char* ENEMY_BODY[ENEMY_ANIM_TILES][ENEMY_HEIGHT] =
{
{"Oo", "/\\"},
{"oO", "()"},
};
void enemy_shoot();
void move_body(Body_part *body);
void move_head(Enemy *enemy);
Enemy *init_head(int row, int col, int direction);
void break_apart(List *body_list, int direction, int row, int col, int at);
//static Enemy *enemy;
void enemyRun(void *enemy_t)
{
Enemy *enemy = (Enemy *)enemy_t;
char** head;
char** tail;
int i = 0;
int shoot = 0;
int prevcol = enemy->row;
int prevrow = enemy->col;
Body_part *body;
int new_enemy = 0;
srand(time(NULL));
//printf("f2 %d %d\n", enemy->col, enemy->row);
i = 0;
while (!killThreads && !enemy->dead)
{
new_enemy++;
freeze(); //if frozen wait
shoot = rand() % RAND;
//shoot at some random interval(when shoot is btw Rand and shoot_now)
if(shoot < SHOOT_NOW || (shoot > SHOOT_PLAYER &&
enemy->col == player->col))
{
enemy_shoot(enemy->row + BULLET_OFFSET, enemy->col);
}
//head = enemy->HEAD[i % ENEMY_ANIM_TILES];
head = ENEMY_HEAD[i % ENEMY_ANIM_TILES];
pthread_mutex_lock(&lock_console);
consoleClearImage(prevrow, prevcol, strlen(head[0]), ENEMY_HEIGHT);
consoleDrawImage(enemy->row, enemy->col, head, ENEMY_HEIGHT);
pthread_mutex_unlock(&lock_console);
//tail = enemy->TAIL[i % ENEMY_ANIM_TILES];
body = (Body_part*)list_firstItem(enemy->BODY);
tail = ENEMY_BODY[i % ENEMY_ANIM_TILES];
//bool broken = false;
int count = 0;
if(new_enemy > GAME_COLS && shoot < SHOOT_NOW)
{
pthread_cond_signal(&cond_enemy);
new_enemy =...