// PROJEK LnT C BNCC // GAME SNAKE // // Anggota: // 1. 2540126430 - Valentinus Gilbert Sanjaya // 2. 2540126891 - Calista Vannia Nathalie // 3. 2501966950 - Christoper Gerry Johanes #include <stdio.h> #include <string.h> #include <stdlib.h> #include <windows.h> #include <time.h> char namaPemain[10]; int lebar = 40, tinggi = 20; int tailX[101], tailY[101]; int lengthTail; int x,y, foodX, foodY, skor; int input, end; void welcomePage(){ printf("\n\t\t\t BNCC\n"); printf("\t______ _____ _____ ___ _____ _ _ \n"); printf("\t| || \\ / _ \\ | || ___|| | / / \n"); printf("\t| [] || [] | | / \\ | | || |___ | |/ / \n"); printf("\t| ___|| __ | | | | | _ | || ____|| < \n"); printf("\t| | | | \\ \\ | \\_/ || |_| || |____| |\\ \\ \n"); printf("\t|__| |_| \\_\\\\_____/|_____/|_____||_| \\_\\ \n\n"); printf("\t\t SNAKE GAME\n\n"); printf("\t\t\t Oleh:\n\n"); printf("\t 1. 2540126430 - Valentinus Gilbert Sanjaya\n"); printf("\t 2. 2540126891 - Calista Vannia Nathalie\n"); printf("\t 3. 2501966950 - Christoper Gerry Johanes\n\n\n"); printf("\t Tekan \"ENTER\" untuk melanjutkan..."); getchar(); system("cls"); } void aturanMain(){ printf("\n\n"); printf("\t\t\t\t CARA BERMAIN\n"); printf("\t===============================================================\n"); printf("\t Untuk Mengubah Arah Jalan Ular Menggunakan Tombol:\n"); printf("\t - TOMBOL 'w' untuk ke ATAS\n"); printf("\t - TOMBOL 'a' untuk ke KIRI\n"); printf("\t - TOMBOL 's' untuk ke BAWAH\n"); printf("\t - TOMBOL 'd' untuk ke KANAN\n\n"); printf("\t - TOMBOL'x' untuk MENGAKHIRI PERMAINAN yang sedang berjalan\n\n"); printf("\t===============================================================\n"); printf("\t\t\t\t [TIPS]\n"); printf("\t Makanlah 'A' yang respawn secara acak di map untuk menambah poin! \n"); printf("\t dan usahakan agar tidak menabrak tembok dan badan sendiri\n"); printf("\t untuk mencapai HIGHSCORE yang baru\n"); printf("\t===============================================================\n\n"); printf("\t\t Tekan \"ENTER\" untuk melanjutkan..."); getchar(); system("cls"); } void inputPlayerName(){ printf("\n\n\n\t\tMASUKKAN NAMA PEMAIN: \n\n\t\t "); scanf("%s",&namaPemain);getchar(); printf("\n\n\n"); printf("\tTekan \"ENTER\" untuk memulai permainan..."); getchar(); system("cls"); } void setup(){ x = lebar/2; y = tinggi/2; foodX = rand()%(lebar-1)+1; foodY = rand()%(tinggi-1)+1; skor = 0; lengthTail = 0; end = 0; } void mapSnake(){ for(int i=0;i<=tinggi;i++){ for(int j=0;j<=lebar;j++){ if(i==0 || i==tinggi || j==0 || j==lebar){ printf("#"); } else{ if(i==y && j==x){ printf("O"); } else if(i==foodY && j==foodX){ printf("A"); } else{ int flag=0; for(int k=0;k<lengthTail;k++){ if(j==tailX[k] && i==tailY[k]){ printf("o"); flag=1; } } if(flag==0){ printf(" "); } } } } printf("\n"); } } void moveButton(){ if(kbhit()){ switch(getch()){ case 'w': //UP input = 1; break; case 'a': //LEFT input = 2; break; case 's': //DOWN input = 3; break; case 'd': //RIGHT input = 4; break; case 'x': //END end = 1; break; } } } void move(){ switch(input){ case 1: //UP y--; break; case 2: //LEFT x--; break; case 3: //DOWN y++; break; case 4: //RIGHT x++; break; default: break; } if(x<=0 || x>= lebar || y<=0 || y>=tinggi){ end = 1; } } void food(){ if(x==foodX && y==foodY){ skor += 1; srand(time(NULL)); foodX = rand()%(lebar-1)+1; srand(time(NULL)); foodY = rand()%(tinggi-1)+1; lengthTail++; } } void length(){ int tempX = tailX[0], tempY = tailY[0]; int temppX, temppY; tailX[0] = x, tailY[0] = y; for(int i=1;i<lengthTail;i++){ temppX = tailX[i]; temppY = tailY[i]; tailX[i] = tempX; tailY[i] = tempY; tempX = temppX; tempY = temppY; } } void head(){ for(int i=0;i<lengthTail;i++){ if(x==tailX[i] && y==tailY[i]){ end = 1; } } } void delay(){ for(int i=0;i<120000;i++){ } } void hideCursor(){ CONSOLE_CURSOR_INFO curs; curs.dwSize = 100; curs.bVisible = FALSE; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &curs); } struct highscore{ int highScore; }; void gameOverText(){ printf("\n\n\n\n"); printf("\t______ ___ _______ _____ \n"); printf("\t| ____| / \\ | || ___| \n"); printf("\t| | _ / _ \\| || || || |___ \n"); printf("\t| | | || |_| || || || || ___| \n"); printf("\t| |_| || _ || || || || |___ \n"); printf("\t|_____||_/ \\_||_||_||_||_____| \n"); printf("\t _____ _ _ _____ _____ \n"); printf("\t| ___ | | | | | | ___| | _ | \n"); printf("\t| | | | | | | | | |___ | |_| | \n"); printf("\t| | | | | | | | | ___| | / \n"); printf("\t| |_| | | \\_/ | | |___ | |\\ \\ \n"); printf("\t|_____| \\___/ |_____| |_| \\_\\ \n"); for(int i=0;i<10000;i++){ for(int j=0;j<20000;j++){ } } } int main(){ welcomePage(); aturanMain(); inputPlayerName(); ulang: struct highscore data; FILE *Fdata = fopen("highscore.txt","r"); fscanf(Fdata, "%d", &data.highScore); fclose(Fdata); setup(); hideCursor(); while(!end){ system("cls"); printf("\t=======SNAKE GAME=======\n\n"); mapSnake(); printf("\nNama Pemain : %s\t\t Score = %d\n\n", namaPemain, skor); printf("\t\t\t High Score = %d\n", data.highScore); delay(); length(); moveButton(); move(); food(); head(); } system("cls"); gameOverText(); system("cls"); char mainLagi; printf("\n\n\n\t Kamu memperoleh skor %d\n",skor); if(skor > data.highScore){ printf("\tSELAMAT Kamu menciptakan HIGHSCORE BARU!\n\n"); Fdata = fopen("highscore.txt","w"); fprintf(Fdata,"%d", skor); fclose(Fdata); } if(skor <= data.highScore){ printf(" Coba lagi lain kali untuk mengalahkan highscore :(\n\n"); } printf(" =========================================\n"); printf("\t APAKAH INGIN BERMAIN LAGI ?\n \t JIKA YA, TEKAN 'y' atau 'Y'....\n"); printf(" =========================================\n"); mainLagi = getch(); if(mainLagi == 'y' || mainLagi == 'Y'){ goto ulang; } return 0; }
Write, Run & Share C Language code online using OneCompiler's C online compiler for free. It's one of the robust, feature-rich online compilers for C language, running the latest C version which is C18. Getting started with the OneCompiler's C editor is really simple and pretty fast. The editor shows sample boilerplate code when you choose language as 'C' and start coding!
OneCompiler's C online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample C program which takes name as input and print your name with hello.
#include <stdio.h>
int main()
{
char name[50];
printf("Enter name:");
scanf("%s", name);
printf("Hello %s \n" , name );
return 0;
}
C language is one of the most popular general-purpose programming language developed by Dennis Ritchie at Bell laboratories for UNIX operating system. The initial release of C Language was in the year 1972. Most of the desktop operating systems are written in C Language.
When ever you want to perform a set of operations based on a condition if-else
is used.
if(conditional-expression) {
// code
} else {
// code
}
You can also use if-else for nested Ifs and if-else-if ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to if-else-if ladder.
switch(conditional-expression) {
case value1:
// code
break; // optional
case value2:
// code
break; // optional
...
default:
// code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
// code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while(condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Array is a collection of similar data which is stored in continuous memory addresses. Array values can be fetched using index. Index starts from 0 to size-1.
data-type array-name[size];
data-type array-name[size][size];
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity.
Two types of functions are present in C
Library functions are the in-built functions which are declared in header files like printf(),scanf(),puts(),gets() etc.,
User defined functions are the ones which are written by the programmer based on the requirement.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
//code
}