www.mtkn.jp

Manuscripts for my personal webpage.
git clone https://git.mtkn.jp/www.mtkn.jp
Log | Files | Refs | LICENSE

xlib_playground6.html (9693B)


      1 <!DOCTYPE html>
      2 <html>
      3 <head>
      4 	<meta charset="utf-8">
      5 	<meta name="viewport" content="width=device-width,initial-scale=1">
      6 	<link rel="stylesheet" type="text/css" href="/style.css">
      7 	<link rel="icon" type="image/x-icon" href="/pics/favicon.ico">
      8 	<title>Xlibで遊んでみる6</title>
      9 </head>
     10 <body>
     11 	<header>
     12 		<a href="/">主頁</a> |
     13 		<a href="/about.html">自己紹介</a> |
     14 		<a href="/journal">日記</a> |
     15 		<a href="/farm">農業</a> |
     16 		<a href="/kitchen">台所</a> |
     17 		<a href="/computer">電算機</a> |
     18 		<a href="/poetry">詩</a> |
     19 		<a href="/books">本棚</a> |
     20 		<a href="/gallery">絵</a> |
     21 		<a href="https://git.mtkn.jp">Git</a>
     22 	</header>
     23 	<main>
     24 		<article>
     25 <h1>Xlibで遊んでみる6</h1>
     26 <time>2023-01-25</time>
     27 
     28 <p>
     29 前回: <a href="xlib_playground5.html">Xlibで遊んでみる5</a>
     30 </p>
     31 <p>
     32 言語: C言語<br />
     33 ソースコード: <a href="https://git.mtkn.jp/xlib_playground">git</a>
     34 </p>
     35 
     36 <h2>ワールドマップの作成</h2>
     37 <p>
     38 ゲームのワールドマップを作製した。ここでは文字列として登録した。なにもないところは「<code>.</code>」、ブロックの場所は「<code>b</code>」、プレーヤーは「<code>p</code>」とした:
     39 </p>
     40 <pre><code>char worldmap[WORLD_WIDTH * WORLD_HEIGHT + 1] =
     41 "................................................................................"
     42 "................................................................................"
     43 "................................................................................"
     44 "................................................................................"
     45 "................................................................................"
     46 "........b......................................................................."
     47 "................................................................................"
     48 "................................................................................"
     49 "....b..........................................................................."
     50 "................................................................................"
     51 "................b..............................................................."
     52 "..........................................................b..........b.........."
     53 "................................................................................"
     54 ".......................b........................................................"
     55 "...........................................b...................................."
     56 "...........................................b...................................."
     57 "................................................................................"
     58 "..................b............................................................."
     59 "................................................................................"
     60 "...........................................b...................................."
     61 "................................................................................"
     62 "................................................................................"
     63 "...........................b...................................................."
     64 "................................................................................"
     65 "................................................................................"
     66 "................................................................................"
     67 "................................................................................"
     68 "................................................................................"
     69 "................................................................................"
     70 "....................................bbbbbbbbbb.................................."
     71 "................................................................................"
     72 "................................................................................"
     73 "................................................................................"
     74 "................................................................................"
     75 "................................................................................"
     76 "................................................................................"
     77 "..............................................bbbbbbbbbb........................"
     78 "................................................................................"
     79 "................................................................................"
     80 "................................................................................"
     81 "................................................................................"
     82 "....................................bbbbbbbbbb.................................."
     83 "................................................................................"
     84 "................................................................................"
     85 "................................................................................"
     86 "................................................................................"
     87 "..........................bbbbbbbbbb............................................"
     88 "................................................................................"
     89 "................................................................................"
     90 "................................................................................"
     91 "................................................................................"
     92 "................bbbbbbbbbb......................................................"
     93 "................................................................................"
     94 "................................................................................"
     95 "...p............................................................................"
     96 "bbbbbbbbbbbbbbbbbbbbbbbbb.......bbbbbbbbbbbbbbbbbbbbbbbb...bbbbbbbbbbbbbbbbbbbbb"
     97 "........................b.......b......................b...b...................."
     98 "........................b.......b......................b...b...................."
     99 "........................b.......b......................b...b...................."
    100 "........................b.......b......................b...b....................";
    101 </code></pre>
    102 
    103 <h2>プレイヤーの作成</h2>
    104 <p>プレイヤーには重力をかけたいので、まずは四角形に加速度を追加:</p>
    105 <pre><code>struct rect {
    106 	float ppx, ppy;
    107 	float px, py;
    108 	float vx, vy;
    109 	float ax, ay;  // acceleration
    110 	int w, h;
    111 	int m;
    112 };
    113 </code></pre>
    114 <p>ワールドマップを読み込み、その際にプレイヤーに重力を付加:</p>
    115 <pre><code>struct rect   block[NUM_RECT];
    116 struct rect   player;
    117 
    118 /* ... */
    119 
    120 	int bi = 0;
    121 	for (int i = 0; i &lt; WORLD_WIDTH * WORLD_HEIGHT; i++) {
    122 		if (world_map[i] == 'b') {
    123 			block[bi].ppx = block[bi].px = i % WORLD_WIDTH * BLOCK_SIZE;
    124 			block[bi].ppy = block[bi].py = i / WORLD_WIDTH * BLOCK_SIZE;
    125 			block[bi].ax = 0;
    126 			block[bi].ay = 0;
    127 			block[bi].vx = 0;
    128 			block[bi].vy = 0;
    129 			block[bi].w = block[bi].h = BLOCK_SIZE;
    130 			block[bi].m = block[bi].w * block[bi].h;
    131 			bi++;
    132 		} else if (world_map[i] == 'p') {
    133 			player.ppx = player.px =  i % WORLD_WIDTH * BLOCK_SIZE;
    134 			player.ppy = player.py = i / WORLD_WIDTH * BLOCK_SIZE;
    135 			player.vx = 0;
    136 			player.vy = 0;
    137 			player.ax = 0;
    138 			player.ay = GRAVITY;
    139 			player.w = player.h = BLOCK_SIZE;
    140 			player.m = player.w * player.h;
    141 		}
    142 	}
    143 </code></pre>
    144 
    145 <p>ユーザーからの入力を受けとり、プレイーヤの加速度等を変更。<code>A</code>、<code>D</code>でそれぞれ左右に加速し、地面に接しているときに<code>space</code>キーでジャンプさせる:
    146 </p>
    147 <pre><code>void
    148 handle_inputs(int key_state[])
    149 {
    150 	if (key_state[KEY_Q] == KEY_DOWN){
    151 		next_menu = GAME_OVER;
    152 		return;
    153 	}
    154 	if (key_state[KEY_D] == KEY_DOWN) {
    155 		if (player.vx &gt; 0) {
    156 			player.ax = 500;
    157 		} else {
    158 			player.ax = 1000;
    159 		}
    160 	} else if (key_state[KEY_A] == KEY_DOWN) {
    161 		if (player.vx &gt; 0) {
    162 			player.ax = -1000;
    163 		} else {
    164 			player.ax = -500;
    165 		}
    166 	} else {
    167 		if (player_is_falling)
    168 			player.ax = -player.vx;
    169 		else
    170 			player.ax = -3 * player.vx;
    171 	}
    172 
    173 	if (player.vx &lt; -200) player.vx = -200;
    174 	if (player.vx &gt; 200) player.vx = 200;
    175 	if (!player_is_falling && key_state[KEY_SPACE] == KEY_DOWN)
    176 		player.vy = -450;
    177 }
    178 </code></pre>
    179 
    180 <p>変更した加速度は<code>rect_next_tick()</code>関数で次の位置を計算するのに使用。また画面の下に落ちた時にゲームオーバーになるように設定:</p>
    181 <pre><code>void
    182 rect_next_tick(struct rect *s, long ndt) // nano second
    183 {
    184 	s-&gt;ppx = s-&gt;px;
    185 	s-&gt;ppy = s-&gt;py;
    186 	s-&gt;vx += s-&gt;ax * ndt / 1000 / 1000 / 1000;
    187 	s-&gt;vy += s-&gt;ay * ndt / 1000 / 1000 / 1000;
    188 	s-&gt;px += s-&gt;vx * ndt / 1000 / 1000 / 1000;
    189 	s-&gt;py += s-&gt;vy * ndt / 1000 / 1000 / 1000;
    190 
    191 	// bind within the window
    192 	if (s-&gt;px &lt; 0) {
    193 		s-&gt;px = 0;
    194 		//s-&gt;vx *= -1;
    195 	}
    196 	if (win_width &lt; s-&gt;px + s-&gt;w) {
    197 		s-&gt;px = win_width - s-&gt;w;
    198 		//s-&gt;vx *= -1;
    199 	}
    200 	// game over when fall out of the screen
    201 	if (s-&gt;py &gt; win_height)
    202 		next_menu = GAME_OVER;
    203 }
    204 </code></pre>
    205 
    206 
    207 <h2>完成品</h2>
    208 <p>
    209 <a href="https://git.mtkn.jp/xlib_playground/file/ex6/ex6.c.html">git</a>
    210 </p>
    211 <p>
    212 <video controls>
    213 <source src="videos/ex6.webm" type="video/webm">
    214 </video>
    215 </p>
    216 
    217 <h2>参考</h2>
    218 <ul>
    219 <li><a href="https://tronche.com/gui/x/xlib/">The Xlib Manual(html conversion)</a></li>
    220 </ul>
    221 		</article>
    222 
    223 	</main>
    224 	<footer>
    225 		<address>info(at)mtkn(dot)jp</address>
    226 		<a href="http://creativecommons.org/publicdomain/zero/1.0?ref=chooser-v1" rel="license noopener noreferrer">CC0 1.0</a>
    227 	</footer>
    228 </body>
    229 </html>