Yang Zhenye 201199680

Dependencies:   mbed

Committer:
yangzhenye
Date:
Thu May 14 14:13:03 2020 +0000
Revision:
5:fcad75e9b9e1
Parent:
2:baca89f466bc
first

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yangzhenye 5:fcad75e9b9e1 1 /*
yangzhenye 5:fcad75e9b9e1 2 ELEC2645 Embedded Systems Project
yangzhenye 5:fcad75e9b9e1 3 School of Electronic & Electrical Engineering
yangzhenye 5:fcad75e9b9e1 4 University of Leeds
yangzhenye 5:fcad75e9b9e1 5 2019/20
yangzhenye 5:fcad75e9b9e1 6
yangzhenye 5:fcad75e9b9e1 7 Name:Yang Zhenye
yangzhenye 5:fcad75e9b9e1 8 Username:el17zy
yangzhenye 5:fcad75e9b9e1 9 Student ID Number:201199680
yangzhenye 5:fcad75e9b9e1 10 Date:2020/5/12
yangzhenye 5:fcad75e9b9e1 11 */
yangzhenye 5:fcad75e9b9e1 12
yangzhenye 1:74f525027af6 13 #include "Ball.h"
yangzhenye 1:74f525027af6 14
yangzhenye 1:74f525027af6 15 Ball::Ball()
yangzhenye 1:74f525027af6 16 {
yangzhenye 1:74f525027af6 17
yangzhenye 1:74f525027af6 18 }
yangzhenye 1:74f525027af6 19
yangzhenye 1:74f525027af6 20 Ball::~Ball()
yangzhenye 5:fcad75e9b9e1 21 {
yangzhenye 1:74f525027af6 22
yangzhenye 1:74f525027af6 23 }
yangzhenye 1:74f525027af6 24
yangzhenye 1:74f525027af6 25 void Ball::init(int size,int speed)
yangzhenye 1:74f525027af6 26 {
yangzhenye 1:74f525027af6 27 _size = size;
yangzhenye 1:74f525027af6 28
yangzhenye 1:74f525027af6 29 _x = WIDTH/2 - _size/2;
yangzhenye 1:74f525027af6 30 _y = HEIGHT/2 - _size/2;
yangzhenye 1:74f525027af6 31
yangzhenye 1:74f525027af6 32 srand(time(NULL));
yangzhenye 1:74f525027af6 33 int direction = rand() % 4; // randomise initial direction.
yangzhenye 1:74f525027af6 34
yangzhenye 1:74f525027af6 35 // 4 possibilities. Get random modulo and set velocities accordingly
yangzhenye 1:74f525027af6 36 if (direction == 0) {
yangzhenye 1:74f525027af6 37 _velocity.x = speed;
yangzhenye 1:74f525027af6 38 _velocity.y = speed;
yangzhenye 1:74f525027af6 39 } else if (direction == 1) {
yangzhenye 1:74f525027af6 40 _velocity.x = speed;
yangzhenye 1:74f525027af6 41 _velocity.y = -speed;
yangzhenye 1:74f525027af6 42 } else if (direction == 2) {
yangzhenye 1:74f525027af6 43 _velocity.x = speed;
yangzhenye 1:74f525027af6 44 _velocity.y = speed;
yangzhenye 1:74f525027af6 45 } else {
yangzhenye 1:74f525027af6 46 _velocity.x = -speed;
yangzhenye 1:74f525027af6 47 _velocity.y = -speed;
yangzhenye 1:74f525027af6 48 }
yangzhenye 1:74f525027af6 49 }
yangzhenye 1:74f525027af6 50
yangzhenye 1:74f525027af6 51 void Ball::draw(N5110 &lcd)
yangzhenye 1:74f525027af6 52 {
yangzhenye 1:74f525027af6 53 lcd.drawRect(_x,_y,_size,_size,FILL_BLACK);
yangzhenye 1:74f525027af6 54 }
yangzhenye 1:74f525027af6 55
yangzhenye 1:74f525027af6 56 void Ball::update()
yangzhenye 1:74f525027af6 57 {
yangzhenye 1:74f525027af6 58 _x += _velocity.x;
yangzhenye 1:74f525027af6 59 _y += _velocity.y;
yangzhenye 1:74f525027af6 60 }
yangzhenye 1:74f525027af6 61
yangzhenye 1:74f525027af6 62 void Ball::set_velocity(Vector2D v)
yangzhenye 1:74f525027af6 63 {
yangzhenye 1:74f525027af6 64 _velocity.x = v.x;
yangzhenye 1:74f525027af6 65 _velocity.y = v.y;
yangzhenye 1:74f525027af6 66 }
yangzhenye 1:74f525027af6 67
yangzhenye 1:74f525027af6 68 Vector2D Ball::get_velocity()
yangzhenye 1:74f525027af6 69 {
yangzhenye 1:74f525027af6 70 Vector2D v = {_velocity.x,_velocity.y};
yangzhenye 1:74f525027af6 71 return v;
yangzhenye 1:74f525027af6 72 }
yangzhenye 1:74f525027af6 73
yangzhenye 1:74f525027af6 74 Vector2D Ball::get_pos()
yangzhenye 1:74f525027af6 75 {
yangzhenye 1:74f525027af6 76 Vector2D p = {_x,_y};
yangzhenye 1:74f525027af6 77 return p;
yangzhenye 1:74f525027af6 78 }
yangzhenye 1:74f525027af6 79
yangzhenye 1:74f525027af6 80 void Ball::set_pos(Vector2D p)
yangzhenye 1:74f525027af6 81 {
yangzhenye 1:74f525027af6 82 _x = p.x;
yangzhenye 1:74f525027af6 83 _y = p.y;
yangzhenye 1:74f525027af6 84 }