2015-07-10 46 views
-2

因此,我開始SDL2並通過一些教程並決定嘗試製作一個簡單的遊戲,而Pong的克隆似乎是最簡單的。但我似乎無法使右側槳在車窗上畫畫。目前我正在使用SDL_FillRenderRect,每個槳都有一個,但似乎只是讓左側出現,而不是右側。這裏是我當前的代碼:在Pong克隆中製作兩個槳的問題SDL2

#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(); 
} 

可能有人請點我在正確的方向上繪製右槳?如果在我的代碼中存在其他錯誤或者使其更加有效,以及如果可以指出這些錯誤,我也會很感激。提前致謝。

回答

2

仔細閱讀本:

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

左右的混合起來,由於複製粘貼編程。我並不是說這很糟糕,我也是這樣做的,以減輕我手上的傷害。你應該檢查這樣的事情兩次。

另外,就你而言,你應該開始使用OOP概念。不要在非成員函數中執行所有操作,而是使用實例執行的成員函數。我不知道getY,getPaddleWidthgetPaddleHeight的值是從哪裏來的,但我很清楚,除了SDL_Rect之外,不應該有任何東西持有它們。

我甚至認爲SDL是浪費時間,我不記得C++是什麼。這是純粹的C,你不會學習任何有用的東西。 SFML是更好的選擇。

+0

非常感謝!我可能從來沒有見過。我愚蠢,但我應該再次檢查它。再次感謝! :d – CharlieFan39