Skip to content

Instantly share code, notes, and snippets.

Created January 21, 2016 18:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/8c375fc060cd275d8747 to your computer and use it in GitHub Desktop.
Save anonymous/8c375fc060cd275d8747 to your computer and use it in GitHub Desktop.
spacepins
/*
char m[] = "Copyright (c) 2016, Christoph-Simon Senjak";
printf("%s <%c%c%c%c%s%c>\n", m, m[11], m[24], m[24], '@', "uxul.d", m[37]);
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
Compile with:
cc \
spacepins.c `pkg-config --cflags ncursesw` \
`pkg-config --libs ncursesw` \
-std=c99
Controls: Up/Down to move up/down, Right to shoot, Esc to end.
To play on a braille line, make sure it supports unicode, and maps unicode
braille characters to the respective characters on your device, and point
your line to the upper left corner. (This should for example work well with
Orca.) The first four braille cells show an 8x4 grid. After them, a score is
shown. When an enemy crosses the left border, the score is decreased, when
you shoot an enemy, your score is increased.
*/
#define _BSD_SOURCE
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <unistd.h>
#include <wchar.h>
#include <locale.h>
#include <string.h>
#include <stdlib.h>
#define NCURSES_WIDECHAR (true)
#include <curses.h>
int player_y = 0;
int score = 0;
bool playfield[8][4];
struct pt {
int x;
int y;
int speed;
};
struct pt enemies[] = {
{ .x = -1, .y = -1 },
{ .x = -1, .y = -1 },
{ .x = -1, .y = -1 },
{ .x = -1, .y = -1 },
{ .x = -1, .y = -1 },
{ .x = -1, .y = -1 },
{ .x = -1, .y = -1 },
{ .x = -1, .y = -1 },
{ .x = -1, .y = -1 },
{ .x = -1, .y = -1 },
{ .x = -1, .y = -1 },
{ .x = -1, .y = -1 }};
struct pt tela[] = {
{ .x = -1, .y = -1 },
{ .x = -1, .y = -1 },
{ .x = -1, .y = -1 },
{ .x = -1, .y = -1 },
{ .x = -1, .y = -1 },
{ .x = -1, .y = -1 },
{ .x = -1, .y = -1 },
{ .x = -1, .y = -1 },
{ .x = -1, .y = -1 },
{ .x = -1, .y = -1 },
{ .x = -1, .y = -1 },
{ .x = -1, .y = -1 },
{ .x = -1, .y = -1 },
{ .x = -1, .y = -1 },
{ .x = -1, .y = -1 },
{ .x = -1, .y = -1 },
{ .x = -1, .y = -1 },
{ .x = -1, .y = -1 },
{ .x = -1, .y = -1 },
{ .x = -1, .y = -1 },
{ .x = -1, .y = -1 },
{ .x = -1, .y = -1 },
{ .x = -1, .y = -1 },
{ .x = -1, .y = -1 },
{ .x = -1, .y = -1 },
{ .x = -1, .y = -1 },
{ .x = -1, .y = -1 },
{ .x = -1, .y = -1 },
{ .x = -1, .y = -1 },
{ .x = -1, .y = -1 },
{ .x = -1, .y = -1 },
{ .x = -1, .y = -1 },
{ .x = -1, .y = -1 },
};
static const int telum_delay_max = 25;
static int telum_delay = 0;
static const int telum_speed_max = 10;
static const int enemy_speed_max = 20;
inline static uint8_t rbyte (uint8_t b) {
static const int nums[] = {0, 1, 2, 6,
3, 4, 5, 7};
uint8_t r = 0;
for (int i = 0; i < 8; ++i) {
r |= ((b >> i) % 2) << nums[i];
}
return r;
}
void shoot() {
if (telum_delay == 0) {
for (int i = 0; i < sizeof(tela)/sizeof(struct pt); ++i) {
if (tela[i].x == -1) {
tela[i].x = 1;
tela[i].y = player_y;
tela[i].speed = telum_speed_max;
telum_delay = telum_delay_max;
return;
}
}
}
}
void proceed_tela () {
if (telum_delay != 0) telum_delay--;
for (int i = 0; i < sizeof(tela)/sizeof(struct pt); ++i) {
if (tela[i].x == 7) {
tela[i].x = -1;
} else if (tela[i].x != -1) {
if (tela[i].speed-- == 0) {
tela[i].x++;
tela[i].speed = telum_speed_max;
}
playfield[tela[i].x][tela[i].y] = true;
}
}
}
int spawn_time;
static const int spawn_time_max = 100;
void maybe_spawn_enemy () {
if (spawn_time != 0) {
spawn_time--;
} else {
spawn_time = spawn_time_max;
for (int i = 0; i < sizeof(enemies)/sizeof(struct pt); ++i) {
if (enemies[i].x == -1) {
enemies[i].x = 7;
enemies[i].speed = enemy_speed_max;
enemies[i].y = rand() % 4;
return;
}
}
}
}
/* notice: we assume that all tela and *only* tela are already there,
that is, the player is not drawn already */
void proceed_enemies () {
maybe_spawn_enemy();
for (int i = 0; i < sizeof(enemies)/sizeof(struct pt); ++i) {
if (enemies[i].x == 0) {
score--; /* hit */
enemies[i].x = -1;
} else if (enemies[i].x != -1) {
if (playfield[enemies[i].x][enemies[i].y]) {
score++; /* shot */
for (int j = 0; j < sizeof(tela)/sizeof(struct pt); ++j) {
if ((tela[j].x == enemies[i].x) && (tela[j].y == enemies[i].y)) {
tela[j].x = -1;
break;
}
}
enemies[i].x = -1;
} else {
if (enemies[i].speed-- == 0) {
enemies[i].x--;
enemies[i].speed = enemy_speed_max;
}
playfield[enemies[i].x][enemies[i].y] = true;
}
}
}
}
void draw (int x, int y) {
wchar_t l[5];
l[4] = 0;
for (int i = 0; i < 4; ++i) {
uint8_t fld = 0;
for (int j = 0; j < 4; ++j) {
fld |= (playfield[2*i ][j] ? 1 << j : 0);
fld |= (playfield[2*i+1][j] ? 1 << (4 + j) : 0);
}
l[i] = 0x2800 | rbyte(fld);
}
mvaddwstr(x, y, l);
mvprintw(x, y + 6, "%0'4d", score);
}
int main (void) {
/* whoever invented this ... */
setlocale(LC_CTYPE, "");
initscr();
cbreak();
noecho();
keypad(stdscr, TRUE);
nodelay(stdscr, TRUE);
curs_set(0);
bool cont = true;
while (cont) {
memset(playfield, 0, sizeof(playfield));
proceed_tela();
proceed_enemies();
playfield[0][player_y] = true;
draw(0, 0);
refresh();
usleep(10000);
playfield[0][player_y] = false;
int ch;
while ((ch = getch()) != ERR) {
switch(ch) {
case KEY_UP:
if (player_y != 0) player_y--;
break;
case KEY_DOWN:
if (player_y != 3) player_y++;
break;
case KEY_RIGHT:
shoot();
break;
case 27:
cont = false;
break;
default:
break;
}
}
}
endwin();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment