Fixed bug when postion array run out of space. Documented the defines...
This commit is contained in:
39
src/game.c
39
src/game.c
@@ -169,11 +169,14 @@ bool game_player_update(game_t* game, player_t* player, uint8_t pixels){
|
|||||||
|
|
||||||
// Check if player is alive
|
// Check if player is alive
|
||||||
if(player->state != alive){
|
if(player->state != alive){
|
||||||
return state_changed; // If player is dead return state
|
return false; //Dont continue executing with the rest of the function. State has not changed (false)
|
||||||
}
|
}
|
||||||
// Change direction
|
// Change direction
|
||||||
if(direction_change) {
|
if(direction_change) {
|
||||||
player_append_position(player,player->position); // Append new position if direction has changed
|
if(!player_append_position(player,player->position)) { // If appending new position failed
|
||||||
|
player->state = dead; // Set player state to dead, even if he didn't made an error
|
||||||
|
return true; //state changed
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(pixels) {
|
if(pixels) {
|
||||||
@@ -189,7 +192,7 @@ bool game_player_update(game_t* game, player_t* player, uint8_t pixels){
|
|||||||
player->position.y+=pixels;
|
player->position.y+=pixels;
|
||||||
LCD_DrawRectF( player->position.x,
|
LCD_DrawRectF( player->position.x,
|
||||||
last_point.y,
|
last_point.y,
|
||||||
PLAYER_WIDTH,
|
0,
|
||||||
player->position.y - last_point.y,
|
player->position.y - last_point.y,
|
||||||
player->color);
|
player->color);
|
||||||
break;
|
break;
|
||||||
@@ -198,14 +201,14 @@ bool game_player_update(game_t* game, player_t* player, uint8_t pixels){
|
|||||||
LCD_DrawRectF( player->position.x,
|
LCD_DrawRectF( player->position.x,
|
||||||
player->position.y,
|
player->position.y,
|
||||||
last_point.x -player->position.x,
|
last_point.x -player->position.x,
|
||||||
PLAYER_WIDTH,
|
0,
|
||||||
player->color);
|
player->color);
|
||||||
break;
|
break;
|
||||||
case up: // render up
|
case up: // render up
|
||||||
player->position.y-=pixels;
|
player->position.y-=pixels;
|
||||||
LCD_DrawRectF( player->position.x,
|
LCD_DrawRectF( player->position.x,
|
||||||
player->position.y,
|
player->position.y,
|
||||||
PLAYER_WIDTH,
|
0,
|
||||||
last_point.y - player->position.y,
|
last_point.y - player->position.y,
|
||||||
player->color);
|
player->color);
|
||||||
break;
|
break;
|
||||||
@@ -214,7 +217,7 @@ bool game_player_update(game_t* game, player_t* player, uint8_t pixels){
|
|||||||
LCD_DrawRectF( last_point.x,
|
LCD_DrawRectF( last_point.x,
|
||||||
player->position.y,
|
player->position.y,
|
||||||
player->position.x - last_point.x,
|
player->position.x - last_point.x,
|
||||||
PLAYER_WIDTH,
|
0,
|
||||||
player->color);
|
player->color);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -484,25 +487,27 @@ bool game_step_running(game_t* game, uint64_t delta_time)
|
|||||||
bool game_step_ended(game_t* game) {
|
bool game_step_ended(game_t* game) {
|
||||||
|
|
||||||
// Kill screen
|
// Kill screen
|
||||||
|
// Border of Kill Screen (White, 1px)
|
||||||
LCD_DrawRect(TG_END_LEFT, // left top x
|
LCD_DrawRect(TG_END_LEFT, // left top x
|
||||||
TG_END_TOP, // left top y
|
TG_END_TOP, // left top y
|
||||||
(TFT_WIDTH - TG_END_LEFT - TG_END_RIGHT - 1), // right bottom x
|
(TFT_WIDTH - TG_END_LEFT - TG_END_RIGHT - 1), // width of the rect
|
||||||
(TFT_HEIGHT - TG_END_TOP - TG_END_BOTTOM - 1), // right bottom y
|
(TFT_HEIGHT - TG_END_TOP - TG_END_BOTTOM - 1), //height of the rect
|
||||||
GUI_COLOR_WHITE); // Color of the boundary
|
GUI_COLOR_WHITE); // Color of the boundary
|
||||||
LCD_DrawRectF(TG_END_LEFT + 1, // left top x
|
//Filling of Kill Screen (Gray)
|
||||||
TG_END_TOP + 1, // left top y
|
LCD_DrawRectF(TG_END_LEFT + 1,
|
||||||
(TFT_WIDTH - TG_END_LEFT - TG_END_RIGHT - 3), // right bottom x
|
TG_END_TOP + 1,
|
||||||
(TFT_HEIGHT - TG_END_TOP - TG_END_BOTTOM - 3), // right bottom y
|
(TFT_WIDTH - TG_END_LEFT - TG_END_RIGHT - 3),
|
||||||
GUI_COLOR_DARK_GREY); // Color of the boundary
|
(TFT_HEIGHT - TG_END_TOP - TG_END_BOTTOM - 3),
|
||||||
|
GUI_COLOR_DARK_GREY); // Color of the filling
|
||||||
|
|
||||||
LCD_SetTextColor(GUI_COLOR_BLACK);
|
LCD_SetTextColor(GUI_COLOR_BLACK);
|
||||||
LCD_SetBackColor(GUI_COLOR_DARK_GREY);
|
LCD_SetBackColor(GUI_COLOR_DARK_GREY);
|
||||||
LCD_SetFont(&font_9x15B);
|
LCD_SetFont(&font_9x15B);
|
||||||
LCD_DisplayStringXY(TG_END_LEFT + TG_START_FONT_OFFSET_Y,
|
LCD_DisplayStringXY(TG_END_LEFT + TG_END_FONT_OFFSET,
|
||||||
TG_END_TOP + TG_START_FONT_OFFSET_Y,
|
TG_END_TOP + TG_END_FONT_OFFSET,
|
||||||
"Game over!");
|
"Game over!");
|
||||||
LCD_DisplayStringXY(TG_END_LEFT + TG_START_FONT_OFFSET_Y,
|
LCD_DisplayStringXY(TG_END_LEFT + TG_END_FONT_OFFSET,
|
||||||
TG_END_TOP + TG_START_FONT_OFFSET_Y + 20,
|
TG_END_TOP + TG_END_FONT_OFFSET + TG_END_FONT_HEIGHT,
|
||||||
"Press T0 to restart.");
|
"Press T0 to restart.");
|
||||||
LCD_SetBackColor(GUI_COLOR_BLACK);
|
LCD_SetBackColor(GUI_COLOR_BLACK);
|
||||||
LCD_SetFont(&font_5x8);
|
LCD_SetFont(&font_5x8);
|
||||||
|
|||||||
68
src/game.h
68
src/game.h
@@ -6,46 +6,64 @@
|
|||||||
#include<stdbool.h>
|
#include<stdbool.h>
|
||||||
#include"player.h"
|
#include"player.h"
|
||||||
|
|
||||||
// Player definitions
|
|
||||||
#define PLAYER_COUNT 2
|
|
||||||
#define PLAYER_WIDTH 0 // Don't change
|
|
||||||
|
|
||||||
// Speed definitions
|
//---- Section 1 - General ------------------------------------------------------------------------------
|
||||||
#define SPEED_SLOW 75
|
//---- Section 1.1 - Player defintions -----------------------------------------------------------------
|
||||||
#define SPEED_FAST 5
|
#define PLAYER_COUNT 2 //Number of players in the game (incrementing this, will probably require some changes in game.c as well)
|
||||||
|
|
||||||
// Button definitions
|
//---- Section 1.2 - Speed definitions ------------------------------------------------------------------
|
||||||
#define BTN_START 0
|
#define SPEED_SLOW 75 //Slowest game speed (ticks per pixel)
|
||||||
|
#define SPEED_FAST 5 //Fastest game speed (ticks per pixel)
|
||||||
|
|
||||||
|
//---- Section 1.3 - Button definitions -----------------------------------------------------------------
|
||||||
|
// Each of the following defines must be number between 0-3 (for T0...T3 on Carme)
|
||||||
|
#define BTN_START 0 //Button to start the game, restart, continue
|
||||||
|
// Buttons to control the players
|
||||||
#define BTN_PLAYER_1_LEFT 3
|
#define BTN_PLAYER_1_LEFT 3
|
||||||
#define BTN_PLAYER_1_RIGHT 2
|
#define BTN_PLAYER_1_RIGHT 2
|
||||||
#define BTN_PLAYER_2_LEFT 1
|
#define BTN_PLAYER_2_LEFT 1
|
||||||
#define BTN_PLAYER_2_RIGHT 0
|
#define BTN_PLAYER_2_RIGHT 0
|
||||||
|
|
||||||
// Display definitions
|
|
||||||
|
//---- Section 2 - Display & Pixel Geometry -------------------------------------------------------------
|
||||||
|
//---- Section 2.1 - During Active Game ----------------------------------------------------------------
|
||||||
|
|
||||||
|
//Margins of the Game field to the display border
|
||||||
#define TG_FIELD_TOP 20
|
#define TG_FIELD_TOP 20
|
||||||
#define TG_FIELD_BOTTOM 5
|
#define TG_FIELD_BOTTOM 5
|
||||||
#define TG_FIELD_LEFT 5
|
#define TG_FIELD_LEFT 5
|
||||||
#define TG_FIELD_RIGHT 5
|
#define TG_FIELD_RIGHT 5
|
||||||
#define TG_FIELD_START_OFFSET 10
|
|
||||||
#define TG_END_TOP 50
|
#define TG_FIELD_START_OFFSET 10 //Offset from the game-border to the start position of the players
|
||||||
#define TG_END_BOTTOM 30
|
|
||||||
|
#define TG_HEADER_TIME_X 5 //x position of the Time-Text in the game-header
|
||||||
|
#define TG_HEADER_TIME_Y 3 //y position of the Time-Text in the game-header
|
||||||
|
#define TG_HEADER_PLAYER_X 100 //x position of the Player1 state-text in the game-header
|
||||||
|
#define TG_HEADER_PLAYER_Y 3 //y position of the Player state-texts in the game-header
|
||||||
|
#define TG_HEADER_PLAYER_WIDTH 100 //width of one Player state-text
|
||||||
|
|
||||||
|
//---- Section 2.2 - During an ended game (kill screen) ------------------------------------------------
|
||||||
|
//Margins of the kill screen to the display border
|
||||||
|
#define TG_END_TOP 180
|
||||||
|
#define TG_END_BOTTOM 10
|
||||||
#define TG_END_LEFT 30
|
#define TG_END_LEFT 30
|
||||||
#define TG_END_RIGHT 30
|
#define TG_END_RIGHT 30
|
||||||
#define TG_HEADER_TIME_X 5
|
|
||||||
#define TG_HEADER_TIME_Y 3
|
#define TG_END_FONT_OFFSET 5 //Start offset (x,y) from the font to the topleft corner of the kill screen
|
||||||
#define TG_HEADER_PLAYER_X 100
|
#define TG_END_FONT_HEIGHT 20 //Height of the font (kill screen)
|
||||||
#define TG_HEADER_PLAYER_Y 3
|
|
||||||
#define TG_HEADER_PLAYER_WIDTH 100
|
|
||||||
#define TG_START_X 10
|
|
||||||
#define TG_START_Y 10
|
|
||||||
#define TG_START_BLOCK_HEIGHT 20
|
|
||||||
#define TG_START_BLOCK_SPACING 5
|
|
||||||
#define TG_START_FONT_OFFSET_Y 5
|
|
||||||
#define TG_START_FONT_HEIGHT 10
|
|
||||||
#define TG_START_COL2_X 90
|
|
||||||
#define TG_START_COL2_WIDTH 100
|
|
||||||
|
|
||||||
|
|
||||||
|
//---- Section 2.3 - Before the game start (config screen) ---------------------------------------------
|
||||||
|
#define TG_START_X 10 //x position of the content
|
||||||
|
#define TG_START_Y 10 //y position of the content
|
||||||
|
#define TG_START_BLOCK_HEIGHT 20 //height of one line of content (aka block)
|
||||||
|
#define TG_START_BLOCK_SPACING 5 //space between one line of content
|
||||||
|
#define TG_START_FONT_OFFSET_Y 5 //y offset for the font, to the start of the block
|
||||||
|
#define TG_START_FONT_HEIGHT 10 //height of the used font
|
||||||
|
#define TG_START_COL2_X 90 //the x position of the second content column
|
||||||
|
#define TG_START_COL2_WIDTH 100 //the width of the second content column
|
||||||
|
|
||||||
|
//----------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Game data type which contains all game data and players.
|
* @brief Game data type which contains all game data and players.
|
||||||
|
|||||||
@@ -15,8 +15,10 @@ void player_init(player_t* player, uint8_t btn_left, uint8_t btn_right,
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void player_append_position(player_t* player, point_t point) {
|
bool player_append_position(player_t* player, point_t point) {
|
||||||
if(player->num_positions < max_positions) {
|
if(player->num_positions < max_positions) {
|
||||||
player->past_positions[player->num_positions++] = point;
|
player->past_positions[player->num_positions++] = point;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,8 +3,9 @@
|
|||||||
|
|
||||||
#include<stdlib.h>
|
#include<stdlib.h>
|
||||||
#include<stdint.h>
|
#include<stdint.h>
|
||||||
|
#include<stdbool.h>
|
||||||
|
|
||||||
#define max_positions 320 // Maximum of points a player object is holding.
|
#define max_positions 500 // Maximum of points a player object is holding.
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Direction data type, used to specify the direction when moving.
|
* @brief Direction data type, used to specify the direction when moving.
|
||||||
@@ -61,7 +62,8 @@ void player_init(player_t* player, uint8_t btn_left, uint8_t btn_right, point_t
|
|||||||
*
|
*
|
||||||
* @param player Player object
|
* @param player Player object
|
||||||
* @param point Point to add to the player object
|
* @param point Point to add to the player object
|
||||||
|
* @return true on success
|
||||||
*/
|
*/
|
||||||
void player_append_position(player_t* player, point_t point);
|
bool player_append_position(player_t* player, point_t point);
|
||||||
|
|
||||||
#endif /* PLAYER_H */
|
#endif /* PLAYER_H */
|
||||||
|
|||||||
Reference in New Issue
Block a user