8 Commits

Author SHA1 Message Date
b7a53bdb43 fix stuff
All checks were successful
continuous-integration/drone/push Build is passing
2022-05-29 11:22:38 +02:00
7700d4fdb0 fix stic compile
All checks were successful
continuous-integration/drone/push Build is passing
2022-05-29 11:10:12 +02:00
1ebdd68873 compile statically
Some checks failed
continuous-integration/drone/push Build is failing
2022-05-29 10:58:43 +02:00
ab4ddaf365 clean up bullets
All checks were successful
continuous-integration/drone/push Build is passing
2022-05-29 10:43:50 +02:00
8ddf1ad110 fix ncurses
Some checks failed
continuous-integration/drone/push Build was killed
continuous-integration/drone Build is passing
2022-05-29 10:33:25 +02:00
a2ec2614ac add drone
Some checks failed
continuous-integration/drone/push Build is failing
2022-05-29 10:29:50 +02:00
c5af2046e2 beautify 2022-05-29 10:22:29 +02:00
daf638d060 clean up comments 2022-05-29 10:21:25 +02:00
4 changed files with 134 additions and 157 deletions

29
.drone.yml Normal file
View File

@@ -0,0 +1,29 @@
kind: pipeline
name: mazegame_compiler
type: docker
steps:
- name: compile main.c
description: Check if the code is compiling
image: alpine
#install make and gcc
commands:
- apk add build-base ncurses-dev ncurses ncurses-terminfo ncurses-static g++
- mkdir build
- gcc -static -o build/main_static_alpine main.c -l:libncurses.so
- gcc -o build/main_dynamic main.c -lncurses
artifacts:
- name: compiled_static
path: build/main_static_alpine
type: file
- name: compiled_dynamic
path: build/main_dynamic
type: file
- name: gitea_release
image: plugins/gitea-release
settings:
api_key:
from_secret: gitea_api_key
base_url: https://brn.systems:3000
files: build/*
when:
event: tag

4
.vscode/tasks.json vendored
View File

@@ -13,13 +13,15 @@
],
"command": "/usr/bin/gcc",
"args": [
"-static",
"-fdiagnostics-color=always",
"-g",
"${workspaceFolder}/main.c",
"-o",
//include ncurses
"${fileDirname}/${fileBasenameNoExtension}",
"-lncurses"
"-lncurses",
"-ltinfo"
],
"options": {
"cwd": "${fileDirname}"

BIN
main

Binary file not shown.

250
main.c
View File

@@ -1,10 +1,17 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdint.h>
#include <ncurses.h>
#include <unistd.h>
#include <math.h>
uint16_t Hole_Div = 40;
uint16_t SCR_X;
uint16_t SCR_Y;
@@ -13,15 +20,12 @@ char blank_char = 32;
char wall_char = 35;
char goal_char = 71;
char player_char = 80;
char bullet_char = 120;
char inchar = ' ';
uint16_t blank_color = 0;
uint16_t wall_color = 1;
uint16_t goal_color = 2;
uint16_t player_color = 3;
uint16_t border_color = 4;
uint16_t bullet1_color = 5;
uint16_t bullet2_color = 5;
char letter = ' ';
struct Object {
uint16_t x;
@@ -30,13 +34,6 @@ struct Object {
uint16_t color;
uint8_t dir;
};
struct Bullet {
uint16_t x;
uint16_t y;
uint8_t dir;
uint8_t color;
};
uint16_t bulletptr = 0;
uint16_t player_x = 0;
uint16_t player_y = 0;
uint16_t goal_x = 0;
@@ -44,155 +41,117 @@ uint16_t goal_y = 0;
uint16_t rng_tmp = 0;
uint16_t orient = 0;
uint16_t inputs_tmp = 0;
struct Object *rendered;
struct Bullet *bullets;
char *playfield;
struct Object * rendered;
char * playfield;
int randomInt(int lower, int upper) {
return rand() % (upper - lower + 1) + lower;
}
void generate_maze(){
void generate_maze() {
//fill playfield with blank
for (uint16_t y = 0; y < SCR_Y; y++) {
for (uint16_t x = 0; x < SCR_X; x++) {
//playfield[y][x] = blank_char;
//do it in one dimension
playfield[y*SCR_X + x] = blank_char;
playfield[y * SCR_X + x] = blank_char;
}
}
//fill every other row with walls
for (uint16_t y = 0; y < SCR_Y; y += 2) {
for (uint16_t x = 0; x < SCR_X; x++) {
//playfield[y][x] = wall_char;
//do it in one dimension
playfield[y*SCR_X + x] = wall_char;
playfield[y * SCR_X + x] = wall_char;
}
}
//punch holes in the walls
for (uint16_t y = 0; y < SCR_Y; y++) {
//punch holes in the walls depending on the SCR_X
for (uint16_t ix = 0; ix < ((SCR_X-(SCR_X%Hole_Div))/Hole_Div); ix += 1) {
for (uint16_t ix = 0; ix < ((SCR_X - (SCR_X % Hole_Div)) / Hole_Div); ix += 1) {
rng_tmp = randomInt(1, SCR_X);
//playfield[y][rng_tmp] = blank_char;
//do it in one dimension
playfield[y*SCR_X + rng_tmp] = blank_char;
playfield[y * SCR_X + rng_tmp] = blank_char;
}
if (orient == 0) {
if(y == 0){
//playfield[y][rng_tmp] = goal_char;
//do it in one dimension
playfield[y*SCR_X + rng_tmp] = goal_char;
if (y == 0) {
playfield[y * SCR_X + rng_tmp] = goal_char;
goal_x = rng_tmp;
goal_y = y;
}
if (y == SCR_Y -1){
//playfield[y][rng_tmp] = player_char;
//do it in one dimension
playfield[y*SCR_X + rng_tmp] = player_char;
if (y == SCR_Y - 1) {
playfield[y * SCR_X + rng_tmp] = player_char;
player_x = rng_tmp;
player_y = y;
}
} else {
if(y == SCR_Y -1){
//playfield[y][rng_tmp] = goal_char;
//do it in one dimension
playfield[y*SCR_X + rng_tmp] = goal_char;
if (y == SCR_Y - 1) {
playfield[y * SCR_X + rng_tmp] = goal_char;
goal_x = rng_tmp;
goal_y = y;
}
if (y == 0){
//playfield[y][rng_tmp] = player_char;
//do it in one dimension
playfield[y*SCR_X + rng_tmp] = player_char;
if (y == 0) {
playfield[y * SCR_X + rng_tmp] = player_char;
player_x = rng_tmp;
player_y = y;
}
}
}
}
void render_maze(){
void render_maze() {
//fill rendered with blank
for (uint16_t y = 0; y < SCR_Y; y++) {
for (uint16_t x = 0; x < SCR_X; x++) {
//rendered[y][x] = blank_char;
//do it in one dimension
rendered[y*SCR_X + x].letter = blank_char;
rendered[y*SCR_X + x].color = blank_color;
rendered[y*SCR_X + x].x = x;
rendered[y*SCR_X + x].y = y;
rendered[y * SCR_X + x].letter = blank_char;
rendered[y * SCR_X + x].color = blank_color;
rendered[y * SCR_X + x].x = x;
rendered[y * SCR_X + x].y = y;
}
}
for (uint16_t y = 0; y < SCR_Y; y++) {
for (uint16_t x = 0; x < SCR_X; x++) {
//letter = playfield[y - 1][x- 1];
//do it in one dimension
letter = playfield[(y)*SCR_X + (x)];
//rendered[x][y].letter = letter;
//do it in one dimension
rendered[(y)*SCR_X + (x)].letter = letter;
//rendered[x][y].x = x;
//do it in one dimension
rendered[(y)*SCR_X + (x)].x = x;
//rendered[x][y].y = y;
//do it in one dimension
rendered[(y)*SCR_X + (x)].y = y;
if (letter == goal_char){
//rendered[x][y].color = goal_color;
//do it in one dimension
rendered[(y)*SCR_X + (x)].color = goal_color;
}
else if (letter == player_char){
//rendered[x][y].color = player_color;
//do it in one dimension
rendered[(y)*SCR_X + (x)].color = player_color;
}
else if (letter == blank_char){
//rendered[x][y].color = blank_color;
//do it in one dimension
rendered[(y)*SCR_X + (x)].color = blank_color;
}
else if (letter == wall_char){
//rendered[x][y].color = wall_color;
//do it in one dimension
rendered[(y)*SCR_X + (x)].color = wall_color;
letter = playfield[(y) * SCR_X + (x)];
rendered[(y) * SCR_X + (x)].letter = letter;
rendered[(y) * SCR_X + (x)].x = x;
rendered[(y) * SCR_X + (x)].y = y;
if (letter == goal_char) {
rendered[(y) * SCR_X + (x)].color = goal_color;
} else if (letter == player_char) {
rendered[(y) * SCR_X + (x)].color = player_color;
} else if (letter == blank_char) {
rendered[(y) * SCR_X + (x)].color = blank_color;
} else if (letter == wall_char) {
rendered[(y) * SCR_X + (x)].color = wall_color;
}
}
}
}
void draw_maze(){
//FILE *fp;
//fp = fopen("logx", "w");
void draw_maze() {
for (uint16_t y = 0; y < SCR_Y; y++) {
for (uint16_t x = 0; x < SCR_X; x++) {
//attron(COLOR_PAIR(rendered[x][y].color));
//do it in one dimension
attron(COLOR_PAIR(rendered[(y)*SCR_X + (x)].color));
//mvaddch(y, x, rendered[x][y].letter);
//do it in one dimension
mvaddch(y, x, rendered[(y)*SCR_X + (x)].letter);
//fprintf(fp, "%c, x:%d y:%d\n", rendered[(y)*SCR_X + (x)].letter, rendered[(y)*SCR_X + (x)].x, rendered[(y)*SCR_X + (x)].y);
//attroff(COLOR_PAIR(rendered[x][y].color));
//do it in one dimension
attroff(COLOR_PAIR(rendered[(y)*SCR_X + (x)].color));
attron(COLOR_PAIR(rendered[(y) * SCR_X + (x)].color));
mvaddch(y, x, rendered[(y) * SCR_X + (x)].letter);
attroff(COLOR_PAIR(rendered[(y) * SCR_X + (x)].color));
}
}
//fclose(fp);
refresh();
}
void init(){
void init() {
initscr();
//get screen size
getmaxyx(stdscr, SCR_Y, SCR_X);
playfield = malloc(sizeof(char) * SCR_Y * SCR_X);
rendered = malloc(sizeof(struct Object) * (SCR_X) * (SCR_Y));
bullets = malloc(sizeof(struct Bullet) * (SCR_X) * (SCR_Y));
noecho();
nodelay(stdscr, TRUE);
start_color();
@@ -201,90 +160,78 @@ void init(){
init_pair(goal_color, COLOR_BLACK, COLOR_GREEN);
init_pair(player_color, COLOR_BLACK, COLOR_YELLOW);
init_pair(border_color, COLOR_WHITE, COLOR_BLUE);
init_pair(bullet1_color, COLOR_BLACK, COLOR_YELLOW);
init_pair(bullet2_color, COLOR_BLACK, COLOR_GREEN);
attron(COLOR_PAIR(blank_color));
clear();
srand(time(NULL));
}
int keyinput(){
int keyinput() {
inchar = getch();
if (inchar == 'q'){
if (inchar == 'q') {
return 1;
}
if (inchar == 'w'){
if (inchar == 'w') {
//if (playfield[player_y - 1][player_x] == blank_char){
if (player_y > 0&&playfield[(player_y-1)*SCR_X + player_x] == blank_char){
//playfield[player_y][player_x] = blank_char;
//do it in one dimension
playfield[(player_y)*SCR_X + player_x] = blank_char;
if (player_y > 0 && playfield[(player_y - 1) * SCR_X + player_x] == blank_char) {
playfield[(player_y) * SCR_X + player_x] = blank_char;
player_y--;
//playfield[player_y][player_x] = player_char;
//do it in one dimension
playfield[(player_y)*SCR_X + player_x] = player_char;
//}else if (playfield[player_y - 1][player_x] == goal_char){
}else if (playfield[(player_y-1)*SCR_X + player_x] == goal_char){
playfield[(player_y) * SCR_X + player_x] = player_char;
//}else if (playfield[player_y - 1][player_x] == goal_char){
} else if (playfield[(player_y - 1) * SCR_X + player_x] == goal_char) {
return 2;
}
}
if (inchar == 'a'){
if (inchar == 'a') {
//if (playfield[player_y][player_x - 1] == blank_char){
if (player_x > 0 &&playfield[(player_y)*SCR_X + (player_x-1)] == blank_char){
//playfield[player_y][player_x] = blank_char;
//do it in one dimension
playfield[(player_y)*SCR_X + player_x] = blank_char;
if (player_x > 0 && playfield[(player_y) * SCR_X + (player_x - 1)] == blank_char) {
playfield[(player_y) * SCR_X + player_x] = blank_char;
player_x--;
//playfield[player_y][player_x] = player_char;
//do it in one dimension
playfield[(player_y)*SCR_X + player_x] = player_char;
playfield[(player_y) * SCR_X + player_x] = player_char;
}
//else if (playfield[player_y][player_x - 1] == goal_char){
else if (playfield[(player_y)*SCR_X + (player_x-1)] == goal_char){
else if (playfield[(player_y) * SCR_X + (player_x - 1)] == goal_char) {
return 2;
}
}
if (inchar == 's'){
if (inchar == 's') {
//if (playfield[player_y + 1][player_x] == blank_char){
if (player_x < SCR_X - 1 &&playfield[(player_y+1)*SCR_X + player_x] == blank_char){
// playfield[player_y][player_x] = blank_char;
playfield[(player_y)*SCR_X + player_x] = blank_char;
if (player_x < SCR_X - 1 && playfield[(player_y + 1) * SCR_X + player_x] == blank_char) {
playfield[(player_y) * SCR_X + player_x] = blank_char;
player_y++;
// playfield[player_y][player_x] = player_char;
playfield[(player_y)*SCR_X + player_x] = player_char;
// }else if (playfield[player_y + 1][player_x] == goal_char){
}else if (playfield[(player_y+1)*SCR_X + player_x] == goal_char){
playfield[(player_y) * SCR_X + player_x] = player_char;
// }else if (playfield[player_y + 1][player_x] == goal_char){
} else if (playfield[(player_y + 1) * SCR_X + player_x] == goal_char) {
return 2;
}
}
if (inchar == 'd'){
if (inchar == 'd') {
// if (playfield[player_y][player_x + 1] == blank_char){
if (player_x < SCR_X - 1 &&playfield[(player_y)*SCR_X + (player_x+1)] == blank_char){
//playfield[player_y][player_x] = blank_char;
playfield[(player_y)*SCR_X + player_x] = blank_char;
if (player_x < SCR_X - 1 && playfield[(player_y) * SCR_X + (player_x + 1)] == blank_char) {
playfield[(player_y) * SCR_X + player_x] = blank_char;
player_x++;
//playfield[player_y][player_x] = player_char;
playfield[(player_y)*SCR_X + player_x] = player_char;
playfield[(player_y) * SCR_X + player_x] = player_char;
}
//else if (playfield[player_y][player_x + 1] == goal_char){
else if (playfield[(player_y)*SCR_X + (player_x+1)] == goal_char){
else if (playfield[(player_y) * SCR_X + (player_x + 1)] == goal_char) {
return 2;
}
}
}
int main(int argc, char *argv[]){
int main(int argc, char * argv[]) {
init();
//parse the command line arguments for width and height
if (argc == 3){
if (argc == 3) {
SCR_X = atoi(argv[1]);
SCR_Y = atoi(argv[2]);
}
if (argc == 2){
if (argc == 2) {
Hole_Div = atoi(argv[1]);
}
if (argc == 4){
if (argc == 4) {
SCR_X = atoi(argv[1]);
SCR_Y = atoi(argv[2]);
Hole_Div = atoi(argv[3]);
@@ -292,14 +239,13 @@ int main(int argc, char *argv[]){
generate_maze();
render_maze();
draw_maze();
while (1)
{
while (1) {
inputs_tmp = keyinput();
if (inputs_tmp == 1){
if (inputs_tmp == 1) {
break;
}
if (inputs_tmp == 2){
if (orient == 0){
if (inputs_tmp == 2) {
if (orient == 0) {
orient = 1;
} else {
orient = 0;
@@ -308,7 +254,7 @@ int main(int argc, char *argv[]){
}
render_maze();
draw_maze();
usleep(round(1000000/FPS));
usleep(round(1000000 / FPS));
}
endwin();
free(playfield);