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