teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

2019/08/10 17:19

投稿

carnage0216
carnage0216

スコア194

title CHANGED
File without changes
body CHANGED
@@ -1,4 +1,132 @@
1
+ 以下はコード全体です。
2
+ ```
3
+ #include "DxLib.h"
4
+
5
+ // コマの横/縦数
6
+ #define FRAME_WIDTH 5
7
+ #define FRAME_HEIGHT 5
8
+
9
+ // コマの描画幅/高さ
10
+ #define CELL_WIDTH 49
11
+ #define CELL_HEIGHT 66
12
+
13
+ // 描画マージン
14
+ #define FIELD_MARGIN_X 50
15
+ #define FIELD_MARGIN_Y 50
16
+
17
+ typedef struct Player_t {
18
+ int x;
19
+ int y;
20
+ int handle;
21
+ } Player;
22
+
23
+ // コマ(x,y)に外枠を描く[おまけ]
24
+ int box_draw(int x, int y) {
25
+ return DrawBox(x * CELL_WIDTH + FIELD_MARGIN_X, y * CELL_HEIGHT + FIELD_MARGIN_Y,
26
+ (x + 1) * CELL_WIDTH + FIELD_MARGIN_X, (y + 1) * CELL_HEIGHT + FIELD_MARGIN_Y,
27
+ GetColor(255, 255, 255), FALSE);
28
+ }
29
+
30
+ // コマ(x,y)に画像を描く
31
+ int cell_draw(int x, int y, int handle) {
32
+ return DrawGraph(x * CELL_WIDTH + FIELD_MARGIN_X, y * CELL_HEIGHT + FIELD_MARGIN_Y, handle, FALSE);
33
+ }
34
+
35
+ int Key[256]; // キーが押されているフレーム数を格納する
36
+
37
+ // キーの入力状態を更新する
38
+ int gpUpdateKey() {
39
+ char tmpKey[256]; // 現在のキーの入力状態を格納する
40
+ GetHitKeyStateAll(tmpKey); // 全てのキーの入力状態を得る
41
+ for (int i = 0; i < 256; i++) {
42
+ if (tmpKey[i] != 0) { // i番のキーコードに対応するキーが押されていたら
43
+ Key[i]++; // 加算
44
+ }
45
+ else { // 押されていなければ
46
+ Key[i] = 0; // 0にする
47
+ }
48
+ }
49
+ return 0;
50
+ }
51
+
52
+ void Player_update(Player* p) {
53
+ int dx = 0;
54
+ int dy = 0;
55
+
56
+ if (Key[KEY_INPUT_RIGHT] == 1) {
57
+ dx = dx + 1;
58
+ p->handle = 2;
59
+ }
60
+ if (Key[KEY_INPUT_LEFT] == 1) {
61
+ dx = dx - 1;
62
+ p->handle = 1;
63
+ }
64
+ if (Key[KEY_INPUT_UP] == 1) {
65
+ dy = dy - 1;
66
+ p->handle = 5;
67
+ }
68
+ if (Key[KEY_INPUT_DOWN] == 1) {
69
+ dy = dy + 1;
70
+ p->handle = 11;
71
+ }
72
+ if (dx == 0 && dy == 0) {
73
+ p->handle = 8;
74
+ }
75
+
76
+ /* 新たな位置へ移動可能であるならplayerの位置を更新する */
77
+ int newX = p->x + dx;
78
+ int newY = p->y + dy;
79
+ if (0 <= newX && newX < FRAME_WIDTH && 0 <= newY && newY < FRAME_HEIGHT) {
80
+ p->x = newX;
81
+ p->y = newY;
82
+ }
83
+ else {
84
+ p->handle = 8; // 動けないなら正面を向く
85
+ }
86
+ }
87
+
88
+ void Player_draw(const Player* p, const int* gh) {
89
+ cell_draw(p->x, p->y, gh[p->handle]);
90
+ }
91
+
92
+ int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int) {
93
+ SetGraphMode(1300, 680, 32); // ウィンドウの大きさを指定
94
+ ChangeWindowMode(TRUE);
95
+
96
+ if (DxLib_Init() == -1) {
97
+ return -1;
98
+ }
99
+
100
+ //グラフィックハンドル格納用配列
101
+ int gh[12];
102
+ LoadDivGraph("charall.png", 12, 3, 4, 49, 66, gh);
103
+
104
+ Player player;
105
+ player.x = FRAME_WIDTH / 2;
106
+ player.y = FRAME_HEIGHT / 2;
107
+
108
+ SetDrawScreen(DX_SCREEN_BACK);
109
+
110
+ while (ProcessMessage() == 0) {
111
+ gpUpdateKey();
112
+
113
+ Player_update(&player);
114
+
115
+ // ここから表現
116
+ ClearDrawScreen();
117
+ for (int x = 0; x < FRAME_WIDTH; ++x)
118
+ for (int y = 0; y < FRAME_HEIGHT; ++y)
119
+ box_draw(x, y);
120
+ Player_draw(&player, gh);
121
+ ScreenFlip();
122
+ }
123
+
124
+ DxLib_End();
125
+ return 0;
126
+ }
127
+ ```
128
+
1
- 書くプログラムを解読したいと思い質問します。
129
+ プログラムを解読したいと思い質問します。
2
130
  1、
3
131
  ```
4
132
  void Player_update(Player* p) {
@@ -7,7 +135,7 @@
7
135
  }
8
136
 
9
137
  ```
10
- 関数void Player_updateが引数に関数Player* pを持ってきて、int型の変数dxは4バイトを占める値0とint型の変数dyは4バイトを占める値0に引数である関数Player* pを渡しますと思いますが、この関数Player* p自体が何を持った関数かなのかわからないのですが、なんでしょうか?①
138
+ 関数void Player_updateが引数に関数Player* pを持ってきて、int型の変数dx値0とint型の変数dy値0に引数である関数Player* pを渡すと思いますが、この関数Player* p自体が何を持った関数かなのかわからないのですが、なんでしょうか?①
11
139
 
12
140
 
13
141
  2、