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. ClearDrawScreen();
  46. if ( CheckHitKey( KEY_INPUT_ESCAPE ) )
  47. break;
  48. if ( CheckHitKey( KEY_INPUT_D ) ) {
  49. if ( !bPreDKey )
  50. bDrawAll = !bDrawAll;
  51. bPreDKey = TRUE;
  52. }
  53. else
  54. bPreDKey = FALSE;
  55. cur_time = GetCurrentTime();
  56. past_time = (double)( cur_time - pre_time) / 1000.0;
  57. pre_time = cur_time;
  58.  
  59. if ( CheckHitKey( KEY_INPUT_RIGHT ) ) vec.x += ( bOnTheFloor ? ACCELL : (ACCELL * 0.25) );
  60. if ( CheckHitKey( KEY_INPUT_LEFT ) ) vec.x -= ( bOnTheFloor ? ACCELL : (ACCELL * 0.25) );
  61. if ( CheckHitKey( KEY_INPUT_SPACE ) ) {
  62. if ( !bPreSPKey && bOnTheFloor )
  63. vec.y += JUMP;
  64. bPreSPKey = TRUE;
  65. }
  66. else
  67. bPreSPKey = FALSE;
  68. if ( bOnTheFloor )
  69. vec.x -= GetSlope( floors, elements, player.x ) * CSLOPE;
  70. if ( vec.x > 0.0 ) {
  71. if ( ( vec.x -= ( bOnTheFloor ? RESIST : (RESIST * 0.25) ) ) < 0.0 ) vec.x = 0.0;
  72. }
  73. if ( vec.x < 0.0 ) {
  74. if ( ( vec.x += ( bOnTheFloor ? RESIST : (RESIST * 0.25) ) ) > 0.0 ) vec.x = 0.0;
  75. }
  76.  
  77. player.x += vec.x * past_time;
  78. player.y += vec.y * past_time;
  79.  
  80. vec.y -= GRAVITY;
  81.  
  82. floor_y = GetFloor( floors, elements, player.x );
  83. if ( floor_y + FERROR > player.y ) {
  84. player.y = floor_y;
  85. vec.y = 0.0;
  86. bOnTheFloor = TRUE;
  87. }
  88. else {
  89. bOnTheFloor = FALSE;
  90. }
  91.  
  92. cam_x = player.x - (CX >> 1);
  93.  
  94. for ( int i = 0; i < elements - 1; i++ ) {
  95. DrawLine( floors[i].x - cam_x, CY - floors[i].y, floors[i+1].x - cam_x, CY - floors[i+1].y, WHITE );
  96. }
  97. if ( bDrawAll ) {
  98. for ( i = floors[0].x; i < floors[elements - 1].x; i++ ) {
  99. DrawLine( i - cam_x, CY - GetFloor( floors, elements, i ), i - cam_x, CY, GRAY( 64 ) );
  100. }
  101. }
  102. DrawBox( player.x - PSIZE - cam_x, CY - player.y - (PSIZE << 1), player.x + PSIZE - cam_x, CY - player.y, GetColor( 128, 128, 255 ), TRUE );
  103.  
  104. DrawFormatString( 0, 0, WHITE, "← →で移動" );
  105. DrawFormatString( 0, 24, WHITE, "スペースキーでジャンプ" );
  106. DrawFormatString( 0, 48, WHITE, "重い時は\"D\"キーで地面非表示" );
  107.  
  108. ScreenFlip();
  109. }
  110.  
  111. DxLib_End();
  112.  
  113. return 0;
  114. }
  115.  
  116. double GetSlope( VECTOR* fdata, int numdata, int x ) {
  117. double slope;
  118.  
  119. for ( int i = 0; i < numdata - 1; i++ ) {
  120. if ( fdata[i].x <= x && x < fdata[i+1].x )
  121. break;
  122. }
  123. if ( i >= numdata - 1 )
  124. slope = 0;
  125. else
  126. slope = (double)(fdata[i+1].y - fdata[i].y) / (double)(fdata[i+1].x - fdata[i].x);
  127.  
  128. return slope;
  129. }
  130.  
  131. double GetFloor( VECTOR* fdata, int numdata, int x ) {
  132. double floor_y;
  133.  
  134. for ( int i = 0; i < numdata - 1; i++ ) {
  135. if ( fdata[i].x <= x && x < fdata[i+1].x )
  136. break;
  137. }
  138. if ( i >= numdata - 1 )
  139. floor_y = -1000;
  140. else
  141. 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;
  142.  
  143. return floor_y;
  144. }
  145.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty