在Pong克隆SDL2中制作两个拨片的问题

时间:2015-07-10 06:47:31

标签: c++ sdl-2 pong

所以我开始学习SDL2并经历了一些教程,并决定尝试制作一个简单的游戏,而Pong的克隆似乎是最简单的。但我似乎无法在窗户上画右手边的画笔。目前我正在使用SDL_FillRenderRect,每个球拍一个,但似乎只是左手边出现而不是右手边。这是我目前的代码:

 
#include <iostream>

#include "Pong.h"
#include "Paddle.h"

//Screen size
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;

bool quit = false;//Has user quit?

Paddle paddleLeft;
Paddle paddleRight;

int Pong::setup(){

    //Initilize SDL
    SDL_Init(SDL_INIT_EVERYTHING);

    window = SDL_CreateWindow("Pong",
                              SDL_WINDOWPOS_UNDEFINED,
                              SDL_WINDOWPOS_UNDEFINED,
                              SCREEN_WIDTH,
                              SCREEN_HEIGHT,
                              SDL_WINDOW_SHOWN);

    if (window == nullptr){
        std::cout<<"SDL_CreateWindow error: "<<SDL_GetError()<<std::endl;
        SDL_Quit();
        return -1;
    }

    SDL_ShowCursor(0);

    renderer = SDL_CreateRenderer(window,
                                  -1,
                                  SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);

    if (renderer == nullptr){
        std::cout<<"SDL_CreateRenderer error: "<<SDL_GetError()<<std::endl;
        SDL_Quit();
        return -1;
    }

    int wH, wW;

    //Setting the paddle to the centre of the left hand side of the screen.
    SDL_GetWindowSize(window, &wW, &wH);
    paddleLeft.setY((wH / 2) - (paddleLeft.getPaddleHeight() / 2));
    paddleRight.setY((wH / 2) - (paddleLeft.getPaddleHeight() / 2));

    //Setting the x, y, height and width of the left paddle
    leftR.x = 20;
    leftR.y = paddleLeft.getY();
    leftR.h = paddleLeft.getPaddleWidth();
    leftR.w = paddleLeft.getPaddleHeight();

    //Setting the x, y, height and width of the right paddle
    rightR.x = 620;
    rightR.y = paddleLeft.getY();
    rightR.h = paddleLeft.getPaddleWidth();
    rightR.w = paddleLeft.getPaddleHeight();

    return 1;//If setup fails then return -1 otherwise return 1
}

void Pong::updateGame(){
    SDL_Event event;

    while (SDL_PollEvent(&event)){
        if (event.type == SDL_QUIT){
            quit = true;
        }
        if (event.type == SDL_KEYDOWN){
            switch (event.key.keysym.sym){
                case SDLK_w:        paddleLeft.setY(paddleLeft.getY() - paddleLeft.getSpeed()); break;
                case SDLK_s:        paddleLeft.setY(paddleLeft.getY() + paddleLeft.getSpeed()); break;
            }
        }
    }
}

void Pong::render(){
    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);

    SDL_RenderClear(renderer);

    leftR.x = paddleLeft.getX();
    leftR.y = paddleLeft.getY();

    rightR.x = paddleRight.getX();
    rightR.y = paddleRight.getY();

    SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);

    SDL_RenderFillRect(renderer, &leftR);

    SDL_RenderFillRect(renderer, &rightR);

    SDL_RenderPresent(renderer);
}

void Pong::cleanup(){
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();
}

void Pong::run(){
    while (!quit){
        updateGame();
        render();
    }
    cleanup();
}

有人可以指出我正确的方向画出正确的划桨吗?如果我的代码中有任何其他错误或使其更有效率的话,我也会感激不尽,如果还可以指出那些错误。提前谢谢。

1 个答案:

答案 0 :(得分:2)

仔细阅读:

rightR.x = 620;
rightR.y = paddleLeft.getY();
rightR.h = paddleLeft.getPaddleWidth();
rightR.w = paddleLeft.getPaddleHeight();

由于复制粘贴编程,左右混合。我不是说这很糟糕,我也是这样做,以减少对我的伤害。你应该检查两次这样的事情。

此外,在您的情况下,您应该开始使用OOP概念。不要在非成员函数中执行所有操作,而是创建在实例上执行的成员函数。我不知道getYgetPaddleWidthgetPaddleHeight的价值来自何处,但我很清楚除SDL_Rect之外不应该有任何其他内容

我甚至认为SDL是浪费时间,我不记得C ++是关于它的。这是纯粹的C,你不会学到任何有用的东西。 SFML是更好的选择。