{"id":1040,"date":"2025-02-12T00:41:13","date_gmt":"2025-02-12T00:41:13","guid":{"rendered":"http:\/\/gameengines.net\/wp\/?p=1040"},"modified":"2025-02-12T00:41:13","modified_gmt":"2025-02-12T00:41:13","slug":"console-based-java-game-code-running-on-onlinegdb","status":"publish","type":"post","link":"https:\/\/gameengines.net\/wp\/?p=1040","title":{"rendered":"Console based Java game code running on onlinegdb"},"content":{"rendered":"<p>Here&#8217;s a simple shoot-&#8217;em-up game in Java that you can run on OnlineGDB. Since OnlineGDB supports text-based Java programs and does not have graphical support, the game will be console-based.<\/p>\n<p>The player controls a spaceship (^) that can move left and right while shooting (|) at incoming enemies (X). The game ends when an enemy reaches the bottom:<\/p>\n<p>The game is displayed on a 10&#215;20 grid.<br \/>\nThe player can move left (a) and right (d) and shoot (w).<br \/>\nEnemies (X) fall from the top.<br \/>\nThe game ends when an enemy reaches the bottom or the player shoots all enemies.<\/p>\n<p>import java.util.Scanner;<br \/>\nimport java.util.Random;<\/p>\n<p>public class Main {<br \/>\nstatic final int WIDTH = 20;<br \/>\nstatic final int HEIGHT = 10;<br \/>\nstatic char[][] grid = new char[HEIGHT][WIDTH];<br \/>\nstatic int playerX = WIDTH \/ 2;<br \/>\nstatic int bulletY = -1;<br \/>\nstatic int bulletX = -1;<br \/>\nstatic int enemyX;<br \/>\nstatic int enemyY = 0;<br \/>\nstatic boolean gameOver = false;<\/p>\n<p>public static void main(String[] args) {<br \/>\nScanner scanner = new Scanner(System.in);<br \/>\nRandom random = new Random();<br \/>\nenemyX = random.nextInt(WIDTH);<\/p>\n<p>while (!gameOver) {<br \/>\nupdateGrid();<br \/>\nprintGrid();<br \/>\nSystem.out.print(&#8220;Move (a\/d) or Shoot (w): &#8220;);<br \/>\nchar move = scanner.next().charAt(0);<br \/>\nhandleInput(move);<br \/>\nmoveEnemy();<br \/>\ncheckCollision();<br \/>\n}<\/p>\n<p>System.out.println(&#8220;Game Over!&#8221;);<br \/>\n}<\/p>\n<p>static void updateGrid() {<br \/>\nfor (int i = 0; i &lt; HEIGHT; i++)<br \/>\nfor (int j = 0; j &lt; WIDTH; j++)<br \/>\ngrid[i][j] = &#8216; &#8216;;<\/p>\n<p>if (bulletY &gt;= 0) grid[bulletY][bulletX] = &#8216;|&#8217;;<br \/>\ngrid[enemyY][enemyX] = &#8216;X&#8217;;<br \/>\ngrid[HEIGHT &#8211; 1][playerX] = &#8216;^&#8217;;<br \/>\n}<\/p>\n<p>static void printGrid() {<br \/>\nfor (char[] row : grid) {<br \/>\nfor (char cell : row) System.out.print(cell);<br \/>\nSystem.out.println();<br \/>\n}<br \/>\n}<\/p>\n<p>static void handleInput(char move) {<br \/>\nif (move == &#8216;a&#8217; &amp;&amp; playerX &gt; 0) playerX&#8211;;<br \/>\nif (move == &#8216;d&#8217; &amp;&amp; playerX &lt; WIDTH &#8211; 1) playerX++;<br \/>\nif (move == &#8216;w&#8217; &amp;&amp; bulletY == -1) {<br \/>\nbulletX = playerX;<br \/>\nbulletY = HEIGHT &#8211; 2;<br \/>\n}<br \/>\nmoveBullet();<br \/>\n}<\/p>\n<p>static void moveBullet() {<br \/>\nif (bulletY &gt;= 0) bulletY&#8211;;<br \/>\nif (bulletY &lt; 0) bulletX = -1;<br \/>\n}<\/p>\n<p>static void moveEnemy() {<br \/>\nenemyY++;<br \/>\nif (enemyY &gt;= HEIGHT &#8211; 1) gameOver = true;<br \/>\n}<\/p>\n<p>static void checkCollision() {<br \/>\nif (bulletY == enemyY &amp;&amp; bulletX == enemyX) {<br \/>\nSystem.out.println(&#8220;Enemy Hit!&#8221;);<br \/>\nenemyY = 0;<br \/>\nenemyX = new Random().nextInt(WIDTH);<br \/>\nbulletY = -1;<br \/>\n}<br \/>\n}<br \/>\n}<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_1040\" class=\"pvc_stats all  \" data-element-id=\"1040\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/gameengines.net\/wp\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s a simple shoot-&#8217;em-up game in Java that you can run on OnlineGDB. Since OnlineGDB supports text-based Java programs and does not have graphical support, the game will be console-based. The player controls a spaceship (^) that can move left and right while shooting (|) at incoming enemies (X). The game ends when an enemy [&hellip;]<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_1040\" class=\"pvc_stats all  \" data-element-id=\"1040\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/gameengines.net\/wp\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":1041,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1040","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/gameengines.net\/wp\/index.php?rest_route=\/wp\/v2\/posts\/1040"}],"collection":[{"href":"https:\/\/gameengines.net\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gameengines.net\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gameengines.net\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gameengines.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1040"}],"version-history":[{"count":1,"href":"https:\/\/gameengines.net\/wp\/index.php?rest_route=\/wp\/v2\/posts\/1040\/revisions"}],"predecessor-version":[{"id":1042,"href":"https:\/\/gameengines.net\/wp\/index.php?rest_route=\/wp\/v2\/posts\/1040\/revisions\/1042"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gameengines.net\/wp\/index.php?rest_route=\/wp\/v2\/media\/1041"}],"wp:attachment":[{"href":"https:\/\/gameengines.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1040"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gameengines.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1040"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gameengines.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1040"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}