java编程一个最简单游戏代码(java经典游戏)

快三技巧准确率100

由于此地板涉嫌违规,因此它将折叠在系统中以隐藏此地板并显示此地板

import java.awt.*; import java.util.LinkedList; import java.util.Scanner;/* * @ author Aachen0* @ date 2018/3/2713:56 * ide : intellij idea */publicclasssnakegame { staticfinalintwidth=ste publicstaticvoidmain (字符串[ ] args ) snakegamesnakegame=newsnakegame ); snakeGame.initBackground (; //初始化背景,虫snakelinesnakeline=newsnakeline (); snakeLine.initSnake (; //蛇snake game.putsnakeinmap (snake line )初始化snakeGame.show (; //显示//用键盘移动蛇游戏scanner scanner=new scanner (system.in ); int move; 输入while(true ) { System.out.println ) AWSD以控制蛇的运动,q退出游戏) ); String choice=scanner.next (; sitch(choice ) { case ‘ a ‘ : case ‘ a ‘ : move=2; 布雷克; case ‘s’: case ‘S’: move=1; 布雷克; case ‘w’: case ‘W’: move=3; 布雷克; case ‘d’: case ‘D’: move=0; 布雷克; case ‘ q ‘ : case ‘ q ‘ : int points=snake line.snake points.size (; snake game.putgameoverinmap (points; default: System.out.println (‘输入错误。 请重试); 继续; }if(snakeline.move(move )=-1 ) (snakegame.putgameoverinmap ) snakeline.snakepoints.size ) ); snakeGame.show (; 布雷克; } snake game.putsnakeinmap (snake line ); snakeGame.show (; }//用文字绘制背景的private void initBackground () for ) intI=0; i HEIGHT; I () /外围控制行for ) intj=0; j WIDTH; j () /内循环控制每行的第几个this.map[I][j]=(j==0||) j==width-1 )|I==0||(I==height-1 )?’ *’ : ‘; }//背景显示公共语音显示() { int height=map.length; int width=map[0].length; for(intI=0; i height; I ) for(intj=0; j width; j ) {system.out.print(map[I] ); } System.out.println (; }//地图voidputsnakeinmap (snakelinesnakeline ) { Point p; this.initBackground (; map [ snake line.food.y ] [ snake line.food.x ]=snake line.worm; for(intI=0; i snakeLine.snakePoints.size (; I () p=snakeline.snakepoints.get ) I; if (p.y0p.y height-1p.x0p.x width-1 ) ) map[p.y][p.x]=(I==0)? snake line.head : snake line.body; } else { putgameoverinmap (snake line.snake points.size (); } } voidputgameoverinmap (int points ) char [ ] game over=(game over score : () points-3 ) ).toCharArray ); for(intI=0; i gameOver.length; I ) {map(height/2-1 ) ) I ) width-gameover.length )/2 )=gameover ) I ); (} show ); 系统. exit (1; } classsnakeline { staticfinalintright=0,DOWN=1,LEFT=2,UP=3; 静态文件头=’ o ‘,body=’o ‘,worm=’~’; //头部和身体为staticpointfood=newpoint () int ) ) Math.random ) * (SnakeGame.WIDTH – 2 ) ) 1,() int ) ) Math.random privatevoidnewfood ((food=new point ) ) int ) Math.random ) * (SnakeGame.WIDTH – 2 ) ) 1,() int ) ) Math.random //蛇身内容void initSnake () pointhead=newpoint ) snakegame.width/2,SnakeGame.HEIGHT/2 ); snakepoints.addfirst(head; //头snakepoints.addlast(newpoint ) head.x-1,head.y ); snakepoints.addlast(newpoint ) head.x-2,head.y ); }intmove(intorient ) { Point p=snakePoints.getFirst ); 点NP=null; 交换机(orient ) ) casesnakeline.right : NP=new point (p.x1,p.y ); 布雷克; casesnakeline.left : NP=new point (p.x-1,p.y ); 布雷克; casesnakeline.down : NP=new point (p.x,p.y 1 ); 布雷克; casesnakeline.up : NP=new point (p.x,p.y – 1 ); 布雷克; (if ) snakepoints.contains(NP ) ) { return -1; //咬住了自己}snakepoints.addfirst(NP ); if(NP.equals(food ) ) /吃了食物newFood ); 返回2; } snakePoints.removeLast (; 返回1; }

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注