Yunting Zou 201199716
Dependencies: mbed MotionSensor
viper/viper.h@2:a2f88b2d5da4, 2020-05-14 (annotated)
- Committer:
- zhouyun123
- Date:
- Thu May 14 16:42:19 2020 +0000
- Revision:
- 2:a2f88b2d5da4
- Parent:
- 1:9a8033d80067
- Child:
- 3:4bd22344278d
third commit
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 | 0:047e14f53977 | 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 | 1:9a8033d80067 | 41 | int d; //set the variable of the 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 | |
zhouyun123 | 0:047e14f53977 | 56 | for(int i = 0; i < SER.n; i++){ |
zhouyun123 | 0:047e14f53977 | 57 | lcd.drawRect(SER.head[i].x,SER.head[i].y,1,1,FILL_TRANSPARENT); |
zhouyun123 | 0:047e14f53977 | 58 | } |
zhouyun123 | 0:047e14f53977 | 59 | |
zhouyun123 | 0:047e14f53977 | 60 | } |
zhouyun123 | 0:047e14f53977 | 61 | |
zhouyun123 | 0:047e14f53977 | 62 | |
zhouyun123 | 1:9a8033d80067 | 63 | //implement the movement function 3 |
zhouyun123 | 0:047e14f53977 | 64 | void Movement() |
zhouyun123 | 0:047e14f53977 | 65 | { |
zhouyun123 | 1:9a8033d80067 | 66 | d = pad.get_direction();//use joystick to control the direction of snake |
zhouyun123 | 0:047e14f53977 | 67 | if (d == N ){ |
zhouyun123 | 0:047e14f53977 | 68 | D = 1;//up |
zhouyun123 | 0:047e14f53977 | 69 | } |
zhouyun123 | 0:047e14f53977 | 70 | else if ( d == S ){ |
zhouyun123 | 0:047e14f53977 | 71 | D = 2;//down |
zhouyun123 | 0:047e14f53977 | 72 | } |
zhouyun123 | 0:047e14f53977 | 73 | else if (d == W ){ |
zhouyun123 | 0:047e14f53977 | 74 | D = 3;//left |
zhouyun123 | 0:047e14f53977 | 75 | } |
zhouyun123 | 0:047e14f53977 | 76 | else if ( d == E ){ |
zhouyun123 | 0:047e14f53977 | 77 | D = 4;//right |
zhouyun123 | 0:047e14f53977 | 78 | } |
zhouyun123 | 0:047e14f53977 | 79 | //@add or substract the array to move the snake in x,y axis |
zhouyun123 | 0:047e14f53977 | 80 | for(int i = SER.n; i >0; i--){ |
zhouyun123 | 0:047e14f53977 | 81 | SER.head[i].x = SER.head[i-1].x; |
zhouyun123 | 0:047e14f53977 | 82 | SER.head[i].y = SER.head[i-1].y; |
zhouyun123 | 0:047e14f53977 | 83 | } |
zhouyun123 | 0:047e14f53977 | 84 | //@let snake move for different direction |
zhouyun123 | 0:047e14f53977 | 85 | if (D == 1 ){ |
zhouyun123 | 0:047e14f53977 | 86 | SER.head[0].x = SER.head[0].x; |
zhouyun123 | 1:9a8033d80067 | 87 | SER.head[0].y -= 1; |
zhouyun123 | 1:9a8033d80067 | 88 | }//turn upwards |
zhouyun123 | 0:047e14f53977 | 89 | else if (D == 2 ){ |
zhouyun123 | 0:047e14f53977 | 90 | SER.head[0].x = SER.head[0].x; |
zhouyun123 | 1:9a8033d80067 | 91 | SER.head[0].y += 1; |
zhouyun123 | 1:9a8033d80067 | 92 | }//turn downwards |
zhouyun123 | 0:047e14f53977 | 93 | else if (D == 3 ){ |
zhouyun123 | 1:9a8033d80067 | 94 | SER.head[0].x -= 1; |
zhouyun123 | 0:047e14f53977 | 95 | SER.head[0].y = SER.head[0].y; |
zhouyun123 | 1:9a8033d80067 | 96 | }//turn left |
zhouyun123 | 0:047e14f53977 | 97 | else if (D == 4 ){ |
zhouyun123 | 1:9a8033d80067 | 98 | SER.head[0].x += 1; |
zhouyun123 | 0:047e14f53977 | 99 | SER.head[0].y = SER.head[0].y; |
zhouyun123 | 1:9a8033d80067 | 100 | }//turn right |
zhouyun123 | 0:047e14f53977 | 101 | |
zhouyun123 | 0:047e14f53977 | 102 | } |
zhouyun123 | 0:047e14f53977 | 103 | |
zhouyun123 | 0:047e14f53977 | 104 | |
zhouyun123 | 1:9a8033d80067 | 105 | //generate a random target 4 |
zhouyun123 | 0:047e14f53977 | 106 | void goal(){ |
zhouyun123 | 0:047e14f53977 | 107 | srand(unsigned (time(NULL))); |
zhouyun123 | 0:047e14f53977 | 108 | target.var.x = rand() % 80; // The target appears at random location |
zhouyun123 | 0:047e14f53977 | 109 | target.var.y = rand() % 40; |
zhouyun123 | 0:047e14f53977 | 110 | |
zhouyun123 | 0:047e14f53977 | 111 | } |
zhouyun123 | 0:047e14f53977 | 112 | |
zhouyun123 | 0:047e14f53977 | 113 | |
zhouyun123 | 1:9a8033d80067 | 114 | // if snake get the target, change the length and the score 6 |
zhouyun123 | 0:047e14f53977 | 115 | void gettarget(){ |
zhouyun123 | 0:047e14f53977 | 116 | //juge if snake eat food |
zhouyun123 | 0:047e14f53977 | 117 | if(SER.head[0].x == target.var.x && SER.head[0].y == target.var.y ){ |
zhouyun123 | 1:9a8033d80067 | 118 | SER.n += 7; |
zhouyun123 | 1:9a8033d80067 | 119 | goal();//generate a new target when the previous target has been eaten |
zhouyun123 | 0:047e14f53977 | 120 | pad.tone(650,1);// @sounds |
zhouyun123 | 1:9a8033d80067 | 121 | score += 1;// when the snake get its target, the score plus one |
zhouyun123 | 0:047e14f53977 | 122 | } |
zhouyun123 | 0:047e14f53977 | 123 | } |
zhouyun123 | 0:047e14f53977 | 124 | |
zhouyun123 | 0:047e14f53977 | 125 | |
zhouyun123 | 0:047e14f53977 | 126 |