Skip to content
This repository was archived by the owner on Oct 9, 2023. It is now read-only.

Commit 637c139

Browse files
authored
Enemy (#41)
2 parents a9da49f + 1d34c35 commit 637c139

File tree

9 files changed

+306
-26
lines changed

9 files changed

+306
-26
lines changed

assets/img/enemyrun.png

13 KB
Loading

build/makefile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
CC = gcc
22
CFLAGS = -Wall -I../include
33
LDFLAGS = -lSDL -lSDL_image -lSDL_mixer -lSDL_ttf
4-
OBJS = main.o menu.o music.o text.o stars.o settings.o start.o
4+
OBJS = main.o menu.o music.o text.o stars.o settings.o start.o enemy.o
5+
56
game: $(OBJS)
67
$(CC) $(OBJS) $(LDFLAGS) -o game
8+
79
%.o: ../src/%.c
810
$(CC) $(CFLAGS) -c $<
911

1012
.PHONY: clean
1113
clean:
12-
rm -f $(OBJS) game
14+
rm -f $(OBJS) game

doc/enemxy.png

73.8 KB
Loading

include/constants.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef CONSTANTS_H
2+
#define CONSTANTS_H
3+
4+
//*screen size
5+
#define SCREEN_W 1280
6+
#define SCREEN_H 720
7+
8+
//*stars stuff
9+
#define STARS_COUNT 100 // number of stars //? need locking
10+
#define STARS_LAYERS 4 // number of stars variations
11+
#define DELTA_TIME 16 // 1000ms / 60fps = 16.6666
12+
13+
//*main menu buttons stuff
14+
#define BUTTON_SPACING 14 // spacing between the buttons (px) //! LOCKED
15+
#define INTIAL_BUTTON_Y 325 // initial button y position (px) //!LOCKED
16+
17+
#endif

include/enemy.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#ifndef ENEMY_H
2+
#define ENEMY_H
3+
4+
typedef struct
5+
{
6+
SDL_Rect img_pos; // position of the sprite on the screen
7+
SDL_Rect img_size; // size of the sprite
8+
SDL_Surface *img; // pointer to the sprite image
9+
int direction; // 0 for idle, 1 for right , 2 for left
10+
float speed; // steps per frame
11+
int max_steps; // maximum number of steps before changing direction
12+
int idle_time; // time when the enemy started idling
13+
int x; // position of the enemy in the grid (screen coordinates)
14+
int y; // position of the enemy in the grid (screen coordinates)
15+
} enemy;
16+
17+
void initEnemy(enemy *e);
18+
void drawEnemy(SDL_Surface *screen, enemy e);
19+
void animateEnemy(enemy *e, int direction);
20+
void moveEnemy(enemy *e); //! added new parameter dont forget
21+
int collisionBB(SDL_Rect player, SDL_Rect enemy);
22+
23+
//****************************************************
24+
25+
void initEnemytest(enemy *e);
26+
void drawEnemytest(SDL_Surface *screen, enemy e);
27+
void animateEnemytest(enemy *e, int direction);
28+
void moveEnemytest(enemy *e);
29+
30+
#endif

include/menu.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11

22
#ifndef MENU_H
33
#define MENU_H
4-
5-
#define SCREEN_W 1280
6-
#define SCREEN_H 720 // screen height and width
74
typedef struct
85
{
96

@@ -46,7 +43,6 @@ void imageDrawClicked_playbutton(SDL_Surface *screen, image img);
4643
void imageDrawClicked_settingsbutton(SDL_Surface *screen, image img);
4744
void imageDrawClicked_quitbutton(SDL_Surface *screen, image img);
4845

49-
5046
void imageDrawHovered_playbutton(SDL_Surface *screen, image img);
5147
void imageDrawHovered_settingsbutton(SDL_Surface *screen, image img);
5248
void imageDrawHovered_quitbutton(SDL_Surface *screen, image img);
@@ -55,5 +51,4 @@ void imageDrawHovered_quitbutton(SDL_Surface *screen, image img);
5551
//**UNIVERSAL FUNCTION**//
5652
// free buttons
5753

58-
5954
#endif

src/enemy.c

Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
#include <SDL/SDL.h>
2+
#include <SDL/SDL_image.h>
3+
#include "constants.h"
4+
#include "enemy.h"
5+
6+
void initEnemy(enemy *e)
7+
{
8+
e->img = IMG_Load("../assets/img/enemyrun.png");
9+
if (e->img == NULL)
10+
{
11+
printf("Error loading enemy image: %s\n", SDL_GetError());
12+
}
13+
e->direction = 1;
14+
e->speed = 1;
15+
e->max_steps = 100;
16+
e->idle_time = 2000;
17+
e->x = (SCREEN_W / 2 - (e->img->w / 6) - 96 / 2) + 100; // screen width - (width of img / sprite size) - (width of sprite / 2)
18+
e->y = (SCREEN_H / 2 - (e->img->h / 3) - 96 / 2) + 100; // screen height - (height of img / sprite size) - (height of sprite / 2)
19+
20+
e->img_size.x = 0;
21+
e->img_size.y = 0;
22+
e->img_size.w = (e->img->w) / 6; // (width of img / sprite frames) :576/6 =96px
23+
e->img_size.h = (e->img->h) / 3; // (height of img / sprite frames) := 288/3= 96px
24+
e->img_pos.x = e->x;
25+
e->img_pos.y = e->y;
26+
}
27+
28+
void drawEnemy(SDL_Surface *screen, enemy e)
29+
{
30+
SDL_BlitSurface(e.img, &(e.img_size), screen, &(e.img_pos));
31+
}
32+
33+
void animateEnemy(enemy *e, int direction) //! added new parameter dont forget
34+
{
35+
static int frame = 0; // static variable to track current frame
36+
int row = 0;
37+
if (direction == 1)
38+
{
39+
row = 0;
40+
}
41+
else if (direction == 2)
42+
{
43+
row = 1;
44+
}
45+
else if (direction == 0)
46+
{
47+
row = 2;
48+
}
49+
e->img_size.x = frame * e->img_size.w; // set x position based on frame
50+
e->img_size.y = row * e->img_size.h; // set y position based on direction
51+
52+
Uint32 current_time = SDL_GetTicks();
53+
static Uint32 last_time = 0;
54+
Uint32 delta_time = current_time - last_time;
55+
if (delta_time >= 100)
56+
{ // add a delay of 100 milliseconds between frames
57+
frame++; // increment frame counter
58+
if (frame >= 6)
59+
{ // if we've reached the end of the sprite sheet, wrap around
60+
frame = 0;
61+
}
62+
last_time = current_time;
63+
}
64+
}
65+
66+
void moveEnemy(enemy *e)
67+
{
68+
if (e->direction == 1)
69+
{ // move right
70+
animateEnemy(e, 1);
71+
e->img_pos.x += e->speed;
72+
if (e->img_pos.x >= e->x + e->max_steps)
73+
{
74+
e->img_pos.x = e->x + e->max_steps;
75+
e->direction = 0; // change direction to idle
76+
e->idle_time = SDL_GetTicks(); // start idle time
77+
}
78+
}
79+
else if (e->direction == 2)
80+
{ // move left
81+
animateEnemy(e, 2);
82+
e->img_pos.x -= e->speed;
83+
if (e->img_pos.x <= e->x)
84+
{
85+
e->img_pos.x = e->x;
86+
e->direction = 0; // change direction to idle
87+
e->idle_time = SDL_GetTicks(); // start idle time
88+
}
89+
}
90+
else if (e->direction == 0)
91+
{ // idle
92+
animateEnemy(e, 0);
93+
Uint32 current_time = SDL_GetTicks();
94+
if (current_time - e->idle_time >= 2000)
95+
{ // check if idle time is over
96+
if (e->img_pos.x == e->x)
97+
{
98+
e->direction = 1; // change direction to right
99+
}
100+
else if (e->img_pos.x == e->x + e->max_steps)
101+
{
102+
e->direction = 2; // change direction to left
103+
}
104+
}
105+
}
106+
}
107+
108+
int collisionBB(SDL_Rect player, SDL_Rect enemy)
109+
{
110+
int collision = 0;
111+
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))
112+
{
113+
collision = 0; // no collision
114+
}
115+
else
116+
{
117+
collision = 1; // collision
118+
}
119+
return collision;
120+
}
121+
122+
//*******************************
123+
124+
void initEnemytest(enemy *e)
125+
{
126+
e->img = IMG_Load("../assets/img/enemyrun.png");
127+
if (e->img == NULL)
128+
{
129+
printf("Error loading enemy image: %s\n", SDL_GetError());
130+
}
131+
e->direction = 1;
132+
e->speed = 1;
133+
e->max_steps = 100;
134+
e->idle_time = 2000;
135+
e->x = (SCREEN_W / 2 - (e->img->w / 6) - 96 / 2) + 200; // screen width - (width of img / sprite size) - (width of sprite / 2)
136+
e->y = (SCREEN_H / 2 - (e->img->h / 3) - 96 / 2) + 100; // screen height - (height of img / sprite size) - (height of sprite / 2)
137+
138+
e->img_size.x = 0;
139+
e->img_size.y = 0;
140+
e->img_size.w = (e->img->w) / 6; // (width of img / sprite frames) :576/6 =96px
141+
e->img_size.h = (e->img->h) / 3; // (height of img / sprite frames) := 288/3= 96px
142+
e->img_pos.x = e->x;
143+
e->img_pos.y = e->y;
144+
}
145+
146+
void drawEnemytest(SDL_Surface *screen, enemy e)
147+
{
148+
SDL_BlitSurface(e.img, &(e.img_size), screen, &(e.img_pos));
149+
}
150+
151+
void animateEnemytest(enemy *e, int direction) //! added new parameter dont forget
152+
{
153+
static int frame = 0; // static variable to track current frame
154+
int row = 0;
155+
if (direction == 1)
156+
{
157+
row = 0;
158+
}
159+
else if (direction == 2)
160+
{
161+
row = 1;
162+
}
163+
else if (direction == 0)
164+
{
165+
row = 2;
166+
}
167+
e->img_size.x = frame * e->img_size.w; // set x position based on frame
168+
e->img_size.y = row * e->img_size.h; // set y position based on direction
169+
170+
Uint32 current_time = SDL_GetTicks();
171+
static Uint32 last_time = 0;
172+
Uint32 delta_time = current_time - last_time;
173+
if (delta_time >= 100)
174+
{ // add a delay of 100 milliseconds between frames
175+
frame++; // increment frame counter
176+
if (frame >= 6)
177+
{ // if we've reached the end of the sprite sheet, wrap around
178+
frame = 0;
179+
}
180+
last_time = current_time;
181+
}
182+
}
183+
184+
void moveEnemytest(enemy *e)
185+
{
186+
if (e->direction == 2)
187+
{ // move right
188+
animateEnemy(e, 1);
189+
e->img_pos.x += e->speed;
190+
if (e->img_pos.x >= e->x + e->max_steps)
191+
{
192+
e->img_pos.x = e->x + e->max_steps;
193+
e->direction = 0; // change direction to idle
194+
e->idle_time = SDL_GetTicks(); // start idle time
195+
}
196+
}
197+
else if (e->direction == 1)
198+
{ // move left
199+
animateEnemy(e, 2);
200+
e->img_pos.x -= e->speed;
201+
if (e->img_pos.x <= e->x)
202+
{
203+
e->img_pos.x = e->x;
204+
e->direction = 0; // change direction to idle
205+
e->idle_time = SDL_GetTicks(); // start idle time
206+
}
207+
}
208+
else if (e->direction == 0)
209+
{ // idle
210+
animateEnemy(e, 0);
211+
Uint32 current_time = SDL_GetTicks();
212+
if (current_time - e->idle_time >= 2000)
213+
{ // check if idle time is over
214+
if (e->img_pos.x == e->x)
215+
{
216+
e->direction = 1; // change direction to right
217+
}
218+
else if (e->img_pos.x == e->x + e->max_steps)
219+
{
220+
e->direction = 2; // change direction to left
221+
}
222+
}
223+
}
224+
}

src/main.c

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,23 @@
33
#include <stdlib.h>
44
#include <string.h>
55
#include <stdbool.h>
6+
#include "constants.h" // for constants
67

78
#include <SDL/SDL.h>
89
#include <SDL/SDL_image.h> //for loading images
910
#include <SDL/SDL_ttf.h> //for loading fonts
1011
#include <SDL/SDL_mixer.h> //for loading sounds
1112

1213
// including the headers
13-
1414
#include "../include/menu.h" //menu header
1515
#include "../include/music.h" //music header
1616
#include "../include/text.h" //text header
1717
#include "../include/stars.h" //stars header
1818
#include "../include/settings.h" //settings header
19+
#include "../include/enemy.h"
1920

2021
// screen
2122
SDL_Surface *screen;
22-
#define SCREEN_W 1280
23-
#define SCREEN_H 720 // screen height and width
24-
25-
#define STARS_COUNT 100 // number of stars //! need locking
26-
#define STARS_LAYERS 4 // number of stars variations
27-
#define DELTA_TIME 16 // 1000ms / 60fps = 16.6666
2823

2924
//* regular -> hovered -> clicked
3025
// images (_C for clicked) (_H for hovered)
@@ -65,6 +60,10 @@ Mix_Chunk *clickFX;
6560
// text
6661
text author;
6762

