2009.06.04 Thursday | 12:10

  3年前に作った2Dゲームサンプルコードの再公開 ふるいむかし

Easygame

ブラウザでダウンロード

最新のDXライブラリを同梱して、ライセンス表示を付加して再公開です。

Visual Studio 2005 で最終テストしていますが、
元々 VC6 で作成したものをそのまま変更しただけで動いています。

※ 得点システム等は無く、ゲームとしては弾を撃って敵に当てるだけです

とりあえず以下は、クラス以外のメインのソースコードです
001.// ********************************************************
002.// EasyGame Ver 061105
003.// ********************************************************
004. 
005.#include "DxLib.h"
006.#include "Game.h"
007. 
008.// ********************************************************
009.// ゲーム環境クラス
010.// ********************************************************
011.Game env;
012. 
013.// ********************************************************
014.// プレイヤークラス
015.// ********************************************************
016.Play me;
017. 
018.// ********************************************************
019.// ショットクラス
020.// ********************************************************
021.Shot shot_s;
022.Shot shot_a;
023. 
024.// ********************************************************
025.// アニメーションクラス
026.// ********************************************************
027.Animation ani1[4];
028.Animation ani2[4];
029. 
030.// ********************************************************
031.// ショットクラス( 敵 )
032.// ********************************************************
033.Shot bad;
034. 
035.// ********************************************************
036.// 背景用の星描画クラス
037.// ********************************************************
038.Star ginga1;
039.Star ginga2;
040. 
041.int back,parts1;
042.int px = 0;
043.int py = 0;
044.int gw,gh;
045. 
046.// ********************************************************
047.// Callback 関数宣言
048.// ********************************************************
049.int GameLoop( Game & );
050.int PlayCheck( Game &, Play & );
051.int ShotLoop1( int curIndex, Game &, Play &, Shot & );
052.int ShotLoop2( int curIndex, Game &, Play &, Shot & );
053.int BadLoop( int curIndex, Game &, Play &, Shot & );
054. 
055.// ********************************************************
056.// ユーザー関数宣言
057.// ********************************************************
058.void NumberFormat( char *Target );
059.void ShotedScroll( );
060. 
061.// ********************************************************
062.// 開始処理
063.// ********************************************************
064.int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
065.                        LPSTR lpCmdLine, int nCmdShow )
066.{
067.    // ********************************************************
068.    // タイトル設定
069.    // ********************************************************
070.    env.SetTitle( "ゲームプログラミング" );
071. 
072.    // ********************************************************
073.    // ウインドウモード設定( SHIFT を押してたらフルスクリーン )
074.    // ( API を利用したキーの処理 )
075.    // ********************************************************
076.    WORD nRet;
077.    nRet = (WORD)::GetKeyState( VK_SHIFT );
078.    if( nRet & 0x1000 ) {
079.    }
080.    else {
081.        env.WindowMode();
082.    }
083.    ///////////////////////////////////////////////////////////
084. 
085.    // ********************************************************
086.    // DirectX 初期化
087.    // ********************************************************
088.    if ( !env.Init( ) ) {
089.        return -1 ;
090.    }
091. 
092.    // ********************************************************
093.    // ディスプレイの色を 16 にして下さい
094.    // ********************************************************
095.    SetGraphMode( 800 , 600 , 16 ) ;
096. 
097.    // ********************************************************
098.    // プレイヤー画像のロード
099.    // ********************************************************
100.    me.Image( "play" );
101. 
102.    // ********************************************************
103.    // プレイヤー初期位置
104.    // ********************************************************
105.    me.Position( 390, 520 );
106. 
107.    // ********************************************************
108.    // ショット初期化(画面内最大4つ発射)
109.    // ********************************************************
110.    shot_s.CreateShot( 4, PAD_INPUT_M, "saveshot.dat" );    // スペースキー
111.    shot_a.CreateShot( 4, PAD_INPUT_X );    // A キー
112.    bad.CreateShot( 4, -1, "badshot.dat" ); // 敵
113. 
114.    // ********************************************************
115.    // ショット画像のロード
116.    // ********************************************************
117.    shot_s.Image( "shot1" );
118.    shot_a.Image( "shot2" );
119.    bad.Image( "shot2" );
120. 
121.    // ********************************************************
122.    // 爆発アニメーションのロード (対応ショットと数を合わせる)
123.    // ********************************************************
124.    ani1[0].Image( "ani1", 4 );
125.    ani1[1] = ani1[0];
126.    ani1[2] = ani1[0];
127.    ani1[3] = ani1[0];
128.    ani2[0].Image( "ani1", 4 );
129.    ani2[1] = ani2[0];
130.    ani2[2] = ani2[0];
131.    ani2[3] = ani2[0];
132. 
133.    // ********************************************************
134.    // 銀河エフェクト
135.    // ********************************************************
136.    env.GetState();
137.    ginga1.CreateStarMemory( 250 );
138.    // 3つ目は、3D 効果を出すかどうか
139.    ginga1.CreateSeeds( 2, env.ScreenX, env.ScreenY, true );
140.    // 回転させるかどうか
141.    ginga1.OptionSpin = true;
142.    ginga1.inc = 0.01;
143.//  回転の中心
144.//  ginga1.SpinX = 100;
145.//  ginga1.SpinY = 200;
146.//  3D効果中心
147.//  ginga1.BaseX = 600;
148.//  ginga1.BaseY = 200;
149.//  オプションの結果を次の処理に加算する
150.//  ginga1.NoRestore = true;
151. 
152.    ginga2.CreateStarMemory( 250 );
153.    ginga2.CreateSeeds( 1, env.ScreenX, env.ScreenY, true );
154.    ginga2.OptionSpin = true;
155.    ginga2.inc = 0.01;
156.    ginga2.rad = ginga2.endRad / 2;
157.    ginga2.endRad += ginga2.rad;
158. 
159.    back = LoadGraph( "field.bmp" );
160.    parts1 = LoadGraph( "parts1.bmp" );
161.    GetGraphSize( back, &gw, &gh );
162.    py = gh;
163. 
164.    // ********************************************************
165.    // 処理開始
166.    // ********************************************************
167.    int ret;
168.    ret = env.Start( GameLoop );
169. 
170.    return 0;
171.}
172. 
173.// ********************************************************
174.// 【ゲーム用ループ】
175.// 終わる時は return false;
176.// ESC で強制終了
177.// ********************************************************
178.int GameLoop( Game &g )
179.{
180.    // ********************************************************
181.    // 背景描画 ( 最初に描画 )
182.    // ********************************************************
183.    // 左上
184.    DrawRectGraph( 0, 0, gw-px, py, px, gh-py, back, false, false ) ;
185.    // 右上
186.    DrawRectGraph( px, 0, 0, py, gw, gh-py, back, false, false ) ;
187.    // 左下
188.    DrawRectGraph( 0, gh-py, gw-px, 0, px, gh, back, false, false ) ;
189.    // 右下
190.    DrawRectGraph( px, gh-py, 0, 0, gw, gh, back, false, false ) ;
191. 
192.    // プレイヤーの処理
193.    me.Check( PlayCheck, g );
194. 
195.    // ********************************************************
196.    // 銀河エフェクト ( 最後に描画 )
197.    // ********************************************************
198.    ginga1.Draw();
199.    ginga2.Draw();
200. 
201.    return true;
202.}
203. 
204.// ********************************************************
205.// 【プレイヤー処理関数】
206.// ********************************************************
207.int PlayCheck( Game &g, Play &p )
208.{
209. 
210.    double toRad;
211. 
212.    // 左上
213. 
214.    if ( px > 800 ) {
215.        toRad = atan2( (double)(p.y+32 - (gh - py - 200)), (double)(p.x+32 - (px - gw + 40)) );
216.        toRad -= 3.14/2;
217.        DrawRotaGraph( px - gw + 40, gh - py - 200,
218.                1, toRad,
219.                parts1, true , false ) ;
220.    }
221.    else {
222.        toRad = atan2( (double)(p.y+32 - (gh - py - 200)), (double)(p.x+32 - (px + 40)) );
223.        toRad -= 3.14/2;
224.        DrawRotaGraph( px + 40, gh - py - 200,
225.                1, toRad,
226.                parts1, true , false ) ;
227.    }
228. 
229. 
230.    py--;
231.    if ( py < 0 ) {
232.        py = gh;
233.    }
234. 
235.    // 右へ移動
236.    if ( p.right ) {
237.        p.Move( PAD_INPUT_RIGHT, 3 );
238.        px -= 3;
239.        if ( px < 0 ) {
240.            px = gw;
241.        }
242.    }
243.    // 左へ移動
244.    if ( p.left ) {
245.        p.Move( PAD_INPUT_LEFT, 3 );
246.        px += 3;
247.        if ( px > gw ) {
248.            px = 0;
249.        }
250.    }
251. 
252.    // 自分自身を描画
253.    p.Draw();
254. 
255.    // 味方のショット処理
256.    shot_s.Action( ShotLoop1, g, p );   // ---> Call Back へジャンプ
257.    shot_a.Action( ShotLoop2, g, p );   // ---> Call Back へジャンプ
258. 
259.    // 敵のショット処理
260.    bad.Action( BadLoop, g, p );        // ---> Call Back へジャンプ
261. 
262.    // 味方の弾と、敵の弾の当り判定
263.    shot_s.HitCheck( bad, ani1 );
264.    shot_a.HitCheck( bad, ani2 );
265. 
266.    int i;
267. 
268.    // 爆発アニメーション
269.    for( i = 0; i < ani1[0].nArrayCount; i++ ) {
270.        ani1[i].Do();
271.    }
272.    for( i = 0; i < ani2[0].nArrayCount; i++ ) {
273.        ani2[i].Do();
274.    }
275. 
276.    return true;
277.}
278. 
279.// ********************************************************
280.// ショット処理関数(有効インデックスしか Call されない)
281.// 味方の弾( SPACE キー )
282.// ********************************************************
283.int ShotLoop1( int i, Game &g, Play &p, Shot &s )
284.{
285. 
286.    // ショットの移動パターンをデータで決定
287.    s.SaveShot( i );
288. 
289.    s.Draw( i );
290. 
291.    return true;
292.}
293. 
294.// ********************************************************
295.// ショット処理関数(有効インデックスしか Call されない)
296.// 味方の弾( A キー )
297.// ********************************************************
298.int ShotLoop2( int i, Game &g, Play &p, Shot &s )
299.{
300. 
301.    // ショットの移動パターンをデータによらず、手動設定
302.    s.ShotY[ i ] -= 6 ;
303.    s.ShotX[ i ] -= 4 ;
304. 
305.    s.Draw( i );
306. 
307.    return true;
308.}
309. 
310.// ********************************************************
311.// ショット処理関数(有効インデックスしか Call されない)
312.// 敵弾の描画
313.// ********************************************************
314.int BadLoop( int i, Game &g, Play &p, Shot &s )
315.{
316. 
317.    // ショットの移動パターンをデータで決定
318.    s.SaveShot( i );
319. 
320.    s.Draw( i );
321. 
322.    return true;
323.}