Player Library
Player.cpp@10:43b537436eb5, 2017-05-03 (annotated)
- Committer:
- ll14c4p
- Date:
- Wed May 03 13:32:14 2017 +0000
- Revision:
- 10:43b537436eb5
- Parent:
- 9:002ea306683d
- Child:
- 11:1a8dc3242e13
Updated Sprite
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ll14c4p | 1:933b7aa73bbc | 1 | #include "Player.h" |
ll14c4p | 0:a88279bdf8c0 | 2 | |
ll14c4p | 0:a88279bdf8c0 | 3 | Player::Player() |
ll14c4p | 0:a88279bdf8c0 | 4 | { |
ll14c4p | 0:a88279bdf8c0 | 5 | |
ll14c4p | 0:a88279bdf8c0 | 6 | } |
ll14c4p | 0:a88279bdf8c0 | 7 | |
ll14c4p | 0:a88279bdf8c0 | 8 | Player::~Player() |
ll14c4p | 0:a88279bdf8c0 | 9 | { |
ll14c4p | 0:a88279bdf8c0 | 10 | |
ll14c4p | 0:a88279bdf8c0 | 11 | } |
ll14c4p | 0:a88279bdf8c0 | 12 | |
ll14c4p | 10:43b537436eb5 | 13 | int sprite[5][6] = { |
ll14c4p | 10:43b537436eb5 | 14 | {0,1,0,0,1,0}, |
ll14c4p | 10:43b537436eb5 | 15 | {1,1,0,0,1,1}, |
ll14c4p | 10:43b537436eb5 | 16 | {1,1,1,1,1,1}, |
ll14c4p | 10:43b537436eb5 | 17 | {1,1,1,1,1,1}, |
ll14c4p | 10:43b537436eb5 | 18 | {0,1,1,1,1,0}, |
ll14c4p | 1:933b7aa73bbc | 19 | }; |
ll14c4p | 1:933b7aa73bbc | 20 | |
ll14c4p | 6:79a9c37a0b80 | 21 | int m = 0; |
ll14c4p | 6:79a9c37a0b80 | 22 | |
ll14c4p | 6:79a9c37a0b80 | 23 | |
ll14c4p | 7:6987a8d682a3 | 24 | void Player::init() //Delete Int |
ll14c4p | 1:933b7aa73bbc | 25 | { |
ll14c4p | 6:79a9c37a0b80 | 26 | |
ll14c4p | 10:43b537436eb5 | 27 | _speed = 0.05;// default speed |
ll14c4p | 1:933b7aa73bbc | 28 | |
ll14c4p | 1:933b7aa73bbc | 29 | } |
ll14c4p | 1:933b7aa73bbc | 30 | |
ll14c4p | 0:a88279bdf8c0 | 31 | |
ll14c4p | 0:a88279bdf8c0 | 32 | void Player::draw(N5110 &lcd) |
ll14c4p | 6:79a9c37a0b80 | 33 | { |
ll14c4p | 6:79a9c37a0b80 | 34 | if(m == 0){ |
ll14c4p | 9:002ea306683d | 35 | _x = WIDTH/2 -1; // Middle of screen // Change these values to change starting position |
ll14c4p | 9:002ea306683d | 36 | _y = HEIGHT/2 -1; // Near bottom of screen |
ll14c4p | 6:79a9c37a0b80 | 37 | m = m+1; |
ll14c4p | 6:79a9c37a0b80 | 38 | } |
ll14c4p | 8:4509ae53ca30 | 39 | //printf("SPRITE %d %d \n", _x, _y); |
ll14c4p | 10:43b537436eb5 | 40 | lcd.drawSprite(_x,_y,5,6,(int *)sprite); |
ll14c4p | 1:933b7aa73bbc | 41 | } |
ll14c4p | 1:933b7aa73bbc | 42 | |
ll14c4p | 1:933b7aa73bbc | 43 | |
ll14c4p | 1:933b7aa73bbc | 44 | |
ll14c4p | 4:6d5ea29f83b0 | 45 | Vector2D Player::get_pos() |
ll14c4p | 4:6d5ea29f83b0 | 46 | { |
ll14c4p | 8:4509ae53ca30 | 47 | Vector2D playerpos = {_x,_y}; |
ll14c4p | 9:002ea306683d | 48 | //printf("playerpos from player = %f %f \n", playerpos.x, playerpos.y); |
ll14c4p | 8:4509ae53ca30 | 49 | return playerpos; |
ll14c4p | 4:6d5ea29f83b0 | 50 | } |
ll14c4p | 4:6d5ea29f83b0 | 51 | |
ll14c4p | 4:6d5ea29f83b0 | 52 | |
ll14c4p | 4:6d5ea29f83b0 | 53 | |
ll14c4p | 4:6d5ea29f83b0 | 54 | |
ll14c4p | 4:6d5ea29f83b0 | 55 | |
ll14c4p | 1:933b7aa73bbc | 56 | |
ll14c4p | 1:933b7aa73bbc | 57 | |
ll14c4p | 1:933b7aa73bbc | 58 | void Player::update(Direction d,float mag) |
ll14c4p | 1:933b7aa73bbc | 59 | { |
ll14c4p | 10:43b537436eb5 | 60 | _speed = int(mag*5.0f); // scale is arbitrary, could be changed in future |
ll14c4p | 1:933b7aa73bbc | 61 | |
ll14c4p | 1:933b7aa73bbc | 62 | |
ll14c4p | 1:933b7aa73bbc | 63 | // update y value depending on direction of movement |
ll14c4p | 1:933b7aa73bbc | 64 | // North is decrement as origin is at the top-left so decreasing moves up |
ll14c4p | 1:933b7aa73bbc | 65 | if (d == N) { |
ll14c4p | 1:933b7aa73bbc | 66 | _y-=_speed; |
ll14c4p | 1:933b7aa73bbc | 67 | } |
ll14c4p | 1:933b7aa73bbc | 68 | if (d == S) { |
ll14c4p | 1:933b7aa73bbc | 69 | _y+=_speed; |
ll14c4p | 1:933b7aa73bbc | 70 | } |
ll14c4p | 1:933b7aa73bbc | 71 | if (d == E) { |
ll14c4p | 1:933b7aa73bbc | 72 | _x+=_speed; |
ll14c4p | 1:933b7aa73bbc | 73 | } |
ll14c4p | 2:108a1fc56c4c | 74 | if (d == W) { |
ll14c4p | 1:933b7aa73bbc | 75 | _x-=_speed; |
ll14c4p | 1:933b7aa73bbc | 76 | } |
ll14c4p | 2:108a1fc56c4c | 77 | if (d == NE) { |
ll14c4p | 2:108a1fc56c4c | 78 | _y-=_speed/2; |
ll14c4p | 2:108a1fc56c4c | 79 | _x+=_speed/2; |
ll14c4p | 2:108a1fc56c4c | 80 | } |
ll14c4p | 2:108a1fc56c4c | 81 | if (d == NW) { |
ll14c4p | 2:108a1fc56c4c | 82 | _y-=_speed/2; |
ll14c4p | 2:108a1fc56c4c | 83 | _x-=_speed/2; |
ll14c4p | 2:108a1fc56c4c | 84 | } |
ll14c4p | 2:108a1fc56c4c | 85 | if (d == SE) { |
ll14c4p | 2:108a1fc56c4c | 86 | _y+=_speed/2; |
ll14c4p | 2:108a1fc56c4c | 87 | _x+=_speed/2; |
ll14c4p | 2:108a1fc56c4c | 88 | } |
ll14c4p | 2:108a1fc56c4c | 89 | if (d == SW) { |
ll14c4p | 2:108a1fc56c4c | 90 | _y+=_speed/2; |
ll14c4p | 2:108a1fc56c4c | 91 | _x-=_speed/2; |
ll14c4p | 2:108a1fc56c4c | 92 | } |
ll14c4p | 2:108a1fc56c4c | 93 | |
ll14c4p | 1:933b7aa73bbc | 94 | |
ll14c4p | 1:933b7aa73bbc | 95 | // check the y origin to ensure that the paddle doesn't go off screen |
ll14c4p | 3:60a09496c839 | 96 | if (_y <= 0) { |
ll14c4p | 3:60a09496c839 | 97 | _y = 0; |
ll14c4p | 1:933b7aa73bbc | 98 | } |
ll14c4p | 3:60a09496c839 | 99 | if (_x <= 0) { |
ll14c4p | 3:60a09496c839 | 100 | _x = 0; |
ll14c4p | 1:933b7aa73bbc | 101 | } |
ll14c4p | 10:43b537436eb5 | 102 | if (_x > 78) { |
ll14c4p | 10:43b537436eb5 | 103 | _x = 78; |
ll14c4p | 1:933b7aa73bbc | 104 | } |
ll14c4p | 10:43b537436eb5 | 105 | if (_y > 43) { |
ll14c4p | 10:43b537436eb5 | 106 | _y = 43; |
ll14c4p | 1:933b7aa73bbc | 107 | } |
ll14c4p | 1:933b7aa73bbc | 108 | |
ll14c4p | 10:43b537436eb5 | 109 | } |
ll14c4p | 10:43b537436eb5 | 110 |