Player Library

Committer:
ll14c4p
Date:
Fri Apr 28 15:52:27 2017 +0000
Revision:
6:79a9c37a0b80
Parent:
5:cd9699b836c3
Child:
7:6987a8d682a3
Starting Position Working

Who changed what in which revision?

UserRevisionLine numberNew 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 1:933b7aa73bbc 13 int sprite[3][3] = {
ll14c4p 1:933b7aa73bbc 14 {1,0,1},
ll14c4p 4:6d5ea29f83b0 15 {0,1,0},
ll14c4p 4:6d5ea29f83b0 16 {1,0,1},
ll14c4p 1:933b7aa73bbc 17 };
ll14c4p 1:933b7aa73bbc 18
ll14c4p 6:79a9c37a0b80 19 int m = 0;
ll14c4p 6:79a9c37a0b80 20
ll14c4p 6:79a9c37a0b80 21
ll14c4p 1:933b7aa73bbc 22 void Player::init(int x, int y)
ll14c4p 1:933b7aa73bbc 23 {
ll14c4p 6:79a9c37a0b80 24
ll14c4p 3:60a09496c839 25 _speed = 0.3; // default speed
ll14c4p 1:933b7aa73bbc 26
ll14c4p 1:933b7aa73bbc 27 }
ll14c4p 1:933b7aa73bbc 28
ll14c4p 0:a88279bdf8c0 29
ll14c4p 0:a88279bdf8c0 30 void Player::draw(N5110 &lcd)
ll14c4p 6:79a9c37a0b80 31 {
ll14c4p 6:79a9c37a0b80 32 if(m == 0){
ll14c4p 6:79a9c37a0b80 33 _x = WIDTH/2; // Middle of screen // Change these values to change starting position
ll14c4p 6:79a9c37a0b80 34 _y = HEIGHT/2; // Near bottom of screen
ll14c4p 6:79a9c37a0b80 35 m = m+1;
ll14c4p 6:79a9c37a0b80 36 }
ll14c4p 6:79a9c37a0b80 37 printf("SPRITE %d %d \n", _x, _y);
ll14c4p 1:933b7aa73bbc 38 lcd.drawSprite(_x,_y,3,3,(int *)sprite);
ll14c4p 1:933b7aa73bbc 39 }
ll14c4p 1:933b7aa73bbc 40
ll14c4p 1:933b7aa73bbc 41
ll14c4p 1:933b7aa73bbc 42
ll14c4p 4:6d5ea29f83b0 43 Vector2D Player::get_pos()
ll14c4p 4:6d5ea29f83b0 44 {
ll14c4p 5:cd9699b836c3 45 Vector2D pos = {_x,_y};
ll14c4p 5:cd9699b836c3 46 return pos;
ll14c4p 4:6d5ea29f83b0 47 }
ll14c4p 4:6d5ea29f83b0 48
ll14c4p 4:6d5ea29f83b0 49
ll14c4p 4:6d5ea29f83b0 50
ll14c4p 4:6d5ea29f83b0 51
ll14c4p 4:6d5ea29f83b0 52
ll14c4p 1:933b7aa73bbc 53
ll14c4p 1:933b7aa73bbc 54
ll14c4p 1:933b7aa73bbc 55 void Player::update(Direction d,float mag)
ll14c4p 1:933b7aa73bbc 56 {
ll14c4p 1:933b7aa73bbc 57 _speed = int(mag*10.0f); // scale is arbitrary, could be changed in future
ll14c4p 1:933b7aa73bbc 58
ll14c4p 1:933b7aa73bbc 59
ll14c4p 1:933b7aa73bbc 60 // update y value depending on direction of movement
ll14c4p 1:933b7aa73bbc 61 // North is decrement as origin is at the top-left so decreasing moves up
ll14c4p 1:933b7aa73bbc 62 if (d == N) {
ll14c4p 1:933b7aa73bbc 63 _y-=_speed;
ll14c4p 1:933b7aa73bbc 64 }
ll14c4p 1:933b7aa73bbc 65 if (d == S) {
ll14c4p 1:933b7aa73bbc 66 _y+=_speed;
ll14c4p 1:933b7aa73bbc 67 }
ll14c4p 1:933b7aa73bbc 68 if (d == E) {
ll14c4p 1:933b7aa73bbc 69 _x+=_speed;
ll14c4p 1:933b7aa73bbc 70 }
ll14c4p 2:108a1fc56c4c 71 if (d == W) {
ll14c4p 1:933b7aa73bbc 72 _x-=_speed;
ll14c4p 1:933b7aa73bbc 73 }
ll14c4p 2:108a1fc56c4c 74 if (d == NE) {
ll14c4p 2:108a1fc56c4c 75 _y-=_speed/2;
ll14c4p 2:108a1fc56c4c 76 _x+=_speed/2;
ll14c4p 2:108a1fc56c4c 77 }
ll14c4p 2:108a1fc56c4c 78 if (d == NW) {
ll14c4p 2:108a1fc56c4c 79 _y-=_speed/2;
ll14c4p 2:108a1fc56c4c 80 _x-=_speed/2;
ll14c4p 2:108a1fc56c4c 81 }
ll14c4p 2:108a1fc56c4c 82 if (d == SE) {
ll14c4p 2:108a1fc56c4c 83 _y+=_speed/2;
ll14c4p 2:108a1fc56c4c 84 _x+=_speed/2;
ll14c4p 2:108a1fc56c4c 85 }
ll14c4p 2:108a1fc56c4c 86 if (d == SW) {
ll14c4p 2:108a1fc56c4c 87 _y+=_speed/2;
ll14c4p 2:108a1fc56c4c 88 _x-=_speed/2;
ll14c4p 2:108a1fc56c4c 89 }
ll14c4p 2:108a1fc56c4c 90
ll14c4p 1:933b7aa73bbc 91
ll14c4p 1:933b7aa73bbc 92 // check the y origin to ensure that the paddle doesn't go off screen
ll14c4p 3:60a09496c839 93 if (_y <= 0) {
ll14c4p 3:60a09496c839 94 _y = 0;
ll14c4p 1:933b7aa73bbc 95 }
ll14c4p 3:60a09496c839 96 if (_x <= 0) {
ll14c4p 3:60a09496c839 97 _x = 0;
ll14c4p 1:933b7aa73bbc 98 }
ll14c4p 1:933b7aa73bbc 99 if (_x > 81) {
ll14c4p 1:933b7aa73bbc 100 _x = 81;
ll14c4p 1:933b7aa73bbc 101 }
ll14c4p 1:933b7aa73bbc 102 if (_y > 45) {
ll14c4p 3:60a09496c839 103 _y = 45;
ll14c4p 1:933b7aa73bbc 104 }
ll14c4p 1:933b7aa73bbc 105
ll14c4p 0:a88279bdf8c0 106 }