Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed MotionSensor
Entity/Snake/Snake.cpp@16:ddb203a74dfc, 2019-04-24 (annotated)
- Committer:
- el17sm
- Date:
- Wed Apr 24 21:21:37 2019 +0000
- Revision:
- 16:ddb203a74dfc
- Child:
- 17:99e533f7f2fb
- Child:
- 19:bfe410c82b45
Screen clear error;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| el17sm | 16:ddb203a74dfc | 1 | #include "Snake.h" |
| el17sm | 16:ddb203a74dfc | 2 | #include "math.h" |
| el17sm | 16:ddb203a74dfc | 3 | #include <complex> |
| el17sm | 16:ddb203a74dfc | 4 | |
| el17sm | 16:ddb203a74dfc | 5 | Snake::Snake(float pos_x, float pos_y) { |
| el17sm | 16:ddb203a74dfc | 6 | moving = true; |
| el17sm | 16:ddb203a74dfc | 7 | face = 0; |
| el17sm | 16:ddb203a74dfc | 8 | hp = 4; |
| el17sm | 16:ddb203a74dfc | 9 | hitbox.width = 6; |
| el17sm | 16:ddb203a74dfc | 10 | hitbox.height = 5; |
| el17sm | 16:ddb203a74dfc | 11 | position.x = pos_x; |
| el17sm | 16:ddb203a74dfc | 12 | position.y = pos_y; |
| el17sm | 16:ddb203a74dfc | 13 | sprite_size.width = 6; |
| el17sm | 16:ddb203a74dfc | 14 | sprite_size.height = 9; |
| el17sm | 16:ddb203a74dfc | 15 | sprite_size.offset_x = 0; |
| el17sm | 16:ddb203a74dfc | 16 | sprite_size.offset_y = 4; |
| el17sm | 16:ddb203a74dfc | 17 | frame.count = 0; |
| el17sm | 16:ddb203a74dfc | 18 | frame.number = 0; |
| el17sm | 16:ddb203a74dfc | 19 | frame.max = 6; |
| el17sm | 16:ddb203a74dfc | 20 | velocity = 0.1; |
| el17sm | 16:ddb203a74dfc | 21 | } |
| el17sm | 16:ddb203a74dfc | 22 | |
| el17sm | 16:ddb203a74dfc | 23 | void Snake::move(float player_x, float player_y) { |
| el17sm | 16:ddb203a74dfc | 24 | std::complex<double> pos_diff(player_x - position.x, player_y - position.y); // defining difference in position as a vector |
| el17sm | 16:ddb203a74dfc | 25 | velocity = velocity_pattern[frame.number]; // Creating slithering effect, changing velocity of movement |
| el17sm | 16:ddb203a74dfc | 26 | |
| el17sm | 16:ddb203a74dfc | 27 | // Setting Face |
| el17sm | 16:ddb203a74dfc | 28 | if (frame.number == 0) { |
| el17sm | 16:ddb203a74dfc | 29 | if (abs(pos_diff.real()) > abs(pos_diff.imag())) { |
| el17sm | 16:ddb203a74dfc | 30 | if (pos_diff.real() > 0) { |
| el17sm | 16:ddb203a74dfc | 31 | face = 1; |
| el17sm | 16:ddb203a74dfc | 32 | } |
| el17sm | 16:ddb203a74dfc | 33 | else { |
| el17sm | 16:ddb203a74dfc | 34 | face = 3; |
| el17sm | 16:ddb203a74dfc | 35 | } |
| el17sm | 16:ddb203a74dfc | 36 | } |
| el17sm | 16:ddb203a74dfc | 37 | else { |
| el17sm | 16:ddb203a74dfc | 38 | if (pos_diff.imag() > 0) { |
| el17sm | 16:ddb203a74dfc | 39 | face = 0; |
| el17sm | 16:ddb203a74dfc | 40 | } |
| el17sm | 16:ddb203a74dfc | 41 | else { |
| el17sm | 16:ddb203a74dfc | 42 | face = 2; |
| el17sm | 16:ddb203a74dfc | 43 | } |
| el17sm | 16:ddb203a74dfc | 44 | } |
| el17sm | 16:ddb203a74dfc | 45 | } |
| el17sm | 16:ddb203a74dfc | 46 | |
| el17sm | 16:ddb203a74dfc | 47 | // Movement, Offset and Update |
| el17sm | 16:ddb203a74dfc | 48 | if (face == 0){ |
| el17sm | 16:ddb203a74dfc | 49 | position.y += velocity; |
| el17sm | 16:ddb203a74dfc | 50 | } |
| el17sm | 16:ddb203a74dfc | 51 | else if (face == 1){ |
| el17sm | 16:ddb203a74dfc | 52 | position.x += velocity; |
| el17sm | 16:ddb203a74dfc | 53 | } |
| el17sm | 16:ddb203a74dfc | 54 | else if (face == 2){ |
| el17sm | 16:ddb203a74dfc | 55 | position.y -= velocity; |
| el17sm | 16:ddb203a74dfc | 56 | } |
| el17sm | 16:ddb203a74dfc | 57 | else if (face == 3){ |
| el17sm | 16:ddb203a74dfc | 58 | position.x -= velocity; |
| el17sm | 16:ddb203a74dfc | 59 | } |
| el17sm | 16:ddb203a74dfc | 60 | |
| el17sm | 16:ddb203a74dfc | 61 | undo_move_x(matrix_collision_test(position.x, prev_pos.y, 0)); |
| el17sm | 16:ddb203a74dfc | 62 | undo_move_y(matrix_collision_test(prev_pos.x, position.y, 0)); |
| el17sm | 16:ddb203a74dfc | 63 | |
| el17sm | 16:ddb203a74dfc | 64 | if (frame.number < frame.max) { |
| el17sm | 16:ddb203a74dfc | 65 | frame.count++; |
| el17sm | 16:ddb203a74dfc | 66 | } |
| el17sm | 16:ddb203a74dfc | 67 | else { |
| el17sm | 16:ddb203a74dfc | 68 | frame.count = 0; |
| el17sm | 16:ddb203a74dfc | 69 | } |
| el17sm | 16:ddb203a74dfc | 70 | frame.number = (frame.count/5) % frame.max; |
| el17sm | 16:ddb203a74dfc | 71 | } |
| el17sm | 16:ddb203a74dfc | 72 | |
| el17sm | 16:ddb203a74dfc | 73 | int * Snake::get_frame() { |
| el17sm | 16:ddb203a74dfc | 74 | return (int *) sprite_snake[face][frame.number]; |
| el17sm | 16:ddb203a74dfc | 75 | } |