c++ - 在 C++ 中为游戏创建对象

标签 c++ object pong

我正在编写 Pong 游戏的代码,但在创建对象时遇到了问题。我有工作代码,但它不包含类或任何对象,因为它是面向对象编程类,我需要修复它。但是,现在我收到了一堆不同的错误消息(它在 100 后停止显示)。他们中的大多数人似乎都在处理我没有正确创建对象的问题。

我从来没有使用过 C++ 中的对象(可能应该使用过,但刚刚在这门课上蒙混过关,并因此而被扣分),但由于学期即将结束,我真的需要好好表现在这个项目上。

更新 我已经设法摆脱了大部分错误,但仍然有一些让我感到难过。我已编辑我的代码以反射(reflect)我所做的更改并更新了错误列表。

这是我的代码:

我的头文件:

#include "stdafx.h"
#include <string>
#include <Windows.h>
#include <iostream>
//#include <conio.h>
#include <sstream>
#include <math.h>
//#include <gl\gl.h>
//#include <gl\glu.h>
#include "GL/freeglut.h"
#pragma comment(lib, "Opengl32.lib")
#define VK_W 0x57
#define VK_S 0x53
using namespace std;

class Pong {
public:
    Pong();
    std::string int2str;
    void drawText(float x, float y, std::string text);
    void drawPaddle(float x, float y, float width, float height);
    void draw();
    void enable2D(int width, int height);
    void keyboard();
    void vec2_norm(float& x, float &y);
    void updateBall();
    void gameOverCheck();
    void update(int value);

    //window size and update rate
    int width;
    int height;
    int interval; // 60 frames per-second

    //scoring
    int p1Score; //Player 1's score
    int p2Score; //Player 2's score
    int winner;

    //the paddles
    int paddleWidth;
    int paddleHeight;
    int paddleSpeed;
    float paddleLeftX;
    float paddleLeftY;
    float paddleRightX;
    float paddleRightY;

    //the ball
    float ballPositionX; 
    float ballPositionY;
    float ballDirectionX;
    float ballDirectionY;
    int ballSize;
    int ballSpeed;
};

我的 .cpp 文件:

#include "stdafx.h"
#include "PongGame.h"
#include <string>
#include <Windows.h>
#include <iostream>
//#include <conio.h>
#include <sstream>
#include <math.h>
//#include <gl\gl.h>
//#include <gl\glu.h>
#include "GL/freeglut.h"
#pragma comment(lib, "Opengl32.lib")
#define VK_W 0x57
#define VK_S 0x53
using namespace std;

Pong pong;
Pong::Pong() {
    width = 500;
    height = 300;
    interval = 1000/60;
    p1Score = 0;
    p2Score = 0;
    winner = 0;
    paddleWidth = 10;
    paddleHeight = 80;
    paddleSpeed = 3;
    paddleLeftX = 10.0f;
    paddleLeftY = 50.0f;
    paddleRightX = width - paddleWidth - 10;
    paddleRightY = 50;
    ballPositionX = width / 2;
    ballPositionY = height / 2;
    ballDirectionX = -1.0f;
    ballDirectionY = 0.0f;
    ballSize = 8;
    ballSpeed = 3;
}

std::string int2str(int x) { //used to convert an integer to a string
    std::stringstream ss;
    ss << x;

    return ss.str( );
}

void drawText(float x, float y, std::string text) {
    glRasterPos2f(x, y);
    glutBitmapString(GLUT_BITMAP_8_BY_13, (const unsigned char*)text.c_str());
}

void drawPaddle(float x, float y, float width, float height) {
    glBegin(GL_QUADS);
        glVertex2f(x, y);
        glVertex2f(x + width, y);
        glVertex2f(x + width, y + height);
        glVertex2f(x, y + height);
    glEnd();
}

void draw() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    //draws the paddles
    drawPaddle(pong.paddleLeftX, pong.paddleLeftY, pong.paddleWidth, pong.paddleHeight);
    drawPaddle(pong.paddleRightX, pong.paddleRightY, pong.paddleWidth, pong.paddleHeight);

    //draws the ball
    drawPaddle(pong.ballPositionX - pong.ballSize / 2, pong.ballPositionY- pong.ballSize / 2, pong.ballSize, pong.ballSize);

    //draws the score at the top center of the screen
    drawText(pong.width / 2 - 10, pong.height - 15, int2str(pong.p1Score) + ":" + int2str(pong.p2Score));

    glutSwapBuffers();
}

void enable2D(int width, int height) {
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0f, width, 0.0f, height, 0.0f, 1.0f);
    glMatrixMode (GL_MODELVIEW);
    glLoadIdentity();
}

