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
Revision 25:f03439ee32c6, committed 2018-05-08
- Comitter:
- JRM1986
- Date:
- Tue May 08 10:52:12 2018 +0000
- Parent:
- 24:4b180348826e
- Child:
- 26:23301f48c1ed
- Commit message:
- Tidying up 1;
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Food/Food-test.h Tue May 08 10:52:12 2018 +0000
@@ -0,0 +1,70 @@
+#ifndef FOOD_TEST_H
+#define FOOD_TEST_H
+
+/**
+ * \brief Check that food changes position within game area
+ *
+ * \returns true if all the tests passed
+ */
+
+ bool food_test_position()
+{
+
+ Food food;
+ int i;
+ int frame_count = g_fc;
+
+ // initialise food with a collision
+ food.set_food_position(1,1,true);
+
+ // get a random position
+
+ Vector2D rand_pos = food.get_rand_pos();
+ printf("%f, %f \n", rand_pos.x, rand_pos.y);
+
+ bool success_flag = true;
+
+ // if the random position is within the screen boundaries return true
+
+ if((0 >= rand_pos.x >= 83) && (0 >= rand_pos.y >= 47)) {
+
+ success_flag = false;
+
+ }
+
+ for(i = 0; i <=1; i++) {
+
+ // check update method
+
+ food.update(true, 0);
+
+ // get new positions
+ wait(1.0);
+ Vector2D rand_pos_1 = food.get_rand_pos();
+ printf("%f, %f \n", rand_pos_1.x, rand_pos_1.y);
+
+ if((rand_pos.x == rand_pos_1.x) && (rand_pos.y == rand_pos_1.y)) {
+
+ success_flag = false;
+
+ }
+
+
+ printf("%i\n", frame_count);
+
+ }
+
+ // check counter increments in update
+
+ if(frame_count != 0) {
+
+ success_flag = false;
+
+ }
+
+
+ return success_flag;
+
+}
+
+#endif
\ No newline at end of file
--- a/Food/Food.cpp Tue May 01 12:20:42 2018 +0000
+++ b/Food/Food.cpp Tue May 08 10:52:12 2018 +0000
@@ -1,5 +1,6 @@
#include "Food.h"
+///////////////// constructor/destructor /////////////////
Food::Food()
{
@@ -34,7 +35,7 @@
}
-void Food::update(bool collision)
+void Food::update(bool collision, int n_frames)
{
// increment frame counter
@@ -42,7 +43,7 @@
int frame_count = g_frame_counter();
- set_food_position(frame_count, 150, collision);
+ set_food_position(frame_count, n_frames, collision);
}
@@ -85,7 +86,6 @@
// reset frame counter to 0
-
g_fc = 0;
}
@@ -98,6 +98,8 @@
_y = pos.y;
// reset frame counter to 0
+
+ printf("Frames Past %i \n ", g_fc);
g_fc = 0;
--- a/Food/Food.h Tue May 01 12:20:42 2018 +0000
+++ b/Food/Food.h Tue May 08 10:52:12 2018 +0000
@@ -31,7 +31,7 @@
/** Updates food state */
- void update(bool collision);
+ void update(bool collision, int n_frames);
/** Draws food on lcd */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Snake/Snake-test.h Tue May 08 10:52:12 2018 +0000
@@ -0,0 +1,46 @@
+#ifndef SNAKE_TEST_H
+#define SNAKE_TEST_H
+
+
+/**
+ * \brief Check that food changes position within game area
+ *
+ * \returns true if all the tests passed
+ */
+
+ bool snake_test_movement()
+{
+ bool success_flag = true;
+
+ Snake snake;
+
+ // next diretion test
+
+ snake.set_current_direction(S);
+ snake.set_snake_direction(N,S);
+ Direction next = snake.get_snake_direction();
+ snake.set_snake_position(next);
+ Vector2D pos = snake.get_snake_position();
+
+
+ printf("%c", next);
+
+ printf("Position %f, %f", pos.x, pos.y);
+
+ if(next == !S) {
+
+ success_flag = false;
+
+ }
+
+ if((pos.x != 0) && (pos.y != 1)) {
+
+ success_flag = false;
+
+ }
+
+ return success_flag;
+
+
+}
+#endif
\ No newline at end of file
--- a/Snake/Snake.cpp Tue May 01 12:20:42 2018 +0000
+++ b/Snake/Snake.cpp Tue May 08 10:52:12 2018 +0000
@@ -1,5 +1,6 @@
#include "Snake.h"
+///////////////// constructor/destructor /////////////////
Snake::Snake()
{
--- a/SnakeEngine/SnakeEngine.cpp Tue May 01 12:20:42 2018 +0000
+++ b/SnakeEngine/SnakeEngine.cpp Tue May 08 10:52:12 2018 +0000
@@ -1,5 +1,6 @@
#include "SnakeEngine.h"
+///////////////// constructor/destructor /////////////////
SnakeEngine::SnakeEngine()
{
@@ -21,15 +22,24 @@
}
+void g_frame_time(int frame_time)
+{
+
+ g_ft = frame_time;
+
+}
+
///////////////// public methods /////////////////
-void SnakeEngine::init(Direction in, Direction cur, int snake_pos_x, int snake_pos_y)
+void SnakeEngine::init(Direction in, Direction cur, int snake_pos_x, int snake_pos_y, int n_frames)
{
_snake_pos_x = snake_pos_x;
_snake_pos_y = snake_pos_y;
_in = in;
_cur = cur;
+ _n_frames = n_frames;
+ g_frame_time(_n_frames);
_snake.init(_in, _cur, _snake_pos_x, _snake_pos_y);
@@ -40,14 +50,23 @@
void SnakeEngine::update(Gamepad &pad)
{
bool food_col;
+ //bool wall_collsion;
+ int food_spawn_time;
++g_engine_fc;
+ increase_speed(5, 1.1);
+ food_spawn_time = food_spawning_time(5);
+ _snake.update(_in, _cur);
+ //_n_frames = 150;
+ //printf("Spawn time %i \n", food_spawn_time);
- _snake.update(_in, _cur);
+ _food.update(_collision, food_spawn_time);
food_col = detect_food_collision(pad);
_collision = food_col;
set_tail_length(food_col);
set_tail_array(pad);
+ g_wall = detect_wall_collision(pad);
+ //printf("%i%", g_wall);
}
@@ -60,7 +79,7 @@
lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
lcd.setContrast(0.5);
- _food.update(_collision);
+ //_food.update(_collision);
_food.draw(lcd);
_snake.draw(lcd);
draw_tail(lcd);
@@ -140,7 +159,6 @@
return success_flag;
}
-
bool SnakeEngine::detect_wall_collision(Gamepad &pad)
{
@@ -296,7 +314,48 @@
}
-
+int SnakeEngine::food_spawning_time(int frame_decrementer)
+{
+
+ int c = 0;
+ c = g_tl - g_n;
+
+ //printf("C = %i", c);
+
+ if((c == 0) && (g_ft > 100)) {
+
+ g_n = g_n + frame_decrementer;
+
+ g_ft -= 10;
+
+ frame_decrementer += frame_decrementer;
+
+ }
+
+ return g_ft;
+
+}
+
+void SnakeEngine::increase_speed(int resetter, float speed_decrementer)
+{
+
+ int c = 0;
+ c = g_tl - g_n;
+ if(g_tl == 0) {
+
+ g_speed = 0.4;
-
-
\ No newline at end of file
+ }
+
+ if(c == 0) {
+
+ g_n = g_n + resetter;
+
+ g_speed = g_speed/speed_decrementer;
+
+ resetter += resetter;
+
+ }
+
+}
+
--- a/SnakeEngine/SnakeEngine.h Tue May 01 12:20:42 2018 +0000
+++ b/SnakeEngine/SnakeEngine.h Tue May 08 10:52:12 2018 +0000
@@ -18,21 +18,31 @@
int g_tail_length();
Vector2D g_tail_position();
+void g_frame_time(int frame_time);
+
int g_get_even_array();
int g_get_odd_array();
int g_get_engine_frame_count();
static Vector2D g_tp;
-static int g_tl;
-static int g_tc;
-static int g_engine_fc;
-static int g_odd_array_x[100];
-static int g_odd_array_y[100];
-static int g_even_array_x[100];
-static int g_even_array_y[100];
-static int g_array_x[100];
-static int g_array_y[100];
+static int g_tl; // tail length
+//static int g_tc; //
+static int g_engine_fc; // frame counter for draw tail functions
+static int g_ft; // frame counter for decreasing time between food spawning
+static int g_n; // ensures spawning of food occurs periodically
+static bool g_wall; // wall collision detected
+
+/** Returns the speed
+*/
+static double g_speed;
+
+static int g_odd_array_x[100]; // array to store odd values for x component of tail segments
+static int g_odd_array_y[100]; // array to store odd values for y component of tail segments
+static int g_even_array_x[100]; // array to store even values for x component of tail segments
+static int g_even_array_y[100]; // array to store even values for y component of tail segments
+//static int g_array_x[100];
+//static int g_array_y[100];
Vector2D g_holder_pos();
@@ -52,10 +62,11 @@
* @param Input direction from pad
* @param Current direction of snake
* @param pos_x x position
- * @param pos_y y position
+ * @param pos_y y position
+ * @param sets initial number of frames between food spawning
*/
- void init(Direction in, Direction cur, int pos_x, int pos_y);
+ void init(Direction in, Direction cur, int pos_x, int pos_y, int n_frames);
/** Updates the Food and Snake classes from the Gamepad
*/
@@ -125,6 +136,21 @@
*/
void set_even_array(Gamepad &pad);
+
+ /** Decreses the amount of frames between spawning of food
+ * @param determines the interval and amount of tail segments are reached
+ before the time between spawning reduces
+ */
+
+ int food_spawning_time(int frame_decrementer);
+
+ /** Changes the frame rate increasing speed
+ * @param determines much food is eaten before increasing the speed
+ * @param defines the increase in speed
+ */
+
+ void increase_speed(int resetter, float speed_decrementer);
+
private:
@@ -141,19 +167,15 @@
Direction _cur;
bool _collision;
-
- static int _odd_array_x[100];
- static int _odd_array_y[100];
- static int _even_array_x[100];
- static int _even_array_y[100];
- int _array_x[100];
- int _array_y[100];
+ int _n_frames;
int _tail_x;
int _tail_y;
int _number_food_frames;
+ void _wall_col_isr();
+
};
#endif
\ No newline at end of file
--- a/main.cpp Tue May 01 12:20:42 2018 +0000
+++ b/main.cpp Tue May 08 10:52:12 2018 +0000
@@ -5,12 +5,19 @@
#include "Testing.h"
#include "SnakeEngine.h"
+
+
#define FOOD_X 20
#define FOOD_Y 20
#define SNAKE_P_X 42
#define SNAKE_P_Y 24
#define IN E
#define CUR E
+#define SPAWN_TIME 200
+
+#ifndef WITH_TESTING
+#include "tests.h"
+//#endif
struct UserInput
{
@@ -36,29 +43,36 @@
Gamepad pad;
Snake snake;
N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
-Testing test1;
+//Testing test1;
// ----- Prototypes -----
void init();
void update_game(UserInput input);
void render();
-void start_menu();
+void start_screen();
int main() {
+//#ifdef WITH_TESTING
+ int number_of_failures = run_tests();
+
+ if(number_of_failures > 0) return number_of_failures;
+#endif
init();
+ start_screen();
render();
wait(0.2);
while(1) {
-
+
+
snake_engine.get_input(pad);
snake_engine.update(pad);
render();
- wait(0.2);
+ wait(0.15);
}
}
@@ -71,7 +85,7 @@
pad.init();
// initialise the game with food, snake...
- snake_engine.init(IN, CUR, SNAKE_P_X, SNAKE_P_Y);
+ snake_engine.init(IN, CUR, SNAKE_P_X, SNAKE_P_Y, SPAWN_TIME);
}
@@ -85,8 +99,37 @@
}
-void start_menu()
+void start_screen()
{
-
-
+ int i = 0;
+ lcd.setContrast(0.5);
+ for(i = 0; i <= 4; ++i) {
+
+ lcd.printString(" NOTENDO ", 0,i);
+ lcd.refresh();
+ wait(0.3);
+ lcd.clear();
+
+
+ if(i == 4) {
+
+ lcd.printString(" NOTENDO ", 0,5);
+ lcd.refresh();
+ pad.tone(0,1.75);
+ wait(1.5);
+ lcd.clear();
+
+ }
+
+ }
+
+ while ( pad.check_event(Gamepad::START_PRESSED) == false ) {
+
+
+ lcd.printString(" Welcome ", 0, 1);
+ lcd.printString(" Press Start ", 0, 3);
+ lcd.refresh();
+
+ }
+
}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tests.h Tue May 08 10:52:12 2018 +0000
@@ -0,0 +1,79 @@
+#ifndef TESTS_H
+#define TESTS_H
+
+#include "Food-test.h"
+#include "Snake-test.h"
+
+/**
+* @brief Runs tests on Food, Snake, and SnakeEngine classes
+* @return Returns number of tests failed
+*/
+
+
+int run_tests()
+{
+
+ int tests_failed = 0; // number of failed tests
+
+ // run food position tests
+
+ printf("Running Food Positions Tests \n");
+
+ bool food_test_passed = food_test_position();
+ bool snake_test_passed = snake_test_movement();
+
+ if(food_test_passed) {
+
+ printf("FOOD TEST PASSED \n");
+
+ }
+
+ else {
+
+ printf("FOOD TEST FAILED \n");
+
+ ++tests_failed;
+
+ }
+
+ if(snake_test_passed) {
+
+ printf("SNAKE TEST PASSED \n");
+
+ }
+
+ else {
+
+ printf("SNAKE TEST FAILED \n");
+
+ ++tests_failed;
+
+ }
+
+
+
+
+ // print how many tests failed or all passed
+
+ if(tests_failed > 0) {
+
+ printf("%i tests failed \n", tests_failed);
+
+ }
+
+ else {
+
+
+ printf("All passed");
+
+ }
+
+ return tests_failed;
+
+}
+
+
+
+
+
+#endif
\ No newline at end of file