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 FXOS8700CQ mbed-rtos
Game_two/Insta_files/Insta.cpp@26:c60becf3f840, 2019-04-30 (annotated)
- Committer:
- yfkwok
- Date:
- Tue Apr 30 09:40:35 2019 +0000
- Revision:
- 26:c60becf3f840
- Parent:
- 19:903d67bb0dea
- Child:
- 33:f7ec806e14b6
30/04/2019 - Documentation comments for main.cpp
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yfkwok | 14:abe64fe0b6a5 | 1 | #include "Insta.h" |
yfkwok | 14:abe64fe0b6a5 | 2 | |
yfkwok | 14:abe64fe0b6a5 | 3 | Insta::Insta() |
yfkwok | 14:abe64fe0b6a5 | 4 | { |
yfkwok | 14:abe64fe0b6a5 | 5 | |
yfkwok | 14:abe64fe0b6a5 | 6 | } |
yfkwok | 14:abe64fe0b6a5 | 7 | |
yfkwok | 14:abe64fe0b6a5 | 8 | Insta::~Insta() |
yfkwok | 14:abe64fe0b6a5 | 9 | { |
yfkwok | 14:abe64fe0b6a5 | 10 | |
yfkwok | 14:abe64fe0b6a5 | 11 | } |
yfkwok | 14:abe64fe0b6a5 | 12 | |
yfkwok | 14:abe64fe0b6a5 | 13 | void Insta::init(int speed) |
yfkwok | 14:abe64fe0b6a5 | 14 | { |
yfkwok | 19:903d67bb0dea | 15 | int position = rand() % 8; // randomise initial direction and position. |
yfkwok | 14:abe64fe0b6a5 | 16 | |
yfkwok | 19:903d67bb0dea | 17 | // 8 possibilities. Get random modulo and set velocities and position accordingly |
yfkwok | 14:abe64fe0b6a5 | 18 | if (position == 0) { |
yfkwok | 26:c60becf3f840 | 19 | _x = -12; |
yfkwok | 14:abe64fe0b6a5 | 20 | _y = 12; |
yfkwok | 14:abe64fe0b6a5 | 21 | _velocity.x = speed * 2; |
yfkwok | 14:abe64fe0b6a5 | 22 | _velocity.y = speed; |
yfkwok | 14:abe64fe0b6a5 | 23 | } else if (position == 1) { |
yfkwok | 14:abe64fe0b6a5 | 24 | _x = 20; |
yfkwok | 26:c60becf3f840 | 25 | _y = -12; |
yfkwok | 14:abe64fe0b6a5 | 26 | _velocity.x = speed * 2; |
yfkwok | 14:abe64fe0b6a5 | 27 | _velocity.y = speed *2; |
yfkwok | 14:abe64fe0b6a5 | 28 | } else if (position == 2) { |
yfkwok | 14:abe64fe0b6a5 | 29 | _x = 60; |
yfkwok | 26:c60becf3f840 | 30 | _y = -12; |
yfkwok | 14:abe64fe0b6a5 | 31 | _velocity.x = -speed * 2; |
yfkwok | 14:abe64fe0b6a5 | 32 | _velocity.y = speed *2; |
yfkwok | 14:abe64fe0b6a5 | 33 | } else if (position == 3) { |
yfkwok | 26:c60becf3f840 | 34 | _x = 84; |
yfkwok | 14:abe64fe0b6a5 | 35 | _y = 14; |
yfkwok | 14:abe64fe0b6a5 | 36 | _velocity.x = -speed * 3; |
yfkwok | 14:abe64fe0b6a5 | 37 | _velocity.y = speed; |
yfkwok | 14:abe64fe0b6a5 | 38 | } else if (position == 4) { |
yfkwok | 26:c60becf3f840 | 39 | _x = 84; |
yfkwok | 14:abe64fe0b6a5 | 40 | _y = 35; |
yfkwok | 14:abe64fe0b6a5 | 41 | _velocity.x = -speed * 3; |
yfkwok | 14:abe64fe0b6a5 | 42 | _velocity.y = -speed *2; |
yfkwok | 14:abe64fe0b6a5 | 43 | } else if (position == 5) { |
yfkwok | 14:abe64fe0b6a5 | 44 | _x = 60; |
yfkwok | 26:c60becf3f840 | 45 | _y = 48; |
yfkwok | 14:abe64fe0b6a5 | 46 | _velocity.x = -speed * 2; |
yfkwok | 14:abe64fe0b6a5 | 47 | _velocity.y = -speed *2; |
yfkwok | 14:abe64fe0b6a5 | 48 | } else if (position == 6) { |
yfkwok | 14:abe64fe0b6a5 | 49 | _x = 20; |
yfkwok | 26:c60becf3f840 | 50 | _y = 48; |
yfkwok | 14:abe64fe0b6a5 | 51 | _velocity.x = speed * 2; |
yfkwok | 14:abe64fe0b6a5 | 52 | _velocity.y = -speed *2; |
yfkwok | 14:abe64fe0b6a5 | 53 | } else if (position == 7) { |
yfkwok | 26:c60becf3f840 | 54 | _x = -12; |
yfkwok | 14:abe64fe0b6a5 | 55 | _y = 30; |
yfkwok | 14:abe64fe0b6a5 | 56 | _velocity.x = speed * 3; |
yfkwok | 14:abe64fe0b6a5 | 57 | _velocity.y = -speed *2; |
yfkwok | 14:abe64fe0b6a5 | 58 | } |
yfkwok | 14:abe64fe0b6a5 | 59 | } |
yfkwok | 14:abe64fe0b6a5 | 60 | |
yfkwok | 14:abe64fe0b6a5 | 61 | void Insta::draw(N5110 &lcd) |
yfkwok | 14:abe64fe0b6a5 | 62 | { |
yfkwok | 14:abe64fe0b6a5 | 63 | int Insta_data[12][12] = { |
yfkwok | 14:abe64fe0b6a5 | 64 | {0,0,1,1,1,1,1,1,1,1,0,0}, |
yfkwok | 14:abe64fe0b6a5 | 65 | {0,1,1,1,1,1,1,1,1,1,1,0}, |
yfkwok | 14:abe64fe0b6a5 | 66 | {1,1,1,0,0,0,0,0,0,1,1,1}, |
yfkwok | 14:abe64fe0b6a5 | 67 | {1,1,0,1,1,1,1,1,0,0,1,1}, |
yfkwok | 14:abe64fe0b6a5 | 68 | {1,1,0,1,1,0,0,1,1,0,1,1}, |
yfkwok | 14:abe64fe0b6a5 | 69 | {1,1,0,1,0,1,1,0,1,0,1,1}, |
yfkwok | 14:abe64fe0b6a5 | 70 | {1,1,0,1,0,1,1,0,1,0,1,1}, |
yfkwok | 14:abe64fe0b6a5 | 71 | {1,1,0,1,1,0,0,1,1,0,1,1}, |
yfkwok | 14:abe64fe0b6a5 | 72 | {1,1,0,1,1,1,1,1,1,0,1,1}, |
yfkwok | 14:abe64fe0b6a5 | 73 | {1,1,1,0,0,0,0,0,0,1,1,1}, |
yfkwok | 14:abe64fe0b6a5 | 74 | {0,1,1,1,1,1,1,1,1,1,1,0}, |
yfkwok | 14:abe64fe0b6a5 | 75 | {0,0,1,1,1,1,1,1,1,1,0,0}, |
yfkwok | 14:abe64fe0b6a5 | 76 | }; |
yfkwok | 14:abe64fe0b6a5 | 77 | |
yfkwok | 14:abe64fe0b6a5 | 78 | lcd.drawSprite(_x, _y, 12, 12, (int*)Insta_data); |
yfkwok | 14:abe64fe0b6a5 | 79 | } |
yfkwok | 14:abe64fe0b6a5 | 80 | |
yfkwok | 14:abe64fe0b6a5 | 81 | void Insta::update() |
yfkwok | 14:abe64fe0b6a5 | 82 | { |
yfkwok | 14:abe64fe0b6a5 | 83 | _x += _velocity.x; |
yfkwok | 14:abe64fe0b6a5 | 84 | _y += _velocity.y; |
yfkwok | 14:abe64fe0b6a5 | 85 | } |
yfkwok | 14:abe64fe0b6a5 | 86 | |
yfkwok | 14:abe64fe0b6a5 | 87 | void Insta::set_velocity(Vector2D v) |
yfkwok | 14:abe64fe0b6a5 | 88 | { |
yfkwok | 14:abe64fe0b6a5 | 89 | _velocity.x = v.x; |
yfkwok | 14:abe64fe0b6a5 | 90 | _velocity.y = v.y; |
yfkwok | 14:abe64fe0b6a5 | 91 | } |
yfkwok | 14:abe64fe0b6a5 | 92 | |
yfkwok | 14:abe64fe0b6a5 | 93 | Vector2D Insta::get_velocity() |
yfkwok | 14:abe64fe0b6a5 | 94 | { |
yfkwok | 14:abe64fe0b6a5 | 95 | Vector2D v = {_velocity.x,_velocity.y}; |
yfkwok | 14:abe64fe0b6a5 | 96 | return v; |
yfkwok | 14:abe64fe0b6a5 | 97 | } |
yfkwok | 14:abe64fe0b6a5 | 98 | |
yfkwok | 14:abe64fe0b6a5 | 99 | Vector2D Insta::get_pos() |
yfkwok | 14:abe64fe0b6a5 | 100 | { |
yfkwok | 14:abe64fe0b6a5 | 101 | Vector2D p = {_x,_y}; |
yfkwok | 14:abe64fe0b6a5 | 102 | return p; |
yfkwok | 14:abe64fe0b6a5 | 103 | } |
yfkwok | 14:abe64fe0b6a5 | 104 | |
yfkwok | 14:abe64fe0b6a5 | 105 | void Insta::set_pos(Vector2D p) |
yfkwok | 14:abe64fe0b6a5 | 106 | { |
yfkwok | 14:abe64fe0b6a5 | 107 | _x = p.x; |
yfkwok | 14:abe64fe0b6a5 | 108 | _y = p.y; |
yfkwok | 14:abe64fe0b6a5 | 109 | } |