1
0
mirror of https://github.com/sehugg/8bitworkshop.git synced 2024-11-30 06:50:39 +00:00
8bitworkshop/presets/vicdual/snake1.c

220 lines
16 KiB
C
Raw Normal View History

2017-02-20 23:50:29 +00:00
#include <string.h>
typedef unsigned char byte;
typedef unsigned short word;
// PLATFORM DEFINITION
__sfr __at (0x0) input0;
__sfr __at (0x1) input1;
__sfr __at (0x2) input2;
__sfr __at (0x3) input3;
__sfr __at (0x40) palette;
byte __at (0xe000) cellram[32][32];
byte __at (0xe800) tileram[256][8];
#define LEFT1 !(input1 & 0x10)
#define RIGHT1 !(input1 & 0x20)
#define UP1 !(input1 & 0x40)
#define DOWN1 !(input1 & 0x80)
#define FIRE1 !(input2 & 0x20)
#define COIN1 (input3 & 0x8)
#define START1 !(input2 & 0x10)
#define START2 !(input3 & 0x20)
#define TIMER500HZ (input2 & 0x8)
2017-02-20 23:50:29 +00:00
// GAME DATA
typedef struct {
byte x;
byte y;
byte dir;
char head_attr;
char tail_attr;
char collided:1;
} Player;
Player players[2];
2017-02-25 15:57:39 +00:00
#define FRAMES_PER_MOVE 10
2017-02-20 23:50:29 +00:00
// GAME CODE
void main();
// start routine @ 0x0
void start() {
__asm
LD SP,#0xE800 ; set up stack pointer
DI ; disable interrupts
__endasm;
main();
}
////////
void delay(byte msec) {
while (msec--) {
while (TIMER500HZ != 0) ;
while (TIMER500HZ == 0) ;
}
2017-02-20 23:50:29 +00:00
}
#define LOCHAR 0x0
#define HICHAR 0xff
#define CHAR(ch) (ch-LOCHAR)
void clrscr() {
memset(cellram, CHAR(' '), sizeof(cellram));
}
byte getchar(byte x, byte y) {
return cellram[x][y];
}
void putchar(byte x, byte y, byte attr) {
cellram[x][y] = attr;
}
void putstring(byte x, byte y, const char* string) {
while (*string) {
putchar(x++, y, (*string++ - LOCHAR));
}
}
// PC font (code page 437)
2017-05-04 23:25:41 +00:00
const byte font8x8[0x100][8] = {/*{w:8,h:8,bpp:1,count:256,xform:"scaleX(-1) rotate(90deg)"}*/
2017-02-20 23:50:29 +00:00
{ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, { 0x7e,0x81,0x95,0xb1,0xb1,0x95,0x81,0x7e }, { 0x7e,0xff,0xeb,0xcf,0xcf,0xeb,0xff,0x7e }, { 0x0e,0x1f,0x3f,0x7e,0x3f,0x1f,0x0e,0x00 }, { 0x08,0x1c,0x3e,0x7f,0x3e,0x1c,0x08,0x00 }, { 0x38,0x3a,0x9f,0xff,0x9f,0x3a,0x38,0x00 }, { 0x10,0x38,0xbc,0xff,0xbc,0x38,0x10,0x00 }, { 0x00,0x00,0x18,0x3c,0x3c,0x18,0x00,0x00 }, { 0xff,0xff,0xe7,0xc3,0xc3,0xe7,0xff,0xff }, { 0x00,0x3c,0x66,0x42,0x42,0x66,0x3c,0x00 }, { 0xff,0xc3,0x99,0xbd,0xbd,0x99,0xc3,0xff }, { 0x70,0xf8,0x88,0x88,0xfd,0x7f,0x07,0x0f }, { 0x00,0x4e,0x5f,0xf1,0xf1,0x5f,0x4e,0x00 }, { 0xc0,0xe0,0xff,0x7f,0x05,0x05,0x07,0x07 }, { 0xc0,0xff,0x7f,0x05,0x05,0x65,0x7f,0x3f }, { 0x99,0x5a,0x3c,0xe7,0xe7,0x3c,0x5a,0x99 }, { 0x7f,0x3e,0x3e,0x1c,0x1c,0x08,0x08,0x00 }, { 0x08,0x08,0x1c,0x1c,0x3e,0x3e,0x7f,0x00 }, { 0x00,0x24,0x66,0xff,0xff,0x66,0x24,0x00 }, { 0x00,0x5f,0x5f,0x00,0x00,0x5f,0x5f,0x00 }, { 0x06,0x0f,0x09,0x7f,0x7f,0x01,0x7f,0x7f }, { 0xda,0xbf,0xa5,0xa5,0xfd,0x59,0x03,0x02 }, { 0x00,0x70,0x70,0x70,0x70,0x70,0x70,0x00 }, { 0x80,0x94,0xb6,0xff,0xff,0xb6,0x94,0x80 }, { 0x00,0x04,0x06,0x7f,0x7f,0x06,0x04,0x00 }, { 0x00,0x10,0x30,0x7f,0x7f,0x30,0x10,0x00 }, { 0x08,0x08,0x08,0x2a,0x3e,0x1c,0x08,0x00 }, { 0x08,0x1c,0x3e,0x2a,0x08,0x08,0x08,0x00 }, { 0x3c,0x3c,0x20,0x20,0x20,0x20,0x20,0x00 }, { 0x08,0x1c,0x3e,0x08,0x08,0x3e,0x1c,0x08 }, { 0x30,0x38,0x3c,0x3e,0x3e,0x3c,0x38,0x30 }, { 0x06,0x0e,0x1e,0x3e,0x3e,0x1e,0x0e,0x06 }, { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }, { 0x00,0x06,0x5f,0x5f,0x06,0x00,0x00,0x00 }, { 0x00,0x07,0x07,0x00,0x07,0x07,0x00,0x00 }, { 0x14,0x7f,0x7f,0x14,0x7f,0x7f,0x14,0x00 }, { 0x24,0x2e,0x6b,0x6b,0x3a,0x12,0x00,0x00 }, { 0x46,0x66,0x30,0x18,0x0c,0x66,0x62,0x00 }, { 0x30,0x7a,0x4f,0x5d,0x37,0x7a,0x48,0x00 }, { 0x04,0x07,0x03,0x00,0x00,0x00,0x00,0x00 }, { 0x00,0x1c,0x3e,0x63,0x41,0x00,0x00,0x00 }, { 0x00,0x41,0x63,0x3e,0x1c,0x00,0x00,0x00 }, { 0x08,0x2a,0x3e,0x1c,0x1c,0x3e,0x2a,0x08 }, { 0x08,0x08,0x3e,0x3e,0x08,0x08,0x00,0x00 }, { 0x00,0xa0,0xe0,0x60,0x00,0x00,0x00,0x00 }, { 0x08,0x08,0x08,0x08,0x08,0x08,0x00,0x00 }, { 0x00,0x00,0x60,0x60,0x00,0x00,0x00,0x00 }, { 0x60,0x30,0x18,0x0c,0x06,0x03,0x01,0x00 }, { 0x3e,0x7f,0x59,0x4d,0x7f,0x3e,0x00,0x00 }, { 0x42,0x42,0x7f,0x7f,0x40,0x40,0x00,0x00 }, { 0x62,0x73,0x59,0x49,0x6f,0x66,0x00,0x00 }, { 0x22,0x63,0x49,0x49,0x7f,0x36,0x00,0x00 }, { 0x18,0x1c,0x16,0x13,0x7f,0x7f,0x10,0x00 }, { 0x27,0x67,0x45,0x45,0x7d,0x39,0x00,0x00 }, { 0x3c,0x7e,0x4b,0x49,0x79,0x30,0x00,0x00 }, { 0x03,0x63,0x71,0x19,0x0f,0x07,0x00,0x00 }, { 0x36,0x7f,0x49,0x49,0x7f,0x36,0x00,0x00 }, { 0x06,0x4f,0x49,0x69,0x3f,0x1e,0x00,0x00 }, { 0x00,0x00,0x6c,0x6c,0x00,0x00,0x00,0x00 }, { 0x00,0xa0,0xec,0x6c,0x00,0x00,0x00,0x00 }, { 0x08,0x1c,0x36,0x63,0x41,0x00,0x00,0x00 }, { 0x14,0x14,0x14,0x14,0x14,0x14,0x00,0x00 }, { 0x00,0x41,0x63,0x36,0x1c,0x08,0x00,0x00 }, { 0x02,0x03,0x51,0x59,0x0f,0x06,0x00,0x00 }, { 0x3e,0x7f,0x41,0x5d,0x5d,0x1f,0x1e,0x00 }, { 0x7c,0x7e,0x13,0x13,0x7e,0x7c,0x00,0x00 }, { 0x41,0x7f,0x7f,0x49,0x49,0x7f,0x36,0x00 }, { 0x1c,0x3e,0x63,0x41,0x41,0x63,0x22,0x00 }, { 0x41,0x7f,0x7f,0x41,0x63,0x7f,0x1c,0x00 }, { 0x41,0x7f,0x7f,0x49,0x5d,0x41,0x63,0x00 }, { 0x41,0x7f,0x7f,0x49,0x1d,0x01,0x03,0x00 }, { 0x1c,0x3e,0x63,0x41,0x51,0x73,0x72,0x00 }, { 0x7f,0x7f,0x08,0x08,0x7f,0x7f,0x00,0x00 }, { 0x00,0x41,0x7f,0x7f,0x41,0x00,0x00,0x00 }, { 0x30,0x70,0x40,0x41,0x7f,0x3f,0x01,0x00 }, { 0x41,0x7f,0x7f,0x08,0x1c,0x77,0x63,0x00 }, { 0x41,0x7f,0x7f,0x41,0x40,0x60,0x70,0x00 }, { 0x7f,0x7f,0x06,0x0c,0x06,0x7f,0x7f,0x00 }, { 0x7f,0x7f,0x06,0x0c,0x18,0x7f,0x7f,0x00 }, { 0x1c,0x3e,0x63,0x41,0x63,0x3e,0x1c,0x00 }, { 0x41,0x7f,0x7f,0x49,0x09,0x0f,0x06,0x00 }, { 0x1e,0x3f,0x21,0x71,0x7f,0x5e,0x00,0x00 }, { 0x41,0x7f,0x7f,0x19,0x39,0x6f,0x46,0x00 }, { 0x26,0x67,0x4d,0x59,0x7b,0x32,0x00,0x00 }, { 0x03,0x41,0x7f,0x7f,0x41,0x03,0x00,0x00 }, { 0x7f,0x7f,0x40,0x40,0x7f,0x7f,0x00,0x00 }, { 0x1f,0x3f,0x60,0x60,0x3f,0x1f,0x00,0x00 }, { 0x7f,0x7f,0x30,0x18,0x30,0x7f,0x7f,0x00 }, { 0x63,0x77,0x1c,0x08,0x1c,0x77,0x63,0x00 }, { 0x07,0x4f,0x78,0x78,0x4f,0x07,0x00,0x00 }, { 0x67,0x73,0x59,0x4d,0x47,0x63,0x71,0x00 },
};
const char BOX_CHARS[8] = { 218, 191, 192, 217, 196, 196, 179, 179 };
2017-04-09 00:43:46 +00:00
void draw_box(byte x, byte y, byte x2, byte y2, const char chars[]) {
2017-02-20 23:50:29 +00:00
byte x1 = x;
putchar(x, y, chars[2]);
putchar(x2, y, chars[3]);
putchar(x, y2, chars[0]);
putchar(x2, y2, chars[1]);
while (++x < x2) {
putchar(x, y, chars[5]);
putchar(x, y2, chars[4]);
}
while (++y < y2) {
putchar(x1, y, chars[6]);
putchar(x2, y, chars[7]);
}
}
void draw_playfield() {
draw_box(0,0,27,29,BOX_CHARS);
}
2017-03-15 14:11:28 +00:00
typedef enum { D_RIGHT, D_DOWN, D_LEFT, D_UP } Direction;
2017-02-20 23:50:29 +00:00
const char DIR_X[4] = { 1, 0, -1, 0 };
const char DIR_Y[4] = { 0, -1, 0, 1 };
void init_game() {
memset(players, 0, sizeof(players));
players[0].head_attr = CHAR('1');
players[1].head_attr = CHAR('2');
players[0].tail_attr = 254;
players[1].tail_attr = 254;
}
void reset_players() {
players[0].x = players[0].y = 6;
players[0].dir = D_RIGHT;
players[1].x = players[1].y = 21;
players[1].dir = D_LEFT;
players[0].collided = players[1].collided = 0;
}
2017-04-09 00:43:46 +00:00
void draw_player(byte p) {
putchar(players[p].x, players[p].y, players[p].head_attr);
2017-02-20 23:50:29 +00:00
}
2017-04-09 00:43:46 +00:00
void erase_player(byte p) {
putchar(players[p].x, players[p].y, players[p].tail_attr);
2017-03-15 14:11:28 +00:00
}
2017-04-09 00:43:46 +00:00
void move_player(byte p) {
2017-03-15 14:11:28 +00:00
erase_player(p);
2017-04-09 00:43:46 +00:00
players[p].x += DIR_X[players[p].dir];
players[p].y += DIR_Y[players[p].dir];
if (getchar(players[p].x, players[p].y) != CHAR(' '))
players[p].collided = 1;
2017-02-20 23:50:29 +00:00
draw_player(p);
}
2017-04-09 00:43:46 +00:00
void human_control(byte p) {
2017-03-15 14:11:28 +00:00
Direction dir = 0xff;
2017-02-20 23:50:29 +00:00
if (LEFT1) dir = D_LEFT;
if (RIGHT1) dir = D_RIGHT;
if (UP1) dir = D_UP;
if (DOWN1) dir = D_DOWN;
// don't let the player reverse
2017-04-09 00:43:46 +00:00
if (dir != 0xff && dir != (players[p].dir ^ 2)) {
players[p].dir = dir;
2017-02-20 23:50:29 +00:00
}
}
2017-04-09 00:43:46 +00:00
void ai_control(byte p) {
2017-02-25 15:57:39 +00:00
byte x,y;
2017-04-09 00:43:46 +00:00
Direction dir = players[p].dir;
x = players[p].x + DIR_X[dir];
y = players[p].y + DIR_Y[dir];
2017-02-25 15:57:39 +00:00
if (getchar(x,y) != CHAR(' ')) {
2017-04-09 00:43:46 +00:00
players[p].dir = (dir + 1) & 3;
2017-02-20 23:50:29 +00:00
}
}
void flash_colliders() {
byte i;
// flash players that collided
for (i=0; i<60; i++) {
if (players[0].collided) players[0].head_attr ^= 0x80;
if (players[1].collided) players[1].head_attr ^= 0x80;
delay(5);
2017-04-09 00:43:46 +00:00
draw_player(0);
draw_player(1);
2017-02-20 23:50:29 +00:00
palette = i;
}
palette = 0;
}
void make_move() {
byte i;
2017-02-25 15:57:39 +00:00
for (i=0; i<FRAMES_PER_MOVE; i++) {
2017-04-09 00:43:46 +00:00
human_control(0);
delay(10);
2017-02-20 23:50:29 +00:00
}
2017-04-09 00:43:46 +00:00
ai_control(1);
2017-02-20 23:50:29 +00:00
// if players collide, 2nd player gets the point
2017-04-09 00:43:46 +00:00
move_player(1);
move_player(0);
2017-02-20 23:50:29 +00:00
}
void play_round() {
reset_players();
clrscr();
draw_playfield();
while (1) {
make_move();
if (players[0].collided || players[1].collided) break;
}
flash_colliders();
}
void main() {
palette = 0;
memcpy(tileram, font8x8, sizeof(font8x8));
draw_playfield();
2017-02-25 15:57:39 +00:00
while (1) {
init_game();
play_round();
}
2017-02-20 23:50:29 +00:00
}