void keyboard() { //allows teh paddles to be controled from the keyboard
    //moves left paddle (player 1)
    if (GetAsyncKeyState(VK_W))
        pong.paddleLeftY += pong.paddleSpeed; //move paddle up with "W" key

    if (GetAsyncKeyState(VK_S)) 
        pong.paddleLeftY -= pong.paddleSpeed; //move paddle down with "S" key

    //moves right paddle (player 2)
    if (GetAsyncKeyState(VK_UP)) 
        pong.paddleRightY += pong.paddleSpeed; //move paddle up with "up" arrow

    if (GetAsyncKeyState(VK_DOWN)) 
        pong.paddleRightY -= pong.paddleSpeed; //move paddle down with "down" arrow
}

void vec2_norm(float& x, float &y) {
    float length = sqrt((x * x) + (y * y));

    if(length != 0.0f) {
        length = 1.0f / length;
        x *= length;
        y *= length;
    }
}

void updateBall() { //allows teh ball to move
    pong.ballPositionX += pong.ballDirectionX * pong.ballSpeed;
    pong.ballPositionY += pong.ballDirectionY * pong.ballSpeed;

    if(pong.ballPositionX < pong.paddleLeftX + pong.paddleWidth && pong.ballPositionX > pong.paddleLeftX && pong.ballPositionY < pong.paddleLeftY + pong.paddleHeight && pong.ballPositionY > pong.paddleLeftY) { //if ball is hit by player 1's paddle
        float t = ((pong.ballPositionY - pong.paddleLeftY) / pong.paddleHeight) - 0.5f;
        pong.ballDirectionX = fabs(pong.ballDirectionX); 
        pong.ballDirectionY = t;
    }

    if (pong.ballPositionX > pong.paddleRightX && pong.ballPositionX < pong.paddleRightX + pong.paddleWidth && pong.ballPositionY < pong.paddleRightY + pong.paddleHeight && pong.ballPositionY > pong.paddleRightY) { //if ball is hit by player 2's paddle
        float t = ((pong.ballPositionY - pong.paddleRightY) / pong.paddleHeight) - 0.5f;
        pong.ballDirectionX = -fabs(pong.ballDirectionX); 
        pong.ballDirectionY = t;
    }

    if (pong.ballPositionX < 0) { //if ball hits the top wall
        ++pong.p2Score;
        pong.ballPositionX = pong.width / 2;
        pong.ballPositionY = pong.height / 2;
        pong.ballDirectionX = fabs(pong.ballDirectionX); 
        pong.ballDirectionY = 0;
    }

    if (pong.ballPositionX > pong.width) { //if ball hits the right wall
        ++pong.p1Score;
        pong.ballPositionX = pong.width / 2;
        pong.ballPositionY = pong.height / 2;
        pong.ballDirectionX = -fabs(pong.ballDirectionX); 
        pong.ballDirectionY = 0;
    }

    if (pong.ballPositionY > pong.height) { //ball hits top wall
        pong.ballDirectionY = -fabs(pong.ballDirectionY); 
    }

    if (pong.ballPositionY < 0) { //ball hits bottom wall
        pong.ballDirectionY = fabs(pong.ballDirectionY); 
    }

    vec2_norm(pong.ballDirectionX, pong.ballDirectionY);
}

void gameOverCheck() {
    const int maxScore = 10;
    if(pong.p1Score == maxScore) {
        cout << "Player 1 Wins!" << endl;
        pong.winner = 1;
    }
    else if(pong.p2Score == maxScore) {
        cout << "Player 2 Wins!" << endl;
        pong.winner = 2;
    }
}

void update(int value) {
   keyboard();

   if(pong.winner == 0) {
        updateBall();
        glutTimerFunc(pong.interval, update, 0);
        glutPostRedisplay();
        gameOverCheck();
    }
}

我的主文件:

//PongGameTest.cpp
#include "stdafx.h"
#include "PongGame.h"
#include <string>
#include <Windows.h>
#include <iostream>
//#include <conio.h>
#include <sstream>
#include <math.h>
//#include <gl\gl.h>
//#include <gl\glu.h>
#include "GL/freeglut.h"
#pragma comment(lib, "Opengl32.lib")
#define VK_W 0x57
#define VK_S 0x53
using namespace std;

int _tmain(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(500, 200);
    glutCreateWindow("Pong");

    glutDisplayFunc(pong.draw);
    glutTimerFunc(pong.interval, pong.update, 0);

    pong.enable2D(pong..width, pong.height);
    glColor3f(1.0f, 0.0f, 0.0f);

    glutMainLoop();

    return 0;
}

