![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Yunting Zou 201199716
Dependencies: mbed MotionSensor
viper/viper.h@3:4bd22344278d, 2020-05-14 (annotated)
- Committer:
- zhouyun123
- Date:
- Thu May 14 17:18:58 2020 +0000
- Revision:
- 3:4bd22344278d
- Parent:
- 2:a2f88b2d5da4
Final Submission. I have read and agreed with Statement of Academic Integrity.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
zhouyun123 | 0:047e14f53977 | 1 | #include <stdio.h> |
zhouyun123 | 0:047e14f53977 | 2 | #include <time.h> |
zhouyun123 | 0:047e14f53977 | 3 | #include "mbed.h" |
zhouyun123 | 0:047e14f53977 | 4 | #include "Gamepad.h" |
zhouyun123 | 0:047e14f53977 | 5 | #include "N5110.h" |
zhouyun123 | 0:047e14f53977 | 6 | #define length 80 |
zhouyun123 | 0:047e14f53977 | 7 | /** main Class |
zhouyun123 | 0:047e14f53977 | 8 | @author Zou yunting, University of Leeds |
zhouyun123 | 0:047e14f53977 | 9 | @brief used to set the lparameters of the snake and the food |
zhouyun123 | 0:047e14f53977 | 10 | @15th May 2020 |
zhouyun123 | 0:047e14f53977 | 11 | **/ |
zhouyun123 | 0:047e14f53977 | 12 | |
zhouyun123 | 0:047e14f53977 | 13 | /** objects **/ |
zhouyun123 | 0:047e14f53977 | 14 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
zhouyun123 | 0:047e14f53977 | 15 | Gamepad pad; |
zhouyun123 | 0:047e14f53977 | 16 | |
zhouyun123 | 3:4bd22344278d | 17 | /**structure**/ |
zhouyun123 | 2:a2f88b2d5da4 | 18 | struct vip { //@set the location of the snake |
zhouyun123 | 0:047e14f53977 | 19 | int x; |
zhouyun123 | 0:047e14f53977 | 20 | int y; |
zhouyun123 | 0:047e14f53977 | 21 | }COOR; |
zhouyun123 | 0:047e14f53977 | 22 | |
zhouyun123 | 0:047e14f53977 | 23 | struct Serpent { |
zhouyun123 | 1:9a8033d80067 | 24 | vip head[length]; //@set the length of the viper |
zhouyun123 | 1:9a8033d80067 | 25 | int n; //@number of snake's section |
zhouyun123 | 0:047e14f53977 | 26 | }SER; |
zhouyun123 | 0:047e14f53977 | 27 | struct target{ |
zhouyun123 | 1:9a8033d80067 | 28 | vip var;//@set the variable of target |
zhouyun123 | 0:047e14f53977 | 29 | }target; |
zhouyun123 | 0:047e14f53977 | 30 | |
zhouyun123 | 0:047e14f53977 | 31 | |
zhouyun123 | 0:047e14f53977 | 32 | |
zhouyun123 | 0:047e14f53977 | 33 | void viper(); |
zhouyun123 | 1:9a8033d80067 | 34 | void Movement(); //set the direction control of the snake |
zhouyun123 | 1:9a8033d80067 | 35 | void goal(); //set the target food |
zhouyun123 | 1:9a8033d80067 | 36 | void gameover(); //set the failure screen |
zhouyun123 | 1:9a8033d80067 | 37 | float wtime; //related to the speed of the snake |
zhouyun123 | 0:047e14f53977 | 38 | int score ; |
zhouyun123 | 1:9a8033d80067 | 39 | int HP; // Snake's health point(HP), if HP=0, game over. |
zhouyun123 | 1:9a8033d80067 | 40 | int D; //set the variable of direction |
zhouyun123 | 3:4bd22344278d | 41 | int d; //set the variable for the control of joystick |
zhouyun123 | 0:047e14f53977 | 42 | |
zhouyun123 | 1:9a8033d80067 | 43 | // define the location of the snake 1 |
zhouyun123 | 0:047e14f53977 | 44 | void defineSnake(){ |
zhouyun123 | 0:047e14f53977 | 45 | for(int i = 1; i < SER.n; i++){ |
zhouyun123 | 0:047e14f53977 | 46 | SER.head[i].x = SER.head[i-1].x; |
zhouyun123 | 0:047e14f53977 | 47 | SER.head[i].y = SER.head[i-1].y-1;//draw snake from top to bottom |
zhouyun123 | 0:047e14f53977 | 48 | } |
zhouyun123 | 0:047e14f53977 | 49 | } |
zhouyun123 | 0:047e14f53977 | 50 | |
zhouyun123 | 0:047e14f53977 | 51 | |
zhouyun123 | 1:9a8033d80067 | 52 | //show a snake by creating array 2 |
zhouyun123 | 0:047e14f53977 | 53 | void viper() |
zhouyun123 | 0:047e14f53977 | 54 | { |
zhouyun123 | 0:047e14f53977 | 55 | for(int i = 0; i < SER.n; i++){ |
zhouyun123 | 0:047e14f53977 | 56 | lcd.drawRect(SER.head[i].x,SER.head[i].y,1,1,FILL_TRANSPARENT); |
zhouyun123 | 0:047e14f53977 | 57 | } |
zhouyun123 | 0:047e14f53977 | 58 | |
zhouyun123 | 0:047e14f53977 | 59 | } |
zhouyun123 | 0:047e14f53977 | 60 | |
zhouyun123 | 0:047e14f53977 | 61 | |
zhouyun123 | 1:9a8033d80067 | 62 | //implement the movement function 3 |
zhouyun123 | 0:047e14f53977 | 63 | void Movement() |
zhouyun123 | 0:047e14f53977 | 64 | { |
zhouyun123 | 1:9a8033d80067 | 65 | d = pad.get_direction();//use joystick to control the direction of snake |
zhouyun123 | 0:047e14f53977 | 66 | if (d == N ){ |
zhouyun123 | 0:047e14f53977 | 67 | D = 1;//up |
zhouyun123 | 0:047e14f53977 | 68 | } |
zhouyun123 | 0:047e14f53977 | 69 | else if ( d == S ){ |
zhouyun123 | 0:047e14f53977 | 70 | D = 2;//down |
zhouyun123 | 0:047e14f53977 | 71 | } |
zhouyun123 | 0:047e14f53977 | 72 | else if (d == W ){ |
zhouyun123 | 0:047e14f53977 | 73 | D = 3;//left |
zhouyun123 | 0:047e14f53977 | 74 | } |
zhouyun123 | 0:047e14f53977 | 75 | else if ( d == E ){ |
zhouyun123 | 0:047e14f53977 | 76 | D = 4;//right |
zhouyun123 | 0:047e14f53977 | 77 | } |
zhouyun123 | 0:047e14f53977 | 78 | //@add or substract the array to move the snake in x,y axis |
zhouyun123 | 0:047e14f53977 | 79 | for(int i = SER.n; i >0; i--){ |
zhouyun123 | 0:047e14f53977 | 80 | SER.head[i].x = SER.head[i-1].x; |
zhouyun123 | 0:047e14f53977 | 81 | SER.head[i].y = SER.head[i-1].y; |
zhouyun123 | 0:047e14f53977 | 82 | } |
zhouyun123 | 0:047e14f53977 | 83 | //@let snake move for different direction |
zhouyun123 | 0:047e14f53977 | 84 | if (D == 1 ){ |
zhouyun123 | 0:047e14f53977 | 85 | SER.head[0].x = SER.head[0].x; |
zhouyun123 | 1:9a8033d80067 | 86 | SER.head[0].y -= 1; |
zhouyun123 | 1:9a8033d80067 | 87 | }//turn upwards |
zhouyun123 | 0:047e14f53977 | 88 | else if (D == 2 ){ |
zhouyun123 | 0:047e14f53977 | 89 | SER.head[0].x = SER.head[0].x; |
zhouyun123 | 1:9a8033d80067 | 90 | SER.head[0].y += 1; |
zhouyun123 | 1:9a8033d80067 | 91 | }//turn downwards |
zhouyun123 | 0:047e14f53977 | 92 | else if (D == 3 ){ |
zhouyun123 | 1:9a8033d80067 | 93 | SER.head[0].x -= 1; |
zhouyun123 | 0:047e14f53977 | 94 | SER.head[0].y = SER.head[0].y; |
zhouyun123 | 1:9a8033d80067 | 95 | }//turn left |
zhouyun123 | 0:047e14f53977 | 96 | else if (D == 4 ){ |
zhouyun123 | 1:9a8033d80067 | 97 | SER.head[0].x += 1; |
zhouyun123 | 0:047e14f53977 | 98 | SER.head[0].y = SER.head[0].y; |
zhouyun123 | 1:9a8033d80067 | 99 | }//turn right |
zhouyun123 | 0:047e14f53977 | 100 | |
zhouyun123 | 0:047e14f53977 | 101 | } |
zhouyun123 | 0:047e14f53977 | 102 | |
zhouyun123 | 0:047e14f53977 | 103 | |
zhouyun123 | 1:9a8033d80067 | 104 | //generate a random target 4 |
zhouyun123 | 0:047e14f53977 | 105 | void goal(){ |
zhouyun123 | 0:047e14f53977 | 106 | srand(unsigned (time(NULL))); |
zhouyun123 | 0:047e14f53977 | 107 | target.var.x = rand() % 80; // The target appears at random location |
zhouyun123 | 0:047e14f53977 | 108 | target.var.y = rand() % 40; |
zhouyun123 | 0:047e14f53977 | 109 | |
zhouyun123 | 0:047e14f53977 | 110 | } |
zhouyun123 | 0:047e14f53977 | 111 | |
zhouyun123 | 0:047e14f53977 | 112 | |
zhouyun123 | 1:9a8033d80067 | 113 | // if snake get the target, change the length and the score 6 |
zhouyun123 | 0:047e14f53977 | 114 | void gettarget(){ |
zhouyun123 | 0:047e14f53977 | 115 | //juge if snake eat food |
zhouyun123 | 0:047e14f53977 | 116 | if(SER.head[0].x == target.var.x && SER.head[0].y == target.var.y ){ |
zhouyun123 | 1:9a8033d80067 | 117 | SER.n += 7; |
zhouyun123 | 1:9a8033d80067 | 118 | goal();//generate a new target when the previous target has been eaten |
zhouyun123 | 0:047e14f53977 | 119 | pad.tone(650,1);// @sounds |
zhouyun123 | 1:9a8033d80067 | 120 | score += 1;// when the snake get its target, the score plus one |
zhouyun123 | 0:047e14f53977 | 121 | } |
zhouyun123 | 0:047e14f53977 | 122 | } |
zhouyun123 | 0:047e14f53977 | 123 | |
zhouyun123 | 0:047e14f53977 | 124 | |
zhouyun123 | 0:047e14f53977 | 125 |