88
99#define PLAYER_X 32
1010
11+ #define PLAYER_UP 20
12+ #define PLAYER_DOWN 20
13+
14+ // ^ maybe 30?
15+
1116#define JUMP_VELOCITY 150
1217#define GRAVITY 500
1318
1924
2025#define GAP_SIZE 3
2126
27+ #define TOLERANCE 1
28+
29+ #define GROUND 5
30+
31+ #define DEAD_TIME 0.4
32+
2233using namespace blit ;
2334
2435struct Squirrel {
@@ -27,8 +38,11 @@ struct Squirrel {
2738
2839 bool alive;
2940 bool started;
41+ bool onGround;
3042
3143 int score;
44+
45+ float deadTimer;
3246};
3347
3448struct Log {
@@ -105,7 +119,18 @@ Log generate_log() {
105119}
106120
107121void render_player (Squirrel player) {
108- int index = 11 ; // need to get animation frame + 10
122+ int index = 12 ; // need to get animation frame
123+
124+ if (player.onGround ) {
125+ index = 15 ;
126+ }
127+ else if (player.yVelocity < -PLAYER_DOWN) {
128+ index = 14 ;
129+ }
130+ else if (player.yVelocity > PLAYER_UP) {
131+ index = 13 ;
132+ }
133+
109134
110135 screen.sprite (index, Point (PLAYER_X - SPRITE_SIZE / 2 , player.yPosition - SPRITE_SIZE / 2 ));
111136 // screen.rectangle(Rect(PLAYER_X - SPRITE_SIZE / 2, player.yPosition - SPRITE_SIZE / 2, 8, 8));
@@ -140,12 +165,13 @@ void fade_background() {
140165}
141166
142167void start_game () {
143- // player.alive = true;
144168 player.yPosition = SCREEN_HEIGHT / 2 ;
145169 player.yVelocity = 0 ;
170+ player.deadTimer = 0 ;
146171 player.started = false ;
147172 player.score = 0 ;
148173 player.alive = true ;
174+ player.onGround = false ;
149175 offset = 0 ;
150176 treeNumber = 0 ;
151177 logs.clear ();
@@ -186,7 +212,7 @@ void render(uint32_t time) {
186212
187213 fade_background ();
188214
189- screen.text (" Jumpy Squirrel" , minimal_font, Point (SCREEN_WIDTH / 2 , SCREEN_HEIGHT * 1 / 3 ), true , TextAlign::center_center);
215+ screen.text (" Jumpy Squirrel" , minimal_font, Point (SCREEN_WIDTH / 2 , SCREEN_HEIGHT * 1 / 4 ), true , TextAlign::center_center);
190216
191217 screen.text (" Press A to Start" , minimal_font, Point (SCREEN_WIDTH / 2 , SCREEN_HEIGHT * 2 / 3 ), true , TextAlign::center_center);
192218 }
@@ -212,9 +238,9 @@ void render(uint32_t time) {
212238
213239 fade_background ();
214240
215- screen.text (std::to_string (player.score ), minimal_font, Point (SCREEN_WIDTH / 2 , 8 ), true , TextAlign::center_center);
241+ screen.text (" You scored " + std::to_string (player.score ), minimal_font, Point (SCREEN_WIDTH / 2 , SCREEN_HEIGHT * 1 / 3 ), true , TextAlign::center_center);
216242
217- screen.text (" You died" , minimal_font, Point (SCREEN_WIDTH / 2 , SCREEN_HEIGHT * 1 / 3 ), true , TextAlign::center_center);
243+ screen.text (" You died" , minimal_font, Point (SCREEN_WIDTH / 2 , SCREEN_HEIGHT * 1 / 4 ), true , TextAlign::center_center);
218244
219245 screen.text (" Press A to Retry" , minimal_font, Point (SCREEN_WIDTH / 2 , SCREEN_HEIGHT * 2 / 3 ), true , TextAlign::center_center);
220246 }
@@ -267,7 +293,7 @@ void update(uint32_t time) {
267293 }
268294
269295 if ((logs.at (i).xPosition - SPRITE_SIZE - offset) < (PLAYER_X + SPRITE_SIZE / 2 ) && (logs.at (i).xPosition + SPRITE_SIZE - offset) > (PLAYER_X - SPRITE_SIZE / 2 )) {
270- if (get_min_y (logs.at (i).gapPosition ) < (player.yPosition - SPRITE_SIZE / 2 ) && get_max_y (logs.at (i).gapPosition ) > (player.yPosition + SPRITE_SIZE / 2 )) {
296+ if (get_min_y (logs.at (i).gapPosition ) - TOLERANCE < (player.yPosition - SPRITE_SIZE / 2 ) && get_max_y (logs.at (i).gapPosition ) + TOLERANCE > (player.yPosition + SPRITE_SIZE / 2 )) {
271297
272298 }
273299 else {
@@ -277,13 +303,6 @@ void update(uint32_t time) {
277303 }
278304 }
279305
280- // logSpawnTimer -= SCROLL_SPEED * dt;
281- /* offset -= LOG_SPEED * dt;
282-
283- if (offset < -SPRITE_SIZE) {
284- offset += SPRITE_SIZE;
285- }*/
286-
287306 if (logs.at (logs.size () - 1 ).xPosition - offset < SCREEN_WIDTH + SPRITE_SIZE) {
288307 logs.push_back (generate_log ());
289308 }
@@ -295,10 +314,16 @@ void update(uint32_t time) {
295314 }
296315 }
297316
298- if (player.yPosition + SPRITE_SIZE / 2 > SCREEN_HEIGHT) {
317+
318+ if (player.yPosition + (SPRITE_SIZE / 2 ) > SCREEN_HEIGHT - GROUND) {
299319 player.alive = false ;
320+ player.yPosition = SCREEN_HEIGHT - (SPRITE_SIZE / 2 ) - GROUND;
321+ player.onGround = true ;
322+ player.deadTimer += dt;
323+ }
324+
325+ if (player.deadTimer >= DEAD_TIME) {
300326 state = 2 ;
301- player.yPosition = SCREEN_HEIGHT - SPRITE_SIZE / 2 ;
302327 }
303328 }
304329 }
0 commit comments