The classic game of snake. Control using a joystick and displayed on a VGA screen
Dependencies: 4DGL direction draw game list mbed
main.cpp@1:aa27e5ef379b, 2012-10-11 (annotated)
- Committer:
- lucoby
- Date:
- Thu Oct 11 20:24:26 2012 +0000
- Revision:
- 1:aa27e5ef379b
- Parent:
- 0:1ca938e58b91
updated drawing and game engine
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lucoby | 0:1ca938e58b91 | 1 | #include "mbed.h" |
lucoby | 0:1ca938e58b91 | 2 | #include "direction.h" |
lucoby | 0:1ca938e58b91 | 3 | #include "list.h" |
lucoby | 0:1ca938e58b91 | 4 | #include "draw.h" |
lucoby | 0:1ca938e58b91 | 5 | #include "game.h" |
lucoby | 0:1ca938e58b91 | 6 | |
lucoby | 0:1ca938e58b91 | 7 | DigitalOut myled(LED1); |
lucoby | 0:1ca938e58b91 | 8 | DigitalOut myled2(LED2); |
lucoby | 0:1ca938e58b91 | 9 | DigitalIn pb(p17); |
lucoby | 0:1ca938e58b91 | 10 | |
lucoby | 0:1ca938e58b91 | 11 | int main() { |
lucoby | 0:1ca938e58b91 | 12 | pb.mode(PullUp); |
lucoby | 0:1ca938e58b91 | 13 | myled2 = 1; |
lucoby | 0:1ca938e58b91 | 14 | SDirection direction, newDirection; |
lucoby | 0:1ca938e58b91 | 15 | List snake = {NULL,NULL}; |
lucoby | 0:1ca938e58b91 | 16 | Apple apple; |
lucoby | 0:1ca938e58b91 | 17 | int points; |
lucoby | 0:1ca938e58b91 | 18 | int tempRow, tempCol; |
lucoby | 0:1ca938e58b91 | 19 | int draw_yes = 0; |
lucoby | 1:aa27e5ef379b | 20 | int draw_y2 = 0; |
lucoby | 1:aa27e5ef379b | 21 | int spx = 0; |
lucoby | 1:aa27e5ef379b | 22 | int spy = 0; |
lucoby | 0:1ca938e58b91 | 23 | int i; |
lucoby | 1:aa27e5ef379b | 24 | State game = splash; |
lucoby | 0:1ca938e58b91 | 25 | drawSetup(); |
lucoby | 0:1ca938e58b91 | 26 | |
lucoby | 0:1ca938e58b91 | 27 | while(1) { |
lucoby | 0:1ca938e58b91 | 28 | wait(0.016666667); |
lucoby | 0:1ca938e58b91 | 29 | myled = !myled; |
lucoby | 0:1ca938e58b91 | 30 | switch(game) { |
lucoby | 1:aa27e5ef379b | 31 | case splash: |
lucoby | 1:aa27e5ef379b | 32 | draw_y2++; |
lucoby | 1:aa27e5ef379b | 33 | if(draw_y2 == 19) { |
lucoby | 1:aa27e5ef379b | 34 | drawBlank(spy,spx); |
lucoby | 1:aa27e5ef379b | 35 | spx = rand() % COL; |
lucoby | 1:aa27e5ef379b | 36 | spy = rand() % ROW; |
lucoby | 1:aa27e5ef379b | 37 | drawApple(spy,spx); |
lucoby | 1:aa27e5ef379b | 38 | draw_y2 = 0; |
lucoby | 1:aa27e5ef379b | 39 | } |
lucoby | 1:aa27e5ef379b | 40 | if((direction = getDirection(none)) != none) game = start; |
lucoby | 1:aa27e5ef379b | 41 | break; |
lucoby | 1:aa27e5ef379b | 42 | case start: |
lucoby | 0:1ca938e58b91 | 43 | if((direction = getDirection(none)) != none) { |
lucoby | 0:1ca938e58b91 | 44 | deleteList(&snake); |
lucoby | 0:1ca938e58b91 | 45 | clearscr(); |
lucoby | 1:aa27e5ef379b | 46 | for(i = -1; i < COL + 2; i++){ |
lucoby | 1:aa27e5ef379b | 47 | drawPixel(-1,i,0xFFFFFF); |
lucoby | 1:aa27e5ef379b | 48 | } |
lucoby | 1:aa27e5ef379b | 49 | for(i = 0; i < ROW + 2; i++) { |
lucoby | 0:1ca938e58b91 | 50 | drawPixel(i,COL+1,0xFFFFFF); |
lucoby | 0:1ca938e58b91 | 51 | } |
lucoby | 1:aa27e5ef379b | 52 | for(i = COL + 1; i > -2; i--){ |
lucoby | 0:1ca938e58b91 | 53 | drawPixel(ROW+1,i,0xFFFFFF); |
lucoby | 0:1ca938e58b91 | 54 | } |
lucoby | 1:aa27e5ef379b | 55 | for(i = ROW + 1; i > -2; i--) { |
lucoby | 1:aa27e5ef379b | 56 | drawPixel(i,-1,0xFFFFFF); |
lucoby | 1:aa27e5ef379b | 57 | } |
lucoby | 0:1ca938e58b91 | 58 | addToHead(&snake, 5, 5); |
lucoby | 0:1ca938e58b91 | 59 | addToHead(&snake, 5, 6); |
lucoby | 0:1ca938e58b91 | 60 | addToHead(&snake, 5, 7); |
lucoby | 0:1ca938e58b91 | 61 | apple.row = rand() % (ROW - 1) + 1; |
lucoby | 0:1ca938e58b91 | 62 | apple.col = rand() % (COL - 3) + 3; |
lucoby | 0:1ca938e58b91 | 63 | points = 0; |
lucoby | 0:1ca938e58b91 | 64 | game = run; |
lucoby | 0:1ca938e58b91 | 65 | } |
lucoby | 0:1ca938e58b91 | 66 | break; |
lucoby | 0:1ca938e58b91 | 67 | case run: |
lucoby | 0:1ca938e58b91 | 68 | // control |
lucoby | 0:1ca938e58b91 | 69 | newDirection = getDirection(newDirection); |
lucoby | 0:1ca938e58b91 | 70 | draw_yes++; |
lucoby | 0:1ca938e58b91 | 71 | if(draw_yes == 14) game = draw; |
lucoby | 0:1ca938e58b91 | 72 | break; |
lucoby | 0:1ca938e58b91 | 73 | |
lucoby | 0:1ca938e58b91 | 74 | case draw: |
lucoby | 0:1ca938e58b91 | 75 | //model |
lucoby | 0:1ca938e58b91 | 76 | draw_yes = 0; |
lucoby | 0:1ca938e58b91 | 77 | game = run; |
lucoby | 0:1ca938e58b91 | 78 | //left |
lucoby | 0:1ca938e58b91 | 79 | if(newDirection == left && direction != right || newDirection == none && direction == left || direction == left && newDirection == right) { |
lucoby | 0:1ca938e58b91 | 80 | direction = left; |
lucoby | 0:1ca938e58b91 | 81 | //pc.printf("left\n\r"); |
lucoby | 0:1ca938e58b91 | 82 | addToHead(&snake, snake.head->row, snake.head->col - 1); |
lucoby | 0:1ca938e58b91 | 83 | if (snake.head->row == apple.row && snake.head->col == apple.col) { |
lucoby | 0:1ca938e58b91 | 84 | tempRow = -1; |
lucoby | 0:1ca938e58b91 | 85 | tempCol = -1; |
lucoby | 0:1ca938e58b91 | 86 | score(&apple, &points); |
lucoby | 0:1ca938e58b91 | 87 | } else { |
lucoby | 0:1ca938e58b91 | 88 | tempRow = snake.tail->row; |
lucoby | 0:1ca938e58b91 | 89 | tempCol = snake.tail->col; |
lucoby | 0:1ca938e58b91 | 90 | removeFromTail(&snake); |
lucoby | 0:1ca938e58b91 | 91 | } |
lucoby | 0:1ca938e58b91 | 92 | //right |
lucoby | 0:1ca938e58b91 | 93 | } else if (newDirection == right && direction != left || newDirection == none && direction == right || direction == right && newDirection == left) { |
lucoby | 0:1ca938e58b91 | 94 | addToHead(&snake, snake.head->row, snake.head->col + 1); |
lucoby | 0:1ca938e58b91 | 95 | direction = right; |
lucoby | 0:1ca938e58b91 | 96 | //pc.printf("right\n\r"); |
lucoby | 0:1ca938e58b91 | 97 | if (snake.head->row == apple.row && snake.head->col == apple.col) { |
lucoby | 0:1ca938e58b91 | 98 | tempRow = -1; |
lucoby | 0:1ca938e58b91 | 99 | tempCol = -1; |
lucoby | 0:1ca938e58b91 | 100 | score(&apple, &points); |
lucoby | 0:1ca938e58b91 | 101 | } else { |
lucoby | 0:1ca938e58b91 | 102 | tempRow = snake.tail->row; |
lucoby | 0:1ca938e58b91 | 103 | tempCol = snake.tail->col; |
lucoby | 0:1ca938e58b91 | 104 | removeFromTail(&snake); |
lucoby | 0:1ca938e58b91 | 105 | } |
lucoby | 0:1ca938e58b91 | 106 | //up |
lucoby | 0:1ca938e58b91 | 107 | } else if (newDirection == up && direction != down || newDirection == none && direction == up|| direction == up && newDirection == down) { |
lucoby | 0:1ca938e58b91 | 108 | addToHead(&snake, snake.head->row - 1, snake.head->col); |
lucoby | 0:1ca938e58b91 | 109 | direction = up; |
lucoby | 0:1ca938e58b91 | 110 | //pc.printf("up\n\r"); |
lucoby | 0:1ca938e58b91 | 111 | if (snake.head->row == apple.row && snake.head->col == apple.col) { |
lucoby | 0:1ca938e58b91 | 112 | tempRow = -1; |
lucoby | 0:1ca938e58b91 | 113 | tempCol = -1; |
lucoby | 0:1ca938e58b91 | 114 | score(&apple, &points); |
lucoby | 0:1ca938e58b91 | 115 | } else { |
lucoby | 0:1ca938e58b91 | 116 | tempRow = snake.tail->row; |
lucoby | 0:1ca938e58b91 | 117 | tempCol = snake.tail->col; |
lucoby | 0:1ca938e58b91 | 118 | removeFromTail(&snake); |
lucoby | 0:1ca938e58b91 | 119 | } |
lucoby | 0:1ca938e58b91 | 120 | //down |
lucoby | 0:1ca938e58b91 | 121 | } else if (newDirection == down && direction != up || newDirection == none && direction == down|| direction == down&& newDirection == up) { |
lucoby | 0:1ca938e58b91 | 122 | addToHead(&snake, snake.head->row + 1, snake.head->col); |
lucoby | 0:1ca938e58b91 | 123 | direction = down; |
lucoby | 0:1ca938e58b91 | 124 | //pc.printf("down\n\r"); |
lucoby | 0:1ca938e58b91 | 125 | if (snake.head->row == apple.row && snake.head->col == apple.col) { |
lucoby | 0:1ca938e58b91 | 126 | tempRow = -1; |
lucoby | 0:1ca938e58b91 | 127 | tempCol = -1; |
lucoby | 0:1ca938e58b91 | 128 | score(&apple, &points); |
lucoby | 0:1ca938e58b91 | 129 | } else { |
lucoby | 0:1ca938e58b91 | 130 | tempRow = snake.tail->row; |
lucoby | 0:1ca938e58b91 | 131 | tempCol = snake.tail->col; |
lucoby | 0:1ca938e58b91 | 132 | removeFromTail(&snake); |
lucoby | 0:1ca938e58b91 | 133 | } |
lucoby | 0:1ca938e58b91 | 134 | } |
lucoby | 0:1ca938e58b91 | 135 | |
lucoby | 0:1ca938e58b91 | 136 | if(!checkGameOver(&snake)){ |
lucoby | 0:1ca938e58b91 | 137 | game = end; |
lucoby | 1:aa27e5ef379b | 138 | break; |
lucoby | 0:1ca938e58b91 | 139 | } |
lucoby | 0:1ca938e58b91 | 140 | |
lucoby | 0:1ca938e58b91 | 141 | //view |
lucoby | 0:1ca938e58b91 | 142 | if(tempRow >= 0) { |
lucoby | 0:1ca938e58b91 | 143 | drawBlank(tempRow, tempCol); |
lucoby | 0:1ca938e58b91 | 144 | } |
lucoby | 0:1ca938e58b91 | 145 | drawApple(apple.row, apple.col); |
lucoby | 0:1ca938e58b91 | 146 | drawSnake(snake.head->row, snake.head->col); |
lucoby | 0:1ca938e58b91 | 147 | drawScore(points); |
lucoby | 0:1ca938e58b91 | 148 | break; |
lucoby | 0:1ca938e58b91 | 149 | case end: |
lucoby | 1:aa27e5ef379b | 150 | if(!pb) { |
lucoby | 1:aa27e5ef379b | 151 | game = splash; |
lucoby | 1:aa27e5ef379b | 152 | clearscr(); |
lucoby | 1:aa27e5ef379b | 153 | } |
lucoby | 0:1ca938e58b91 | 154 | break; |
lucoby | 0:1ca938e58b91 | 155 | } |
lucoby | 0:1ca938e58b91 | 156 | } |
lucoby | 0:1ca938e58b91 | 157 | } |