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
Ball.cpp
00001 /* 00002 ELEC2645 Embedded Systems Project 00003 School of Electronic & Electrical Engineering 00004 University of Leeds 00005 2019/20 00006 00007 Name:Yang Zhenye 00008 Username:el17zy 00009 Student ID Number:201199680 00010 Date:2020/5/12 00011 */ 00012 00013 #include "Ball.h" 00014 00015 Ball::Ball() 00016 { 00017 00018 } 00019 00020 Ball::~Ball() 00021 { 00022 00023 } 00024 00025 void Ball::init(int size,int speed) 00026 { 00027 _size = size; 00028 00029 _x = WIDTH/2 - _size/2; 00030 _y = HEIGHT/2 - _size/2; 00031 00032 srand(time(NULL)); 00033 int direction = rand() % 4; // randomise initial direction. 00034 00035 // 4 possibilities. Get random modulo and set velocities accordingly 00036 if (direction == 0) { 00037 _velocity.x = speed; 00038 _velocity.y = speed; 00039 } else if (direction == 1) { 00040 _velocity.x = speed; 00041 _velocity.y = -speed; 00042 } else if (direction == 2) { 00043 _velocity.x = speed; 00044 _velocity.y = speed; 00045 } else { 00046 _velocity.x = -speed; 00047 _velocity.y = -speed; 00048 } 00049 } 00050 00051 void Ball::draw(N5110 &lcd) 00052 { 00053 lcd.drawRect(_x,_y,_size,_size,FILL_BLACK); 00054 } 00055 00056 void Ball::update() 00057 { 00058 _x += _velocity.x; 00059 _y += _velocity.y; 00060 } 00061 00062 void Ball::set_velocity(Vector2D v) 00063 { 00064 _velocity.x = v.x; 00065 _velocity.y = v.y; 00066 } 00067 00068 Vector2D Ball::get_velocity() 00069 { 00070 Vector2D v = {_velocity.x,_velocity.y}; 00071 return v; 00072 } 00073 00074 Vector2D Ball::get_pos() 00075 { 00076 Vector2D p = {_x,_y}; 00077 return p; 00078 } 00079 00080 void Ball::set_pos(Vector2D p) 00081 { 00082 _x = p.x; 00083 _y = p.y; 00084 }
Generated on Tue Jul 12 2022 18:57:15 by
1.7.2