{"id":614,"date":"2004-05-30T11:14:29","date_gmt":"2004-05-30T02:14:29","guid":{"rendered":"\/?p=614"},"modified":"2004-05-30T11:14:29","modified_gmt":"2004-05-30T02:14:29","slug":"race-game","status":"publish","type":"post","link":"https:\/\/www.hiro345.net\/blogs\/hiro345\/archives\/614.html","title":{"rendered":"Race game"},"content":{"rendered":"<p>\u3055\u3063\u305d\u304f\u3001\u30ec\u30fc\u30b9\u30b2\u30fc\u30e0\u3092\u4f5c\u3063\u3066\u307f\u305f\u3002\u57fa\u672c\u7684\u306b\u306f\u300c<a href=\"http:\/\/www.amazon.co.jp\/exec\/obidos\/ASIN\/4797318422\/249-6456194-5450768\">Java\u30b2\u30fc\u30e0\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3000\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3068\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af \u9577\u4e45 \u52dd (\u8457)<\/a>\u300d\u306b\u66f8\u3044\u3066\u3042\u308b\u3082\u306e\u3068\u540c\u3058\u3060\u304c\u3001\u58c1\u30af\u30e9\u30b9\u3092\u4f5c\u3063\u305f\u308a\u3001View\u3068\u306a\u308b\u30af\u30e9\u30b9\u306e\u4ed5\u4e8b\u3092\u5897\u3084\u3057\u305f\u308a\u3057\u3066\u3001\u3082\u3046\u5c11\u3057\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u6307\u5411\u3089\u3057\u304f\u3057\u3066\u307f\u305f\u3002\u30b3\u30e1\u30f3\u30c8\u306f\u66f8\u3044\u3066\u3044\u306a\u3044\u306e\u3067\u3059\u304c\u3001\u3084\u3063\u3066\u3044\u308b\u3053\u3068\u306f\u5927\u4f53\u308f\u304b\u308b\u3067\u3057\u3087\u3046\u3002<\/p>\n<p><!--more--><\/p>\n<div class=\"code\">package org.sssg.four.game;<br \/>\nimport java.applet.Applet;<br \/>\nimport java.awt.AWTEvent;<br \/>\nimport java.awt.TextArea;<br \/>\nimport java.awt.event.KeyEvent;<\/p>\n<p>public class Race extends Applet implements Runnable {<br \/>\n\tprivate int fieldWidth = 38;<br \/>\n\tprivate int carX = fieldWidth \/ 2;<br \/>\n\tprivate GameField field;<br \/>\n\tprivate Wall wall = new Wall();<br \/>\n\tprivate volatile Thread runner;<\/p>\n<p>\tclass Wall {<br \/>\n\t\tprivate int left = 14;<br \/>\n\t\tprivate int length = 10;<br \/>\n\t\tint getLeft() {<br \/>\n\t\t\treturn left;<br \/>\n\t\t}<br \/>\n\t\tint getRight() {<br \/>\n\t\t\treturn left+length;<br \/>\n\t\t}<br \/>\n\t\tvoid setLeft(int left) {<br \/>\n\t\t\tthis.left = left;<br \/>\n\t\t}<br \/>\n\t\tvoid setLength(int length) {<br \/>\n\t\t\tthis.length = length;<br \/>\n\t\t}<br \/>\n\t\tvoid left() {<br \/>\n\t\t\tif (left &gt; 0) {<br \/>\n\t\t\t\tleft&#8211;;<br \/>\n\t\t\t}<br \/>\n\t\t}<br \/>\n\t\tvoid right() {<br \/>\n\t\t\tif (left+length &lt; fieldWidth) {<br \/>\n\t\t\t\tleft++;<br \/>\n\t\t\t}<br \/>\n\t\t}<br \/>\n\t\tboolean isIntersect(int x) {<br \/>\n\t\t\treturn getLeft() &lt; x &#038;&#038; x &lt; getRight();<br \/>\n\t\t}<br \/>\n\t}<\/p>\n<p>\tclass GameField extends TextArea {<br \/>\n\t\tpublic GameField(int width, int height) {<br \/>\n\t\t\tsuper(&#8220;&#8221;, height, width, TextArea.SCROLLBARS_NONE);<br \/>\n\t\t\tenableEvents(AWTEvent.KEY_EVENT_MASK);<br \/>\n\t\t\tsetEditable(false);<br \/>\n\t\t}<\/p>\n<p>\t\tpublic void processKeyEvent(KeyEvent e) {<br \/>\n\t\t\tif (e.getID() != KeyEvent.KEY_PRESSED) {<br \/>\n\t\t\t\treturn;<br \/>\n\t\t\t}<br \/>\n\t\t\tswitch (e.getKeyCode()) {<br \/>\n\t\t\t\tcase KeyEvent.VK_LEFT :<br \/>\n\t\t\t\t\tif (carX &gt; 0) {<br \/>\n\t\t\t\t\t\tcarX&#8211;;<br \/>\n\t\t\t\t\t}<br \/>\n\t\t\t\t\tbreak;<br \/>\n\t\t\t\tcase KeyEvent.VK_RIGHT :<br \/>\n\t\t\t\t\tif (carX &lt; 38) {<br \/>\n\t\t\t\t\t\tcarX++;<br \/>\n\t\t\t\t\t}<br \/>\n\t\t\t\t\tbreak;<br \/>\n\t\t\t}<br \/>\n\t\t}<\/p>\n<p>\t\tprivate String WALL = &#8220;#&#8221;;<br \/>\n\t\tprivate String CAR  = &#8220;*&#8221;;<br \/>\n\t\tprivate String LOAD = &#8221; &#8220;;<br \/>\n\t\tpublic void draw() {<br \/>\n\t\t\tString str;<br \/>\n\t\t\tstr = &#8220;&#8221;;<br \/>\n\t\t\tfor (int i = 0; i &lt; 40; i++) {<br \/>\n\t\t\t\tif (i == wall.getLeft() || i == wall.getRight()) {<br \/>\n\t\t\t\t\tstr += WALL;<br \/>\n\t\t\t\t}<br \/>\n\t\t\t\telse if (i == carX) {<br \/>\n\t\t\t\t\tstr += CAR;<br \/>\n\t\t\t\t}<br \/>\n\t\t\t\telse {<br \/>\n\t\t\t\t\tstr += LOAD;<br \/>\n\t\t\t\t}<br \/>\n\t\t\t}<br \/>\n\t\t\tstr += &#8220;\\n&#8221;;<br \/>\n\t\t\tfield.insert(str, 0);<br \/>\n\t\t}<\/p>\n<p>\t\tpublic void drawGameOver(int score) {<br \/>\n\t\t\tinsert(&#8220;Crash!!\\n&#8221;, 0);<br \/>\n\t\t\tinsert(&#8220;Score : &#8221; + score + &#8220;\\n&#8221;, 0);<br \/>\n\t\t}<br \/>\n\t}<\/p>\n<p>\tpublic void init() {<br \/>\n\t\tfield = new GameField(35, 10);<br \/>\n\t\tadd(field);<br \/>\n\t}<\/p>\n<p>\tpublic void start() {<br \/>\n\t\tif (runner == null) {<br \/>\n\t\t\trunner = new Thread(this);<br \/>\n\t\t}<br \/>\n\t\trunner.start();<br \/>\n\t}<\/p>\n<p>\tpublic void stop() {<br \/>\n\t\trunner = null;<br \/>\n\t}<\/p>\n<p>\tpublic void run() {<br \/>\n\t\tint speed = 200;<br \/>\n\t\tint score = 0;<br \/>\n\t\tThread current = Thread.currentThread();<br \/>\n\t\tfield.requestFocus();<\/p>\n<p>\t\twhile (runner == current) {<br \/>\n\t\t\tif (isGameOver(carX)) {<br \/>\n\t\t\t\tfield.drawGameOver(score);<br \/>\n\t\t\t\tbreak;<br \/>\n\t\t\t}<br \/>\n\t\t\telse {<br \/>\n\t\t\t\tfield.draw();<br \/>\n\t\t\t}<\/p>\n<p>\t\t\tif ((int) (Math.random() * 2.) % 2 == 0) {<br \/>\n\t\t\t\twall.left();<br \/>\n\t\t\t} else {<br \/>\n\t\t\t\twall.right();<br \/>\n\t\t\t}<\/p>\n<p>\t\t\tif (speed &gt; 100) {<br \/>\n\t\t\t\tspeed&#8211;;<br \/>\n\t\t\t}<br \/>\n\t\t\tscore++;<br \/>\n\t\t\ttry {<br \/>\n\t\t\t\tThread.sleep(speed);<br \/>\n\t\t\t} catch (Exception e) {<br \/>\n\t\t\t}<br \/>\n\t\t}<br \/>\n\t}<\/p>\n<p>\tpublic boolean isGameOver(int x) {<br \/>\n\t\treturn !wall.isIntersect(x);<br \/>\n\t}<br \/>\n}<\/p><\/div>\n<p>\n\u30a2\u30d7\u30ec\u30c3\u30c8\u306a\u306e\u3067\u3001\u52d5\u4f5c\u3055\u305b\u308b\u306b\u306f HTML\u30d5\u30a1\u30a4\u30eb\u304c\u5fc5\u8981\u3067\u3059\u3002\n<\/p>\n<div class=\"code\">&lt;html&gt;<br \/>\n&lt;head&gt;<br \/>\n&lt;title&gt;Race&lt;\/title&gt;<br \/>\n&lt;\/head&gt;<br \/>\n&lt;applet code=&#8221;org.sssg.four.game.Race.class&#8221; width=&#8221;300&#8243; height=&#8221;180&#8243;&gt;<br \/>\n&lt;\/applet&gt;<br \/>\n&lt;\/html&gt;<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u3055\u3063\u305d\u304f\u3001\u30ec\u30fc\u30b9\u30b2\u30fc\u30e0\u3092\u4f5c\u3063\u3066\u307f\u305f\u3002\u57fa\u672c\u7684\u306b\u306f\u300cJava\u30b2\u30fc\u30e0\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3000\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0\u3068\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af \u9577\u4e45 \u52dd (\u8457)\u300d\u306b\u66f8\u3044\u3066\u3042\u308b\u3082\u306e\u3068\u540c\u3058\u3060\u304c\u3001\u58c1\u30af\u30e9\u30b9\u3092\u4f5c\u3063\u305f\u308a\u3001View\u3068\u306a\u308b\u30af\u30e9\u30b9\u306e\u4ed5\u4e8b\u3092\u5897\u3084\u3057\u305f\u308a\u3057\u3066\u3001\u3082 &hellip; <a href=\"https:\/\/www.hiro345.net\/blogs\/hiro345\/archives\/614.html\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-614","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/posts\/614","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/comments?post=614"}],"version-history":[{"count":0,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/posts\/614\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/media?parent=614"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/categories?post=614"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hiro345.net\/blogs\/hiro345\/wp-json\/wp\/v2\/tags?post=614"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}