The classic game of snake. Control using a joystick and displayed on a VGA screen
Dependencies: 4DGL direction draw game list mbed
main.cpp@0:1ca938e58b91, 2012-10-11 (annotated)
- Committer:
- lucoby
- Date:
- Thu Oct 11 19:00:03 2012 +0000
- Revision:
- 0:1ca938e58b91
- Child:
- 1:aa27e5ef379b
Game of snake input from a joystick and outputs to VGA
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 | 0:1ca938e58b91 | 20 | int i; |
lucoby | 0:1ca938e58b91 | 21 | State game = start; |
lucoby | 0:1ca938e58b91 | 22 | drawSetup(); |
lucoby | 0:1ca938e58b91 | 23 | |
lucoby | 0:1ca938e58b91 | 24 | while(1) { |
lucoby | 0:1ca938e58b91 | 25 | wait(0.016666667); |
lucoby | 0:1ca938e58b91 | 26 | myled = !myled; |
lucoby | 0:1ca938e58b91 | 27 | switch(game) { |
lucoby | 0:1ca938e58b91 | 28 | case start: |
lucoby | 0:1ca938e58b91 | 29 | if((direction = getDirection(none)) != none) { |
lucoby | 0:1ca938e58b91 | 30 | deleteList(&snake); |
lucoby | 0:1ca938e58b91 | 31 | clearscr(); |
lucoby | 0:1ca938e58b91 | 32 | for(i = -1; i < ROW + 2; i++) { |
lucoby | 0:1ca938e58b91 | 33 | drawPixel(i,-1,0xFFFFFF); |
lucoby | 0:1ca938e58b91 | 34 | drawPixel(i,COL+1,0xFFFFFF); |
lucoby | 0:1ca938e58b91 | 35 | } |
lucoby | 0:1ca938e58b91 | 36 | for(i = -1; i < COL + 2; i++){ |
lucoby | 0:1ca938e58b91 | 37 | drawPixel(-1,i,0xFFFFFF); |
lucoby | 0:1ca938e58b91 | 38 | drawPixel(ROW+1,i,0xFFFFFF); |
lucoby | 0:1ca938e58b91 | 39 | } |
lucoby | 0:1ca938e58b91 | 40 | addToHead(&snake, 5, 5); |
lucoby | 0:1ca938e58b91 | 41 | addToHead(&snake, 5, 6); |
lucoby | 0:1ca938e58b91 | 42 | addToHead(&snake, 5, 7); |
lucoby | 0:1ca938e58b91 | 43 | apple.row = rand() % (ROW - 1) + 1; |
lucoby | 0:1ca938e58b91 | 44 | apple.col = rand() % (COL - 3) + 3; |
lucoby | 0:1ca938e58b91 | 45 | points = 0; |
lucoby | 0:1ca938e58b91 | 46 | game = run; |
lucoby | 0:1ca938e58b91 | 47 | } |
lucoby | 0:1ca938e58b91 | 48 | break; |
lucoby | 0:1ca938e58b91 | 49 | case run: |
lucoby | 0:1ca938e58b91 | 50 | // control |
lucoby | 0:1ca938e58b91 | 51 | newDirection = getDirection(newDirection); |
lucoby | 0:1ca938e58b91 | 52 | draw_yes++; |
lucoby | 0:1ca938e58b91 | 53 | if(draw_yes == 14) game = draw; |
lucoby | 0:1ca938e58b91 | 54 | break; |
lucoby | 0:1ca938e58b91 | 55 | |
lucoby | 0:1ca938e58b91 | 56 | case draw: |
lucoby | 0:1ca938e58b91 | 57 | //model |
lucoby | 0:1ca938e58b91 | 58 | draw_yes = 0; |
lucoby | 0:1ca938e58b91 | 59 | game = run; |
lucoby | 0:1ca938e58b91 | 60 | //left |
lucoby | 0:1ca938e58b91 | 61 | if(newDirection == left && direction != right || newDirection == none && direction == left || direction == left && newDirection == right) { |
lucoby | 0:1ca938e58b91 | 62 | direction = left; |
lucoby | 0:1ca938e58b91 | 63 | //pc.printf("left\n\r"); |
lucoby | 0:1ca938e58b91 | 64 | addToHead(&snake, snake.head->row, snake.head->col - 1); |
lucoby | 0:1ca938e58b91 | 65 | if (snake.head->row == apple.row && snake.head->col == apple.col) { |
lucoby | 0:1ca938e58b91 | 66 | tempRow = -1; |
lucoby | 0:1ca938e58b91 | 67 | tempCol = -1; |
lucoby | 0:1ca938e58b91 | 68 | score(&apple, &points); |
lucoby | 0:1ca938e58b91 | 69 | } else { |
lucoby | 0:1ca938e58b91 | 70 | tempRow = snake.tail->row; |
lucoby | 0:1ca938e58b91 | 71 | tempCol = snake.tail->col; |
lucoby | 0:1ca938e58b91 | 72 | removeFromTail(&snake); |
lucoby | 0:1ca938e58b91 | 73 | } |
lucoby | 0:1ca938e58b91 | 74 | //right |
lucoby | 0:1ca938e58b91 | 75 | } else if (newDirection == right && direction != left || newDirection == none && direction == right || direction == right && newDirection == left) { |
lucoby | 0:1ca938e58b91 | 76 | addToHead(&snake, snake.head->row, snake.head->col + 1); |
lucoby | 0:1ca938e58b91 | 77 | direction = right; |
lucoby | 0:1ca938e58b91 | 78 | //pc.printf("right\n\r"); |
lucoby | 0:1ca938e58b91 | 79 | if (snake.head->row == apple.row && snake.head->col == apple.col) { |
lucoby | 0:1ca938e58b91 | 80 | tempRow = -1; |
lucoby | 0:1ca938e58b91 | 81 | tempCol = -1; |
lucoby | 0:1ca938e58b91 | 82 | score(&apple, &points); |
lucoby | 0:1ca938e58b91 | 83 | } else { |
lucoby | 0:1ca938e58b91 | 84 | tempRow = snake.tail->row; |
lucoby | 0:1ca938e58b91 | 85 | tempCol = snake.tail->col; |
lucoby | 0:1ca938e58b91 | 86 | removeFromTail(&snake); |
lucoby | 0:1ca938e58b91 | 87 | } |
lucoby | 0:1ca938e58b91 | 88 | //up |
lucoby | 0:1ca938e58b91 | 89 | } else if (newDirection == up && direction != down || newDirection == none && direction == up|| direction == up && newDirection == down) { |
lucoby | 0:1ca938e58b91 | 90 | addToHead(&snake, snake.head->row - 1, snake.head->col); |
lucoby | 0:1ca938e58b91 | 91 | direction = up; |
lucoby | 0:1ca938e58b91 | 92 | //pc.printf("up\n\r"); |
lucoby | 0:1ca938e58b91 | 93 | if (snake.head->row == apple.row && snake.head->col == apple.col) { |
lucoby | 0:1ca938e58b91 | 94 | tempRow = -1; |
lucoby | 0:1ca938e58b91 | 95 | tempCol = -1; |
lucoby | 0:1ca938e58b91 | 96 | score(&apple, &points); |
lucoby | 0:1ca938e58b91 | 97 | } else { |
lucoby | 0:1ca938e58b91 | 98 | tempRow = snake.tail->row; |
lucoby | 0:1ca938e58b91 | 99 | tempCol = snake.tail->col; |
lucoby | 0:1ca938e58b91 | 100 | removeFromTail(&snake); |
lucoby | 0:1ca938e58b91 | 101 | } |
lucoby | 0:1ca938e58b91 | 102 | //down |
lucoby | 0:1ca938e58b91 | 103 | } else if (newDirection == down && direction != up || newDirection == none && direction == down|| direction == down&& newDirection == up) { |
lucoby | 0:1ca938e58b91 | 104 | addToHead(&snake, snake.head->row + 1, snake.head->col); |
lucoby | 0:1ca938e58b91 | 105 | direction = down; |
lucoby | 0:1ca938e58b91 | 106 | //pc.printf("down\n\r"); |
lucoby | 0:1ca938e58b91 | 107 | if (snake.head->row == apple.row && snake.head->col == apple.col) { |
lucoby | 0:1ca938e58b91 | 108 | tempRow = -1; |
lucoby | 0:1ca938e58b91 | 109 | tempCol = -1; |
lucoby | 0:1ca938e58b91 | 110 | score(&apple, &points); |
lucoby | 0:1ca938e58b91 | 111 | } else { |
lucoby | 0:1ca938e58b91 | 112 | tempRow = snake.tail->row; |
lucoby | 0:1ca938e58b91 | 113 | tempCol = snake.tail->col; |
lucoby | 0:1ca938e58b91 | 114 | removeFromTail(&snake); |
lucoby | 0:1ca938e58b91 | 115 | } |
lucoby | 0:1ca938e58b91 | 116 | } |
lucoby | 0:1ca938e58b91 | 117 | |
lucoby | 0:1ca938e58b91 | 118 | if(!checkGameOver(&snake)){ |
lucoby | 0:1ca938e58b91 | 119 | game = end; |
lucoby | 0:1ca938e58b91 | 120 | } |
lucoby | 0:1ca938e58b91 | 121 | |
lucoby | 0:1ca938e58b91 | 122 | //view |
lucoby | 0:1ca938e58b91 | 123 | if(tempRow >= 0) { |
lucoby | 0:1ca938e58b91 | 124 | drawBlank(tempRow, tempCol); |
lucoby | 0:1ca938e58b91 | 125 | } |
lucoby | 0:1ca938e58b91 | 126 | drawApple(apple.row, apple.col); |
lucoby | 0:1ca938e58b91 | 127 | drawSnake(snake.head->row, snake.head->col); |
lucoby | 0:1ca938e58b91 | 128 | drawScore(points); |
lucoby | 0:1ca938e58b91 | 129 | break; |
lucoby | 0:1ca938e58b91 | 130 | case end: |
lucoby | 0:1ca938e58b91 | 131 | if(!pb) game = start; |
lucoby | 0:1ca938e58b91 | 132 | break; |
lucoby | 0:1ca938e58b91 | 133 | } |
lucoby | 0:1ca938e58b91 | 134 | } |
lucoby | 0:1ca938e58b91 | 135 | } |