Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
SnakeEngine.cpp
00001 #include "SnakeEngine.h" 00002 00003 SnakeEngine::SnakeEngine() 00004 { 00005 00006 } 00007 00008 SnakeEngine::~SnakeEngine() 00009 { 00010 00011 } 00012 void SnakeEngine::init(int apple_size, int snake_speed, int snake_score, int snake_snakeheight, int snake_snakewidth) // Inital values of the SnakeEngine 00013 { 00014 //Apple Functions 00015 _apple_size = apple_size; 00016 _a.init(_apple_size); //Allows access to the apple 00017 00018 //Snake Functions 00019 snake_speed = snake_speed; 00020 _snake_snakeheight = snake_snakeheight; 00021 _snake_snakewidth = snake_snakewidth; 00022 _s.init(sx, sy, _snake_score, _snake_speed, _snake_snakewidth, _snake_snakeheight); //Alows access to the snake 00023 } 00024 00025 void SnakeEngine::read_input(Gamepad &pad) 00026 { 00027 //Reads the input of the thumbstick for controlling the snake 00028 _d = pad.get_direction(); 00029 _mag = pad.get_mag(); 00030 00031 // Only Show Yellow LEDS when in game 00032 // In this void as we have gamepad access 00033 pad.led(2,1); 00034 pad.led(5,1); 00035 pad.led(3,0); 00036 pad.led(6,0); 00037 } 00038 00039 00040 void SnakeEngine::crash(Gamepad &pad, N5110 &lcd) 00041 { 00042 //For moving the snake depending on the position of the thumbstick 00043 Vector2D snakepos = _s.get_pos(); 00044 _s.update(_d,_mag); 00045 00046 // Collision with wall Secition having this on will make it so if the snake colides with the wall then the snake dies 00047 // If you want to have it so the walls are boundaries and not deadly comment this section out and uncomment the section in Snake.CPP 00048 if (snakepos.x<=0){ // If it colides with the left wall 00049 _dead=true; //This function 00050 dead(lcd,pad);} //Ends the Game 00051 if (snakepos.x>=84-_snake_snakewidth){ // The right wall 00052 _dead=true; 00053 dead(lcd,pad);} 00054 if (snakepos.y<=8){ // Set to 8 Due to the score being included 00055 _dead=true; 00056 dead(lcd,pad);} 00057 if (snakepos.y>=48-_snake_snakeheight){ // The bottom wall 00058 _dead=true; 00059 dead(lcd,pad);} 00060 // End of Wall of Death Section! 00061 } 00062 00063 void SnakeEngine::draw(N5110 &lcd) 00064 { 00065 // draw what should be displayed on the screen 00066 00067 // Game Area this allows us space for a line of text to display score 00068 lcd.drawRect(0,8,WIDTH,HEIGHT-8,FILL_TRANSPARENT); 00069 00070 // Displays the score 00071 print_score(lcd); 00072 00073 // Draws the game objects Apple and Snake 00074 _a.draw(lcd); // Draws Apple 00075 _s.draw(lcd); // Draws Snake 00076 } 00077 00078 void SnakeEngine::eat(N5110 &lcd, Gamepad &pad) 00079 00080 { 00081 Vector2D apple_pos = _a.get_pos(); // Gets the position of the apple 00082 Vector2D snake_pos = _s.get_pos(); // Gets the position of the snake 00083 00084 // Meant to get the position of the food and the position of the snake and adds to score and spawns a new apple but doesn't seem to work :( 00085 00086 if ( 00087 (apple_pos.y >= snake_pos.y) && 00088 (apple_pos.y <= snake_pos.y + _snake_snakewidth) && 00089 (apple_pos.x >= snake_pos.y) && 00090 (apple_pos.x <= snake_pos.y + _snake_snakeheight)) 00091 00092 { 00093 _s.add_score(); 00094 apple_pos.x = (rand() % (WIDTH)); // Spawns the food in a random place 00095 apple_pos.y = (rand() % (HEIGHT)); 00096 00097 pad.led(3,1); // Flashes the LEDS green 00098 pad.led(6,1); 00099 wait(0.5); 00100 pad.led(3,0); 00101 pad.led(6,0); 00102 } 00103 00104 } 00105 00106 void SnakeEngine::print_score(N5110 &lcd) 00107 { 00108 //Everything to do with displaying the score 00109 int score = _s.get_score(); 00110 char buffer[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14) max characters is 14 00111 int ScoreLength = sprintf(buffer,"Score:%2d", score); // 00112 if (ScoreLength <= 14) // Will only display if the buffer is less than 14 (Shouldn't be an issue here) 00113 lcd.printString(buffer,WIDTH/2-25,0); // Puts the score in the center of the display 00114 } 00115 00116 00117 void SnakeEngine::dead(N5110 &lcd, Gamepad &pad) 00118 00119 { 00120 while(_dead == true){ 00121 lcd.clear(); 00122 00123 // Shows the Score when you are dead 00124 int score = _s.get_score(); 00125 char buffer[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14) max characters is 14 00126 int ScoreLength = sprintf(buffer,"Score:%2d", score); // 00127 if (ScoreLength <= 14) // Will only display if the buffer is less than 14 (Shouldn't be an issue here) 00128 lcd.printString(buffer,WIDTH/2-25,0); // Puts the score in the center of the display 00129 00130 // Notifies that you are dead and how to play again 00131 00132 lcd.printString(" You're Dead! ",0,2); 00133 lcd.printString(" Play Again? ",0,3); 00134 lcd.printString(" Press Restart ",0,4); 00135 00136 //Turns the Red LEDS on 00137 pad.led(2,0); 00138 pad.led(5,0); 00139 pad.led(1,1); 00140 pad.led(4,1); 00141 wait(0.4); 00142 pad.led(1,0); // Makes the flash :) 00143 pad.led(4,0); 00144 wait(0.4); 00145 lcd.refresh(); 00146 } 00147 }
Generated on Sat Jul 16 2022 21:38:04 by
1.7.2