This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
306 additions
and
26 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,14 @@ | ||
CC = gcc | ||
CFLAGS = -Wall -I../include | ||
LDFLAGS = -lSDL -lSDL_image -lSDL_mixer -lSDL_ttf | ||
OBJS = main.o menu.o music.o text.o stars.o settings.o start.o | ||
OBJS = main.o menu.o music.o text.o stars.o settings.o start.o enemy.o | ||
|
||
game: $(OBJS) | ||
$(CC) $(OBJS) $(LDFLAGS) -o game | ||
|
||
%.o: ../src/%.c | ||
$(CC) $(CFLAGS) -c $< | ||
|
||
.PHONY: clean | ||
clean: | ||
rm -f $(OBJS) game | ||
rm -f $(OBJS) game |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#ifndef CONSTANTS_H | ||
#define CONSTANTS_H | ||
|
||
//*screen size | ||
#define SCREEN_W 1280 | ||
#define SCREEN_H 720 | ||
|
||
//*stars stuff | ||
#define STARS_COUNT 100 // number of stars //? need locking | ||
#define STARS_LAYERS 4 // number of stars variations | ||
#define DELTA_TIME 16 // 1000ms / 60fps = 16.6666 | ||
|
||
//*main menu buttons stuff | ||
#define BUTTON_SPACING 14 // spacing between the buttons (px) //! LOCKED | ||
#define INTIAL_BUTTON_Y 325 // initial button y position (px) //!LOCKED | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#ifndef ENEMY_H | ||
#define ENEMY_H | ||
|
||
typedef struct | ||
{ | ||
SDL_Rect img_pos; // position of the sprite on the screen | ||
SDL_Rect img_size; // size of the sprite | ||
SDL_Surface *img; // pointer to the sprite image | ||
int direction; // 0 for idle, 1 for right , 2 for left | ||
float speed; // steps per frame | ||
int max_steps; // maximum number of steps before changing direction | ||
int idle_time; // time when the enemy started idling | ||
int x; // position of the enemy in the grid (screen coordinates) | ||
int y; // position of the enemy in the grid (screen coordinates) | ||
} enemy; | ||
|
||
void initEnemy(enemy *e); | ||
void drawEnemy(SDL_Surface *screen, enemy e); | ||
void animateEnemy(enemy *e, int direction); | ||
void moveEnemy(enemy *e); //! added new parameter dont forget | ||
int collisionBB(SDL_Rect player, SDL_Rect enemy); | ||
|
||
//**************************************************** | ||
|
||
void initEnemytest(enemy *e); | ||
void drawEnemytest(SDL_Surface *screen, enemy e); | ||
void animateEnemytest(enemy *e, int direction); | ||
void moveEnemytest(enemy *e); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,224 @@ | ||
#include <SDL/SDL.h> | ||
#include <SDL/SDL_image.h> | ||
#include "constants.h" | ||
#include "enemy.h" | ||
|
||
void initEnemy(enemy *e) | ||
{ | ||
e->img = IMG_Load("../assets/img/enemyrun.png"); | ||
if (e->img == NULL) | ||
{ | ||
printf("Error loading enemy image: %s\n", SDL_GetError()); | ||
} | ||
e->direction = 1; | ||
e->speed = 1; | ||
e->max_steps = 100; | ||
e->idle_time = 2000; | ||
e->x = (SCREEN_W / 2 - (e->img->w / 6) - 96 / 2) + 100; // screen width - (width of img / sprite size) - (width of sprite / 2) | ||
e->y = (SCREEN_H / 2 - (e->img->h / 3) - 96 / 2) + 100; // screen height - (height of img / sprite size) - (height of sprite / 2) | ||
|
||
e->img_size.x = 0; | ||
e->img_size.y = 0; | ||
e->img_size.w = (e->img->w) / 6; // (width of img / sprite frames) :576/6 =96px | ||
e->img_size.h = (e->img->h) / 3; // (height of img / sprite frames) := 288/3= 96px | ||
e->img_pos.x = e->x; | ||
e->img_pos.y = e->y; | ||
} | ||
|
||
void drawEnemy(SDL_Surface *screen, enemy e) | ||
{ | ||
SDL_BlitSurface(e.img, &(e.img_size), screen, &(e.img_pos)); | ||
} | ||
|
||
void animateEnemy(enemy *e, int direction) //! added new parameter dont forget | ||
{ | ||
static int frame = 0; // static variable to track current frame | ||
int row = 0; | ||
if (direction == 1) | ||
{ | ||
row = 0; | ||
} | ||
else if (direction == 2) | ||
{ | ||
row = 1; | ||
} | ||
else if (direction == 0) | ||
{ | ||
row = 2; | ||
} | ||
e->img_size.x = frame * e->img_size.w; // set x position based on frame | ||
e->img_size.y = row * e->img_size.h; // set y position based on direction | ||
|
||
Uint32 current_time = SDL_GetTicks(); | ||
static Uint32 last_time = 0; | ||
Uint32 delta_time = current_time - last_time; | ||
if (delta_time >= 100) | ||
{ // add a delay of 100 milliseconds between frames | ||
frame++; // increment frame counter | ||
if (frame >= 6) | ||
{ // if we've reached the end of the sprite sheet, wrap around | ||
frame = 0; | ||
} | ||
last_time = current_time; | ||
} | ||
} | ||
|
||
void moveEnemy(enemy *e) | ||
{ | ||
if (e->direction == 1) | ||
{ // move right | ||
animateEnemy(e, 1); | ||
e->img_pos.x += e->speed; | ||
if (e->img_pos.x >= e->x + e->max_steps) | ||
{ | ||
e->img_pos.x = e->x + e->max_steps; | ||
e->direction = 0; // change direction to idle | ||
e->idle_time = SDL_GetTicks(); // start idle time | ||
} | ||
} | ||
else if (e->direction == 2) | ||
{ // move left | ||
animateEnemy(e, 2); | ||
e->img_pos.x -= e->speed; | ||
if (e->img_pos.x <= e->x) | ||
{ | ||
e->img_pos.x = e->x; | ||
e->direction = 0; // change direction to idle | ||
e->idle_time = SDL_GetTicks(); // start idle time | ||
} | ||
} | ||
else if (e->direction == 0) | ||
{ // idle | ||
animateEnemy(e, 0); | ||
Uint32 current_time = SDL_GetTicks(); | ||
if (current_time - e->idle_time >= 2000) | ||
{ // check if idle time is over | ||
if (e->img_pos.x == e->x) | ||
{ | ||
e->direction = 1; // change direction to right | ||
} | ||
else if (e->img_pos.x == e->x + e->max_steps) | ||
{ | ||
e->direction = 2; // change direction to left | ||
} | ||
} | ||
} | ||
} | ||
|
||
int collisionBB(SDL_Rect player, SDL_Rect enemy) | ||
{ | ||
int collision = 0; | ||
if ((player.x + player.w < enemy.x) || (player.x > enemy.x + enemy.w) || (player.y + player.h < enemy.y) || (player.y > enemy.y + enemy.h)) | ||
{ | ||
collision = 0; // no collision | ||
} | ||
else | ||
{ | ||
collision = 1; // collision | ||
} | ||
return collision; | ||
} | ||
|
||
//******************************* | ||
|
||
void initEnemytest(enemy *e) | ||
{ | ||
e->img = IMG_Load("../assets/img/enemyrun.png"); | ||
if (e->img == NULL) | ||
{ | ||
printf("Error loading enemy image: %s\n", SDL_GetError()); | ||
} | ||
e->direction = 1; | ||
e->speed = 1; | ||
e->max_steps = 100; | ||
e->idle_time = 2000; | ||
e->x = (SCREEN_W / 2 - (e->img->w / 6) - 96 / 2) + 200; // screen width - (width of img / sprite size) - (width of sprite / 2) | ||
e->y = (SCREEN_H / 2 - (e->img->h / 3) - 96 / 2) + 100; // screen height - (height of img / sprite size) - (height of sprite / 2) | ||
|
||
e->img_size.x = 0; | ||
e->img_size.y = 0; | ||
e->img_size.w = (e->img->w) / 6; // (width of img / sprite frames) :576/6 =96px | ||
e->img_size.h = (e->img->h) / 3; // (height of img / sprite frames) := 288/3= 96px | ||
e->img_pos.x = e->x; | ||
e->img_pos.y = e->y; | ||
} | ||
|
||
void drawEnemytest(SDL_Surface *screen, enemy e) | ||
{ | ||
SDL_BlitSurface(e.img, &(e.img_size), screen, &(e.img_pos)); | ||
} | ||
|
||
void animateEnemytest(enemy *e, int direction) //! added new parameter dont forget | ||
{ | ||
static int frame = 0; // static variable to track current frame | ||
int row = 0; | ||
if (direction == 1) | ||
{ | ||
row = 0; | ||
} | ||
else if (direction == 2) | ||
{ | ||
row = 1; | ||
} | ||
else if (direction == 0) | ||
{ | ||
row = 2; | ||
} | ||
e->img_size.x = frame * e->img_size.w; // set x position based on frame | ||
e->img_size.y = row * e->img_size.h; // set y position based on direction | ||
|
||
Uint32 current_time = SDL_GetTicks(); | ||
static Uint32 last_time = 0; | ||
Uint32 delta_time = current_time - last_time; | ||
if (delta_time >= 100) | ||
{ // add a delay of 100 milliseconds between frames | ||
frame++; // increment frame counter | ||
if (frame >= 6) | ||
{ // if we've reached the end of the sprite sheet, wrap around | ||
frame = 0; | ||
} | ||
last_time = current_time; | ||
} | ||
} | ||
|
||
void moveEnemytest(enemy *e) | ||
{ | ||
if (e->direction == 2) | ||
{ // move right | ||
animateEnemy(e, 1); | ||
e->img_pos.x += e->speed; | ||
if (e->img_pos.x >= e->x + e->max_steps) | ||
{ | ||
e->img_pos.x = e->x + e->max_steps; | ||
e->direction = 0; // change direction to idle | ||
e->idle_time = SDL_GetTicks(); // start idle time | ||
} | ||
} | ||
else if (e->direction == 1) | ||
{ // move left | ||
animateEnemy(e, 2); | ||
e->img_pos.x -= e->speed; | ||
if (e->img_pos.x <= e->x) | ||
{ | ||
e->img_pos.x = e->x; | ||
e->direction = 0; // change direction to idle | ||
e->idle_time = SDL_GetTicks(); // start idle time | ||
} | ||
} | ||
else if (e->direction == 0) | ||
{ // idle | ||
animateEnemy(e, 0); | ||
Uint32 current_time = SDL_GetTicks(); | ||
if (current_time - e->idle_time >= 2000) | ||
{ // check if idle time is over | ||
if (e->img_pos.x == e->x) | ||
{ | ||
e->direction = 1; // change direction to right | ||
} | ||
else if (e->img_pos.x == e->x + e->max_steps) | ||
{ | ||
e->direction = 2; // change direction to left | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.