main.cpp
#include "DxLib.h"
#include "player.h"
#include "Keyboard.h"
int WINAPI WinMain(HINSTANCE,HINSTANCE,LPSTR,int){
ChangeWindowMode(TRUE), DxLib_Init(), SetDrawScreen( DX_SCREEN_BACK ); //ウィンドウモード変更と初期化と裏画面設定
Player_Initialize();
// while(裏画面を表画面に反映, メッセージ処理, 画面クリア, キーの更新)
while( ScreenFlip()==0 && ProcessMessage()==0 && ClearDrawScreen()==0 ){
Keyboard_Update(); //キーボード入力
Player_Update(); // キャラ移動計算
Player_Shot(); //キャラショット計算
Player_Draw(); // キャラ描画
Player_Shot_Draw(); //キャラショット描画
Back_Draw(); //背景、フレーム描画
}
Player_Finalize(); // 終了処理
DxLib_End(); // DXライブラリ終了処理
return 0;
}
#include "DxLib.h"
#include "Keyboard.h"
static int m_Image; //自機画像ハンドル格納変数
static int m_x = 200; //自機初期X座標
static int m_y = 400; //自機初期Y座標
int x,y; //弾座標
int Bflag = 0; //弾フラグ
int Bimg = LoadGraph("画像/Mbullet00.png"); //変数に弾画像をロード
// 初期化をする
void Player_Initialize(){
m_Image = LoadGraph("画像/キャラクタ01.png");
}
// 動きを計算する
void Player_Update(){
if( Keyboard_Get( KEY_INPUT_UP ) > 0 ){
if( Keyboard_Get( KEY_INPUT_LSHIFT ) > 0 ){
m_y=m_y-2;
}else if( Keyboard_Get( KEY_INPUT_UP ) > 0 ){
m_y=m_y-5;
}
}
if( Keyboard_Get( KEY_INPUT_DOWN ) > 0 ){
if( Keyboard_Get( KEY_INPUT_LSHIFT ) > 0 ){
m_y=m_y+2;
}else if( Keyboard_Get( KEY_INPUT_DOWN ) > 0 ){
m_y=m_y+5;
}
}
if( Keyboard_Get( KEY_INPUT_RIGHT ) > 0 ){
if( Keyboard_Get( KEY_INPUT_LSHIFT ) > 0 ){
m_x=m_x+2;
}else if( Keyboard_Get( KEY_INPUT_RIGHT ) > 0 ){
m_x=m_x+5;
}
}
if( Keyboard_Get( KEY_INPUT_LEFT ) > 0 ){
if( Keyboard_Get( KEY_INPUT_LSHIFT ) > 0 ){
m_x=m_x-2;
}else if( Keyboard_Get( KEY_INPUT_LEFT ) > 0 ){
m_x=m_x-5;
}
}
//画面左端からはみ出そうになっていたら
if( m_x < 18 ) m_x = 18 ;
//画面右端からはみ出そうになっていたら
if( m_x > 380 ) m_x = 380 ;
//画面上端からはみ出そうになっていたら
if( m_y < 14 ) m_y = 14 ;
//画面下端からはみ出そうになっていたら
if( m_y > 420 ) m_y = 420 ;
}
// Zキーを押していて、且弾が撃ち出されていなかったら弾を発射する
void Player_Shot(){
if( Keyboard_Get( KEY_INPUT_Z ) > 0 && Bflag == 0 )
{
// 弾の位置をセット
x = m_x ;
y = m_y ;
// 弾は現時点を持って存在するので、存在状態を保持する変数に1を代入する
Bflag = 1 ;
}
}
// 自機の弾の移動ルーチン( 存在状態を保持している変数の内容が1(存在する)の場合のみ行う )
void Player_Shot_Draw(){
if( Bflag == 1 )
{
// 弾を16ドット上に移動させる
y = y-16 ;
// 画面外に出てしまった場合は存在状態を保持している変数に0(存在しない)を代入する
if( y < -80 )
{
Bflag = 0 ;
}
// 画面に弾を描画する
DrawGraph( x , y , Bimg , FALSE ) ;
}
}
// 描画する
void Player_Draw(){
DrawGraph( m_x, m_y, m_Image, TRUE );
}
// 終了処理をする
void Player_Finalize(){
DeleteGraph( m_Image );
}
#include "DxLib.h"
static int m_Key[256]; // キーの入力状態格納用変数
// キーの入力状態更新
void Keyboard_Update(){
char tmpKey[256]; // 現在のキーの入力状態を格納する
GetHitKeyStateAll( tmpKey ); // 全てのキーの入力状態を得る
for( int i=0; i<256; i++ ){
if( tmpKey[i] != 0 ){ // i番のキーコードに対応するキーが押されていたら
m_Key[i]++; // 加算
} else { // 押されていなければ
m_Key[i] = 0; // 0にする
}
}
}
// KeyCodeのキーの入力状態を取得する
int Keyboard_Get( int KeyCode ){
return m_Key[ KeyCode ]; // KeyCodeの入力状態を返す
}
#ifndef DEF_PLAYER_H //二重include防止
#define DEF_PLAYER_H
// 初期化をする
void Player_Initialize();
// 動きを計算する
void Player_Update();
//キャラショットの計算
void Player_Shot();
//キャラショットの描画
void Player_Shot_Draw();
// 描画する
void Player_Draw();
// 終了処理をする
void Player_Finalize();
#endif