fork download
  1. //#include <windows.h>
  2. #include "dxlib.h"
  3.  
  4. #define CX 1024
  5. #define CY 768
  6. #define BPP 16
  7. #define WHITE GetColor( 255, 255, 255 )
  8. #define GRAY(a) GetColor( a, a, a )
  9.  
  10. #define ACCELL 7.5
  11. #define GRAVITY 15.0
  12. #define CSLOPE 7.5
  13. #define RESIST 2.5
  14. #define JUMP 300.0
  15. #define FERROR 2.5
  16. #define PSIZE 8
  17.  
  18. double GetSlope( VECTOR* fdata, int numdata, int x );
  19. double GetFloor( VECTOR* fdata, int numdata, int x );
  20.  
  21. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrev, LPSTR lpCmdLine, int nCmdShow)
  22. {
  23. SetGraphMode( CX, CY, BPP );
  24. ChangeWindowMode( TRUE );
  25. if( DxLib_Init() == -1 ) return -1;
  26. SetDrawScreen( DX_SCREEN_BACK );
  27.  
  28. VECTOR floors[] = { { 0, 64, 0 }, { 100, 64, 0 }, { 200, 96, 0 }, { 250, 104, 0 },
  29. { 500, 32, 0 }, { 800, 32, 0 }, { 1000, 256, 0 }, { 1500, 128, 0 }, { 2000, 64, 0 },
  30. { 2500, 96, 0 }, { 3000, 128, 0 }, { 3200, 160, 0 }, { 3400, 10, 0 }, { 5000, 64, 0 } };
  31. int elements = sizeof(floors) / sizeof(VECTOR);
  32. VECTOR player = { 50, 100, 0 };
  33. VECTOR vec = { 0, 0 };
  34. int cam_x;
  35. double floor_y;
  36. int pre_time, cur_time;
  37. double past_time;
  38. int bOnTheFloor = FALSE;
  39. int bDrawAll = TRUE;
  40. int bPreDKey = FALSE;
  41. int bPreSPKey = FALSE;
  42.  
  43. pre_time = GetCurrentTime();
  44. while ( ProcessMessage() != -1 ) {
  45. if ( CheckHitKey( KEY_INPUT_ESCAPE ) )
  46. break;
  47. if ( CheckHitKey( KEY_INPUT_D ) ) {
  48. if ( !bPreDKey )
  49. bDrawAll = !bDrawAll;
  50. bPreDKey = TRUE;
  51. }
  52. else
  53. bPreDKey = FALSE;
  54. cur_time = GetCurrentTime();
  55. past_time = (double)( cur_time - pre_time) / 1000.0;
  56. pre_time = cur_time;
  57.  
  58. if ( CheckHitKey( KEY_INPUT_RIGHT ) ) vec.x += ( bOnTheFloor ? ACCELL : (ACCELL * 0.25) );
  59. if ( CheckHitKey( KEY_INPUT_LEFT ) ) vec.x -= ( bOnTheFloor ? ACCELL : (ACCELL * 0.25) );
  60. if ( bOnTheFloor ) {
  61. vec.x -= GetSlope( floors, elements, player.x ) * CSLOPE;
  62. if ( GetSlope( floors, elements, player.x ) * vec.x < 0.0 )
  63. vec.y -= abs( GetSlope( floors, elements, player.x ) * 500 );
  64. }
  65. if ( vec.x > 0.0 ) {
  66. if ( ( vec.x -= ( bOnTheFloor ? RESIST : (RESIST * 0.25) ) ) < 0.0 ) vec.x = 0.0;
  67. }
  68. if ( vec.x < 0.0 ) {
  69. if ( ( vec.x += ( bOnTheFloor ? RESIST : (RESIST * 0.25) ) ) > 0.0 ) vec.x = 0.0;
  70. }
  71.  
  72. if ( CheckHitKey( KEY_INPUT_SPACE ) ) {
  73. if ( !bPreSPKey && bOnTheFloor )
  74. vec.y = JUMP;
  75. bPreSPKey = TRUE;
  76. }
  77. else
  78. bPreSPKey = FALSE;
  79. player.x += vec.x * past_time;
  80. player.y += vec.y * past_time;
  81.  
  82. vec.y -= GRAVITY;
  83.  
  84. floor_y = GetFloor( floors, elements, player.x );
  85. if ( floor_y + FERROR > player.y ) {
  86. player.y = floor_y;
  87. vec.y = 0.0;
  88. bOnTheFloor = TRUE;
  89. }
  90. else {
  91. bOnTheFloor = FALSE;
  92. }
  93.  
  94. cam_x = player.x - (CX >> 1);
  95.  
  96. ClearDrawScreen();
  97.  
  98. for ( int i = 0; i < elements - 1; i++ ) {
  99. DrawLine( floors[i].x - cam_x, CY - floors[i].y, floors[i+1].x - cam_x, CY - floors[i+1].y, WHITE );
  100. }
  101. if ( bDrawAll ) {
  102. for ( i = floors[0].x; i < floors[elements - 1].x; i++ ) {
  103. DrawLine( i - cam_x, CY - GetFloor( floors, elements, i ), i - cam_x, CY, GRAY( 64 ) );
  104. }
  105. }
  106. DrawBox( player.x - PSIZE - cam_x, CY - player.y - (PSIZE << 1), player.x + PSIZE - cam_x, CY - player.y, GetColor( 128, 128, 255 ), TRUE );
  107.  
  108. DrawFormatString( 0, 0, WHITE, "← →で移動" );
  109. DrawFormatString( 0, 24, WHITE, "スペースキーでジャンプ" );
  110. DrawFormatString( 0, 48, WHITE, "重い時は\"D\"キーで地面非表示" );
  111.  
  112. ScreenFlip();
  113. }
  114.  
  115. DxLib_End();
  116.  
  117. return 0;
  118. }
  119.  
  120. double GetSlope( VECTOR* fdata, int numdata, int x ) {
  121. double slope;
  122.  
  123. for ( int i = 0; i < numdata - 1; i++ ) {
  124. if ( fdata[i].x <= x && x < fdata[i+1].x )
  125. break;
  126. }
  127. if ( i >= numdata - 1 )
  128. slope = 0;
  129. else
  130. slope = (double)(fdata[i+1].y - fdata[i].y) / (double)(fdata[i+1].x - fdata[i].x);
  131.  
  132. return slope;
  133. }
  134.  
  135. double GetFloor( VECTOR* fdata, int numdata, int x ) {
  136. double floor_y;
  137.  
  138. for ( int i = 0; i < numdata - 1; i++ ) {
  139. if ( fdata[i].x <= x && x < fdata[i+1].x )
  140. break;
  141. }
  142. if ( i >= numdata - 1 )
  143. floor_y = -1000;
  144. else
  145. floor_y = (double)(fdata[i+1].y - fdata[i].y) / (double)(fdata[i+1].x - fdata[i].x) * (double)(x - fdata[i].x) + fdata[i].y;
  146.  
  147. return floor_y;
  148. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty