1+ #include <stdlib.h>
12#include <SDL/SDL.h>
23#include <SDL/SDL_image.h>
34#include "constants.h"
@@ -32,7 +33,7 @@ void drawEnemy(SDL_Surface *screen, enemy e)
3233
3334void animateEnemy (enemy * e , int direction ) //! added new parameter dont forget
3435{
35- static int frame = 0 ; // static variable to track current frame
36+ static int frame = 0 ;
3637 int row = 0 ;
3738 if (direction == 1 )
3839 {
@@ -46,17 +47,17 @@ void animateEnemy(enemy *e, int direction) //! added new parameter dont forget
4647 {
4748 row = 2 ;
4849 }
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
50+ e -> img_size .x = frame * e -> img_size .w ;
51+ e -> img_size .y = row * e -> img_size .h ;
5152
5253 Uint32 current_time = SDL_GetTicks ();
5354 static Uint32 last_time = 0 ;
5455 Uint32 delta_time = current_time - last_time ;
5556 if (delta_time >= 100 )
56- { // add a delay of 100 milliseconds between frames
57- frame ++ ; // increment frame counter
57+ {
58+ frame ++ ;
5859 if (frame >= 6 )
59- { // if we've reached the end of the sprite sheet, wrap around
60+ {
6061 frame = 0 ;
6162 }
6263 last_time = current_time ;
@@ -72,8 +73,8 @@ void moveEnemy(enemy *e)
7273 if (e -> img_pos .x >= e -> x + e -> max_steps )
7374 {
7475 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
76+ e -> direction = 0 ;
77+ e -> idle_time = SDL_GetTicks () + rand () % 1500 + 500 ;
7778 }
7879 }
7980 else if (e -> direction == 2 )
@@ -83,23 +84,23 @@ void moveEnemy(enemy *e)
8384 if (e -> img_pos .x <= e -> x )
8485 {
8586 e -> img_pos .x = e -> x ;
86- e -> direction = 0 ; // change direction to idle
87- e -> idle_time = SDL_GetTicks (); // start idle time
87+ e -> direction = 0 ;
88+ e -> idle_time = SDL_GetTicks () + rand () % 1500 + 500 ;
8889 }
8990 }
9091 else if (e -> direction == 0 )
9192 { // idle
9293 animateEnemy (e , 0 );
9394 Uint32 current_time = SDL_GetTicks ();
94- if (current_time - e -> idle_time >= 2000 )
95+ if (current_time >= e -> idle_time )
9596 { // check if idle time is over
9697 if (e -> img_pos .x == e -> x )
9798 {
98- e -> direction = 1 ; // change direction to right
99+ e -> direction = 1 ;
99100 }
100101 else if (e -> img_pos .x == e -> x + e -> max_steps )
101102 {
102- e -> direction = 2 ; // change direction to left
103+ e -> direction = 2 ;
103104 }
104105 }
105106 }
@@ -110,11 +111,11 @@ int collisionBB(SDL_Rect player, SDL_Rect enemy)
110111 int collision = 0 ;
111112 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 ))
112113 {
113- collision = 0 ; // no collision
114+ collision = 0 ;
114115 }
115116 else
116117 {
117- collision = 1 ; // collision
118+ collision = 1 ;
118119 }
119120 return collision ;
120121}
@@ -164,17 +165,17 @@ void animateEnemytest(enemy *e, int direction) //! added new parameter dont forg
164165 {
165166 row = 2 ;
166167 }
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
168+ e -> img_size .x = frame * e -> img_size .w ;
169+ e -> img_size .y = row * e -> img_size .h ;
169170
170171 Uint32 current_time = SDL_GetTicks ();
171172 static Uint32 last_time = 0 ;
172173 Uint32 delta_time = current_time - last_time ;
173174 if (delta_time >= 100 )
174- { // add a delay of 100 milliseconds between frames
175- frame ++ ; // increment frame counter
175+ {
176+ frame ++ ;
176177 if (frame >= 6 )
177- { // if we've reached the end of the sprite sheet, wrap around
178+ {
178179 frame = 0 ;
179180 }
180181 last_time = current_time ;
@@ -184,14 +185,14 @@ void animateEnemytest(enemy *e, int direction) //! added new parameter dont forg
184185void moveEnemytest (enemy * e )
185186{
186187 if (e -> direction == 2 )
187- { // move right
188+ {
188189 animateEnemy (e , 1 );
189190 e -> img_pos .x += e -> speed ;
190191 if (e -> img_pos .x >= e -> x + e -> max_steps )
191192 {
192193 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
194+ e -> direction = 0 ;
195+ e -> idle_time = SDL_GetTicks ();
195196 }
196197 }
197198 else if (e -> direction == 1 )
@@ -201,23 +202,23 @@ void moveEnemytest(enemy *e)
201202 if (e -> img_pos .x <= e -> x )
202203 {
203204 e -> img_pos .x = e -> x ;
204- e -> direction = 0 ; // change direction to idle
205- e -> idle_time = SDL_GetTicks (); // start idle time
205+ e -> direction = 0 ;
206+ e -> idle_time = SDL_GetTicks ();
206207 }
207208 }
208209 else if (e -> direction == 0 )
209- { // idle
210+ {
210211 animateEnemy (e , 0 );
211212 Uint32 current_time = SDL_GetTicks ();
212213 if (current_time - e -> idle_time >= 2000 )
213- { // check if idle time is over
214+ {
214215 if (e -> img_pos .x == e -> x )
215216 {
216- e -> direction = 1 ; // change direction to right
217+ e -> direction = 1 ;
217218 }
218219 else if (e -> img_pos .x == e -> x + e -> max_steps )
219220 {
220- e -> direction = 2 ; // change direction to left
221+ e -> direction = 2 ;
221222 }
222223 }
223224 }
0 commit comments