XJEL2645 (18/19) / Mbed 2 deprecated 2645_Mygame

Dependencies:   mbed Gamepad N5110

Committer:
694617778
Date:
Sat Apr 20 08:15:10 2019 +0000
Revision:
9:18b059e5abb9
Parent:
7:d61290faf602
Child:
10:68076e3dcc33
ffffffffffffffff

Who changed what in which revision?

UserRevisionLine numberNew contents of line
694617778 0:b67614a0c4cf 1 #include "snake.h"
694617778 0:b67614a0c4cf 2
694617778 0:b67614a0c4cf 3 // nothing doing in the constructor and destructor
694617778 0:b67614a0c4cf 4 snake::snake()
694617778 0:b67614a0c4cf 5 {
694617778 0:b67614a0c4cf 6
694617778 0:b67614a0c4cf 7 }
694617778 0:b67614a0c4cf 8
694617778 0:b67614a0c4cf 9 snake::~snake()
694617778 0:b67614a0c4cf 10 {
694617778 0:b67614a0c4cf 11
694617778 0:b67614a0c4cf 12 }
694617778 0:b67614a0c4cf 13
694617778 1:b49c36604125 14 void snake::init(int size,int length)
694617778 0:b67614a0c4cf 15 {
694617778 0:b67614a0c4cf 16 _size = size;
694617778 1:b49c36604125 17 _length = length;
694617778 6:feb6351e6f8e 18 for (int i = 0; i < 100;i++){
694617778 6:feb6351e6f8e 19 _x[i] = 0;
694617778 6:feb6351e6f8e 20 _y[i] = 0;
694617778 6:feb6351e6f8e 21 }
694617778 1:b49c36604125 22 _x[0] = WIDTH/2 - _size/2;
694617778 1:b49c36604125 23 _y[0] = HEIGHT/2 - _size/2;
694617778 0:b67614a0c4cf 24
694617778 2:934daa65f73d 25 _x[1] = _x[0] -size;
694617778 2:934daa65f73d 26 _y[1] = _y[0];
694617778 1:b49c36604125 27
694617778 2:934daa65f73d 28 _x[2] = _x[0] -2*size;
694617778 2:934daa65f73d 29 _y[2] = _y[0];
694617778 0:b67614a0c4cf 30
694617778 2:934daa65f73d 31 ball( _length);
694617778 0:b67614a0c4cf 32 _score = 0; // start score from zero
694617778 2:934daa65f73d 33 over = 0;
694617778 0:b67614a0c4cf 34 }
694617778 0:b67614a0c4cf 35
694617778 1:b49c36604125 36 int snake::get_length(){
694617778 1:b49c36604125 37 int d=_length;
694617778 1:b49c36604125 38 return d;
694617778 1:b49c36604125 39 }
694617778 1:b49c36604125 40
694617778 1:b49c36604125 41 void snake::update(int direction, int length)
694617778 1:b49c36604125 42 {
694617778 1:b49c36604125 43 for(int k = length - 1; k > 0; k--){
694617778 1:b49c36604125 44 _x[k] = _x[k-1];
694617778 1:b49c36604125 45 _y[k] = _y[k-1];
694617778 1:b49c36604125 46 }
694617778 1:b49c36604125 47 if(direction == 0){
694617778 2:934daa65f73d 48 _x[0] = _x[0] + _size;
694617778 1:b49c36604125 49 }else if(direction == 1){
694617778 2:934daa65f73d 50 _y[0] = _y[0] + _size;
694617778 1:b49c36604125 51 }else if(direction == 2){
694617778 2:934daa65f73d 52 _x[0] = _x[0] - _size;
694617778 1:b49c36604125 53 }else if(direction == 3){
694617778 2:934daa65f73d 54 _y[0] = _y[0] - _size;
694617778 1:b49c36604125 55 }
694617778 1:b49c36604125 56 }
694617778 1:b49c36604125 57
694617778 1:b49c36604125 58
694617778 1:b49c36604125 59 void snake::draw(N5110 &lcd, int length)
694617778 0:b67614a0c4cf 60 {
694617778 0:b67614a0c4cf 61
694617778 0:b67614a0c4cf 62 // draw snake in screen buffer.
694617778 1:b49c36604125 63 lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
694617778 2:934daa65f73d 64 lcd.drawRect(1,1,WIDTH-2,HEIGHT-2,FILL_TRANSPARENT);
694617778 1:b49c36604125 65 for (int i = 0; i < length; i++){
694617778 1:b49c36604125 66 lcd.drawRect(_x[i],_y[i],_size,_size,FILL_BLACK);
694617778 2:934daa65f73d 67 }
694617778 2:934daa65f73d 68
694617778 2:934daa65f73d 69 // draw ball in screen buffer.
694617778 2:934daa65f73d 70 lcd.drawCircle(ball_x,ball_y,1,FILL_BLACK);
694617778 2:934daa65f73d 71 }
694617778 2:934daa65f73d 72
694617778 2:934daa65f73d 73 void snake::ball(int length)
694617778 2:934daa65f73d 74 {
694617778 2:934daa65f73d 75 srand(time(NULL));
694617778 2:934daa65f73d 76 ball_x = 3 + rand() % (WIDTH - 6); // randomise initial position of ball.
694617778 2:934daa65f73d 77 ball_y = 3 + rand() % (HEIGHT - 6);
694617778 2:934daa65f73d 78 for (int i = 0; i < length; i++) {
694617778 9:18b059e5abb9 79 if(ball_x + _size >= _x[i] && ball_x - _size <= _x[i] && ball_y + _size >= _y[i] && ball_y - _size <= _y[i]){
694617778 7:d61290faf602 80 srand(time(NULL));
694617778 2:934daa65f73d 81 ball_x = 3 + rand() % (WIDTH - 6); // randomise initial position of ball.
694617778 2:934daa65f73d 82 ball_y = 3 + rand() % (HEIGHT - 6);
694617778 2:934daa65f73d 83 }
694617778 2:934daa65f73d 84 }
694617778 2:934daa65f73d 85
694617778 2:934daa65f73d 86 }
694617778 1:b49c36604125 87
694617778 2:934daa65f73d 88 void snake::check_eat(Gamepad &pad)
694617778 2:934daa65f73d 89 {
694617778 2:934daa65f73d 90 //
694617778 2:934daa65f73d 91 if (ball_x + _size >= _x[0] && ball_x - _size <= _x[0] && ball_y + _size >= _y[0] && ball_y - _size <= _y[0]) {
694617778 2:934daa65f73d 92 _length++;
694617778 2:934daa65f73d 93 _score++;
694617778 2:934daa65f73d 94 ball(_length);
694617778 2:934daa65f73d 95 pad.tone(1500.0,0.5);
694617778 2:934daa65f73d 96 pad.leds_on();
694617778 2:934daa65f73d 97 wait(0.05);
694617778 2:934daa65f73d 98 pad.leds_off();
694617778 2:934daa65f73d 99 }
694617778 2:934daa65f73d 100 }
694617778 2:934daa65f73d 101
694617778 2:934daa65f73d 102 void snake::check_over(N5110 &lcd)
694617778 2:934daa65f73d 103 {
694617778 2:934daa65f73d 104 if ( _x[0] >= WIDTH - 2 | _x[0] <= 2 | _y[0] >= HEIGHT - 2 | _y[0] <= 2){
694617778 2:934daa65f73d 105 over = 1;
694617778 2:934daa65f73d 106 }
694617778 2:934daa65f73d 107 for (int i = 1; i < _length; i++){
694617778 2:934daa65f73d 108 if(_x[0] == _x[i] && _y[0] == _y[i]){
694617778 2:934daa65f73d 109 over = 1;
694617778 2:934daa65f73d 110 }
694617778 2:934daa65f73d 111 }
694617778 0:b67614a0c4cf 112 }
694617778 0:b67614a0c4cf 113
694617778 0:b67614a0c4cf 114
694617778 0:b67614a0c4cf 115 int snake::get_score()
694617778 0:b67614a0c4cf 116 {
694617778 2:934daa65f73d 117 return _score;
694617778 0:b67614a0c4cf 118 }
694617778 0:b67614a0c4cf 119
694617778 1:b49c36604125 120