コード:
#include "DxLib.h"
#define map_size 64
#define map_width 20
#define map_height 16
#define move_frame 32
int muki=3,a_flag=0,i,j;
int px=0,py=420,px_old=px,py_old=py;
int y_tmp=32,y_prev=0;
bool p_flag=false;
char Key[256];
float speed=1.0f;
int mode=0;
int h_color=(255,0,0),st=(255,255,222);
int color=GetColor(255,255,255);
int haikei,haikei1,player[16],Handle;
int stage_haikei,sh_x=0,sh_oldx=sh_x,stage_haikei2,sh2_x=0;
int count=0;
int t_moji;
int goki,ex=0,ey=420,ex_prev;
int x=32,y;
int yuka,kx=0,kx_old=kx,ky=0;
bool k_flag=false;
int MapDrawPoint_X,MapDrawPoint_Y;
int DrawMapChipNum_X,DrawMapChipNum_Y;
int move,move_x,move_y,movecount;
int scroll_x,scroll_y;
int hantei[map_height][map_width]={
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } ,
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } ,
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } ,
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } ,
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } ,
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 , 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 } ,
{ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 , 0, 0, 0, 0, 0, 0, 1, 0, 1, 0 } ,
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 , 0, 0, 1, 0, 0, 1, 1, 0, 1, 0 } ,
{ 0, 0, 1, 1, 1, 1, 1, 1, 1, 0 , 0, 1, 1, 0, 0, 1, 1, 0, 1, 0 } ,
{ 0, 0, 0, 0, 1, 0, 0, 1, 0, 0 , 0, 1, 1, 0, 0, 1, 1, 0, 1, 0 } ,
{ 0, 0, 0, 0, 1, 0, 0, 1, 0, 0 , 0, 1, 1, 0, 0, 1, 1, 0, 1, 0 } ,
{ 0, 0, 0, 1, 1, 0, 0, 1, 0, 0 , 0, 1, 1, 0, 0, 1, 1, 0, 1, 0 } ,
{ 0, 0, 1, 1, 1, 0, 0, 1, 1, 1 , 1, 1, 1, 0, 0, 1, 1, 0, 1, 0 } ,
{ 0, 0, 1, 1, 1, 0, 0, 0, 0, 0 , 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 } ,
{ 0, 1, 1, 1, 1, 0, 0, 0, 0, 0 , 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 } ,
{ 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } ,
} ;
//ゲームオーバー
void gameover(){
ClearDrawScreen();
LoadGraphScreen(0,0,"アクション画像/Gameover.png",FALSE);
if(Key[KEY_INPUT_C]==1){
mode=1;
}
}
//タイトル
void title(){
DrawGraph(0,0,haikei,FALSE);
count=(count+1)%30;
if( count < 2 ) {
WaitTimer(200);
DrawGraph(240,320,t_moji,FALSE);
}
if(Key[KEY_INPUT_G]==1){
mode=1;
}
}
//操作説明
void setumei(){
ClearDrawScreen();
DrawString(320,230,"↑",st);
DrawString(310,240,"←",st);
DrawString(330,240,"→",st);
DrawString(305,200,"ジャンプ",st);
DrawString(250,240,"左移動",st);
DrawString(360,240,"右移動",st);
if(Key[KEY_INPUT_G]==1) mode=1;
if(Key[KEY_INPUT_M]==1) mode=0;
}
void kabe(){
DrawMapChipNum_X =640/map_size+2;
DrawMapChipNum_Y=480/map_size+2;
// 画面左上に描画するマップ座標をセット
MapDrawPoint_X=px-(DrawMapChipNum_X/2-1);
MapDrawPoint_Y=py-(DrawMapChipNum_Y/2-1);
// マップを描く
for(i=0;i<DrawMapChipNum_Y;i++){
for(j=0;j<DrawMapChipNum_X;j++){
// 画面からはみ出た位置なら描画しない
if(j+MapDrawPoint_X<0||i+MapDrawPoint_Y<0||j+MapDrawPoint_X>=map_width||i+MapDrawPoint_Y>=map_height){
continue;
}
// マップデータが1だったら四角を描画する
if(hantei[i+MapDrawPoint_Y][j+MapDrawPoint_X]==1){
DrawBox(j*map_size,i*map_size,j*map_size+map_size,i*map_size+map_size,GetColor(255,0,0),TRUE);
}
}
}
}
void pmove(){
//プレイヤー移動スピード
if(Key[KEY_INPUT_LEFT]==1||Key[KEY_INPUT_RIGHT]==1){
if(Key[KEY_INPUT_UP]==1|| Key[KEY_INPUT_DOWN]==1){
speed=0.71f;
}else{
speed=1.0f;
}
}else if(Key[KEY_INPUT_UP]==1||Key[KEY_INPUT_DOWN]==1){
speed=0.7f;
}
//左キーが押されたとき
if(Key[KEY_INPUT_LEFT]==1){
if(px>=320-32){
px-=(int)4*speed;
if(px<=320-32){
px=320-32;
sh_x+=(int)4*speed;
kx+=(int)4*speed;
}
}
if(sh_x>=0){
sh_x=0;
if(kabe>=0){
kx=0;
if(px<=320-32){
px-=(int)4*speed;
}
}
}
}
//右キーが押されたとき
if(Key[KEY_INPUT_RIGHT]==1){
if(px<=320-32){
px+=(int)4*speed;
if(px>=320-32){
px=320-32;
sh_x-=(int)4*speed;
kx-=(int)4*speed;
}
if(sh_x<=-3200){
if(kx<=-3200){
sh_x=-3200;
kx=-3200;
if(px>=320-32){
px+=(int)4*speed;
}
}
}
}
}
//上キーが押されたとき(ジャンプ処理)
if(Key[KEY_INPUT_UP]==1 && p_flag==false){ //ジャンプしていないときにスペースを押すと
p_flag=true; //ジャンプフラグを立ててから
y_prev=py; //現在のpを保存させて
py=py-20; //今の場所から20引いたのを、pyに
}
//落下処理
if(p_flag==true){
y_tmp=py; //pyを保存し
py+=(py-y_prev)+1; //ジャンプする前の値をpyから引いて、1足したのをpyに足す
y_prev=y_tmp;
}
//進めるかどうかの判定
if(hantei[px][py]==1){
printfDx("dsa");
px=px_old;
py=py_old;
sh_x=sh_oldx;
kx=kx_old;
}
//キャラクタの移動制
if(px<=0){
px=0;
}else if(px>=640-32){
px=640-32;
}
if(py<=0){
py=0;
}else if(py>=480+32){
py=480+32;
}
//ゲームオーバー
if(py>=480){
mode=3;
}
DrawGraph(sh_x,0,stage_haikei,FALSE);
DrawGraph(kx,ky,yuka,TRUE);
DrawString(0,0,"M:メニュー画面",GetColor(255,0,0));
Handle=player[(sh_x%32+py%32)/8+muki*4];
DrawGraph(px,py,Handle,TRUE);
//メニュー画面
if(Key[KEY_INPUT_M]==1){
mode=0;
}
//操作説明画面
if(Key[KEY_INPUT_S]==1){
setumei();
}
//ステージ移動
if(sh_x<=-3200+32&&px>=640-32){
mode=2;
}
//ステージ先でのプレイヤーの初期値
if(mode==2){
px=0;
py=420;
}
}
void enemy(){
//敵の移動制御
if(sh_x>=0||sh_x<=640-128){
ex++;
}
DrawGraph(ex,420,goki,TRUE);
}
void stage(){
pmove();
enemy();
kabe();
}
void stage2(){
if(px>=640-32){
px=640-32;
}else if(px<=0){
px=0;
}
if(py>=480){
mode=3;
}
DrawGraph(0,0,stage_haikei2,TRUE);
DrawGraph(kx,ky,yuka,TRUE);
DrawString(0,0,"M:メニュー画面",GetColor(255,0,0));
Handle=player[(sh2_x%32+py%32)/8+muki*4];
DrawGraph(px,py,Handle,TRUE);
}
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow ){
ChangeWindowMode(TRUE);
SetGraphMode(1220,900,128);
if( DxLib_Init() == -1 ){
return -1;
}
SetDrawScreen(DX_SCREEN_BACK);
LoadDivGraph("アクション画像/キャラクタ10.png",16,4,4,32,32,player);
haikei=LoadGraph("アクション画像/メニュー背景.png");
stage_haikei=LoadGraph("アクション画像/ゲーム背景.png");
yuka=LoadGraph("アクション画像/画像1.png");
t_moji=LoadGraph("アクション画像/teki.png");
goki=LoadGraph("アクション画像/試し.png");
stage_haikei2=LoadGraph("アクション画像/背景2.png");
SetDrawScreen( DX_SCREEN_BACK );
while(1){
ClearDrawScreen();
GetHitKeyStateAll(Key);
switch(mode){
case 0:
title();
break;
case 1:
stage();
break;
case 2:
stage2();
break;
case 3:
gameover();
break;
default:
DxLib_End();
return 0;
break;
}
if(Key[KEY_INPUT_ESCAPE]==1){
break;
}
if(ProcessMessage()==-1){
break;
}
ScreenFlip();
}
DxLib_End();
return 0;
}