Question

I am doing my Object oriented programming assignment in which I am asked to create a game of catching numbers for children so that during their enjoyment they also learn counting numbers.

In here , I am supposed to create a Point class and an x-y coordinate. In here , I have to create a shift function which takes P (point object as a parameter). This function shift the first point when user presses key i.e arrow keys.

The problem is I am confused about what are the actual keywords use for arrow keys (like move UP, DOWN, LEFT, RIGHT) in c++ like we use in normal games to move an object or a person! ???

Here below is my code! Class Point.h

#ifndef POINT_H
#define POINT_H

class Point
{
public:
    Point();                                    // Default Constructor
    Point(double, double, int);                // Three argument constructor
    void initialize(double, double, int);    
    void shift(Point p);                      // Shift the first point when user press keys
    void setValue(int value);
    int getValue() const;
    void setX();
    double getX() const;
    void setY();
    double gety() const;
    void AddPointValue(Point p2);             /*This function add the TWO points value  

    void displayPoint();                     //This will use to display value of point  
    bool checkCoordinates();
    bool checkTime();                        // Check time remaining
private:
    double x;
    double y;
    int value;
};

#endif

Implementation File

#include <iostream>
#include <windows.h>
#include "point.h"



using namespace std;

Point::Point()    // Default Constructor
{
x = 0;
y = 0;
value = 0;
}

Point::Point(double x1, double y1, int value1){   // Three argument constructor
x = x1;
y = y1;
value = value1;

}

void Point::initialize(double init_x, double init_y, int init_value)
{
x = init_x;
y = init_y;
value = init_value;

}

void Point::shift(Point p){
if(p == VK_LEFT)
{

}else if(p == VK_RIGHT)
{

}else if(p == VK_UP)
{

}else if(p == VK_DOWN)
{

}
}

Its giving me an error right now that no match for an operator==(Operand type 'point' and 'int')

Was it helpful?

Solution

The issue with point and int is because you're trying to compare a 2d coordinate against an ASCII value (VK_*), change that part the following and it should be easier to maintain:

Point Point::shift(Point p, int keyPress)
{
    Point maxSize = new Point();
    Point minSize = new Point();
    maxSize.x=80;
    maxSize.y=40; 
 //  Assuming a coordinate system of 0,0 (x,y) at top left of the display
    switch (keyPress)
    {
      case (VK_LEFT):    // increment the x coord by 1 to go left
        p.x += 1;
        if (p.x < minSize.x) p.x = minSize.x;
        break;
      case (VK_RIGHT):    // decrement the x coord by 1 to go right
        p.x -= 1;
        if (p.x > maxize.x) p.x = maxSize.x;
        break;
      case (VK_UP):    // decrement the y coord by 1 to go up
        p.y -= 1;
        if (p.y < minSize.y) p.y = minSize.y;
        break;
      case (VK_DOWN):    // increment the y coord by 1 to go down
        p.y += 1;
        if (p.y > maxize.y) p.y = maxSize.y;
        break;
    }
    return p;
}

you will also have to check that x and y are never smaller than 0 as that would take them off the display/cause an exception depending on how you structure the code and play area.

Hope this helps for your movement issues, but let me know if you need more info:)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top