Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "windows.h"
- #include <conio.h>
- #include <fstream>
- #include <string>
- #include <cstdlib>
- using namespace std;
- struct game_properties {
- int score;
- int level;
- int dela;
- } ;
- struct game_controllers {
- int g;
- int bul;
- int l;
- int r;
- int key;
- } ;
- int mat[6][16];
- game_controllers controllers;
- controllers.g=0;
- controllers.bul=1;
- controllers.l=0;
- game_properties options;
- options.score = 1;
- optionslevel = 1;
- options.dela = 100;
- int i=0;
- void gotoxy(int x, int y)
- {
- COORD coord;
- coord.X = x;
- coord.Y = y;
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
- }
- void logo()
- {
- int enter;
- gotoxy(0, 0);
- cout << "";
- gotoxy(1, 1);
- cout << " O))))))) O) O)) O)))))))" << endl;
- cout << " O)) O)) O) )) O)) O)) O)) " << endl;
- cout << " O)) O)) O) O)) O)) O)) " << endl;
- cout << " O) O)) O)) O)) O)) O)))))) " << endl;
- cout << " O)) O)) O)))))) O)) O)) O)) " << endl;
- cout << " O)) O)) O)) O)) O)) O)) O)) " << endl;
- cout << " O)) O)) O)) O)) O)))) O))))))))" << endl;
- Sleep(1000);
- for (int i = 25; i > 0; i--)
- {
- gotoxy(1, i);
- cout << " ";
- Sleep(100);
- //system("cls");
- }
- }
- void resetb()
- {
- for(int j=0;j<16;j++){
- for(int i=0;i<6;i++){mat[i][j]=0;}
- }
- }
- void drawp(int x,int y)
- {
- if(x>=0&x<6&y>=0&y<16){mat[x][y]=1;}
- }
- void drawc(int x,int y)
- {
- drawp(1+x,0+y);
- drawp(0+x,1+y);drawp(1+x,1+y);drawp(2+x,1+y);
- drawp(1+x,2+y);
- drawp(0+x,3+y);drawp(1+x,3+y);drawp(2+x,3+y);
- }
- int getr()
- {
- int n;
- if(rand()%2==0){n=0;}else{n=3;}
- return n;
- }
- HANDLE hstdout = GetStdHandle( STD_OUTPUT_HANDLE );
- int index=1;
- void colorit(int col){;
- SetConsoleTextAttribute( hstdout, col );
- }
- int showMenu();
- DWORD WINAPI ThreadFn(LPVOID vpParam)
- {
- while (1) {
- for(int i=8;i<=190;i++)
- {
- if(GetAsyncKeyState(i)==-32767){
- if(i==37){controllers.l=0;}else if(i==39){controllers.l=3;}
- }
- }
- }
- return 0;
- }
- DWORD qThreadID;
- HANDLE hThread;
- void startInput(){
- hThread = CreateThread(0, 0, ThreadFn, 0, 0, &qThreadID);
- }
- void startGame(){
- system("cls");
- gotoxy(7,2);cout<<" CONTROLS";
- gotoxy(7,3);cout<<" ========";
- gotoxy(7,4);cout<<"<<== [LEFT ARROW]";
- gotoxy(7,5);cout<<"==>> [RIGHT ARROW]";
- for(int i=3;i>=0;i--){
- gotoxy(7,7);cout<<i;
- gotoxy(14,14);
- Sleep(1000);
- }
- system("cls");
- while(controllers.bul){
- gotoxy(14,4);cout<<"SCORE:"<<options.score;
- gotoxy(14,5);cout<<"LEVEL:"<<options.level;
- resetb();
- drawc(controllers.r,controllers.g);
- drawc(controllers.l,12);
- for(int j=0;j<16;j++){
- gotoxy(4,j+1);cout<<"8";
- gotoxy(5,j+1);
- for(int i=0;i<6;i++){if(mat[i][j]==1){cout <<"O";}else{cout<<" ";}}gotoxy(11,j+1);cout<<"8";
- if(controllers.g>=12&controllers.l==controllers.r){controllers.bul=0;}
- }
- controllers.g++;
- if(controllers.g==15){controllers.g=-4;controllers.r=getr();;options.score=options.score+1;}
- if(options.score%5==0&(controllers.g==15|controllers.g==-4)){options.level++;options.dela=options.dela-1;}
- if(options.dela<0){gotoxy(14,4);cout<<"YOU WIN!"<<endl;controllers.bul=0;}
- Sleep(options.dela);
- }
- if(options.dela>0){ gotoxy(14,4);colorit(12);
- cout<<"GAME OVER"<<endl;colorit(15);
- }
- Sleep(1000);
- system("cls");
- gotoxy(14,5);colorit(12);cout<<"YOUR SCORE: "<< options.score <<endl;colorit(15);;
- fstream plik1("D:plik.txt", ios::out|ios::app);
- plik1 << options.score << endl;
- for (int tab[i],int n)
- {
- for(int i=0;i<n;i++)
- for(int j=1;j<n-i;j++)
- if(tab[j-1]>tab[j])
- swap(tab[j-1], tab[j]);
- }
- plik1.close();
- gotoxy(14,4);colorit(12);cout<<"GAME OVER"<<endl;colorit(15);
- Sleep(1500);
- controllers.g=0,controllers.bul=1,controllers.l=0,controllers.r;
- options.score=1,options.level=1,options.dela=100;
- showMenu();
- }
- void exitGame(){
- CloseHandle(hThread);
- system("cls");
- gotoxy(3,3);cout<<"GOOD BYE!";
- colorit(15);
- Sleep(2500);
- }
- int showMenu(){
- int ret=1;
- system("cls");
- controllers.key=0;
- system("cls");
- while(controllers.key!=13){
- gotoxy(3,3);
- colorit(15);
- cout<<"CAR GAME CONSOLE";
- gotoxy(3,4);
- cout<<"================";
- gotoxy(3,5);
- colorit(15);
- if(index==1){colorit(12);}
- cout<<"START GAME";
- gotoxy(3,6);
- colorit(15);
- if(index==2){colorit(12);}
- cout<<"BEST SCORES";
- gotoxy(3,7);
- colorit(15);
- if(index==3){colorit(12);}
- cout<<"EXIT";
- controllers.key=getch();
- if(controllers.key==80){index++;}
- else if(controllers.key==72){index--;}
- if(index==5){index=1;}
- if(index==0){index=4;}
- }
- if(index==1){
- startGame();
- }
- else if (index==2){
- system("cls");
- int zmienna;
- FILE* plik1;
- plik1 = fopen("D:plik.txt", "rb");
- int liczba;
- fread(&liczba, sizeof(int), 10, plik1);
- cout << liczba << endl;
- fclose(plik1);
- Sleep(5000);
- showMenu();
- }
- else if (index==3){
- ret=1;
- exitGame();
- }
- return ret;
- }
- int main()
- {
- logo();
- startInput();
- showMenu();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement