C言語で作ったゲームからC++への改良でわからないこと
Posted: 2014年2月04日(火) 13:19
こんにちは、現在C言語からC++へ改良する際で訳が分からなくなっているものです。
自機のショットなのですが、連射をさせるときがうまくいかなくて困っています。C言語ではDXライブラリ置き場というサイトを参考にしながらやったのですんなり行けたのですが、C++ではそうもいかないみたいです。これくらい自分で考えろという人もいるかもしれませんが、かなり困っています。ご回答お待ちしております。
[main文]
#include"Player.h"
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow )
{
SetGraphMode(640,480,16);
ChangeWindowMode(true);
if( DxLib_Init() == -1 )
{
return -1 ;
}
SetTransColor(0,255,255);
SetDrawScreen( DX_SCREEN_BACK ) ;
Player *p=new Player(300,320);
Shot *s[ShotNum];
for(int i=0;i<ShotNum;i++)
{
s=new Shot(i);
}
while( 1 )
{
ClearDrawScreen() ;
//以下プレイヤーの動き
p->PlayerMove();
p->PlayerDraw();
//以下ショットの関数を使っています
for(int i=0;i<ShotNum;i++)
{
s->PlayerShot(p->GetData());
s->ShotMove();
s->ShotDraw();
}
//------------------------
ScreenFlip();
if( ProcessMessage() < 0 ) break ;
if( CheckHitKey( KEY_INPUT_ESCAPE ) ) break ;
}
delete p;
for(int i=0;i<ShotNum;i++)
{
delete s;
}
WaitKey();
DxLib_End() ; // DXライブラリ使用の終了処理
return 0 ; // ソフトの終了
}
[Player.cpp]
#include"Player.h"
Player::Player(int x,int y)
{
this->date.x=x;
this->date.y=y;
this->frag=true;
this->date.Graph=LoadGraph("img/Bziki.png");
GetGraphSize(this->date.Graph,&this->date.w,&this->date.h);
}
Player::~Player()
{}
//Shotクラスの初期化
Shot::Shot(int num):Player(this->date.x,this->date.y)
{
this->num=num;
this->frag=false;
this->date.x=0;
this->date.y=0;
this->pastY = 0;
this->GoFlag=false;
this->date.Graph=LoadGraph("img/aka.png");
GetGraphSize(this->date.Graph,&this->date.w,&this->date.h);
}
Shot::~Shot()
{}
//自機の動き
void Player::PlayerMove()
{
if(CheckHitKey(KEY_INPUT_LEFT))//左を押したら
{
this->date.x-=4;
if(this->date.x<=1)
{
this->date.x=1;
}
}
if(CheckHitKey(KEY_INPUT_RIGHT))//右を押したら
{
this->date.x+=4;
if(this->date.x>620)
{
this->date.x=620;
}
}
if(CheckHitKey(KEY_INPUT_UP))//上を押したら
{
this->date.y-=4;
if(this->date.y<=1)
{
this->date.y=1;
}
}
if(CheckHitKey(KEY_INPUT_DOWN))//下を押したら
{
this->date.y+=4;
if(this->date.y>450)
{
this->date.y=450;
}
}
}
//ショットの処理をする関数
void Shot::PlayerShot(image date)
{
if(CheckHitKey(KEY_INPUT_1))
{
if(this->frag==false)
{
this->date.x=(date.x+date.w)-24;
this->date.y=(date.y+date.h)-30;
this->pastY=this->date.y;
this->frag=true;
}
}
}
bool Shot::GetFrag()
{
return this->frag;
}
bool Shot::GetGoFlag()
{
return this->GoFlag;
}
//弾の動きの関数
void Shot::ShotMove()
{
if(this->frag==true)
{
this->date.y-=16;
if(this->date.y <-80)
{
this->frag=false;
}
}
}
void Player::PlayerDraw()
{
if(this->frag==true)
{
DrawGraph(this->date.x,this->date.y,this->date.Graph,true);
}
DrawFormatString(2,20,(0,0,200),"P%d",this->date.y);
}
void Shot::ShotDraw()
{
for(int i=0;i<ShotNum;i++)
{
if(Shot::frag==true)
{
DrawGraph(this->date.x,this->date.y,this->date.Graph,true);
}
}
/*
if(Shot::frag2==true)
{
DrawGraph(this->date.x2,this->date.y2,this->date.Graph,true);
}
*/
DrawFormatString(2,3,(0,0,200),"S%d",this->frag);
DrawFormatString(2,40,(0,0,200),"S%d",this->date.y);
}
image Player::GetData()
{
return this->date;
}
[Player.h]
#pragma once
#ifndef H_PLAYER
#define H_PLAYER
#include "DxLib.h"
#define ShotNum 30
typedef struct tag_image
{
int x,y,h,w,Graph;
//draw
//Set(クラスの変数を書き換える)
//Get(変数を手に入れる)
}image;
class Player
{
private:
int HP;
int num;
protected:
bool frag;
bool Bfrag;
image date;
public:
Player(int x,int y);
~Player();
void PlayerMove();
void PlayerDraw();
image GetData();
};
class Shot:public Player
{
private:
int num;
int pastY;
bool GoFlag;
public:
Shot(int num);
~Shot();
void PlayerShot(image data);
void ShotMove();
void ShotDraw();
bool GetFrag();
bool GetGoFlag();
};
#endif
自機のショットなのですが、連射をさせるときがうまくいかなくて困っています。C言語ではDXライブラリ置き場というサイトを参考にしながらやったのですんなり行けたのですが、C++ではそうもいかないみたいです。これくらい自分で考えろという人もいるかもしれませんが、かなり困っています。ご回答お待ちしております。
[main文]
#include"Player.h"
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow )
{
SetGraphMode(640,480,16);
ChangeWindowMode(true);
if( DxLib_Init() == -1 )
{
return -1 ;
}
SetTransColor(0,255,255);
SetDrawScreen( DX_SCREEN_BACK ) ;
Player *p=new Player(300,320);
Shot *s[ShotNum];
for(int i=0;i<ShotNum;i++)
{
s=new Shot(i);
}
while( 1 )
{
ClearDrawScreen() ;
//以下プレイヤーの動き
p->PlayerMove();
p->PlayerDraw();
//以下ショットの関数を使っています
for(int i=0;i<ShotNum;i++)
{
s->PlayerShot(p->GetData());
s->ShotMove();
s->ShotDraw();
}
//------------------------
ScreenFlip();
if( ProcessMessage() < 0 ) break ;
if( CheckHitKey( KEY_INPUT_ESCAPE ) ) break ;
}
delete p;
for(int i=0;i<ShotNum;i++)
{
delete s;
}
WaitKey();
DxLib_End() ; // DXライブラリ使用の終了処理
return 0 ; // ソフトの終了
}
[Player.cpp]
#include"Player.h"
Player::Player(int x,int y)
{
this->date.x=x;
this->date.y=y;
this->frag=true;
this->date.Graph=LoadGraph("img/Bziki.png");
GetGraphSize(this->date.Graph,&this->date.w,&this->date.h);
}
Player::~Player()
{}
//Shotクラスの初期化
Shot::Shot(int num):Player(this->date.x,this->date.y)
{
this->num=num;
this->frag=false;
this->date.x=0;
this->date.y=0;
this->pastY = 0;
this->GoFlag=false;
this->date.Graph=LoadGraph("img/aka.png");
GetGraphSize(this->date.Graph,&this->date.w,&this->date.h);
}
Shot::~Shot()
{}
//自機の動き
void Player::PlayerMove()
{
if(CheckHitKey(KEY_INPUT_LEFT))//左を押したら
{
this->date.x-=4;
if(this->date.x<=1)
{
this->date.x=1;
}
}
if(CheckHitKey(KEY_INPUT_RIGHT))//右を押したら
{
this->date.x+=4;
if(this->date.x>620)
{
this->date.x=620;
}
}
if(CheckHitKey(KEY_INPUT_UP))//上を押したら
{
this->date.y-=4;
if(this->date.y<=1)
{
this->date.y=1;
}
}
if(CheckHitKey(KEY_INPUT_DOWN))//下を押したら
{
this->date.y+=4;
if(this->date.y>450)
{
this->date.y=450;
}
}
}
//ショットの処理をする関数
void Shot::PlayerShot(image date)
{
if(CheckHitKey(KEY_INPUT_1))
{
if(this->frag==false)
{
this->date.x=(date.x+date.w)-24;
this->date.y=(date.y+date.h)-30;
this->pastY=this->date.y;
this->frag=true;
}
}
}
bool Shot::GetFrag()
{
return this->frag;
}
bool Shot::GetGoFlag()
{
return this->GoFlag;
}
//弾の動きの関数
void Shot::ShotMove()
{
if(this->frag==true)
{
this->date.y-=16;
if(this->date.y <-80)
{
this->frag=false;
}
}
}
void Player::PlayerDraw()
{
if(this->frag==true)
{
DrawGraph(this->date.x,this->date.y,this->date.Graph,true);
}
DrawFormatString(2,20,(0,0,200),"P%d",this->date.y);
}
void Shot::ShotDraw()
{
for(int i=0;i<ShotNum;i++)
{
if(Shot::frag==true)
{
DrawGraph(this->date.x,this->date.y,this->date.Graph,true);
}
}
/*
if(Shot::frag2==true)
{
DrawGraph(this->date.x2,this->date.y2,this->date.Graph,true);
}
*/
DrawFormatString(2,3,(0,0,200),"S%d",this->frag);
DrawFormatString(2,40,(0,0,200),"S%d",this->date.y);
}
image Player::GetData()
{
return this->date;
}
[Player.h]
#pragma once
#ifndef H_PLAYER
#define H_PLAYER
#include "DxLib.h"
#define ShotNum 30
typedef struct tag_image
{
int x,y,h,w,Graph;
//draw
//Set(クラスの変数を書き換える)
//Get(変数を手に入れる)
}image;
class Player
{
private:
int HP;
int num;
protected:
bool frag;
bool Bfrag;
image date;
public:
Player(int x,int y);
~Player();
void PlayerMove();
void PlayerDraw();
image GetData();
};
class Shot:public Player
{
private:
int num;
int pastY;
bool GoFlag;
public:
Shot(int num);
~Shot();
void PlayerShot(image data);
void ShotMove();
void ShotDraw();
bool GetFrag();
bool GetGoFlag();
};
#endif