63+
// characters
64+
enemy enemy1;
65+
enemy enemy2;
66+
6867
// logic
6968
SDL_Event event;
7069
int loop = 1; // game loop
@@ -141,6 +140,10 @@ int main()
141140
// loading text
142141
textLoad(&author);
143142

143+
// loading enemy
144+
initEnemy(&enemy1);
145+
initEnemytest(&enemy2);
146+
144147
//* loading settings menu images
145148
//? maybe add background instead of solid color for settings menu
146149

@@ -440,6 +443,17 @@ int main()
440443
imageDraw_lvl1(screen, lvl1);
441444
imageDraw_backbutton(screen, backButton); //! used twice, but it's ok for now (universal fucntion)
442445

446+
drawEnemy(screen, enemy1);
447+
moveEnemy(&enemy1); //* moveEnemy will call animateEnemy
448+
//************
449+
drawEnemytest(screen, enemy2);
450+
moveEnemytest(&enemy2); //* moveEnemy will call animateEnemy
451+
//************
452+
if (collisionBB(enemy1.img_pos, enemy2.img_pos) == 1)
453+
{
454+
printf(" collision detected \t");
455+
}
456+
443457
while (SDL_PollEvent(&event))
444458
{
445459
switch (event.type)

0 commit comments

Comments
 (0)