Skip to content

Instantly share code, notes, and snippets.

/coin.cpp Secret

Created August 12, 2016 18:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/dec9b753e5bef4ee2b734d0060e2e99d to your computer and use it in GitHub Desktop.
Save anonymous/dec9b753e5bef4ee2b734d0060e2e99d to your computer and use it in GitHub Desktop.
#include "coin.h"
using namespace std;
Coin::Coin
(char icon, int x, int y,
int draw_color, int draw_background_color,
int default_color, int default_background_color)
:Object(icon, x, y, draw_color, draw_background_color,
default_color, default_background_color)
{
this->collected = false; //moneta nie jest zebrana
}
Coin::~Coin() {}
bool Coin::isCollected()
{
if(collected) //jesli moneta jest zebrana
{
collected = false; //moneta nie jest juz zebrana
return true; //zwroc wartosc prawda
}
else return false; //zwroc wartosc falsz
}
bool Coin::check_collision(Player* p)
{
if (p->getX() == getX() && p->getY() == getY())
//jesli X i Y gracza jest takie same jak X i Y monety
{
collected = true; //moneta jest zebrana
return true; //zwroc prawde
}
else return false; //zwroc falsz
}
void Coin::collision(Player* p, int x, int y)
{
p->addPoints(); //dodaj graczowi punkt
this->gotoxy(x, y); //przesun monete na pozycje X,Y
}
void Coin::tick(Player* p, int x, int y)
{
Object::tick(); //tick klasy obiekt
if(check_collision(p)) collision(p, x, y);
//jesli gracz zbierze monete wykonaj kolizje
}
#ifndef COIN_H
#define COIN_H
#include "object.h"
#include "player.h"
class Coin :public Object
{
bool collected; //czy moneta jest zebrana
bool check_collision(Player*); //sprawdzanie kolizji z graczem
void collision(Player*, int, int); //w momencie kolizji z graczem
public:
Coin(char = 'O', int = 0, int = 0,
int=YELLOW, int=YELLOW, int=GRAY, int=BLACK);
//konstruktor
~Coin(); //destruktor
void tick(Player*, int, int); //tick monety
bool isCollected(); //sprawdzanie czy gracz zebral monete
};
#endif
#include "color.h"
#include <windows.h>
using namespace std;
Color::Color(int draw_color, int draw_background_color,
int default_color, int default_background_color)
{
this->default_color = default_color;
this->default_background_color = default_background_color;
this->draw_color = draw_color;
this->draw_background_color = draw_background_color;
}
Color::~Color() {}
void Color::paint(int text_color, int bg_color)
{
WORD color = 0x0000; //kolor
color += 16 * bg_color; //ustawianie koloru tla
color += text_color; //ustawianie koloru tekstu
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
//zmiana koloru tla i tekstu
}
void Color::setDefaultColor(int color)
{
default_color = color;
}
void Color::setDefaultBackgroundColor(int color)
{
default_background_color = color;
}
void Color::setDrawColor(int color)
{
draw_color = color;
}
void Color::setDrawBackgroundColor(int color)
{
draw_background_color = color;
}
int Color::getDefaultColor()
{
return default_color;
}
int Color::getDefaultBackgroundColor()
{
return default_background_color;
}
int Color::getDrawColor()
{
return draw_color;
}
int Color::getDrawBackgroundColor()
{
return draw_background_color;
}
#ifndef COLOR_H
#define COLOR_H
class Color
{
int default_color;
int default_background_color;
int draw_color;
int draw_background_color;
//kolory tekstu i tla
public:
enum COLOR
{
BLACK = 0,
DARKBLUE,
DARKGREEN,
DARKCYAN,
DARKRED,
DARKMAGNETA,
DARKYELLOW,
GRAY,
DARKGRAY,
BLUE,
GREEN,
CYAN,
RED,
MAGNETA,
YELLOW,
WHITE
};
//wartosci dla kolorow
Color(int=GRAY, int=BLACK, int=GRAY, int=BLACK);
~Color();
//konstruktor i destruktor
void paint(int, int); //zmienianie koloru tekstu
void setDefaultColor(int);
void setDefaultBackgroundColor(int);
void setDrawColor(int);
void setDrawBackgroundColor(int);
int getDefaultColor();
int getDefaultBackgroundColor();
int getDrawColor();
int getDrawBackgroundColor();
//gettery i settery
};
#endif
#include "coursor.h"
#include <iostream>
#include <windows.h>
using namespace std;
Coursor::Coursor
(int x, int y,
int draw_color, int draw_background_color,
int default_color, int default_background_color)
:Position(x, y),
Color(draw_color, draw_background_color, default_color, default_background_color)
{}
Coursor::~Coursor()
{}
void Coursor::draw_color()
{
paint(getDrawColor(), getDrawBackgroundColor());
}
void Coursor::default_color()
{
paint(getDefaultColor(), getDefaultBackgroundColor());
}
void Coursor::display(string text)
{
draw_color();
//zmiana koloru na wlasny
gotoxy(getX(), getY());
//poruszenie kursora na pozycje X,Y
cout << text;
//wypisanie tekstu
default_color();
//ustawienie koloru na domyslny
}
void Coursor::clear_console()
{
system("cls");
}
void Coursor::display(int value)
{
draw_color();
//zmiana koloru na wlasny
gotoxy(getX(), getY());
//poruszenie kursora na pozycje X,Y
cout << value;
//wypisanie wartosci
default_color();
//ustawienie koloru na domyslny
}
#ifndef COURSOR_H
#define COURSOR_H
#include "color.h"
#include "position.h"
#include <iostream>
class Coursor :public Position,public Color
{
void draw_color();
void default_color();
//ustawianie koloru tekstu
public:
Coursor(int=0, int=0, int=GRAY, int=BLACK, int=GRAY, int=BLACK);
~Coursor();
//konstruktor i destruktor
void display(std::string);
void display(int);
//wyswietlanie tekstu / liczby
void clear_console();
//czyszczenie konsoli
};
#endif // COURSOR_H
#include "enemy.h"
Enemy::Enemy
(char direction, int damage, int slow, char icon, int x, int y,
int draw_color, int draw_background_color,
int default_color, int default_background_color)
:Object(icon, x, y,
draw_color, draw_background_color, default_color, default_background_color)
{
this->direction = direction;
this->damage = damage;
this->slow = slow;
this->tick_timer = 0;
}
Enemy::~Enemy() {}
bool Enemy::check_player_collision(Player* p)
{
if (p->getX() == getX() && p->getY() == getY()) return true;
//jesli X i Y gracza i przeciwnika sa takie same zwroc prawde
else return false;
//jesli nie zwroc falsz
}
void Enemy::player_colision(Player* p)
{
p->hit(damage);
//zadaj graczowi obrazenia
}
void Enemy::move_on()
{
switch(direction)
{
case 'u':
move_up(1);
break;
case 'd':
move_down(1);
break;
case 'l':
move_left(1);
break;
case 'r':
move_right(1);
break;
}
//w zaleznosci od DIRECTION porusz sie o 1 w gore, dol, lewo, prawo
}
void Enemy::tick(Player* p)
{
Object::tick();
//wywolaj tick klasy obiekt
if(check_player_collision(p)) player_colision(p);
//w razie zderzenia z graczem wywolaj kolizje
tick_timer++;
//zwieksz o 1 ilosc tickow od ostatniego ruchu
if(tick_timer < slow) return;
//jesli ilosc tickow od ostatniego ruchu jest mniejsza niz opuznienie
//zakoncz funkcje
else tick_timer = 0;
//jesli nie to ilosc tickow od ostatniego ruchu jest rowna 0
int px = p->getX();
int py = p->getY();
//pobranie wartosci X i Y gracza
if(px < getX()) direction = 'l';
else if (px > getX()) direction = 'r';
else if (py < getY()) direction = 'u';
else if (py > getY()) direction = 'd';
//decydowanie gdzie skrecic aby trafic na gracza
move_on();
//poruszanie sie
}
void Enemy::setDirection(char direction)
{
this->direction = direction;
}
char Enemy::getDirection()
{
return direction;
}
void Enemy::setDamage(int damage)
{
this->damage = damage;
}
int Enemy::getDamage()
{
return damage;
}
void Enemy::setSlow(int slow)
{
this->slow = slow;
}
int Enemy::getSlow()
{
return slow;
}
#ifndef ENEMY_H
#define ENEMY_H
#include "object.h"
#include "player.h"
class Enemy :public Object
{
char direction; //kierunek
int damage; //ilosc obrazen jaka zadaje
int slow; //ile ruchow gracza przypada na ruch wroga
int tick_timer; //ile tickow minelo od ostatniego ruchu
bool check_player_collision(Player*); //sprawdzanie kolizji z graczem
void player_colision(Player*); //w razie kolizji z graczem
void move_on(); //poruszanie sie
public:
Enemy(
char = 'r', int = 1, int = 2, char = 'X', int = 0, int = 0,
int=RED, int=RED, int=GRAY, int=BLACK);
~Enemy();
//konstruktor i destruktor
void tick(Player*); //tick przeciwnika
void setDirection(char);
char getDirection();
void setDamage(int);
int getDamage();
void setSlow(int);
int getSlow();
//gettery i settery
};
#endif
#include <iostream>
#include "player.h"
#include "coin.h"
#include "enemy.h"
#include "coursor.h"
#include "virtualkeys.h"
#include "menu.h"
#include <time.h>
#include <cstdlib>
#include <windows.h>
#include <vector>
#include <stdlib.h>
#include <conio.h>
using namespace std;
int random(int x)
{
return rand()%x + 1;
}
int main()
{
SMALL_RECT r;
r.Left = 0;
r.Top = 0;
r.Right = 63;
r.Bottom = 24;
//kwadrat okreslajacy wielkosc okna konsoli
COORD console_size;
console_size.X = 63;//liczba komorek (poziom)
console_size.Y = 24;//liczba komorek (pion)
SetConsoleScreenBufferSize(GetStdHandle( STD_OUTPUT_HANDLE ),console_size);
SetConsoleWindowInfo(GetStdHandle( STD_OUTPUT_HANDLE ), TRUE, &r );
//zmiana ilosci komorek konsoli
Menu* menu = new Menu(0, Color::BLUE, Color::WHITE);
//wywolanie menu
delete menu;
//usuwanie wskaznika menu
Coursor* cu = new Coursor(0, 0, Color::GREEN);
//tworzenie "kursora"
vector<Enemy> enemies;
//lista przeciwnikow
srand(time(NULL));
//rozpoczecie losowania liczb calkowitych
int tps = 15;
//ilosc tickow na sekunde
enemies.push_back(Enemy('r', 1, 2, 'O', random(64), random(25)));
//tworzenie pierwszego przeciwnika
Coin c('O', random(64), random(25));
//tworzenie monety
Player* p = new Player(1, 'r', 0, 'X', random(64), random(25), Color::WHITE,
Color::WHITE);
//tworzenie gracza
while(p->getHealth() > 0)
{
cu->clear_console();
//czyszczenie okna konsoli
if(c.isCollected()) //jesli gracz zdobedzie punkt
enemies.push_back(Enemy('r', 1, 2, 'O', random(64), random(25)));
//tworzenie nowego przeciwnika
for(int i=0; i < enemies.size(); i++)
{
enemies[i].tick(p);
enemies[i].draw();
}
//ruch i pokazywanie na ekranie przeciwnikow
p->tick();
p->draw();
//ruch i pokazywanie na ekranie gracza
c.tick(p, random(64), random(25));
c.draw();
//pokazywanie na ekranie monety i losowanie jej nowego polozenia
Sleep(1000/tps);
//ustalanie predkosci petli
}
//petla gry
char znak;
do
{
cu->clear_console();
cu->gotoxy(0, 0);
cu->display("Przegrales! \nTwoje punkty: ");
cu->display(p->getPoints());
cu->display("\nWcisnij ENTER aby zakonczyc");
znak = getch();
} while (znak != KB_ENTER); //powtarzaj dopuki znak nie jest rowny ENTER
//wyswietlanie tekstu na koncu gry
delete p;
//usuwanie wskaznika gracza
return 0;
}
#include "menu.h"
#include <iostream>
#include <conio.h>
#include <vector>
#include <windows.h>
#include "virtualkeys.h"
using namespace std;
Menu::Menu(int choose, int selected_color, int selected_background_color)
{
this->choose = choose;
this->selected_color = selected_color;
this->selected_background_color = selected_background_color;
item.push_back(MenuItem("Start", 0, selected_color, selected_background_color));
item.push_back(MenuItem("Wyjdz", 1, selected_color, selected_background_color));
//dodawanie elementow menu
options = 2;
//ilosc opcji menu
loop();
//poczatek petli
}
Menu::~Menu() {}
void Menu::wypisz()
{
for(int i = 0; i < item.size(); i++)
{
if (item[i].getNumber() == choose) item[i].change_color();
//jesli element jest wybrany zmien jego tlo i kolor
cout << item[i].getText() << endl;
//wyswietl element
item[i].reset_color();
//ustaw domyslny kolor tekstu i tla
}
//petla wyswietlajaca wszystkie elementy menu
}
void Menu::loop()
{
char znak; //wybrany znak
bool choosen = false; //czy zostala wybrana jakas opcja
while(!choosen)
{
wypisz();
//wypisz opcje menu
znak = getch();
//pobierz znak z klawiatury
system("cls");
//czysc ekran
if (znak == KB_ENTER) choosen = true;
//jesli znak to enter to wybrano opcje
else if (znak == KB_UP)
//jesli znak to strzalka w gore
{
choose--;
//numer wybranej opcji zmnieszony o 1
if (choose < 0) choose = options - 1;
//jesli numer opcji jest mniejszy niz 0 wybrana jest ostatnia opcja
}
else if (znak == KB_DOWN)
//jesli znak to strzalka w gore
{
choose++;
//numer wybranej opcji zwiekszony o 1
if (choose > options - 1) choose = 0;
//jesli numer opcji jest wiekszy niz ilosc opcji wybierz pierwsza opcje
}
}
//powtarzaj dopuki nie jest wybrana zadna opcja
if(choose == 0) return;
//jesli wybrano opcje o numerze 0 wyjdz z menu i kontynuuj program
else if (choose == 1) exit(0);
//jesli wybrano opcje o numerze 1 zakoncz dzialanie aplikacji
}
#ifndef MENU_H
#define MENU_H
#include "menuitem.h"
#include <vector>
class Menu
{
std::vector<MenuItem> item; //lista opcji
int choose; //obecnie wybrana opcja
int options; //ilosc opcji
int selected_color;
int selected_background_color;
//kolor tekstu i tla wybranej opcji
void wypisz(); //wypisywanie listy pocji
void loop(); //petla menu
public:
Menu(int=0, int=Color::WHITE, int=Color::BLACK);
~Menu();
//konstruktor i destruktor
};
#endif // MENU_H
#include "menuitem.h"
#include <iostream>
using namespace std;
MenuItem::MenuItem
(string text, int number,
int draw_color, int draw_background_color,
int default_color, int default_background_color)
:Color(draw_color, draw_background_color, default_color, default_background_color)
{
this->text = text;
this->number = number;
}
MenuItem::~MenuItem() {}
void MenuItem::reset_color()
{
paint(getDefaultColor(), getDefaultBackgroundColor());
//ustaw kolor na domyslny
}
void MenuItem::change_color()
{
paint(getDrawColor(), getDrawBackgroundColor());
//ustaw kolor na wlasny
}
void MenuItem::setNumber(int number)
{
this->number = number;
}
int MenuItem::getNumber()
{
return number;
}
void MenuItem::setText(string text)
{
this->text = text;
}
string MenuItem::getText()
{
return text;
}
#ifndef MENUITEM_H
#define MENUITEM_H
#include <iostream>
#include "color.h"
class MenuItem :public Color
{
std::string text; //tekst opcji w menu
int number; //numer opcji
public:
MenuItem(std::string="Napis", int=0, int=GRAY, int=BLACK, int=GRAY, int=BLACK);
~MenuItem();
//konstruktor i destruktor
void reset_color();
void change_color();
//ustawianie koloru tekstu
void setNumber(int);
int getNumber();
void setText(std::string);
std::string getText();
//gettery i settery
};
#endif // MENUITEM_H
#include "object.h"
#include <iostream>
using namespace std;
Object::Object
(char icon, int x, int y,
int draw_color, int draw_background_color,
int default_color, int default_background_color)
:Position(x, y),
Color(draw_color, draw_background_color, default_color, default_background_color)
{
this->icon = icon;
}
Object::~Object() {}
void Object::draw()
{
paint(getDrawColor(), getDrawBackgroundColor());
//ustaw kolor tekstu i tla na wlasny
gotoxy(getX(), getY());
//przesun kursor na koordynaty X,Y
cout << icon;
//wyswietl znak obiektu
paint(getDefaultColor(), getDefaultBackgroundColor());
//ustaw kolor na domyslny
go();
//przesun sie na koordynaty X,Y
}
void Object::tick() {}
void Object::setIcon(char icon)
{
this->icon = icon;
}
char Object::getIcon()
{
return icon;
}
#ifndef OBJECT_H
#define OBJECT_H
#include "position.h"
#include "color.h"
class Object :public Position,public Color
{
char icon; //znak obiektu w konsoli
public:
Object(
char = 'X', int = 0, int = 0,
int=GRAY, int=BLACK, int=GRAY, int=BLACK);
~Object();
//konstruktor i destruktor
void draw(); //wyswietlanie znaku obiektu
void tick(); //tick obiektu
void setIcon(char);
char getIcon();
//gettery i settery
};
#endif
#include "player.h"
#include <conio.h>
Player::Player
(int health, char direction, int points, char icon, int x, int y,
int draw_color, int draw_background_color,
int default_color, int default_background_color)
:Object(icon, x, y, draw_color, draw_background_color, default_color, default_background_color)
{
this->health = health;
this->direction = direction;
this->points = points;
}
bool Player::wall_collision(int x, int y)
{
switch (direction)
{
case 'u':
if (y <= 1) return true;
break;
case 'd':
if (y >= 25) return true;
break;
case 'l':
if (x <= 1) return true;
break;
case 'r':
if (x >= 64) return true;
break;
}
//porownywanie X i Y gracza w zaleznosci od kierunku ze "scianami"
//jesli jest kolizja zwroc wartosc prawda
return false;
//jesli nie zwroc wartosc falsz
}
void Player::move_on()
{
switch(direction)
{
case 'u':
move_up(1);
break;
case 'd':
move_down(1);
break;
case 'l':
move_left(1);
break;
case 'r':
move_right(1);
break;
}
//porusz sie w zaleznosci od DIRECTION w lewo, prawo, gore, dol
}
void Player::addPoints()
{
points++;
//dodaj 1 punkt
}
void Player::tick()
{
Object::tick();
//tick klasy obiekt
if(kbhit())
//jesli wcisnieto klawisz
{
char znak = getch();
//pobierz znak z klawiatury
switch(znak)
{
case KB_UP:
direction = 'u';
break;
case KB_DOWN:
direction = 'd';
break;
case KB_LEFT:
direction = 'l';
break;
case KB_RIGHT:
direction = 'r';
break;
}
//zmien kierunek w zaleznosci od tego ktora strzalka jest wcisnieta
}
if(!wall_collision(getX(), getY())) move_on();
//jesli nie ma kolizji ze sciana porusz sie
}
void Player::hit(int damage)
{
this->health -= damage;
//odejmij od punktow zycia okreslona liczbe
}
void Player::setPoints(int points)
{
this->points = points;
}
int Player::getPoints()
{
return points;
}
void Player::setHealth(int health)
{
this->health = health;
}
int Player::getHealth()
{
return health;
}
void Player::setDirection(char direction)
{
this->direction = direction;
}
char Player::getDirection()
{
return direction;
}
#ifndef PLAYER_H
#define PLAYER_H
#include "virtualkeys.h"
#include "object.h"
class Player :public Object
{
int points; //punkty
int health; //zycia
char direction; //kierunek
bool wall_collision(int, int); //sprawdzanie kolizji z brzegiem konsoli
void move_on(); //poruszanie sie
public:
Player(int=1, char='r', int=0, char='X', int=0, int=0,
int=GRAY, int=BLACK, int=GRAY, int=BLACK);
void addPoints();
//konstruktor i destruktor
void tick(); //tick gracza
void hit(int); //zderzenie z wrogiem
void setPoints(int);
int getPoints();
void setHealth(int);
int getHealth();
void setDirection(char);
char getDirection();
//gettery i settery
};
#endif
#include "position.h"
#include <windows.h>
using namespace std;
Position::Position(int x, int y)
{
this->x = x;
this->y = y;
go();
//porusz sie na koordynaty X,Y
}
Position::~Position() {}
void Position::gotoxy(int x, int y)
{
COORD c;
c.X = x - 1;
c.Y = y - 1;
//ustawienie X i Y kursora w konsoli
this->x = x;
this->y = y;
//ustawienie wartosci X i Y klasy position
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
//przesuniecie kursora na koordynaty X,Y
}
void Position::go()
{
gotoxy(x, y);
//przesuniecie kursora na koordynaty X,Y
}
void Position::move_up(int a)
{
y -= a;
go();
//porusz sie o A pol w gore
}
void Position::move_down(int a)
{
y += a;
go();
//porusz sie o A pol w dol
}
void Position::move_left(int a)
{
x -= a;
go();
//porusz sie o A pol w lewo
}
void Position::move_right(int a)
{
x += a;
go();
//porusz sie o A pol w prawo
}
void Position::setX(int x)
{
this->x = x;
}
void Position::setY(int y)
{
this->y = y;
}
int Position::getX()
{
return this->x;
}
int Position::getY()
{
return this->y;
}
#ifndef POSITION_H
#define POSITION_H
class Position
{
int x, y; //pozycja kursora
public:
Position(int = 0, int = 0);
~Position();
//konstruktor i destruktor
void gotoxy(int, int); //poruszenie kursora na dowolna pozycje
void go(); //poruszenie kursora na zapisana pozycje
void move_up(int);
void move_down(int);
void move_left(int);
void move_right(int);
//poruszanie kursora gora, dol, lewo, prawo
void setX(int);
void setY(int);
int getX();
int getY();
//gettery i settery
};
#endif
#ifndef VIRTUALKEYS_H
#define VIRTUALKEYS_H
#define KB_UP 72
#define KB_DOWN 80
#define KB_LEFT 75
#define KB_RIGHT 77
#define KB_ENTER 13
//stale odpowiadajace konkretnym klawisza
#endif // VIRTUALKEYS_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment