Tuesday, June 3, 2014

#5 turtle Include kornjaca

#ifndef C_TURTLE_H
#define C_TURTLR_H
#include <graphics.h>
#include "c_shape.h"

class c_turtle:public c_shape{
public:
c_turtle(int _x, int _y, int _r, int _color);
void drawMe(int x, int y);
void eraseMe(int x, int y, int backColor);
};
c_turtle::c_turtle(int _x, int _y, int _r, int _color){

x=_x;
y=_y;
radius=_r;
color=_color;
drawMe(x,y);
}
void c_turtle::drawMe(int x, int y){
setcolor(this->getColor());
     circle(x,y,this->getRadius());  

}
void c_turtle::eraseMe(int x, int y, int backColor){
setcolor(backColor);
    circle(x,y,this->getRadius());
}
#endif

#5 square.h include pravougaonik

#ifndef C_SQUARE_H
#define C_SQUARE_H
#include <graphics.h>
#include "c_shape.h"

class c_square:public c_shape{
public:
c_square(int _x, int _y, int _r, int _color);
void drawMe(int x, int y);
void eraseMe(int x, int y, int backColor);
};
c_square::c_square(int _x, int _y, int _r, int _color){

x=_x;
y=_y;
radius=_r;
color=_color;
drawMe(x,y);
}
void c_square::drawMe(int x, int y){
setcolor(this->getColor());
rectangle(x-this->getRadius(),y-this->getRadius(),x+getRadius(),y+getRadius());
     //circle(x,y,this->getRadius());  

}
void c_square::eraseMe(int x, int y, int backColor){
setcolor(backColor);
rectangle(x-this->getRadius(),y-this->getRadius(),x+getRadius(),y+getRadius());
    //circle(x,y,this->getRadius());
}
#endif

#8 shape.h

#ifndef C_SHAPE_H
#define C_SHAPE_H
#include <graphics.h>
class c_shape{
protected:
int x;
int y;
int color;
int radius;
public:
   //c_shape(int _x, int _y, int _r, int _color);
   c_shape();
~c_shape();
virtual void drawMe(int x, int y)=0;
virtual void eraseMe(int x, int y, int backColor)=0;
void moveMe(int x, int y, int backColor);
                        //get
                        const int & getX() const;
                        const int & getY() const;
                        const int & getColor() const;
                        const int & getRadius()const;
                        // set
                        void setColor(const int & _color);
                        void setRadius(const int & _radius);
};
c_shape::c_shape(){
//constructor;
}
c_shape:: ~c_shape(){

}
/*
c_shape::c_shape(int _x, int _y, int _r, int _color){

x=_x;
y=_y;
radius=_r;
color=_color;

}*/
void c_shape::moveMe(int x, int y, int backColor){
this->eraseMe(this->x,this->y, backColor);
this->drawMe(x,y);
this->x=x;
this->y=y;
}
const int & c_shape::getX() const{
    return x;
}
const int & c_shape::getY() const{
    return y;
}
const int & c_shape::getColor() const{
    return color;
}
const int & c_shape::getRadius() const{
    return radius;
}

void c_shape::setColor(const int& _color){
    color=_color;
}
void c_shape::setRadius(const int& _radius){
    radius=_radius;
}
#endif

#8 game.h Include Igra

#ifndef C_GAME_H
#define C_GAME_H
#include <graphics.h>
#include "c_shape.h"
#include "c_turtle.h"
#include "c_square.h"
class c_game{

private:

public:

c_game();
~ c_game();
void play(int _maxX,int _maxY);
void move (char direction, int step,int _maxX,int _maxY, int backColor, c_shape* shape);
};

c_game::c_game(){
    // constructor
}
c_game::~c_game(){
    //destructor
}
void c_game::play(int _maxX,int _maxY){

     c_shape *shape=new c_square(_maxX/2,_maxY/2,20,GREEN);
     char c=' ';
while(c!='q'){
  if(kbhit( )){
      c=getch();
      this->move((int) c,10,_maxX,_maxY,BLACK,shape);          
  }
}  
}
void c_game::move(char direction, int step,int _maxX,int _maxY, int backColor, c_shape* shape){
  switch(direction){
    case KEY_LEFT:
       if (shape->getX()-shape->getRadius()-step>0) {
        shape->moveMe(shape->getX()-step,shape->getY(),backColor);            
       }
    break;
    case KEY_RIGHT:
       if (shape->getX()+shape->getRadius()+step<maxX) {
        shape->moveMe(shape->getX()+step,shape->getY(),backColor);  
       }
    break;
    case KEY_UP:
       if (shape->getY()-shape->getRadius()-step>0) {
       shape->moveMe(shape->getX(),shape->getY()-step,backColor);
       }
    break;
    case KEY_DOWN:
       if (shape->getY()+shape->getRadius()+step<maxY) {
        shape->moveMe(shape->getX(),shape->getY()+step,backColor);
       }
    break;                                  
 }  

}

#endif

#8 Abstraktna klasa nasledjivanje Igrica

#define maxX 800
#define maxY 800
#include <conio.h>
#include <stdio.h>
#include "c_game.h"
main(){
  initwindow(maxX,maxY); //open a 400x300 graphics window
        c_game *game= new c_game();
        game->play(maxX,maxY);
      while(!kbhit());     //wait for user to press a key
      closegraph();        //close graphics window
       
}