Skip to content
Permalink
Browse files

Working version, pending optimizations

  • Loading branch information...
brovador committed Jun 27, 2016
1 parent 183bb24 commit ba978f1de435ed707517111e3dfe7f9116752f8f
Showing with 246 additions and 80 deletions.
  1. +69 −80 src/main.c
  2. +65 −0 src/snake-map.c
  3. +112 −0 src/snake-tiles.c
@@ -2,76 +2,30 @@
#include <gb/drawing.h>
#include <gb/rand.h>

#define gprintf_xy(x, y, str) gotogxy(x, y);gprintf(str);

const unsigned char snake_tiles[] =
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x7E,0x7E,0x81,0x66,0x99,0x5A,0xA5,
0x5A,0xA5,0x66,0x99,0x7E,0x81,0x00,0x7E,
0x00,0x3C,0x32,0x4E,0x73,0x8F,0x63,0x9F,
0x03,0xFF,0x07,0xFF,0x7E,0x7E,0x3C,0x3C,
0x90,0xE0,0x88,0xF0,0x84,0xF8,0xC2,0xFC,
0x60,0x7F,0x30,0x3F,0x18,0x1F,0x0F,0x0F
};
#include "snake-tiles.c"
#include "snake-map.c"

#define mapWidth 20
#define mapHeight 18
#define mapBank 0

const unsigned char map[] =
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x00,
0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,
0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,
0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,
0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,
0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,
0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,
0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,
0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,
0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,
0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
};

#define SCREEN_WIDTH 160
#define SCREEN_HEIGHT 144

const unsigned char levelBoundaries[4] = {
2, 5, 17, 13
};
#define NUM_SPRITES 40
#define SPRITE_SIZE 8
#define SPRITE_X_COORD(x) (x + 1) * SPRITE_SIZE
#define SPRITE_Y_COORD(y) (y + 2) * SPRITE_SIZE

#define SNAKE_SPRITE 0x25
#define PILL_SPRITE 0x26
#define EMPTY_SPRITE 0x27

#define EMPTY_SPRITE 0x00
unsigned char spriteIdx;
#define SET_NEXT_SPRITE(sprite) set_sprite_tile(spriteIdx++, sprite)

//SNAKE INFO
#define SNAKE_SPRITE_SIZE 8
#define SNAKE_SPRITE 0x02
#define MAX_SNAKE_SIZE 35
#define SnakeHead snakeCoords[0]

@@ -80,19 +34,16 @@ unsigned char snakeCleanCoords[2];
unsigned char snakeSize;
unsigned char x, y;

//PILLS INFO
#define PILL_SPRITE_SIZE 8
#define PILL_SPRITE 0x03
#define MAX_PILLS 8
#define PILL_SPRITE_X_COORD(x) (x + 1) * PILL_SPRITE_SIZE
#define PILL_SPRITE_Y_COORD(y) (y + 2) * PILL_SPRITE_SIZE
//GAME
#define MAX_LEVEL 10
#define MAX_PILLS 8

unsigned char pillsLive = 0;
unsigned char pillsPositions[MAX_PILLS][2];

//GAME
#define MAX_LEVEL 10

const unsigned char levelBoundaries[4] = {
2, 4, 17, 15
};

const unsigned char pointsPerLevel[MAX_LEVEL] = {
2, 5, 10, 20, 30, 40, 50, 100, 200, 255
@@ -114,19 +65,51 @@ unsigned char pad;
unsigned char gameover = 1;
unsigned char i, j, k, l, m;
unsigned char vFrameCount;
unsigned char playing;
unsigned char numSprites;


void clearSprites()
{
for (i = 0; i < spriteIdx; i++) {
set_sprite_tile(i, EMPTY_SPRITE);
}
spriteIdx = 0;
}


void drawUI()
{
//TODO
//Draw score
for (i = 0; i < 4; i++) {
SET_NEXT_SPRITE(scoreParts[i] == 0 ? 0x23 : scoreParts[i] + 0x19);
move_sprite(spriteIdx - 1, SPRITE_X_COORD(15 + i), SPRITE_Y_COORD(1));
}

//Draw level
k = (level == 0xFF) ? 0 : level + 1;
i = k / 10;
j = k - i * 10;
SET_NEXT_SPRITE(i == 0 ? 0x23 : 0x19 + i );
move_sprite(spriteIdx - 1, SPRITE_X_COORD(4), SPRITE_Y_COORD(1));
SET_NEXT_SPRITE(j == 0 ? 0x23 : 0x19 + j );
move_sprite(spriteIdx - 1, SPRITE_X_COORD(5), SPRITE_Y_COORD(1));

if (gameover) {
SET_NEXT_SPRITE('G' - 0x41); move_sprite(spriteIdx - 1, SPRITE_X_COORD(8), SPRITE_Y_COORD(8));
SET_NEXT_SPRITE('A' - 0x41); move_sprite(spriteIdx - 1, SPRITE_X_COORD(9), SPRITE_Y_COORD(8));
SET_NEXT_SPRITE('M' - 0x41); move_sprite(spriteIdx - 1, SPRITE_X_COORD(10), SPRITE_Y_COORD(8));
SET_NEXT_SPRITE('E' - 0x41); move_sprite(spriteIdx - 1, SPRITE_X_COORD(11), SPRITE_Y_COORD(8));

SET_NEXT_SPRITE('O' - 0x41); move_sprite(spriteIdx - 1, SPRITE_X_COORD(8), SPRITE_Y_COORD(10));
SET_NEXT_SPRITE('V' - 0x41); move_sprite(spriteIdx - 1, SPRITE_X_COORD(9), SPRITE_Y_COORD(10));
SET_NEXT_SPRITE('E' - 0x41); move_sprite(spriteIdx - 1, SPRITE_X_COORD(10), SPRITE_Y_COORD(10));
SET_NEXT_SPRITE('R' - 0x41); move_sprite(spriteIdx - 1, SPRITE_X_COORD(11), SPRITE_Y_COORD(10));
}
}


void drawSnake()
{
//Draw snake code
l = gameover ? EMPTY_SPRITE : SNAKE_SPRITE;
i = gameover ? 1 : 0;
for (i; i < snakeSize; ++i) {
@@ -147,10 +130,10 @@ void drawPills()
{
for (i = 0; i < pillsPerLevel[level]; ++i) {
if (pillsPositions[i][0] != 0xFF && pillsPositions[i][1] != 0xFF && !gameover) {
set_sprite_tile(i, PILL_SPRITE);
move_sprite(i, PILL_SPRITE_X_COORD(pillsPositions[i][0]), PILL_SPRITE_Y_COORD(pillsPositions[i][1]));
SET_NEXT_SPRITE(PILL_SPRITE);
move_sprite(spriteIdx - 1, SPRITE_X_COORD(pillsPositions[i][0]), SPRITE_Y_COORD(pillsPositions[i][1]));
} else {
set_sprite_tile(i, EMPTY_SPRITE);
SET_NEXT_SPRITE(EMPTY_SPRITE);
}
}
}
@@ -181,14 +164,12 @@ void resetGame()

void enter()
{
resetGame();
pad = joypad();
if (pad & J_START
|| (pad & J_A)
|| (pad & J_B)) {
gameover = 0;
}
drawUI();
}


@@ -283,33 +264,41 @@ void game()
break;
}
}

drawSnake();
drawPills();
drawUI();

if (gameover) {
resetGame();
}
}


void main()
{
SPRITES_8x8;

set_sprite_data(0, 4, snake_tiles);

set_bkg_data(0, 4, snake_tiles);
set_sprite_data(0, NUM_SPRITES, tiles);
set_bkg_data(0, NUM_SPRITES, tiles);
set_bkg_tiles(0, 0, mapWidth, mapHeight, map);

SHOW_BKG;
SHOW_SPRITES;

wait_vbl_done();
resetGame();

while (1) {
wait_vbl_done();
vFrameCount++;

clearSprites();
if (gameover) {
enter();
} else {
game();
}
drawUI();

}
}
@@ -0,0 +1,65 @@
/*
SNAKE-MAP.C
Map Source File.
Info:
Section :
Bank : 0
Map size : 20 x 18
Tile set : snake-tiles.gbr
Plane count : 1 plane (8 bits)
Plane order : Tiles are continues
Tile offset : 0
Split data : No
This file was generated by GBMB v1.8
*/

#define mapWidth 20
#define mapHeight 18
#define mapBank 0

unsigned char map[] =
{
0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x27,
0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x27,
0x27,0x0B,0x15,0x27,0x27,0x27,0x27,0x27,0x27,0x12,
0x02,0x0E,0x11,0x04,0x27,0x27,0x27,0x27,0x27,0x27,
0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x27,
0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x27,
0x27,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,
0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x27,
0x27,0x24,0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x27,
0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x24,0x27,
0x27,0x24,0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x27,
0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x24,0x27,
0x27,0x24,0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x27,
0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x24,0x27,
0x27,0x24,0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x27,
0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x24,0x27,
0x27,0x24,0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x27,
0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x24,0x27,
0x27,0x24,0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x27,
0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x24,0x27,
0x27,0x24,0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x27,
0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x24,0x27,
0x27,0x24,0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x27,
0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x24,0x27,
0x27,0x24,0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x27,
0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x24,0x27,
0x27,0x24,0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x27,
0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x24,0x27,
0x27,0x24,0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x27,
0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x24,0x27,
0x27,0x24,0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x27,
0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x24,0x27,
0x27,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,
0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x27,
0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x27,
0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x27,0x27
};

/* End of SNAKE-MAP.C */

0 comments on commit ba978f1

Please sign in to comment.
You can’t perform that action at this time.