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@21:be18f33da757, 2019-04-25 (annotated)
- Committer:
- el17sm
- Date:
- Thu Apr 25 05:27:43 2019 +0000
- Revision:
- 21:be18f33da757
- Parent:
- 19:bfe410c82b45
Snake half done no clear screen 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 | 21:be18f33da757 | 5 | Snake::Snake(float pos_x, float pos_y) |
| el17sm | 21:be18f33da757 | 6 | { |
| el17sm | 16:ddb203a74dfc | 7 | moving = true; |
| el17sm | 16:ddb203a74dfc | 8 | face = 0; |
| el17sm | 16:ddb203a74dfc | 9 | hp = 4; |
| el17sm | 16:ddb203a74dfc | 10 | hitbox.width = 6; |
| el17sm | 16:ddb203a74dfc | 11 | hitbox.height = 5; |
| el17sm | 16:ddb203a74dfc | 12 | position.x = pos_x; |
| el17sm | 16:ddb203a74dfc | 13 | position.y = pos_y; |
| el17sm | 16:ddb203a74dfc | 14 | sprite_size.width = 6; |
| el17sm | 16:ddb203a74dfc | 15 | sprite_size.height = 9; |
| el17sm | 16:ddb203a74dfc | 16 | sprite_size.offset_x = 0; |
| el17sm | 16:ddb203a74dfc | 17 | sprite_size.offset_y = 4; |
| el17sm | 16:ddb203a74dfc | 18 | frame.count = 0; |
| el17sm | 16:ddb203a74dfc | 19 | frame.number = 0; |
| el17sm | 16:ddb203a74dfc | 20 | frame.max = 6; |
| el17sm | 19:bfe410c82b45 | 21 | velocity = 0; |
| el17sm | 16:ddb203a74dfc | 22 | } |
| el17sm | 16:ddb203a74dfc | 23 | |
| el17sm | 21:be18f33da757 | 24 | void Snake::move(float player_x, float player_y) |
| el17sm | 21:be18f33da757 | 25 | { |
| el17sm | 16:ddb203a74dfc | 26 | std::complex<double> pos_diff(player_x - position.x, player_y - position.y); // defining difference in position as a vector |
| el17sm | 16:ddb203a74dfc | 27 | velocity = velocity_pattern[frame.number]; // Creating slithering effect, changing velocity of movement |
| el17sm | 21:be18f33da757 | 28 | |
| el17sm | 16:ddb203a74dfc | 29 | // Setting Face |
| el17sm | 16:ddb203a74dfc | 30 | if (frame.number == 0) { |
| el17sm | 16:ddb203a74dfc | 31 | if (abs(pos_diff.real()) > abs(pos_diff.imag())) { |
| el17sm | 16:ddb203a74dfc | 32 | if (pos_diff.real() > 0) { |
| el17sm | 16:ddb203a74dfc | 33 | face = 1; |
| el17sm | 21:be18f33da757 | 34 | } else { |
| el17sm | 16:ddb203a74dfc | 35 | face = 3; |
| el17sm | 16:ddb203a74dfc | 36 | } |
| el17sm | 21:be18f33da757 | 37 | } else { |
| el17sm | 16:ddb203a74dfc | 38 | if (pos_diff.imag() > 0) { |
| el17sm | 16:ddb203a74dfc | 39 | face = 0; |
| el17sm | 21:be18f33da757 | 40 | } else { |
| el17sm | 16:ddb203a74dfc | 41 | face = 2; |
| el17sm | 16:ddb203a74dfc | 42 | } |
| el17sm | 16:ddb203a74dfc | 43 | } |
| el17sm | 16:ddb203a74dfc | 44 | } |
| el17sm | 21:be18f33da757 | 45 | |
| el17sm | 16:ddb203a74dfc | 46 | // Movement, Offset and Update |
| el17sm | 21:be18f33da757 | 47 | if (face == 0) { |
| el17sm | 16:ddb203a74dfc | 48 | position.y += velocity; |
| el17sm | 21:be18f33da757 | 49 | } else if (face == 1) { |
| el17sm | 16:ddb203a74dfc | 50 | position.x += velocity; |
| el17sm | 21:be18f33da757 | 51 | } else if (face == 2) { |
| el17sm | 16:ddb203a74dfc | 52 | position.y -= velocity; |
| el17sm | 21:be18f33da757 | 53 | } else if (face == 3) { |
| el17sm | 16:ddb203a74dfc | 54 | position.x -= velocity; |
| el17sm | 16:ddb203a74dfc | 55 | } |
| el17sm | 21:be18f33da757 | 56 | |
| el17sm | 16:ddb203a74dfc | 57 | undo_move_x(matrix_collision_test(position.x, prev_pos.y, 0)); |
| el17sm | 16:ddb203a74dfc | 58 | undo_move_y(matrix_collision_test(prev_pos.x, position.y, 0)); |
| el17sm | 21:be18f33da757 | 59 | |
| el17sm | 21:be18f33da757 | 60 | frame.count++; |
| el17sm | 21:be18f33da757 | 61 | if (frame.count >= 5) { |
| el17sm | 16:ddb203a74dfc | 62 | frame.count = 0; |
| el17sm | 21:be18f33da757 | 63 | frame.number++; |
| el17sm | 21:be18f33da757 | 64 | if (frame.number >= frame.max) { |
| el17sm | 19:bfe410c82b45 | 65 | frame.number = 0; |
| el17sm | 19:bfe410c82b45 | 66 | } |
| el17sm | 16:ddb203a74dfc | 67 | } |
| el17sm | 16:ddb203a74dfc | 68 | } |
| el17sm | 16:ddb203a74dfc | 69 | |
| el17sm | 21:be18f33da757 | 70 | int * Snake::get_frame() |
| el17sm | 21:be18f33da757 | 71 | { |
| el17sm | 16:ddb203a74dfc | 72 | return (int *) sprite_snake[face][frame.number]; |
| el17sm | 16:ddb203a74dfc | 73 | } |