我是 C++ 的新手,但我学了一点 Java,并且我/我第一次同时使用 SDL,但我的问题是我正在尝试创建一个对象(桨)p1 和 p2。
这
#ifndef PONG_H_
#define PONG_H_
#undef main
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_ttf.h>
class paddle{
private:
int y;
int yv;
int x;
public:
paddle(){
y = 0;
yv = 0;
x = 0;
}
paddle(int ex, int why, int vel){
y = why;
yv = vel;
x = ex;
}
void setY(int ycoord) {y = ycoord;}
void setV(int velocity){ yv = velocity;}
void setX(int xcoord) {x = xcoord;}
int getX() {return x;}
int getY() {return y;}
int update(){y += yv; return y;}
};
class Pong { //pong class where everythin gets its name and a purpose.
private:
SDL_Surface * Paddle;
SDL_Surface * Ball; //SDL_Surface pointer for Ball
SDL_Surface * screen; //SDL_Surface pointer to backbuffer
public:
int Running;
Pong(); //eh..
int OnExecute(); //another function returning a value.
int w; //width of screen
int h; //height of screen
bool OnInit();
void OnEvent(SDL_Event* Event);
void OnLoop();
void OnRender();
void OnCleanup();
void redraw( int x, int y, SDL_Surface* source, SDL_Surface* destination ) { //Make a temporary rectangle to hold the offsets
SDL_Rect offset; //Give the offsets to the rectangle
offset.x = x; offset.y = y;
SDL_BlitSurface( source, NULL, destination, &offset );//Blit the surface
offset.x =0; offset.y=0; //resets the offsets sicne they apaprently dont reset per use.
};
};
#endif /* PONG_H_ */
是我的头文件,它也包含了 pong 类的主要功能,但我省略了这些以减少复杂性。让我知道这些是否需要一个好的答案。
我在这里声明对象...
#include <iostream>
#include <SDL/SDL.h>
#include "Pong.h"
paddle p1;
paddle p2;
Pong::Pong(){
h = 768;
w = 1024;
screen = NULL;
Paddle = NULL;
Ball = NULL;
Running = true;
atexit(SDL_Quit);
}
int Pong::OnExecute() {
if(OnInit() == false)
return-1;
SDL_Event Event;
while(Running) {
while(SDL_PollEvent(&Event)) {
OnEvent(&Event);
}
OnLoop();
OnRender();
}
OnCleanup();
return 0;
}
int main( int argc, char* args[]) {
Pong theApp;
return theApp.OnExecute();
}
但是我在……这里使用它是什么让我很困难:
#include "Pong.h"
void Pong::OnEvent(SDL_Event* Event) {
if(Event->type == SDL_QUIT)
Running = false;
switch(Event->type)
{
case SDL_KEYDOWN: //look for key holds
switch(Event->key.keysym.sym) //check key values and changes coords respectiely
{
default:
break;
case SDLK_UP:
p2.setV(-2);
break;
case SDLK_DOWN:
p2.setV(2);
break;
case SDLK_w:
p1.setV(-2);
break;
case SDLK_s:
p1.setV(2);
break;
case SDLK_ESCAPE:
Running = false;
break;
}
break;
case SDL_KEYUP:
switch(Event->key.keysym.sym)
{
default:
break;
case SDLK_UP:
p2.setV(0);
break;
case SDLK_DOWN:
p2.setV(0);
break;
case SDLK_w:
p1.setV(0);
break;
case SDLK_s:
p1.setV(0);
break;
}
break;
break;//break of Key holding event check
}
}
我很难问这个问题,因为我不知道从哪里开始。我尝试了许多不同的奇怪的东西,我现在迷路了。如果您需要更多信息,我很乐意提供。
void Pong::OnLoop(){
if(p1.getY()<=0){
p1.setV(0);
p1.setY(0);
}
if(p2.getY()<=0){
p2.setV(0);
p2.setY(0);
}
if(p1.getY()>=h - Paddle->h){
p1.setV(0);
p1.setY(h - Paddle->h);
}
if(p2.getY()>=h - Paddle->h){
p2.setV(0);
p2.setY(h - Paddle->h);
}
}
..\src\Pong_OnLoop.cpp:11:5: 错误: 'p1' 未在此范围内声明
..\src\Pong_OnLoop.cpp:15:5: 错误: 'p2' 未在此范围内声明
..\src\Pong_OnLoop.cpp:19:5: 错误: 'p1' 未在此范围内声明
..\src\Pong_OnLoop.cpp:23:5: 错误: 'p2' 未在此范围内声明