Skip to content

Instantly share code, notes, and snippets.

@Bdabrowsky
Created January 25, 2024 09:23
Show Gist options
  • Save Bdabrowsky/978c0cf46acedae1ffe29e526d99827a to your computer and use it in GitHub Desktop.
Save Bdabrowsky/978c0cf46acedae1ffe29e526d99827a to your computer and use it in GitHub Desktop.
projekt
#define _CRT_SECURE_NO_WARNINGS
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#include "winbgi2.h"
#define speed 4
#define player_speed 8
bool DEBUG = false;
int N = 800;
int score = 0;
int counter = 0;
void generatePillar(int N, int **board){
int posY = rand() % (N/2)+N/4;
int width = N/50;
int height = N/5;
for(int i=0;i<posY;i++){
for(int j=N-width;j<N;j++){
board[i][j] = 1;
}
}
for(int i=posY+height;i<N;i++){
for(int j=N-width;j<N;j++){
board[i][j] = 1;
}
}
}
void generatePlayer(int N, int **board, int x, int y){
for(int i=y-10;i<y+10;i++){
for(int j=x-20;j<x+20;j++){
board[i][j] = 2;
}
}
}
void init(int N, int **board){
for(int i=0;i<N;i++){
for(int j =0;j<N;j++){
board[i][j] = 0;
}
}
generatePillar(N, board);
generatePlayer(N, board, 100, N/2);
}
void update(int N, int **board, char c){
int **boardTemp;
bool edge = false;
boardTemp = (int**)malloc(N*sizeof(int*));
for(int i=0;i<N;i++){
boardTemp[i] = (int*)malloc(N*sizeof(int));
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
boardTemp[i][j] = 0;
}
}
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
if(board[i][j] == 1){
if(j-speed >= 0){
boardTemp[i][j- speed] += 1;
}
else {
score++;
}
}
else if(board[i][j] == 2 && !edge){
if(c =='w'){
if(i - player_speed >= 0){
boardTemp[i- player_speed][j] += 2;
}
else{
boardTemp[i][j] += 2;
edge = true;
}
}
else if(c == 's'){
if(i+ player_speed < N){
boardTemp[i+ player_speed][j] += 2;
}
else{
boardTemp[i][j] += 2;
edge = true;
}
}
else{
if(i+ player_speed < N){
boardTemp[i+ player_speed][j] += 2;
}
else{
boardTemp[i][j] += 2;
edge = true;
}
}
}
else if(board[i][j] == 2 && edge){
boardTemp[i][j] += 2;
}
}
}
for(int i=0;i<N;i++){
for(int j =0;j<N;j++){
board[i][j] = boardTemp[i][j];
}
}
for(int i=0;i<N;i++){
free(boardTemp[i]);
}
free(boardTemp);
}
void draw(int N, int **board){
for(int i=0;i<N;i++){
for(int j =0;j<N;j++){
if(board[i][j] == 1){
putpixel(j, i, 2); //green
}
else if(board[i][j] == 2){
putpixel(j, i, 14); //yellow
}
else if(board[i][j] == 3){
putpixel(j, i, 1); //blue
}
}
}
}
bool detectCollision(int N, int **board){
for(int i=0;i<N;i++){
for(int j =0;j<N;j++){
if(board[i][j] == 3){
return true;
}
}
}
return false;
}
void drawStart(){
cleardevice();
setbkcolor(9);
settextjustify(CENTER_TEXT, CENTER_TEXT);
settextstyle(0, HORIZ_DIR, 3);
setcolor(6);
outtextxy(N / 2, N/2-200, "Floppy birb");
outtextxy(N / 2, N / 2, "Press any key to start");
outtextxy(N / 2, N / 2 + 200, "Bartosz Dabrowski 2k24");
wait();
cleardevice();
setbkcolor(9);
settextjustify(CENTER_TEXT, CENTER_TEXT);
settextstyle(0, HORIZ_DIR, 3);
setcolor(6);
outtextxy(N / 2, N / 2 - 200, "Controls");
outtextxy(N / 2, N / 2, "Press W to fly");
outtextxy(N / 2, N / 2 + 200, "Hitting a pillar WILL KILL YOU,");
outtextxy(N / 2, N / 2 + 240, "get 20 points to win!");
return;
}
void drawWin(){
return;
}
void drawEnd(){
cleardevice();
setbkcolor(9);
settextjustify(CENTER_TEXT, CENTER_TEXT);
settextstyle(0, HORIZ_DIR, 3);
setcolor(6);
outtextxy(N / 2, N / 2 - 200, "YOU DIED");
outtextxy(N / 2, N / 2, "Press any key to END");
outtextxy(N / 2, N / 2 + 200, "ADIOS");
wait();
return;
}
void drawCredits(){
cleardevice();
setbkcolor(9);
settextjustify(CENTER_TEXT, CENTER_TEXT);
settextstyle(0, HORIZ_DIR, 3);
setcolor(6);
outtextxy(N / 2, N / 2 - 200, "Floppy birb");
outtextxy(N / 2, N / 2 + 200, "Bartosz Dabrowski 2k24");
wait();
cleardevice();
return;
}
void debug(int N, int **board){
for(int i=0;i<N;i++){
for(int j =0;j<N;j++){
printf("%d ",board[i][j]);
}
printf("\n");
}
}
int main(){
srand(time(NULL));
int **board;
board = (int**)malloc(N*sizeof(int*));
for(int i=0;i<N;i++){
board[i] = (int*)malloc(N*sizeof(int));
}
graphics(N+20,N+20);
setbkcolor(9);
init(N, board);
drawStart();
wait();
cleardevice();
draw(N, board);
while(1){
//debug(N, board);
printf("\n\n\n");
char temp = ' ';
clearviewport();
if (kbhit()) {
temp = getch();
if (temp == 'w' || temp == 's') {
update(N, board, temp);
}
}
else {
update(N, board, 's');
}
counter++;
draw(N, board);
if(detectCollision(N, board)){
drawEnd();
break;
}
if(score == 1011970){
drawWin();
break;
}
if(counter >= N/(speed) ){
counter = 0;
generatePillar(N, board);
}
if (DEBUG) { wait(); };
}
drawCredits();
wait();
return 0;
}
//Floppy birb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment