simple snake game

Dependencies:   C12832_lcd EthernetInterface WebSocketClient mbed-rtos mbed

Committer:
lyukx
Date:
Thu Dec 08 11:20:34 2016 +0000
Revision:
0:2e5a51003afe
snake game

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lyukx 0:2e5a51003afe 1 #include "Snake.h"
lyukx 0:2e5a51003afe 2 #include "mbed.h"
lyukx 0:2e5a51003afe 3 #include "EthernetInterface.h"
lyukx 0:2e5a51003afe 4 #include "Websocket.h"
lyukx 0:2e5a51003afe 5
lyukx 0:2e5a51003afe 6 Serial pc(USBTX, USBRX); // tx, rx
lyukx 0:2e5a51003afe 7
lyukx 0:2e5a51003afe 8 // Joy stick input
lyukx 0:2e5a51003afe 9 DigitalIn joy_left(p13);
lyukx 0:2e5a51003afe 10 DigitalIn joy_right(p16);
lyukx 0:2e5a51003afe 11 DigitalIn joy_up(p15);
lyukx 0:2e5a51003afe 12 DigitalIn joy_down(p12);
lyukx 0:2e5a51003afe 13 DigitalIn fire(p14);
lyukx 0:2e5a51003afe 14
lyukx 0:2e5a51003afe 15 char json_str[2048];
lyukx 0:2e5a51003afe 16
lyukx 0:2e5a51003afe 17 int main(){
lyukx 0:2e5a51003afe 18 EthernetInterface eth;
lyukx 0:2e5a51003afe 19 eth.init(); //Use DHCP
lyukx 0:2e5a51003afe 20 eth.connect();
lyukx 0:2e5a51003afe 21 printf("IP Address is %s\n", eth.getIPAddress());
lyukx 0:2e5a51003afe 22 //TCP testing code
lyukx 0:2e5a51003afe 23 TCPSocketConnection sock;
lyukx 0:2e5a51003afe 24 sock.connect("mbed.org", 80);
lyukx 0:2e5a51003afe 25
lyukx 0:2e5a51003afe 26 char http_cmd[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\n\n";
lyukx 0:2e5a51003afe 27 sock.send_all(http_cmd, sizeof(http_cmd)-1);
lyukx 0:2e5a51003afe 28
lyukx 0:2e5a51003afe 29 char buffer[300];
lyukx 0:2e5a51003afe 30 int ret;
lyukx 0:2e5a51003afe 31 while (true) {
lyukx 0:2e5a51003afe 32 ret = sock.receive(buffer, sizeof(buffer)-1);
lyukx 0:2e5a51003afe 33 if (ret <= 0)
lyukx 0:2e5a51003afe 34 break;
lyukx 0:2e5a51003afe 35 buffer[ret] = '\0';
lyukx 0:2e5a51003afe 36 printf("Received %d chars from server:\n%s\n", ret, buffer);
lyukx 0:2e5a51003afe 37 }
lyukx 0:2e5a51003afe 38
lyukx 0:2e5a51003afe 39 sock.close();
lyukx 0:2e5a51003afe 40 // TCP test end
lyukx 0:2e5a51003afe 41
lyukx 0:2e5a51003afe 42 // Websocket Testing Code
lyukx 0:2e5a51003afe 43 Websocket ws("ws://107.191.61.81:8888/ws");
lyukx 0:2e5a51003afe 44 ws.connect();
lyukx 0:2e5a51003afe 45
lyukx 0:2e5a51003afe 46 printf("Preparing for net connecting");
lyukx 0:2e5a51003afe 47
lyukx 0:2e5a51003afe 48 //Initial the game screen
lyukx 0:2e5a51003afe 49 pc.printf("Connected to board...\r\n");
lyukx 0:2e5a51003afe 50
lyukx 0:2e5a51003afe 51 pc.printf("preparing...");
lyukx 0:2e5a51003afe 52 Screen scrn;
lyukx 0:2e5a51003afe 53 SnakeControll ctrl;
lyukx 0:2e5a51003afe 54 ctrl.init();
lyukx 0:2e5a51003afe 55 scrn.init();
lyukx 0:2e5a51003afe 56 scrn.generate_lcd();
lyukx 0:2e5a51003afe 57 scrn.write_to_screen();
lyukx 0:2e5a51003afe 58 pc.printf("done. Press joystick to start\r\n");
lyukx 0:2e5a51003afe 59 while(!fire){} // Press the joystick to start
lyukx 0:2e5a51003afe 60 //Game main loop
lyukx 0:2e5a51003afe 61 while(1){
lyukx 0:2e5a51003afe 62 char *gamedata = const_cast<char*>(ctrl.get_gamedata().c_str());
lyukx 0:2e5a51003afe 63 pc.printf(gamedata);
lyukx 0:2e5a51003afe 64 pc.printf("\r\n");
lyukx 0:2e5a51003afe 65 ws.send(gamedata);
lyukx 0:2e5a51003afe 66 ctrl.set_direction(joy_left + joy_right*2 + joy_up*3 + joy_down*4 - 1);
lyukx 0:2e5a51003afe 67 int flag = ctrl.step();
lyukx 0:2e5a51003afe 68 if(flag){
lyukx 0:2e5a51003afe 69 ws.close();
lyukx 0:2e5a51003afe 70 eth.disconnect();
lyukx 0:2e5a51003afe 71 exit(0);
lyukx 0:2e5a51003afe 72 }
lyukx 0:2e5a51003afe 73 scrn.generate_lcd();
lyukx 0:2e5a51003afe 74 scrn.write_to_screen();
lyukx 0:2e5a51003afe 75 wait(0.1);
lyukx 0:2e5a51003afe 76 }
lyukx 0:2e5a51003afe 77 }