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 wave_player 4DGL-uLCD-SE MMA8452
main.cpp
- Committer:
- ajorgih3
- Date:
- 2020-11-16
- Revision:
- 3:bb6f73642f01
- Parent:
- 2:4947d6a82971
- Child:
- 4:697e1120f821
File content as of revision 3:bb6f73642f01:
//=================================================================
// The main program file.
//
// Copyright 2020 Georgia Tech. All rights reserved.
// The materials provided by the instructor in this course are for
// the use of the students currently enrolled in the course.
// Copyrighted course materials may not be further disseminated.
// This file must not be made publicly available anywhere.
//==================================================================
// Project includes
#include "globals.h"
#include "hardware.h"
#include "map.h"
#include "graphics.h"
#include "snake.h"
#include <math.h>
#include<stdio.h>
#define CITY_HIT_MARGIN 1
#define CITY_UPPER_BOUND (SIZE_Y-(LANDSCAPE_HEIGHT+MAX_BUILDING_HEIGHT))
// Helper function declarations
void playSound(char* wav);
/**
* The main game state. Must include snake locations and previous locations for
* drawing to work properly. Other items can be added as needed.
*/
/**
* Given the game inputs, determine what kind of update needs to happen.
* Possbile return values are defined below.
*/
Snake snake;
int score;
GameInputs input;
int main();
void go_right();
void go_left();
void go_up();
void go_down();
int previousState;
int thisState;
int lives = 0;
int main2();
MapItem *nextHead;
// Function prototypes
/**
* Given the game inputs, determine what kind of update needs to happen.
* Possible return values are defined below.
*/
#define NO_RESULT 0
#define NO_ACTION 0
#define ACTION_BUTTON 1
#define MENU_BUTTON 2
#define GO_LEFT 3
#define GO_RIGHT 4
#define GO_UP 5
#define GO_DOWN 6
#define GAME_OVER 7
#define FULL_DRAW 8
#define INVINCIBILITY 9
#define NOT_UP 10
#define IN_GAME_MENU 11
#define NOT_RIGHT 12
// Get Actions from User (push buttons & accelerometer)
// Based on push button and accelerometer inputs, determine which action
// needs to be performed (may be no action).
int get_action(GameInputs inputs)
{
// threshold for moving right
if (inputs.ax >= 0.30 && inputs.ax <= 0.60) {
return GO_RIGHT;
}
// threshold for moving left
if (inputs.ax <= -0.30 && inputs.ax >= -0.60) {
return GO_LEFT;
}
// threshold for moving up
if (inputs.ay >= 0.30 && inputs.ay <= 0.60) {
return GO_UP;
}
// threshold for moving down
if (inputs.ay <= -0.30 && inputs.ay >= -0.60) {
return GO_DOWN;
}
if (!inputs.b1) {
return INVINCIBILITY;
}
// if we do not satisfy these conditions then
// no action is done
return NO_ACTION;
}
/**
* Update the game state based on the user action. For example, if the user
* requests GO_UP, then this function should determine if that is possible by
* consulting the map, and update the snake position accordingly.
*
* Return values are defined below. FULL_DRAW indicates that for this frame,
* draw_game should not optimize drawing and should draw every tile, even if
* the snake has not moved.
*/
/**
* Draw the upper status bar.
*/
void draw_upper_status(int x, int y, int z)
{
uLCD.line(0, 9, 127, 9, GREEN);
uLCD.text_width(1);
uLCD.text_height(1);
uLCD.color(0xFFFF00);
uLCD.locate(0,0);
uLCD.printf("POS:%d,%d", x, y);
uLCD.locate(10,0);
uLCD.printf("SCORE:%d", score);
}
/**
* Draw the lower status bar.
*/
void draw_lower_status(int lives)
{
uLCD.line(0, 118, 127, 118, 0xFFFF00);
uLCD.text_width(1);
uLCD.text_height(1);
uLCD.color(0xFFFF00);
uLCD.locate(0,15);
uLCD.printf("LIVES:%d", lives);
}
/**
* Draw the border for the map.
*/
void draw_border()
{
uLCD.filled_rectangle(0, 9, 127, 14, WHITE); // Top
uLCD.filled_rectangle(0, 13, 2, 114, WHITE); // Left
uLCD.filled_rectangle(0, 114, 127, 117, WHITE); // Bottom
uLCD.filled_rectangle(124, 14, 127, 117, WHITE); // Right
}
/**
* Entry point for frame drawing. This should be called once per iteration of
* the game loop. This draws all tiles on the screen, followed by the status
* bars. Unless init is nonzero, this function will optimize drawing by only
* drawing tiles that have changed from the previous frame.
*/
void draw_game(int draw_option)
{
// prints game over if the player loses.
if (draw_option == GAME_OVER) {
int u = 58;
int v = 56;
uLCD.filled_rectangle(0, 0, 255, 255, BLACK);
uLCD.text_width(1);
uLCD.text_height(1);
uLCD.locate(5,7);
uLCD.color(RED);
uLCD.printf("GAME OVER\n");
draw_snake_head(u+22, v+11);
draw_snake_tail(u-22, v+11);
uLCD.textbackground_color(BLACK);
uLCD.color(0xFFFFFF);
uLCD.text_width(1);
uLCD.text_height(1);
uLCD.locate(4, 10);
uLCD.printf("Press 'B2'\n");
uLCD.locate(5, 11);
uLCD.printf("to retry!\n");
do {
input = read_inputs();
} while(input.b2);
uLCD.filled_rectangle(0, 0, 255, 255, BLACK);
main();
}
// Draw game border first
if(draw_option == FULL_DRAW)
{
draw_border();
int u = 58;
int v = 59;
snake.head_pi = u;
snake.head_pj = v;
snake.body_pi = u - 11;
snake.body_pj = v;
snake.tail_pi = u - 22;
snake.tail_pj = v;
draw_snake_head(u, v);
draw_snake_body(u-11, v);
draw_snake_tail(u-22, v);
return;
}
// Iterate over all visible map tiles
for (int i = -5; i <= 5; i++) { // Iterate over columns of tiles
for (int j = -4; j <= 4; j++) { // Iterate over one column of tiles
// Here, we have a given (i,j)
// Compute the current map (x,y) of this tile
int x = i + snake.head_x;
int y = j + snake.head_y;
// Compute the previous map (px, py) of this tile
int px = i + snake.head_px;
int py = j + snake.head_py;
// Compute u,v coordinates for drawing
int u = (i+5)*11 + 3;
int v = (j+4)*11 + 15;
// Figure out what to draw
DrawFunc draw = NULL;
if (x >= 0 && y >= 0 && x < map_width() && y < map_height()) { // Current (i,j) in the map
MapItem* curr_item = get_here(x, y);
MapItem* prev_item = get_here(px, py);
if (draw_option || curr_item != prev_item) { // Only draw if they're different
if (curr_item) { // There's something here! Draw it
draw = curr_item->draw;
} else { // There used to be something, but now there isn't
draw = draw_nothing;
}
} else if (curr_item && curr_item->type == CLEAR) {
// This is a special case for erasing things like doors.
draw = curr_item->draw; // i.e. draw_nothing
}
} else if (draw_option) { // If doing a full draw, but we're out of bounds, draw the walls.
draw = draw_wall;
}
// Actually draw the tile
if (draw) draw(u, v);
}
}
// Draw status bars
draw_upper_status(snake.head_x, snake.head_y, score);
draw_lower_status(lives);
}
void start_game() {
int u = 58;
int v = 56;
uLCD.textbackground_color(GREEN);
uLCD.filled_rectangle(0, 9, 127, 14, WHITE); // Top
uLCD.filled_rectangle(0, 13, 2, 114, WHITE); // Left
uLCD.filled_rectangle(0, 114, 127, 117, WHITE); // Bottom
uLCD.filled_rectangle(124, 14, 127, 117, WHITE); // Right
uLCD.text_width(1);
uLCD.text_height(1);
uLCD.locate(2, 3);
uLCD.color(RED);
uLCD.printf("-Sneaky Snakey-");
uLCD.textbackground_color(BLUE);
uLCD.text_width(1);
uLCD.text_height(1);
uLCD.locate(5, 4);
uLCD.color(0xFFFF00);
uLCD.printf("ECE 2035");
uLCD.locate(7,5);
draw_snake_head(u, v);
draw_snake_body(u-11, v);
draw_snake_tail(u-22, v);
draw_goodie(u+22, v);
uLCD.textbackground_color(BLACK);
uLCD.color(0xFFFFFF);
uLCD.text_width(1);
uLCD.text_height(1);
uLCD.locate(4, 10);
uLCD.printf("Press 'B2'\n");
uLCD.locate(5, 11);
uLCD.printf("to start!\n");
do {
input = read_inputs();
} while(input.b2);
uLCD.filled_rectangle(0, 0, 255, 255, BLACK);
}
void title_page() {
int u = 58;
int v = 56;
uLCD.filled_rectangle(0, 9, 127, 14, 0xFFFF00); // Top
uLCD.filled_rectangle(0, 13, 2, 114, 0xFFFF00); // Left
uLCD.filled_rectangle(0, 114, 127, 117, 0xFFFF00); // Bottom
uLCD.filled_rectangle(124, 14, 127, 117, 0xFFFF00); // Right
uLCD.text_width(1);
uLCD.text_height(1);
uLCD.locate(5, 3);
uLCD.color(RED);
uLCD.printf("OBJECTIVE");
uLCD.text_width(1);
uLCD.text_height(1);
uLCD.locate(2, 4);
uLCD.color(0xFFFFFF);
uLCD.printf("Eat 20 goodies!");
uLCD.locate(7,5);
draw_goodie(u, v);
draw_goodie(u-22, v);
draw_goodie(u+22, v);
uLCD.textbackground_color(BLACK);
uLCD.color(0xFFFFFF);
uLCD.text_width(1);
uLCD.text_height(1);
uLCD.locate(3, 10);
uLCD.printf("Press 'B2'\n");
uLCD.locate(3, 11);
uLCD.printf("to continue!\n");
do {
input = read_inputs();
} while(input.b2);
uLCD.filled_rectangle(0, 0, 255, 255, BLACK);
}
void helpful_items() {
int u = 58;
int v = 56;
uLCD.filled_rectangle(0, 9, 127, 14, GREEN); // Top
uLCD.filled_rectangle(0, 13, 2, 114, GREEN); // Left
uLCD.filled_rectangle(0, 114, 127, 117, GREEN); // Bottom
uLCD.filled_rectangle(124, 14, 127, 117, GREEN); // Right
uLCD.text_width(1);
uLCD.text_height(1);
uLCD.locate(6, 3);
uLCD.color(0xFFFF00);
uLCD.printf("BUFFS");
uLCD.text_width(1);
uLCD.text_height(1);
uLCD.locate(3, 4);
uLCD.color(0xFFFFFF);
uLCD.printf("Extra Life!");
uLCD.text_width(1);
uLCD.text_height(1);
uLCD.locate(2, 5);
uLCD.color(0xFFFFFF);
uLCD.printf("Invincibility");
draw_life(u-22, v);
draw_shield(u+22, v);
uLCD.textbackground_color(BLACK);
uLCD.color(0xFFFFFF);
uLCD.text_width(1);
uLCD.text_height(1);
uLCD.locate(3, 10);
uLCD.printf("Press 'B2'\n");
uLCD.locate(3, 11);
uLCD.printf("to continue!\n");
do {
input = read_inputs();
} while(input.b2);
uLCD.filled_rectangle(0, 0, 255, 255, BLACK);
}
void harmful_items() {
int u = 58;
int v = 56;
uLCD.filled_rectangle(0, 9, 127, 14, RED); // Top
uLCD.filled_rectangle(0, 13, 2, 114, RED); // Left
uLCD.filled_rectangle(0, 114, 127, 117, RED); // Bottom
uLCD.filled_rectangle(124, 14, 127, 117, RED); // Right
uLCD.text_width(1);
uLCD.text_height(1);
uLCD.locate(5, 3);
uLCD.color(0xFFA500);
uLCD.printf("DEBUFFS");
uLCD.text_width(1);
uLCD.text_height(1);
uLCD.locate(4, 4);
uLCD.color(0xFFFFFF);
uLCD.printf("Slowflakes");
uLCD.text_width(1);
uLCD.text_height(1);
uLCD.locate(3, 5);
uLCD.color(0xFFFFFF);
uLCD.printf("Life drainer");
draw_snowflake(u-22, v);
draw_sword(u+22, v);
uLCD.textbackground_color(BLACK);
uLCD.color(0xFFFFFF);
uLCD.text_width(1);
uLCD.text_height(1);
uLCD.locate(3, 10);
uLCD.printf("Press 'B2'\n");
uLCD.locate(3, 11);
uLCD.printf("to continue!\n");
do {
input = read_inputs();
} while(input.b2);
uLCD.filled_rectangle(0, 0, 255, 255, BLACK);
}
/**
* Initialize the main world map. Add walls around the edges, interior chambers,
* and plants in the background so you can see motion.
*/
void init_main_map()
{
// "Random" plants
Map* map = set_active_map(0);
pc.printf("plants\r\n");
add_sword(10, 10);
add_goodie(8, 4);
add_goodie(11, 3);
add_goodie(5, 3);
add_shield(8, 7);
add_life(14,15);
add_snowflake(20, 20);
add_plant(7, 19);
add_snake_head(5, 4);
pc.printf("Adding walls!\r\n");
add_wall(0, 0, HORIZONTAL, map_width());
add_wall(0, map_height()-1, HORIZONTAL, map_width());
add_wall(0, 0, VERTICAL, map_height());
add_wall(map_width()-1, 0, VERTICAL, map_height());
pc.printf("Walls done!\r\n");
//add_snake_head(snake.locations[0].x, snake.locations[0].y);
//add_snake_body(snake.locations[1].x, snake.locations[1].y);
//add_snake_tail(snake.locations[2].x, snake.locations[2].y);
pc.printf("Add extra chamber\r\n");
add_wall(30, 0, VERTICAL, 10);
add_wall(30, 10, HORIZONTAL, 10);
add_wall(39, 0, VERTICAL, 10);
pc.printf("Added!\r\n");
// Add stairs to chamber (map 1)
//add_stairs(15, 5, 1, 5, 5);
// profile_hashtable();
print_map();
}
void go_right() {
if (previousState == 0 || previousState == GO_RIGHT) {
snake.head_x += 1;
snake.head_y += 0;
snake.body_x = snake.head_px;
snake.tail_x = snake.body_px;
snake.body_y = snake.head_py;
snake.tail_y = snake.body_py;
previousState = GO_RIGHT;
}
if (previousState == GO_UP || thisState == NOT_UP || previousState == GO_DOWN) {
if (snake.head_pj != snake.body_pj && (previousState == GO_UP || thisState == NOT_UP)) {
draw_nothing(snake.tail_pi, snake.tail_pj);
draw_nothing(snake.body_pi, snake.body_pj);
draw_nothing(snake.head_pi, snake.head_pj);
snake.body_pi = snake.head_pi - 11;
snake.body_pj = snake.head_pj;
draw_snake_body(snake.body_pi, snake.body_pj);
snake.tail_pi = snake.body_pi;
snake.tail_pj = snake.body_pj + 11;
draw_snake_tail(snake.tail_pi, snake.tail_pj);
snake.head_pi = snake.head_pi;
snake.head_pj = snake.head_pj;
draw_snake_head(snake.head_pi, snake.head_pj);
snake.tail_x = snake.body_px;
snake.tail_y = snake.body_py;
snake.body_x = snake.head_px;
snake.body_y = snake.head_py;
snake.head_x += 0;
snake.head_y -= 0;
thisState = NOT_RIGHT;
}
else if (previousState == GO_DOWN && (thisState == NOT_RIGHT)) {
if (snake.head_pj != snake.body_pj) {
draw_nothing(snake.tail_pi, snake.tail_pj);
draw_nothing(snake.body_pi, snake.body_pj);
draw_nothing(snake.head_pi, snake.head_pj);
snake.body_pi = snake.head_pi - 11;
snake.body_pj = snake.head_pj;
draw_snake_body(snake.body_pi, snake.body_pj);
snake.tail_pi = snake.body_pi;
snake.tail_pj = snake.body_pj - 11;
draw_snake_tail(snake.tail_pi, snake.tail_pj);
snake.head_pi = snake.head_pi;
snake.head_pj = snake.head_pj;
draw_snake_head(snake.head_pi, snake.head_pj);
snake.tail_x = snake.body_px;
snake.tail_y = snake.body_py;
snake.body_x = snake.head_px;
snake.body_y = snake.head_py;
snake.head_x += 0;
snake.head_y -= 0;
}
}
else {
draw_nothing(snake.tail_pi, snake.tail_pj);
draw_nothing(snake.body_pi, snake.body_pj);
draw_nothing(snake.head_pi, snake.head_pj);
snake.body_pi = snake.body_pi;
snake.body_pj = snake.body_pj;
draw_snake_body(snake.body_pi, snake.body_pj);
snake.tail_pi = snake.body_pi - 11;
snake.tail_pj = snake.body_pj;
draw_snake_tail(snake.tail_pi, snake.tail_pj);
snake.head_pi = snake.head_pi;
snake.head_pj = snake.head_pj;
draw_snake_head(snake.head_pi, snake.head_pj);
snake.tail_x = snake.body_px;
snake.tail_y = snake.body_py;
snake.body_x = snake.head_px;
snake.body_y = snake.head_py;
snake.head_x += 0;
snake.head_y -= 0;
}
}
if ((snake.head_pj == snake.tail_pj) && (snake.body_pj == snake.tail_pj)) {
previousState = GO_RIGHT;
}
wait(0.1);
}
void go_left() {
snake.head_x -= 1;
snake.body_x = snake.head_px;
snake.tail_x = snake.body_px;
snake.head_y += 0;
snake.body_y = snake.head_py;
snake.tail_y = snake.body_py;
}
void go_up() {
if (previousState == GO_UP) {
snake.head_x += 0;
snake.head_y -= 1;
snake.body_x = snake.head_px;
snake.tail_x = snake.body_px;
snake.body_y = snake.head_py;
snake.tail_y = snake.body_py;
}
if (previousState == 0 || previousState == GO_RIGHT) {
if (snake.head_pi != snake.body_pi) {
draw_nothing(snake.tail_pi, snake.tail_pj);
draw_nothing(snake.body_pi, snake.body_pj);
draw_nothing(snake.head_pi, snake.head_pj);
snake.body_pi = snake.head_pi;
snake.body_pj = snake.head_pj + 11;
draw_snake_body(snake.body_pi, snake.body_pj);
snake.tail_pi = snake.body_pi - 11;
snake.tail_pj = snake.body_pj;
draw_snake_tail(snake.tail_pi, snake.tail_pj);
snake.head_pi = snake.head_pi;
snake.head_pj = snake.head_pj;
draw_snake_head(snake.head_pi, snake.head_pj);
snake.tail_x = snake.body_px;
snake.tail_y = snake.body_py;
snake.body_x = snake.head_px;
snake.body_y = snake.head_py;
snake.head_x += 0;
snake.head_y -= 0;
thisState = NOT_UP;
}
else {
draw_nothing(snake.tail_pi, snake.tail_pj);
draw_nothing(snake.body_pi, snake.body_pj);
draw_nothing(snake.head_pi, snake.head_pj);
snake.body_pi = snake.body_pi;
snake.body_pj = snake.body_pj;
draw_snake_body(snake.body_pi, snake.body_pj);
snake.tail_pi = snake.body_pi;
snake.tail_pj = snake.body_pj + 11;
draw_snake_tail(snake.tail_pi, snake.tail_pj);
snake.head_pi = snake.head_pi;
snake.head_pj = snake.head_pj;
draw_snake_head(snake.head_pi, snake.head_pj);
snake.tail_x = snake.body_px;
snake.tail_y = snake.body_py;
snake.body_x = snake.head_px;
snake.body_y = snake.head_py;
snake.head_x += 0;
snake.head_y -= 0;
}
}
if ((snake.head_pi == snake.tail_pi) && (snake.body_pi == snake.tail_pi)) {
previousState = GO_UP;
}
wait(0.1);
}
void go_down() {
if (previousState == GO_DOWN) {
snake.head_x += 0;
snake.head_y += 1;
snake.body_x = snake.head_px;
snake.tail_x = snake.body_px;
snake.body_y = snake.head_py;
snake.tail_y = snake.body_py;
}
if (previousState == 0 || previousState == GO_RIGHT) {
if (snake.head_pi != snake.body_pi) {
draw_nothing(snake.tail_pi, snake.tail_pj);
draw_nothing(snake.body_pi, snake.body_pj);
draw_nothing(snake.head_pi, snake.head_pj);
snake.body_pi = snake.head_pi;
snake.body_pj = snake.head_pj - 11;
draw_snake_body(snake.body_pi, snake.body_pj);
snake.tail_pi = snake.body_pi - 11;
snake.tail_pj = snake.body_pj;
draw_snake_tail(snake.tail_pi, snake.tail_pj);
snake.head_pi = snake.head_pi;
snake.head_pj = snake.head_pj;
draw_snake_head(snake.head_pi, snake.head_pj);
snake.tail_x = snake.body_px;
snake.tail_y = snake.body_py;
snake.body_x = snake.head_px;
snake.body_y = snake.head_py;
snake.head_x += 0;
snake.head_y -= 0;
}
else {
draw_nothing(snake.tail_pi, snake.tail_pj);
draw_nothing(snake.body_pi, snake.body_pj);
draw_nothing(snake.head_pi, snake.head_pj);
snake.body_pi = snake.body_pi;
snake.body_pj = snake.body_pj;
draw_snake_body(snake.body_pi, snake.body_pj);
snake.tail_pi = snake.body_pi;
snake.tail_pj = snake.body_pj - 11;
draw_snake_tail(snake.tail_pi, snake.tail_pj);
snake.head_pi = snake.head_pi;
snake.head_pj = snake.head_pj;
draw_snake_head(snake.head_pi, snake.head_pj);
snake.tail_x = snake.body_px;
snake.tail_y = snake.body_py;
snake.body_x = snake.head_px;
snake.body_y = snake.head_py;
snake.head_x += 0;
snake.head_y -= 0;
}
}
if ((snake.head_pi == snake.tail_pi) && (snake.body_pi == snake.tail_pi)) {
previousState = GO_DOWN;
wait(0.1);
}
}
int update_game(int action)
{
//MapItem *thisHead;
MapItem *nextBody;
snake.head_px = snake.head_x;
snake.body_px = snake.body_x;
snake.tail_px = snake.tail_x;
snake.head_py = snake.head_y;
snake.body_py = snake.body_y;
snake.tail_py = snake.head_y;
switch(action) {
case GO_RIGHT:
nextHead = get_east(snake.head_px, snake.head_py);
pc.printf("GO RIGHT\n");
if (nextHead->walkable) {
go_right();
}
if (!nextHead->walkable) {
draw_game(GAME_OVER);
}
break;
case GO_LEFT:
nextBody = get_west(snake.body_px, snake.body_py);
pc.printf("GO LEFT\n");
if (nextHead->walkable) {
go_left();
}
if (!nextBody->walkable) {
draw_game(GAME_OVER);
}
break;
case GO_UP:
nextHead = get_north(snake.head_x, snake.head_y);
pc.printf("GO UP\n");
if (nextHead->walkable) {
go_up();
}
if (!nextHead->walkable) {
draw_game(GAME_OVER);
}
break;
case GO_DOWN:
nextHead = get_south(snake.head_px, snake.head_py);
pc.printf("GO DOWN\n");
if (nextHead->walkable) {
go_down();
}
break;
case IN_GAME_MENU:
pc.printf("Menu Pressed!");
uLCD.filled_rectangle(0, 0, 255, 255, BLACK);
title_page();
helpful_items();
harmful_items();
main2();
break;
default: break;
}
return NO_RESULT;
}
/**
* Program entry point! This is where it all begins.
* This function or all the parts of the game. Most of your
* implementation should be elsewhere - this holds the game loop, and should
* read like a road map for the rest of the code.
*/
int main()
{
// First things first: initialize hardware
ASSERT_P(hardware_init() == ERROR_NONE, "Hardware init failed!");
start_game();
//title_page();
//helpful_items();
//harmful_items();
// loading screen
//uLCD.filled_rectangle(0, 0, 255, 255, BLACK);
//uLCD.textbackground_color(BLACK);
//uLCD.color(0xFFFFFF);
//uLCD.locate(4,7);
//uLCD.printf("GOOD LUCK!");
//uLCD.locate(4,8);
//uLCD.printf("Loading...");
//wait(4);
//uLCD.filled_rectangle(0, 0, 255, 255, BLACK);
snake_init(&snake);
// 0. Initialize the maps -- implement this function:
maps_init();
init_main_map();
// Initialize game state
set_active_map(0);
snake.head_x = snake.head_y = 5;
snake.body_x = 4;
snake.body_y = 5;
snake.tail_x = 3;
snake.tail_y = 5;
// Initial drawing
draw_game(FULL_DRAW);
// Main game loop
while(1) {
// Timer to measure game update speed
Timer t;
t.start();
// 1. Read inputs -- implement this function:
GameInputs inputs = read_inputs();
// 2. Determine action (move, act, menu, etc.) -- implement this function:
int action = get_action(inputs);
// 3. Update game -- implement this function:
int result = update_game(action);
// 3b. Check for game over based on result
// and if so, handle game over -- implement this.
// 4. Draw screen -- provided:
draw_game(result);
// Compute update time
t.stop();
int dt = t.read_ms();
// Display and wait
// NOTE: Text is 8 pixels tall
if (dt < 100) wait_ms(100 - dt);
}
}
int main2() {
//uLCD.filled_rectangle(0, 0, 255, 255, BLACK);
//snake_init(&snake);
// 0. Initialize the maps -- implement this function:
//maps_init();
//init_main_map();
// Initialize game state
set_active_map(0);
snake.head_x = snake.head_y = 5;
snake.body_x = 4;
snake.body_y = 5;
snake.tail_x = 3;
snake.tail_y = 5;
// Initial drawing
draw_game(FULL_DRAW);
// Main game loop
while(1) {
// Timer to measure game update speed
Timer t;
t.start();
// 1. Read inputs -- implement this function:
GameInputs inputs = read_inputs();
// 2. Determine action (move, act, menu, etc.) -- implement this function:
int action = get_action(inputs);
// 3. Update game -- implement this function:
int result = update_game(action);
// 3b. Check for game over based on result
// and if so, handle game over -- implement this.
// 4. Draw screen -- provided:
draw_game(result);
// Compute update time
t.stop();
int dt = t.read_ms();
// Display and wait
// NOTE: Text is 8 pixels tall
if (dt < 100) wait_ms(100 - dt);
}
}
// Plays a wavfile
void playSound(char* wav)
{
}