【c++builder starter チュートリアルシリーズ】シーズン2 c++builderの部 第1回...
TRANSCRIPT
© 2016 Embarcadero Technologies, Inc.
All rights reserved. Proprietary and confidential.
第1回
‟シューティングゲームのプログラム„
シーズン2:プログラミング言語をやさしく覚えよう
C++Builderの部
2© 2016 Embarcadero Technologies, Inc. All rights reserved. Proprietary and confidential. #embtwebi_jp
Delphi / C++Builder Starter チュートリアルシリーズ
シーズン2 :2017年1月23日~ 3月27日全 9 回
時間 :毎週月曜 17 時 00分~17時 50分
Delphi 17時00分~17時20分 / C++Builder 17時30分~17時50分
ねらい :プログラミング言語をやさしく覚えよう
シーズン2
第1回 2017年1月23日 シューティングゲームのプログラム
第2回 1月30日 変数と型
第3回 2月6日 条件とループ
第4回 2月13日 Function と Procedure (関数と手続き)
第5回 2月20日 配列とレコード
第6回 2月27日 文字列とオブジェクト
第7回 3月6日 オブジェクト指向
第8回 3月13日 作ってみよう(仮)
第9回 3月27日 コミュニティと勉強会
セミナー情報 :下記のWebサイト
http://forms.embarcadero.com/starter-tutorial-webinar
3© 2016 Embarcadero Technologies, Inc. All rights reserved. Proprietary and confidential. #embtwebi_jp
第1回「シューティングゲームのプログラム」
今日のねらい• シーズン1・シューティングゲーム
• プログラムの理解を深める
• C++プログラムの基本構成の理解と読み方• シーズン2で学ぶプログラム言語の概略を俯瞰する
実施内容• C++ の基本コーディングの確認• 使用した型、文、関数の確認
4© 2016 Embarcadero Technologies, Inc. All rights reserved. Proprietary and confidential. #embtwebi_jp
無料版 C++Builder 10.1 Berlin Starter Edition 入手方法
• シリアルキーを知らせるメール内にも再ダウンロードリンク有
エンバ
Web製品
C++
Builder
Starter
バナー登録 Get
無料で使える開発環境をダウンロード
EDN*に登録済の方はEDNアカウントでダウンロード可
登録完了後、自動でインストーラーのダウンロード開始
インストール時にシリアルキーを入力
登録時のメールアドレスにシリアルキーが配信される
5© 2016 Embarcadero Technologies, Inc. All rights reserved. Proprietary and confidential. #embtwebi_jp
参考資料
シューティングゲーム / シーズン1で作成• https://github.com/mojeld/embarcadero_jp_shooting_game
6© 2016 Embarcadero Technologies, Inc. All rights reserved. Proprietary and confidential. #embtwebi_jp
シューティングゲームに入っているプログラム要素
コメント
識別子
ブロック
言語キーワード
プログラムの構造
コンパイラ指令
文
7© 2016 Embarcadero Technologies, Inc. All rights reserved. Proprietary and confidential. #embtwebi_jp
コメント
コメント
• ソースコードのうち、人間が理解することを目的に記述する情報
• プログラムの視認性、可読性を高めるために利用
• コンピュータ(コンパイラ)はコメント部分を無視するvoid __fastcall TfmShooting_main::Button_missileClick(TObject *Sender){
TRectangle* em_buf_{nullptr}; //敵一旦保存KanokeBuf = nullptr; //敵保存変数をクリアButton_missile->Enabled = false;Rectangle_missile->Position->Y = Rectangle_player->Position->Y + 25;FloatAnimation_missile->StopValue = missile_max; //ミサイル最終地点設定FloatAnimation_missile->StartValue = Rectangle_player->Position->X + 20;auto iExPos = missile_max;auto iEnmX = missile_max;for (int i = 0; i < 3; i++) {/*ループで敵3つを順番に撃破可能か調べる*/
switch(i){case 0: em_buf_ = Rectangle_Enm1; break;case 1: em_buf_ = Rectangle_Enm2; break;case 2: em_buf_ = Rectangle_Enm3;
}
8© 2016 Embarcadero Technologies, Inc. All rights reserved. Proprietary and confidential. #embtwebi_jp
コメント
コメントの書き方の例
/* ここからコメントが始まります。
ここまでコメントです。 */
/* ここからコメントが始まります。
これもコメントです。
ここまでコメントです。 */
/*
/*
この書き方はできません。ネスト(入れ子)不可。
*/
*/
// 単一行のコメントです。
/* 単一行コメントをこう書いてもOKです */
/*
// 単一行コメントがブロックコメントに
// 含まれていても大丈夫です。
// この場合は // も含めてコメントです。
*/
9© 2016 Embarcadero Technologies, Inc. All rights reserved. Proprietary and confidential. #embtwebi_jp
プログラム上で下記の項目を示す名前
• データ型、変数
• 関数
• オブジェクト、クラス
事前に定義されているもの
• 言語キーワード
自分で定義するもの
• 変数名、データ型名、
• オブジェクト名…
void __fastcall TfmShooting_main::Button_missileClick(TObject *Sender){
TRectangle* em_buf_{nullptr}; //敵一旦保存KanokeBuf = nullptr; //敵保存変数をクリアButton_missile->Enabled = false;Rectangle_missile->Position->Y = Rectangle_player->Position->Y + 25;FloatAnimation_missile->StopValue = missile_max; //ミサイル最終地点設定FloatAnimation_missile->StartValue = Rectangle_player->Position->X + 20;auto iExPos = missile_max;auto iEnmX = missile_max;for (int i = 0; i < 3; i++) {/*ループで敵3つを順番に撃破可能か調べる*/
switch(i){case 0: em_buf_ = Rectangle_Enm1; break;case 1: em_buf_ = Rectangle_Enm2; break;case 2: em_buf_ = Rectangle_Enm3;
}auto iTemp = EnmExist(em_buf_);if ((iTemp < missile_max) && (iEnmX > em_buf_->Position->X)){ /*敵の前に敵が居る場合, 手前の敵を優先*/
iExPos = iTemp;KanokeBuf = em_buf_;
}if (KanokeBuf != nullptr) {//撃破出来る敵変数に存在するか確認
iEnmX = KanokeBuf->Position->X;}
}
識別子
10© 2016 Embarcadero Technologies, Inc. All rights reserved. Proprietary and confidential. #embtwebi_jp
識別子に付ける文字種別
使える文字種別• アルファベット(大文字、小文字は区別されるので、下記2つは別の変数扱い)
int CppBuilder ;int cppbuilder ;
• アンダースコア _int Cpp_Builder ;
• 数字int Cpp_Builder_2007 ;Int 2007_Cpp_Builder ; // 最初の1文字を数字で始めることはできない。
• ユニバーサル文字名Int ¥u30c7¥u30eb¥u30d5¥u30a1¥u30a4 ; // “デルファイ” と記述している。
• 実装依存の文字int デルファイ ;
11© 2016 Embarcadero Technologies, Inc. All rights reserved. Proprietary and confidential. #embtwebi_jp
ブロック
複数の文で構成される処理を
明示的にまとめたもので、{ }
で囲む
読みやすくするために字下げ
(インデント)を行う
ブロックの中で定義された変
数は、そのブロックだけで利
用できる
void __fastcall TfmShooting_main::Button_missileClick(TObject *Sender){
TRectangle* em_buf_{nullptr}; //敵一旦保存KanokeBuf = nullptr; //敵保存変数をクリアButton_missile->Enabled = false;Rectangle_missile->Position->Y = Rectangle_player->Position->Y + 25;FloatAnimation_missile->StopValue = missile_max; //ミサイル最終地点設定FloatAnimation_missile->StartValue = Rectangle_player->Position->X + 20;auto iExPos = missile_max;auto iEnmX = missile_max;for (int i = 0; i < 3; i++) {/*ループで敵3つを順番に撃破可能か調べる*/
switch(i){case 0: em_buf_ = Rectangle_Enm1; break;case 1: em_buf_ = Rectangle_Enm2; break;case 2: em_buf_ = Rectangle_Enm3;
}auto iTemp = EnmExist(em_buf_);if ((iTemp < missile_max) && (iEnmX > em_buf_->Position->X)){ /*敵の前に敵が居る場合, 手前の敵を優先*/
iExPos = iTemp;KanokeBuf = em_buf_;
}if (KanokeBuf != nullptr) {//撃破出来る敵変数に存在するか確認
iEnmX = KanokeBuf->Position->X;}
}
if( iExPos < (Rectangle_player->Position->X + Rectangle_player->Width))iExPos = missile_max; //敵がプレーヤーより後ろにいる場合標的から外す
FloatAnimation_missile->StopValue = iExPos; //ミサイルの目標値をセットRectangle_missile->Visible = true; //ミサイルを表示FloatAnimation_missile->Start(); //ミサイル発射
}
12© 2016 Embarcadero Technologies, Inc. All rights reserved. Proprietary and confidential. #embtwebi_jp
言語キーワード
プログラム内で使用するためにあらかじめ用意されている識別子
• データ型、変数の宣言に使われる一例
• char, int, short, long, float, double, signed, unsigned, void, bool, auto
• 言語の文に使用されるものの一例
• if, then, else, for, while, do, switch, case, break, continue, try, catch, ……
• クラスに関するもの
• class, public, protected, private, operator, this
13© 2016 Embarcadero Technologies, Inc. All rights reserved. Proprietary and confidential. #embtwebi_jp
プログラムの構造
シューティングゲームは5つのファイルから成る
Unit1.cppフォームに関連する処理を実装
したソースコード
Unit1.hフォームで実装した関数や変数に関する定義を記述したファイ
ル(ヘッダファイル)
Unit1.fmxフォームのデザイン情報。IDEが自動的に生成する。
shooting_game_cpp.cpp(説明省略)
shooting_game_cppPCH1.h(説明省略)
14© 2016 Embarcadero Technologies, Inc. All rights reserved. Proprietary and confidential. #embtwebi_jp
プログラムの構造
プログラムの構造(抜粋)
#include <fmx.h> // マルチデバイスプラットフォーム(FireMonkey) 向けの共通ヘッダーファイルを参照する。
#pragma hdrstop // このヘッダ以降はプリコンパイルを行わない。
#include “Unit1.h” // このフォームのヘッダファイルを参照する。ここでは変数の定義や関数の宣言が行われている。
#pragma package(smart_init) // 関数の実行優先順序に関する指定。(詳細は省略)
#pragma resource “*.fmx” // フォームデザインの参照指定
TfmShooting_main *fmShooting_main;
// __fastcall は関数の実行に関する規約。ここでは説明は省略します。// C++Builder では個々の関数にこれをつけるのが基本です。
__fastcall TfmShooting_main::TfmShooting_main(TComponent* Owner) : TForm(Owner){
game_reset(); // ゲームで使用する変数やコンポーネントのプロパティを初期化する処理
}
void __fastcall TfmShooting_main::game_reset()// ここから先は関数の定義が続く。宣言は Unit1.h に記述されている。…
15© 2016 Embarcadero Technologies, Inc. All rights reserved. Proprietary and confidential. #embtwebi_jp
コンパイラ指令
#include
• 別ファイル(主にヘッダファイル)の参照
#define, #undef
• マクロ定義と定義解除
#ifdef, #ifndef
• マクロ定義の有無に基づくコンパイル処理の切り分け
#if - #elif - #else - #endif
• 式の真偽に基づくコンパイル処理の切り分け
#pragma
• コンパイラ指令(コンパイラに依存する機能の制御)の設定
16© 2016 Embarcadero Technologies, Inc. All rights reserved. Proprietary and confidential. #embtwebi_jp
文
選択文
• if ( 条件 ) 処理 ;
• 条件を満たす場合に限って処理を実行する
• if ( 条件 ) 処理1 ; else 処理2 ;
条件を満たす場合と満たさない場合の処理に分岐する。
• switch ( 条件 ) { case 1 : 処理1 ; case 2: 処理2 ; }
条件の値によって実行する文を選択する。
17© 2016 Embarcadero Technologies, Inc. All rights reserved. Proprietary and confidential. #embtwebi_jp
文
繰り返し文
• while ( 条件 ) { 処理; }
条件が満たされる間は処理を繰り返し実行する。条件は処理の前に評価される。
• do { 処理; } while ( 条件 )
条件が満たされる間は処理を繰り返し実行する。条件は処理の後に評価される。
• for ( 初期化文 ; 条件 ; 式 ) { 処理; }
条件が満たされる間は処理を繰り返し実行する。文を実行したら最後に式を実行する。
• for ( 変数宣言 ; 値の集合 ) { xxxx ; }
値の集合に含まれる値を変数に1つづつ代入して処理を実行する。
18© 2016 Embarcadero Technologies, Inc. All rights reserved. Proprietary and confidential. #embtwebi_jp
文
ジャンプ文
• break
処理を中断して一つ外側のブロックに出る。
• continue
処理を中断してループの先頭に戻る。
• return
実行中の関数を終了して、
関数の呼び出し元に戻る。
do {
if ( ) { …. };
switch ( ) {
case xx:
…
continue;
case yy:
…
return;
case zz:
….
break;
default:
….
}
for () {
….
}
}
19© 2016 Embarcadero Technologies, Inc. All rights reserved. Proprietary and confidential. #embtwebi_jp
シューティングゲームに入っているプログラム要素
コメント
識別子
ブロック
言語キーワード
プログラムの構造
コンパイラ指令
文
20© 2016 Embarcadero Technologies, Inc. All rights reserved. Proprietary and confidential. #embtwebi_jp
本日のセミナー内容は弊社ブログに掲載予定
[コミュニティ]↓
[日本人ブログ]
実施内容の再視聴
皆さんが見ているWebページの下に、順次アップロード
エンバカデロWebサイト : http://forms.embarcadero.com/starter-tutorial-webinar
[リソース] – [イベント]の「Delphi / C++Builder Starter チュートリアルシリーズ」ページ
実施内容サマリー
• Community embarcadero (コミュニティエンバカデロ)にWebリンク、サンプルコード情報等
http://community.embarcadero.com/
「エンバカデロ」で検索
21© 2016 Embarcadero Technologies, Inc. All rights reserved. Proprietary and confidential. #embtwebi_jp
次回のC++Builderパート
1月30日(月)17:30より
“変数と型„