我敢肯定你们会因此而讨厌我,但这是我在编译时遇到的错误:

1>  PongGameTest.cpp
1>c:\users\hitechredneck\desktop\fall 2014\object-oriented programming\game\ponggame\ponggame\ponggametest.cpp(25): error C2065: 'pong' : undeclared identifier
1>c:\users\hitechredneck\desktop\fall 2014\object-oriented programming\game\ponggame\ponggame\ponggametest.cpp(25): error C2228: left of '.draw' must have class/struct/union
1>          type is ''unknown-type''
1>c:\users\hitechredneck\desktop\fall 2014\object-oriented programming\game\ponggame\ponggame\ponggametest.cpp(26): error C2065: 'pong' : undeclared identifier
1>c:\users\hitechredneck\desktop\fall 2014\object-oriented programming\game\ponggame\ponggame\ponggametest.cpp(26): error C2228: left of '.interval' must have class/struct/union
1>          type is ''unknown-type''
1>c:\users\hitechredneck\desktop\fall 2014\object-oriented programming\game\ponggame\ponggame\ponggametest.cpp(26): error C2065: 'pong' : undeclared identifier
1>c:\users\hitechredneck\desktop\fall 2014\object-oriented programming\game\ponggame\ponggame\ponggametest.cpp(26): error C2228: left of '.update' must have class/struct/union
1>          type is ''unknown-type''
1>c:\users\hitechredneck\desktop\fall 2014\object-oriented programming\game\ponggame\ponggame\ponggametest.cpp(28): error C2065: 'pong' : undeclared identifier
1>c:\users\hitechredneck\desktop\fall 2014\object-oriented programming\game\ponggame\ponggame\ponggametest.cpp(28): error C2228: left of '.enable2D' must have class/struct/union
1>          type is ''unknown-type''
1>c:\users\hitechredneck\desktop\fall 2014\object-oriented programming\game\ponggame\ponggame\ponggametest.cpp(28): error C2065: 'pong' : undeclared identifier
1>c:\users\hitechredneck\desktop\fall 2014\object-oriented programming\game\ponggame\ponggame\ponggametest.cpp(28): error C2059: syntax error : '.'
1>c:\users\hitechredneck\desktop\fall 2014\object-oriented programming\game\ponggame\ponggame\ponggametest.cpp(29): error C2228: left of '.glColor3f' must have class/struct/union
1>          type is ''unknown-type''

我很清楚没有人会阅读所有这些消息(我知道我肯定不会),但您能否浏览一下它们,看看是否能找出我做错了什么?就像我说的,我几乎 100% 肯定其中很多问题与我没有正确创建对象和正确使用它的成员有关。

最佳答案

1.

Pong Pong;

是非法的:您不能将类型名称声明为标识符的对象。这可能是 Pong pong

2.

Pong::enable2D()

是非法的:enable2D 不是静态函数,而是成员(每个实例)函数,因此您必须通过对象调用它,例如 pong.enable2D( );

3.

extern Pong pong;  // in the header
Pong pong;         // and both of these in the source
Pong Pong;

这绝对是错误的。你想做什么?无论是否为全局,您都不能声明类型为 Pong 的名为 Pong 的对象,如上文 (1) 所述。此外,您可能不想要一个全局变量,而只想在 main 函数中使用一个自动变量。

错误

error C2143: syntax error : missing ';' before '.'

如果编译器没有标识符的声明,它将无法接受 token 并且会期待一个 ;

error C2248: 'Pong::height' : cannot access private member declared in class 'Pong'

这就像 C++ 编译器错误一样清晰。

请阅读a good C++ beginner book并解决此类后勤问题。互联网不是一个好老师,它是一个很好的引用资料库,但它大多不能代替一本好书。

关于c++ - 在 C++ 中为游戏创建对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27035522/

相关文章:

c++ - 我听说 Python 有自动化 "garbage collection",但 C++ 没有。那是什么意思?

c++ - SDL:初始化 TTF 问题。可能是自由打字?

c++ - Templates::Stroustrup 的示例未编译

javascript - 是否可以从 JavaScript 中的对象数组中获取特定属性?

java - 选定的变量在乒乓球游戏中不会改变 - Swing

java - 墙壁上的 Pong 反射

Javascript/HTML5 乒乓球游戏仅适用于 Chrome

c++ - #导入 : Cannot open file "soap12.h" for reading gSoap in VS 2010

javascript - API 对象变成字符串 - JSON 中出现意外标记 o

java - 子类需要设置父类的唯一对象字段?