Improvements

Added copy-constructor and operator=
This commit is contained in:
Valentino Orlandi 2023-01-27 02:02:57 +01:00
parent 224c224e29
commit f946f96fdf
Signed by: elB4RTO
GPG Key ID: 1719E976DB2D4E71
2 changed files with 35 additions and 12 deletions

View File

@ -9,6 +9,26 @@ Food::Food( const bool& can_move )
this->x = 0;
this->y = 0;
}
Food::Food( const Food& other )
{
this->x = other.x;
this->y = other.y;
this->img_egg = other.img_egg;
this->img_rat = other.img_rat;
this->movable = other.movable;
this->image = new QGraphicsPixmapItem( (this->movable) ? this->img_rat : this->img_egg );
}
const Food& Food::operator=( const Food& other )
{
if ( this == &other ) return other;
this->x = other.x;
this->y = other.y;
this->img_egg = other.img_egg;
this->img_rat = other.img_rat;
this->movable = other.movable;
this->image = new QGraphicsPixmapItem( (this->movable) ? this->img_rat : this->img_egg );
return *this;
}
const unsigned int& Food::X() const
@ -37,7 +57,8 @@ const bool Food::inTile( const unsigned int x, const unsigned int y ) const
}
void Food::update( const unsigned int new_x, const unsigned int new_y ) {
void Food::update( const unsigned int new_x, const unsigned int new_y )
{
this->x = new_x;
this->y = new_y;
this->image->setOffset( 16+(new_x*32), 16+(new_y*32) );
@ -47,15 +68,15 @@ void Food::update( const unsigned int new_x, const unsigned int new_y ) {
void Food::spawn( const Snake& snake, const Snake& snake_ )
{
// pick a new random position
unsigned int x, y;
unsigned int new_x, new_y;
while (true) {
x = rand() % 16;
y = rand() % 16;
new_x = rand() % 16;
new_y = rand() % 16;
// check it's actually inside the field
if ( x < 16 && y < 16 ) {
if ( new_x < 16 && new_y < 16 ) {
// check the tile is empty
if ( x != this->x && y != this->y ) {
if ( !(snake.inTile( x, y, false ) || snake_.inTile( x, y, false )) ) {
if ( new_x != this->x && new_y != this->y ) {
if ( !(snake.inTile( new_x, new_y, false ) || snake_.inTile( new_x, new_y, false )) ) {
break;
}
}
@ -63,7 +84,7 @@ void Food::spawn( const Snake& snake, const Snake& snake_ )
}
// update to new position
this->update( x, y );
this->update( new_x, new_y );
// randomly rotate the image
int rand_ = rand()%4;
@ -150,17 +171,17 @@ void Food::move( const Snake& snake )
move_up += 30;
} else if ( this->y > snake_y ) {
move_down += 30;
} else if ( this->y == snake_y ) {
} else /*if ( this->y == snake_y )*/ {
move_up += 30;
move_down += 30;
}
} else if ( this->x > snake_x ) {
} else /*if ( this->x > snake_x )*/ {
move_right += 30;
if ( this->y < snake_y ) {
move_up += 30;
} else if ( this->y > snake_y ) {
move_down += 30;
} else if ( this->y == snake_y ) {
} else /*if ( this->y == snake_y )*/ {
move_up += 30;
move_down += 30;
}

View File

@ -9,6 +9,8 @@ class Food
{
public:
explicit Food( const bool& can_move=false );
explicit Food( const Food& other );
const Food& operator =( const Food& other );
//! Returns the position on the X-axis
const unsigned int& X() const;
@ -47,7 +49,7 @@ private:
unsigned int y;
//!< The image which graphically represents the food
QGraphicsPixmapItem* image;
QGraphicsPixmapItem* image = nullptr;
};
#endif // FOOD_H