こんにちは、現在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言語で作ったゲームからC++への改良でわからないこと
- softya(ソフト屋)
- 副管理人
- 記事: 11677
- 登録日時: 15年前
- 住所: 東海地方
- 連絡を取る:
Re: C言語で作ったゲームからC++への改良でわからないこと
codeタグをご利用ください。コードが見づらいですしインデントが反映されません。
http://dixq.net/board/board.html
これからコードをみて見ますが、何がどう上手く行かないか説明してもらえますか?
「うまくいかなくて」は何も説明していないのと同じぐらい曖昧な説明なので。
http://dixq.net/board/board.html
これからコードをみて見ますが、何がどう上手く行かないか説明してもらえますか?
「うまくいかなくて」は何も説明していないのと同じぐらい曖昧な説明なので。
YYYYY7113 さんが書きました:こんにちは、現在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[i]=new Shot(i); } while( 1 ) { ClearDrawScreen() ; //以下プレイヤーの動き p->PlayerMove(); p->PlayerDraw(); //以下ショットの関数を使っています for(int i=0;i<ShotNum;i++) { s[i]->PlayerShot(p->GetData()); s[i]->ShotMove(); s[i]->ShotDraw(); } //------------------------ ScreenFlip(); if( ProcessMessage() < 0 ) break ; if( CheckHitKey( KEY_INPUT_ESCAPE ) ) break ; } delete p; for(int i=0;i<ShotNum;i++) { delete s[i]; } 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
by softya(ソフト屋) 方針:私は仕組み・考え方を理解して欲しいので直接的なコードを回答することはまれですので、すぐコードがほしい方はその旨をご明記下さい。私以外の方と交代したいと思います(代わりの方がいる保証は出来かねます)。
- softya(ソフト屋)
- 副管理人
- 記事: 11677
- 登録日時: 15年前
- 住所: 東海地方
- 連絡を取る:
Re: C言語で作ったゲームからC++への改良でわからないこと
とりあえず、意味が無い逆に危険な継承とか分かりづらい名前だけ直してみました。
コメントがないコードは変数名に気をつけないと解読が困難ですので、ご注意ください。
たぶん、弾が連続発射されるのが問題かなと予想されます。
[補足]画像が無いため動作は未確認です。
一箇所直し忘れていたので修正。
コメントがないコードは変数名に気をつけないと解読が困難ですので、ご注意ください。
たぶん、弾が連続発射されるのが問題かなと予想されます。
[補足]画像が無いため動作は未確認です。
一箇所直し忘れていたので修正。
//[Player.h]
#pragma once
#ifndef H_PLAYER
#define H_PLAYER
#include "DxLib.h"
#define ShotNum 30
typedef struct tag_ObjRect {
int x, y, h, w;
//draw
//Set(クラスの変数を書き換える)
//Get(変数を手に入れる)
} ObjRect;
class Player {
private:
int HP;
int graph;
bool liveflag;
// bool Bfrag;
ObjRect rect;
public:
Player( int x, int y );
~Player();
void PlayerMove();
void PlayerDraw();
ObjRect GetRect();
};
class Shot {
private:
int num;
int pastY;
bool GoFlag;
int graph;
bool liveflag;
ObjRect rect;
public:
Shot( int num );
~Shot();
void PlayerShot( ObjRect rect );
void ShotMove();
void ShotDraw();
bool GetFrag();
bool GetGoFlag();
};
#endif
//[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 *player = new Player( 300, 320 );
Shot *shot[ShotNum];
for( int i = 0; i < ShotNum; i++ ) {
shot[i] = new Shot( i );
}
while( 1 ) {
ClearDrawScreen() ;
//以下プレイヤーの動き
player->PlayerMove();
player->PlayerDraw();
//以下ショットの関数を使っています
for( int i = 0; i < ShotNum; i++ ) {
shot[i]->PlayerShot( player->GetRect() );
shot[i]->ShotMove();
shot[i]->ShotDraw();
}
//------------------------
ScreenFlip();
if( ProcessMessage() < 0 ) break ;
if( CheckHitKey( KEY_INPUT_ESCAPE ) ) break ;
}
delete player;
for( int i = 0; i < ShotNum; i++ ) {
delete shot[i];
}
WaitKey();
DxLib_End() ; // DXライブラリ使用の終了処理
return 0 ; // ソフトの終了
}
//[Player.cpp]
//#include"Player.h"
Player::Player( int x, int y ) {
this->rect.x = x;
this->rect.y = y;
this->liveflag = true;
this->graph = LoadGraph( "img/Bziki.png" );
GetGraphSize( this->graph, &this->rect.w, &this->rect.h );
}
Player::~Player()
{}
//Shotクラスの初期化
Shot::Shot( int num ) {
this->num = num;
this->liveflag = false;
this->rect.x = 0;
this->rect.y = 0;
this->pastY = 0;
this->GoFlag = false;
this->graph = LoadGraph( "img/aka.png" );
GetGraphSize( this->graph, &this->rect.w, &this->rect.h );
}
Shot::~Shot()
{}
//自機の動き
void Player::PlayerMove() {
if( CheckHitKey( KEY_INPUT_LEFT ) ) { //左を押したら
this->rect.x -= 4;
if( this->rect.x <= 1 ) {
this->rect.x = 1;
}
}
if( CheckHitKey( KEY_INPUT_RIGHT ) ) { //右を押したら
this->rect.x += 4;
if( this->rect.x > 620 ) {
this->rect.x = 620;
}
}
if( CheckHitKey( KEY_INPUT_UP ) ) { //上を押したら
this->rect.y -= 4;
if( this->rect.y <= 1 ) {
this->rect.y = 1;
}
}
if( CheckHitKey( KEY_INPUT_DOWN ) ) { //下を押したら
this->rect.y += 4;
if( this->rect.y > 450 ) {
this->rect.y = 450;
}
}
}
//ショットの処理をする関数
void Shot::PlayerShot( ObjRect rect ) {
if( CheckHitKey( KEY_INPUT_1 ) ) {
if( this->liveflag == false ) {
this->rect.x = ( rect.x + rect.w ) - 24;
this->rect.y = ( rect.y + rect.h ) - 30;
this->pastY = this->rect.y;
this->liveflag = true;
}
}
}
bool Shot::GetFrag() {
return this->liveflag;
}
bool Shot::GetGoFlag() {
return this->GoFlag;
}
//弾の動きの関数
void Shot::ShotMove() {
if( this->liveflag == true ) {
this->rect.y -= 16;
if( this->rect.y < -80 ) {
this->liveflag = false;
}
}
}
void Player::PlayerDraw() {
if( this->liveflag == true ) {
DrawGraph( this->rect.x, this->rect.y, this->graph, true );
}
DrawFormatString( 2, 20, ( 0, 0, 200 ), "P%d", this->rect.y );
}
void Shot::ShotDraw() {
for( int i = 0; i < ShotNum; i++ ) {
if( Shot::liveflag == true ) {
DrawGraph( this->rect.x, this->rect.y, this->graph, true );
}
}
/*
if(Shot::frag2==true)
{
DrawGraph(this->rect.x2,this->rect.y2,this->graph,true);
}
*/
DrawFormatString( 2, 3, ( 0, 0, 200 ), "S%d", this->liveflag );
DrawFormatString( 2, 40, ( 0, 0, 200 ), "S%d", this->rect.y );
}
ObjRect Player::GetRect() {
return this->rect;
}
by softya(ソフト屋) 方針:私は仕組み・考え方を理解して欲しいので直接的なコードを回答することはまれですので、すぐコードがほしい方はその旨をご明記下さい。私以外の方と交代したいと思います(代わりの方がいる保証は出来かねます)。