ZIYI CHEN ml17z4c 201214999

Dependencies:   mbed

Committer:
ziyi11
Date:
Wed May 08 20:48:33 2019 +0000
Revision:
9:a8b2086a46e5
Child:
11:1812f04382fa
try1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ziyi11 9:a8b2086a46e5 1 #include "Eng.h"
ziyi11 9:a8b2086a46e5 2
ziyi11 9:a8b2086a46e5 3 Eng::Eng()
ziyi11 9:a8b2086a46e5 4 {
ziyi11 9:a8b2086a46e5 5
ziyi11 9:a8b2086a46e5 6 }
ziyi11 9:a8b2086a46e5 7
ziyi11 9:a8b2086a46e5 8 Eng::~Eng()
ziyi11 9:a8b2086a46e5 9 {
ziyi11 9:a8b2086a46e5 10
ziyi11 9:a8b2086a46e5 11 }
ziyi11 9:a8b2086a46e5 12
ziyi11 9:a8b2086a46e5 13 void Eng::init()
ziyi11 9:a8b2086a46e5 14 {
ziyi11 9:a8b2086a46e5 15 dSnake.init(5,10); //she de chu shi zuo biao
ziyi11 9:a8b2086a46e5 16 _noodles.init(20,20);
ziyi11 9:a8b2086a46e5 17 _gameOver = false;
ziyi11 9:a8b2086a46e5 18
ziyi11 9:a8b2086a46e5 19 }
ziyi11 9:a8b2086a46e5 20
ziyi11 9:a8b2086a46e5 21 void Eng::loadMap()
ziyi11 9:a8b2086a46e5 22 {
ziyi11 9:a8b2086a46e5 23
ziyi11 9:a8b2086a46e5 24 memset(snake1, 0, sizeof(snake1));
ziyi11 9:a8b2086a46e5 25
ziyi11 9:a8b2086a46e5 26 }
ziyi11 9:a8b2086a46e5 27
ziyi11 9:a8b2086a46e5 28 void Eng::userinput(Gamepad &pad)
ziyi11 9:a8b2086a46e5 29 {
ziyi11 9:a8b2086a46e5 30 _d = pad.get_direction();
ziyi11 9:a8b2086a46e5 31 }
ziyi11 9:a8b2086a46e5 32
ziyi11 9:a8b2086a46e5 33 void Eng::draw(N5110 &lcd)
ziyi11 9:a8b2086a46e5 34 {
ziyi11 9:a8b2086a46e5 35 lcd.drawRect(0,0,84,48,FILL_TRANSPARENT);
ziyi11 9:a8b2086a46e5 36 //
ziyi11 9:a8b2086a46e5 37 for (int i = 0; i < 84; i++) {
ziyi11 9:a8b2086a46e5 38 for (int j = 0; j < 48; j++) {
ziyi11 9:a8b2086a46e5 39 if (snake1[j][i] != 0) {
ziyi11 9:a8b2086a46e5 40 lcd.drawRect((2 * j) + 2,(2 * i) + 2,2,2,FILL_BLACK);
ziyi11 9:a8b2086a46e5 41 }
ziyi11 9:a8b2086a46e5 42 }
ziyi11 9:a8b2086a46e5 43 }
ziyi11 9:a8b2086a46e5 44 }
ziyi11 9:a8b2086a46e5 45
ziyi11 9:a8b2086a46e5 46 void Eng::score(N5110 &lcd)
ziyi11 9:a8b2086a46e5 47 {
ziyi11 9:a8b2086a46e5 48
ziyi11 9:a8b2086a46e5 49 lcd.clear();
ziyi11 9:a8b2086a46e5 50 lcd.printString("Score ",0,0);
ziyi11 9:a8b2086a46e5 51 char buffer[14];
ziyi11 9:a8b2086a46e5 52 int _score = (dSnake.getLength() - 5);
ziyi11 9:a8b2086a46e5 53
ziyi11 9:a8b2086a46e5 54 sprintf(buffer,"%3d",_score);
ziyi11 9:a8b2086a46e5 55 lcd.printString(buffer,0,1);
ziyi11 9:a8b2086a46e5 56
ziyi11 9:a8b2086a46e5 57
ziyi11 9:a8b2086a46e5 58
ziyi11 9:a8b2086a46e5 59 lcd.refresh();
ziyi11 9:a8b2086a46e5 60
ziyi11 9:a8b2086a46e5 61 wait(5.0);
ziyi11 9:a8b2086a46e5 62 }
ziyi11 9:a8b2086a46e5 63
ziyi11 9:a8b2086a46e5 64 void Eng::update(Gamepad &pad)
ziyi11 9:a8b2086a46e5 65 {
ziyi11 9:a8b2086a46e5 66 loadMap();
ziyi11 9:a8b2086a46e5 67
ziyi11 9:a8b2086a46e5 68 dSnake.update(_d);
ziyi11 9:a8b2086a46e5 69
ziyi11 9:a8b2086a46e5 70 dead();
ziyi11 9:a8b2086a46e5 71
ziyi11 9:a8b2086a46e5 72 snake1[_noodles.xcoordinate()][_noodles.ycoordinate()] = 2;
ziyi11 9:a8b2086a46e5 73
ziyi11 9:a8b2086a46e5 74 if (checkFood()) {
ziyi11 9:a8b2086a46e5 75
ziyi11 9:a8b2086a46e5 76 pad.tone(750.0,0.1);
ziyi11 9:a8b2086a46e5 77
ziyi11 9:a8b2086a46e5 78 growSnake();
ziyi11 9:a8b2086a46e5 79
ziyi11 9:a8b2086a46e5 80 bool empty = false;
ziyi11 9:a8b2086a46e5 81
ziyi11 9:a8b2086a46e5 82 while (!empty) {
ziyi11 9:a8b2086a46e5 83
ziyi11 9:a8b2086a46e5 84 _noodles.random();
ziyi11 9:a8b2086a46e5 85
ziyi11 9:a8b2086a46e5 86 if ( snake1[_noodles.xcoordinate()][_noodles.ycoordinate()] == 0) {
ziyi11 9:a8b2086a46e5 87
ziyi11 9:a8b2086a46e5 88 empty = true;
ziyi11 9:a8b2086a46e5 89 }
ziyi11 9:a8b2086a46e5 90 }
ziyi11 9:a8b2086a46e5 91 }
ziyi11 9:a8b2086a46e5 92 }
ziyi11 9:a8b2086a46e5 93
ziyi11 9:a8b2086a46e5 94 void Eng::dead()
ziyi11 9:a8b2086a46e5 95 {
ziyi11 9:a8b2086a46e5 96
ziyi11 9:a8b2086a46e5 97 int _l =dSnake.getLength();
ziyi11 9:a8b2086a46e5 98
ziyi11 9:a8b2086a46e5 99 for (int z = 0; z < _l; z++) {
ziyi11 9:a8b2086a46e5 100
ziyi11 9:a8b2086a46e5 101 if (dSnake.xcoordinate(z) > 44 || dSnake.xcoordinate(z) < 0) {
ziyi11 9:a8b2086a46e5 102 _gameOver = true;
ziyi11 9:a8b2086a46e5 103 } else if (dSnake.ycoordinate(z) > 22 || dSnake.ycoordinate(z) < 0) {
ziyi11 9:a8b2086a46e5 104 _gameOver = true;
ziyi11 9:a8b2086a46e5 105 }
ziyi11 9:a8b2086a46e5 106
ziyi11 9:a8b2086a46e5 107 if (snake1[dSnake.xcoordinate(z)][dSnake.ycoordinate(z)] != 1) {
ziyi11 9:a8b2086a46e5 108 snake1[dSnake.xcoordinate(z)][dSnake.ycoordinate(z)] = 1;
ziyi11 9:a8b2086a46e5 109 } else {
ziyi11 9:a8b2086a46e5 110 _gameOver = true;
ziyi11 9:a8b2086a46e5 111 }
ziyi11 9:a8b2086a46e5 112 }
ziyi11 9:a8b2086a46e5 113 }
ziyi11 9:a8b2086a46e5 114
ziyi11 9:a8b2086a46e5 115 bool Eng::checkFood()
ziyi11 9:a8b2086a46e5 116 {
ziyi11 9:a8b2086a46e5 117 if (snake1[dSnake.xcoordinate(0)][dSnake.ycoordinate(0)] == 2) {
ziyi11 9:a8b2086a46e5 118 return true;
ziyi11 9:a8b2086a46e5 119 } else {
ziyi11 9:a8b2086a46e5 120 return false;
ziyi11 9:a8b2086a46e5 121 }
ziyi11 9:a8b2086a46e5 122 }
ziyi11 9:a8b2086a46e5 123
ziyi11 9:a8b2086a46e5 124 void Eng::growSnake()
ziyi11 9:a8b2086a46e5 125 {
ziyi11 9:a8b2086a46e5 126 if (dSnake.getLength()<4032) {
ziyi11 9:a8b2086a46e5 127
ziyi11 9:a8b2086a46e5 128 dSnake.grow();
ziyi11 9:a8b2086a46e5 129
ziyi11 9:a8b2086a46e5 130 }
ziyi11 9:a8b2086a46e5 131 }
ziyi11 9:a8b2086a46e5 132
ziyi11 9:a8b2086a46e5 133 float Eng::getScore()
ziyi11 9:a8b2086a46e5 134 {
ziyi11 9:a8b2086a46e5 135 return _score;
ziyi11 9:a8b2086a46e5 136 }
ziyi11 9:a8b2086a46e5 137
ziyi11 9:a8b2086a46e5 138
ziyi11 9:a8b2086a46e5 139 bool Eng::getGameOver()
ziyi11 9:a8b2086a46e5 140 {
ziyi11 9:a8b2086a46e5 141 return _gameOver;
ziyi11 9:a8b2086a46e5 142 }