java实现好玩的代码 java实现好玩的代码是什么

跪地求好玩的JAVA 源代码~连连看java源代码
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class lianliankan implements ActionListener
{
JFrame mainFrame; //主面板
Container thisContainer;
JPanel centerPanel,southPanel,northPanel; //子面板
JButton diamondsButton[][] = new JButton[6][5];//游戏按钮数组
JButton exitButton,resetButton,newlyButton; //退出 , 重列 , 重新开始按钮
JLabel fractionLable=new JLabel("0"); //分数标签
JButton firstButton,secondButton; //分别记录两次被选中java实现好玩的代码的按钮
int grid[][] = new int[8][7];//储存游戏按钮位置
static boolean pressInformation=false; //判断是否有按钮被选中
int x0=0,y0=0,x=0,y=0,fristMsg=0,secondMsg=0,validateLV; //游戏按钮的位置坐标
int i,j,k,n;//消除方法控制
public void init(){
mainFrame=new JFrame("JKJ连连看");
thisContainer = mainFrame.getContentPane();
thisContainer.setLayout(new BorderLayout());
centerPanel=new JPanel();
southPanel=new JPanel();
northPanel=new JPanel();
thisContainer.add(centerPanel,"Center");
thisContainer.add(southPanel,"South");
thisContainer.add(northPanel,"North");
centerPanel.setLayout(new GridLayout(6,5));
for(int cols = 0;cols6;cols){
for(int rows = 0;rows5;rows){
diamondsButton[cols][rows]=new JButton(String.valueOf(grid[cols 1][rows 1]));
diamondsButton[cols][rows].addActionListener(this);
centerPanel.add(diamondsButton[cols][rows]);
}
}
exitButton=new JButton("退出");
exitButton.addActionListener(this);
resetButton=new JButton("重列");
resetButton.addActionListener(this);
newlyButton=new JButton("再来一局");
newlyButton.addActionListener(this);
southPanel.add(exitButton);
southPanel.add(resetButton);
southPanel.add(newlyButton);
fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText())));
northPanel.add(fractionLable);
mainFrame.setBounds(280,100,500,450);
mainFrame.setVisible(true);
}
public void randomBuild() {
int randoms,cols,rows;
for(int twins=1;twins=15;twins) {
randoms=(int)(Math.random()*25 1);
for(int alike=1;alike=2;alike) {
cols=(int)(Math.random()*6 1);
rows=(int)(Math.random()*5 1);
while(grid[cols][rows]!=0) {
cols=(int)(Math.random()*6 1);
rows=(int)(Math.random()*5 1);
}
this.grid[cols][rows]=randoms;
}
}
}
public void fraction(){
fractionLable.setText(String.valueOf(Integer.parseInt(fractionLable.getText()) 100));
}
public void reload() {
int save[] = new int[30];
int n=0,cols,rows;
int grid[][]= new int[8][7];
for(int i=0;i=6;i) {
for(int j=0;j=5;j) {
if(this.grid[i][j]!=0) {
save[n]=this.grid[i][j];
n;
}
}
}
n=n-1;
this.grid=grid;
while(n=0) {
cols=(int)(Math.random()*6 1);
rows=(int)(Math.random()*5 1);
while(grid[cols][rows]!=0) {
cols=(int)(Math.random()*6 1);
rows=(int)(Math.random()*5 1);
}
this.grid[cols][rows]=save[n];
n--;
}
mainFrame.setVisible(false);
pressInformation=false; //这里一定要将按钮点击信息归为初始
init();
for(int i = 0;i6;i){
for(int j = 0;j5;j){
if(grid[i 1][j 1]==0)
diamondsButton[i][j].setVisible(false);
}
}
}
public void estimateEven(int placeX,int placeY,JButton bz) {
if(pressInformation==false) {
x=placeX;
y=placeY;
secondMsg=grid[x][y];
secondButton=bz;
pressInformation=true;
}
else {
x0=x;
y0=y;
fristMsg=secondMsg;
firstButton=secondButton;
x=placeX;
y=placeY;
secondMsg=grid[x][y];
secondButton=bz;
if(fristMsg==secondMsgsecondButton!=firstButton){
xiao();
}
}
}
public void xiao() { //相同的情况下能不能消去 。仔细分析 , 不一条条注释
if((x0==x (y0==y 1||y0==y-1)) || ((x0==x 1||x0==x-1)(y0==y))){ //判断是否相邻
remove();
}
else{
for (j=0;j7;j) {
if (grid[x0][j]==0){ //判断第一个按钮同行哪个按钮为空
if (yj) { //如果第二个按钮的Y坐标大于空按钮的Y坐标说明第一按钮在第二按钮左边
for (i=y-1;i=j;i-- ){ //判断第二按钮左侧直到第一按钮中间有没有按钮
if (grid[x][i]!=0) {
k=0;
break;
}
else{ k=1; } //K=1说明通过了第一次验证
}
if (k==1) {
linePassOne();
}
}
if (yj){ //如果第二个按钮的Y坐标小于空按钮的Y坐标说明第一按钮在第二按钮右边
for (i=y 1;i=j ;i){ //判断第二按钮左侧直到第一按钮中间有没有按钮
if (grid[x][i]!=0){
k=0;
break;
}
else { k=1; }
}
if (k==1){
linePassOne();
}
}
if (y==j ) {
linePassOne();
}
}
if (k==2) {
if (x0==x) {
remove();
}
if (x0x) {
for (n=x0;n=x-1;n) {
if (grid[n][j]!=0) {
k=0;
break;
}
if(grid[n][j]==0n==x-1) {
remove();
}
}
}
if (x0x) {
for (n=x0;n=x 1 ;n-- ) {
if (grid[n][j]!=0) {
k=0;
break;
}
if(grid[n][j]==0n==x 1) {
remove();
}
}
}
}
}
for (i=0;i8;i) { //列
if (grid[i][y0]==0) {
if (xi) {
for (j=x-1;j=i ;j-- ) {
if (grid[j][y]!=0) {
k=0;
break;
}
else { k=1; }
}
if (k==1) {
rowPassOne();
}
}
if (xi) {
for (j=x 1;j=i;j) {
if (grid[j][y]!=0) {
k=0;
break;
}
else { k=1; }
}
if (k==1) {
rowPassOne();
}
}
if (x==i) {
rowPassOne();
}
}
if (k==2){
if (y0==y) {
remove();
}
if (y0y) {
for (n=y0;n=y-1 ;n) {
if (grid[i][n]!=0) {
k=0;
break;
}
if(grid[i][n]==0n==y-1) {
remove();
}
}
}
if (y0y) {
for (n=y0;n=y 1 ;n--) {
if (grid[i][n]!=0) {
k=0;
break;
}
if(grid[i][n]==0n==y 1) {
remove();
}
}
}
}
}
}
}
public void linePassOne(){
if (y0j){ //第一按钮同行空按钮在左边
for (i=y0-1;i=j ;i-- ){ //判断第一按钮同左侧空按钮之间有没按钮
if (grid[x0][i]!=0) {
k=0;
break;
}
else { k=2; } //K=2说明通过了第二次验证
}
}
if (y0j){ //第一按钮同行空按钮在与第二按钮之间
for (i=y0 1;i=j ;i){
if (grid[x0][i]!=0) {
k=0;
break;
}
else{ k=2; }
}
}
}
public void rowPassOne(){
if (x0i) {
for (j=x0-1;j=i ;j-- ) {
if (grid[j][y0]!=0) {
k=0;
break;
}
else { k=2; }
}
}
if (x0i) {
for (j=x0 1;j=i ;j) {
if (grid[j][y0]!=0) {
k=0;
break;
}
else { k=2; }
}
}
}
public void remove(){
firstButton.setVisible(false);
secondButton.setVisible(false);
fraction();
pressInformation=false;
k=0;
grid[x0][y0]=0;
grid[x][y]=0;
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==newlyButton){
int grid[][] = new int[8][7];
this.grid = grid;
randomBuild();
mainFrame.setVisible(false);
pressInformation=false;
init();
}
if(e.getSource()==exitButton)
System.exit(0);
if(e.getSource()==resetButton)
reload();
for(int cols = 0;cols6;cols){
for(int rows = 0;rows5;rows){
if(e.getSource()==diamondsButton[cols][rows])
estimateEven(cols 1,rows 1,diamondsButton[cols][rows]);
}
}
}
public static void main(String[] args) {
lianliankan llk = new lianliankan();
llk.randomBuild();
llk.init();
}
}
//old 998 lines
//new 318 lines
基于JAVA的3D坦克游戏源代码
JAVA猜数字小游戏源代码
/*1、编写一个猜数字的游戏 , 由电脑随机产生一个100以内的整数,让用户去猜,如果用户猜的比电脑大,则输出“大了,再小点!” , 反之则输出“小了 , 再大点!”,用户总共只能猜十次 , 并根据用户正确猜出答案所用的次数输出相应的信息,如java实现好玩的代码:只用一次就猜对,输出“java实现好玩的代码你是个天才!”,八次才猜对,输出“笨死了!”,如果十次还没有猜对,则游戏结束!*/
import java.util.*;
import java.io.*;
public class CaiShu{
public static void main(String[] args) throws IOException{
Random a=new Random();
int num=a.nextInt(100);
System.out.println("请输入一个100以内的整数:");
for (int i=0;i=9;i){
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
String str=bf.readLine();
int shu=Integer.parseInt(str);
if (shunum)
System.out.println("输入的数大了,输小点的!");
else if (shunum)
System.out.println("输入的数小了,输大点的!");
else {
System.out.println("恭喜你 , 猜对了!");
if (i=2)
System.out.println("你真是个天才!");
else if (i=6)
System.out.println("还将就 , 你过关了!");
else if (i=8)
System.out.println("但是你还……真笨!");
else
System.out.println("你和猪没有两样了!");
break;}
}
}
}
免费可以玩大游戏的代码本文实例为大家分享了Java实现方块赛跑小游戏的具体代码,供大家参考 , 具体内容如下
在一个图形界面上构造两个位于同一起跑线方块,起跑线位于界面靠左位置 , A 方块先开始运动,向右移动 50 像素后停止,B 方块开始运动,向右移动 100 像素后停 止,A 方块继续向右运动 100 像素后停止,B 方块开始运动,如此循环接替执行,直至 某一个方块到达终点,界面显示该方块胜利信息 。
1) 自定义一个threadA,ThreadB, ThreadFrame类(均继承自Thread) 。
2) 定义全局变量,方块的位置 , 总长度 , 冠军,睡眠时间等,布尔值方块等待变量、游戏继续变量、绘图变量
3) ThreadA(ThreadB):等待waitA(waitB)变量释放,即:等待另一个方块更新完位置;然后随机产生要移动的长度,检查运动后位置与总长度的关系,以此判断游戏是否结束 。更新位置信息,更改绘图变量,通知绘图线程重绘 。自锁本身,释放另一个方块线程 。
4) ThreadFrame:创建类对象,重写run函数,等待绘图变量的命令 。接到绘图命令,重绘 , 判断游戏释放结束 , 重置绘图命令 。
5) 构造函数,paint函数绘制,并进行游戏是否结束的判断,若结束,则打印冠军是谁 , 退出
6) 主函数,定义threadA,ThreadB, ThreadFrame类的对象 , 运行 。用jion函数等待子线程的结束 。
import java.awt.*;
import javax.swing.*;
public class four3 extends JFrame {
// 全局变量
static int positionA = 50, positionB = 50, distanceAll = 1600;
static int RecWidth = 50, RecHeight = 50;
static char winner;
static long sleeptime = 300;
static boolean waitA = true, waitB = true, gaming = true, unrepaint = true;
//构造函数
public four3() {
setTitle("多线程:方块赛跑");
setBackground(Color.WHITE);
setSize(1600, 500);
setLocation(0, 200);
setResizable(false);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
//绘图函数
public void paint(Graphics g) {
// TODO Auto-generated method stub
g.clearRect(0, 0, 1600, 900);
g.setColor(Color.RED);
g.fillRect(positionA - 50, 100, RecWidth, RecHeight);
g.setColor(Color.BLUE);
g.fillRect(positionB - 50, 300, RecWidth, RecHeight);
if (!gaming) {
g.setFont(new Font("宋体", ALLBITS, 50));
if (winner == 'A') {
g.setColor(Color.RED);
g.drawString(new String("Winner Is The Red One!"), 550, 250);
} else if (winner == 'B') {
g.setColor(Color.BLUE);
g.drawString(new String("Winner Is The Blue One!"), 550, 250);
}
}
}
public static void main(String[] args) {
waitA = false;
waitB = true;
unrepaint = false;
threadframe tf = new threadframe();
threadA tA = new threadA();
threadB tB = new threadB();
tf.start();
tA.start();
tB.start();
try {
tf.join();
tA.join();
tB.join();
} catch (Exception e) {
// TODO: handle exception
}
return;
}
//红色方块线程
public static class threadA extends Thread {
public void run() {
while (gaming) {
while (waitA) {
if (!gaming)return;
System.out.print("");
}
try {
sleep(sleeptime);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int distance = (int) (Math.random() * 100000) % 100;
positionA= distance;
if (positionA = distanceAll) {
positionA = distanceAll;
unrepaint = false;
gaming = false;
winner = 'A';
}
unrepaint = false;
waitA = true;
waitB = false;
}
}
}
//蓝色方块线程
public static class threadB extends Thread {
public void run() {
while (gaming) {
while (waitB) {
if (!gaming)return;
System.out.print("");
}
try {
sleep(sleeptime);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int distance = (int) (Math.random() * 100000) % 100;
positionB= distance;
if (positionB = distanceAll) {
positionB = distanceAll;
unrepaint = false;
gaming = false;
winner = 'B';
}
unrepaint = false;
waitB = true;
waitA = false;
}
}
}
//框架刷新线程
public static class threadframe extends Thread {
four3 jiemian = new four3();
public void run() {
while (gaming) {
while (unrepaint) {
if (!gaming)return;
System.out.print("");
}
jiemian.repaint();
unrepaint = true;
}
}
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助 , 也希望大家多多支持 。
求一个简单又有趣的JAVA小游戏代码具体如下:
连连看的小源码
package Lianliankan;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class lianliankan implements ActionListener
{
JFrame mainFrame; //主面板
Container thisContainer;
JPanel centerPanel,southPanel,northPanel; //子面板
JButton diamondsButton[][] = new JButton[6][5];//游戏按钮数组
JButton exitButton,resetButton,newlyButton; //退出 , 重列,重新开始按钮
JLabel fractionLable=new JLabel("0"); //分数标签
JButton firstButton,secondButton; //
分别记录两次62616964757a686964616fe59b9ee7ad9431333335326239被选中的按钮
int grid[][] = new int[8][7];//储存游戏按钮位置
static boolean pressInformation=false; //判断是否有按钮被选中
int x0=0,y0=0,x=0,y=0,fristMsg=0,secondMsg=0,validateLV; //游戏按钮的位置坐标
int i,j,k,n;//消除方法控制
代码(code)是程序员用开发工具所支持的语言写出来的源文件,是一组由字符、符号或信号码元以离散形式表示信息的明确的规则体系 。
对于字符和Unicode数据的位模式的定义,此模式代表特定字母、数字或符号(例如 0x20 代表一个空格 , 而 0x74 代表字符“t”) 。一些数据类型每个字符使用一个字节;每个字节可以具有 256 个不同的位模式中的一个模式 。
在计算机中,字符由不同的位模式(ON 或 OFF)表示 。每个字节有 8 位,这 8 位可以有 256 种不同的 ON 和 OFF 组合模式 。对于使用 1 个字节存储每个字符的程序,通过给每个位模式指派字符可表示最多 256 个不同的字符 。2 个字节有 16 位,这 16 位可以有 65,536 种唯一的 ON 和 OFF 组合模式 。使用 2 个字节表示每个字符的程序可表示最多 65,536 个字符 。
单字节代码页是字符定义,这些字符映射到每个字节可能有的 256 种位模式中的每一种 。代码页定义大小写字符、数字、符号以及 !、@、#、% 等特殊字符的位模式 。每种欧洲语言(如德语和西班牙语)都有各自的单字节代码页 。
虽然用于表示 A 到 Z 拉丁字母表字符的位模式在所有的代码页中都相同,但用于表示重音字符(如"é"和"á")的位模式在不同的代码页中却不同 。如果在运行不同代码页的计算机间交换数据 , 必须将所有字符数据由发送计算机的代码页转换为接收计算机的代码页 。如果源数据中的扩展字符在接收计算机的代码页中未定义,那么数据将丢失 。
如果某个数据库为来自许多不同国家的客户端提供服务 , 则很难为该数据库选择这样一种代码页,使其包括所有客户端计算机所需的全部扩展字符 。而且,在代码页间不停地转换需要花费大量的处理时间 。
求java小游戏源代码表1. CheckerDrag.java
// CheckerDrag.javaimport java.awt.*;import java.awt.event.*;public class CheckerDrag extends java.applet.Applet{// Dimension of checkerboard square.// 棋盘上每个小方格的尺寸final static int SQUAREDIM = 40;// Dimension of checkerboard -- includes black outline.// 棋盘的尺寸 – 包括黑色的轮廓线final static int BOARDDIM = 8 * SQUAREDIM2;// Dimension of checker -- 3/4 the dimension of a square.// 棋子的尺寸 – 方格尺寸的3/4final static int CHECKERDIM = 3 * SQUAREDIM / 4;// Square colors are dark green or white.// 方格的颜色为深绿色或者白色final static Color darkGreen = new Color (0, 128, 0);// Dragging flag -- set to true when user presses mouse button over checker// and cleared to false when user releases mouse button.// 拖动标记 --当用户在棋子上按下鼠标按键时设为true,// 释放鼠标按键时设为falseboolean inDrag = false;// Left coordinate of checkerboard's upper-left corner.// 棋盘左上角的左方向坐标int boardx;// Top coordinate of checkerboard's upper-left corner.//棋盘左上角的上方向坐标int boardy;// Left coordinate of checker rectangle origin (upper-left corner).// 棋子矩形原点(左上角)的左方向坐标int ox;// Top coordinate of checker rectangle origin (upper-left corner).// 棋子矩形原点(左上角)的上方向坐标int oy;// Left displacement between mouse coordinates at time of press and checker// rectangle origin.// 在按键时的鼠标坐标与棋子矩形原点之间的左方向位移int relx;// Top displacement between mouse coordinates at time of press and checker// rectangle origin.// 在按键时的鼠标坐标与棋子矩形原点之间的上方向位移int rely;// Width of applet drawing area.// applet绘图区域的宽度int width;// Height of applet drawing area.// applet绘图区域的高度int height;// Image buffer.// 图像缓冲Image imBuffer;// Graphics context associated with image buffer.// 图像缓冲相关联的图形背景Graphics imG;public void init (){// Obtain the size of the applet's drawing area.// 获取applet绘图区域的尺寸width = getSize ().width;height = getSize ().height;// Create image buffer.// 创建图像缓冲imBuffer = createImage (width, height);// Retrieve graphics context associated with image buffer.// 取出图像缓冲相关联的图形背景imG = imBuffer.getGraphics ();// Initialize checkerboard's origin, so that board is centered.// 初始化棋盘的原点 , 使棋盘在屏幕上居中boardx = (width - BOARDDIM) / 21;boardy = (height - BOARDDIM) / 21;// Initialize checker's rectangle's starting origin so that checker is// centered in the square located in the top row and second column from// the left.// 初始化棋子矩形的起始原点 , 使得棋子在第一行左数第二列的方格里居中ox = boardxSQUAREDIM(SQUAREDIM - CHECKERDIM) / 21;oy = boardy(SQUAREDIM - CHECKERDIM) / 21;// Attach a mouse listener to the applet. That listener listens for// mouse-button press and mouse-button release events.// 向applet添加一个用来监听鼠标按键的按下和释放事件的鼠标监听器addMouseListener (new MouseAdapter (){public void mousePressed (MouseEvent e){// Obtain mouse coordinates at time of press.// 获取按键时的鼠标坐标int x = e.getX ();int y = e.getY ();// If mouse is over draggable checker at time// of press (i.e., contains (x, y) returns// true), save distance between current mouse// coordinates and draggable checker origin// (which will always be positive) and set drag// flag to true (to indicate drag in progress).// 在按键时如果鼠标位于可拖动的棋子上方// (也就是contains (x, y)返回true),则保存当前// 鼠标坐标与棋子的原点之间的距离(始终为正值)并且// 将拖动标志设为true(用来表明正处在拖动过程中)if (contains (x, y)){relx = x - ox;rely = y - oy;inDrag = true;}}boolean contains (int x, int y){// Calculate center of draggable checker.// 计算棋子的中心位置int cox = oxCHECKERDIM / 2;int coy = oyCHECKERDIM / 2;// Return true if (x, y) locates with bounds// of draggable checker. CHECKERDIM / 2 is the// radius.// 如果(x, y)仍处于棋子范围内则返回true// CHECKERDIM / 2为半径return (cox - x) * (cox - x)(coy - y) * (coy - y)CHECKERDIM / 2 * CHECKERDIM / 2;}public void mouseReleased (MouseEvent e){// When mouse is released, clear inDrag (to// indicate no drag in progress) if inDrag is// already set.// 当鼠标按键被释放时,如果inDrag已经为true,// 则将其置为false(用来表明不在拖动过程中)if (inDrag)inDrag = false;}});// Attach a mouse motion listener to the applet. That listener listens// for mouse drag events.//向applet添加一个用来监听鼠标拖动事件的鼠标运动监听器addMouseMotionListener (new MouseMotionAdapter (){public void mouseDragged (MouseEvent e){if (inDrag){// Calculate draggable checker's new// origin (the upper-left corner of// the checker rectangle).// 计算棋子新的原点(棋子矩形的左上角)int tmpox = e.getX () - relx;int tmpoy = e.getY () - rely;// If the checker is not being moved// (at least partly) off board,// assign the previously calculated// origin (tmpox, tmpoy) as the// permanent origin (ox, oy), and// redraw the display area (with the// draggable checker at the new// coordinates).// 如果棋子(至少是棋子的一部分)没有被// 移出棋盘,则将之前计算的原点// (tmpox, tmpoy)赋值给永久性的原点(ox, oy),// 并且刷新显示区域(此时的棋子已经位于新坐标上)if (tmpoxboardxtmpoyboardytmpoxCHECKERDIMboardxBOARDDIMtmpoyCHECKERDIMboardyBOARDDIM){ox = tmpox;oy = tmpoy;repaint ();}}}});}public void paint (Graphics g){// Paint the checkerboard over which the checker will be dragged.// 在棋子将要被拖动的位置上绘制棋盘paintCheckerBoard (imG, boardx, boardy);// Paint the checker that will be dragged.// 绘制即将被拖动的棋子paintChecker (imG, ox, oy);// Draw contents of image buffer.// 绘制图像缓冲的内容g.drawImage (imBuffer, 0, 0, this);}void paintChecker (Graphics g, int x, int y){// Set checker shadow color.// 设置棋子阴影的颜色g.setColor (Color.black);// Paint checker shadow.// 绘制棋子的阴影g.fillOval (x, y, CHECKERDIM, CHECKERDIM);// Set checker color.// 设置棋子颜色g.setColor (Color.red);// Paint checker.// 绘制棋子g.fillOval (x, y, CHECKERDIM - CHECKERDIM / 13, CHECKERDIM - CHECKERDIM / 13);}void paintCheckerBoard (Graphics g, int x, int y){// Paint checkerboard outline.// 绘制棋盘轮廓线g.setColor (Color.black);g.drawRect (x, y, 8 * SQUAREDIM1, 8 * SQUAREDIM1);// Paint checkerboard.// 绘制棋盘for (int row = 0; row8; row){g.setColor (((row1) != 0) ? darkGreen : Color.white);for (int col = 0; col8; col){g.fillRect (x1col * SQUAREDIM, y1row * SQUAREDIM,SQUAREDIM, SQUAREDIM);g.setColor ((g.getColor () == darkGreen) ? Color.white :darkGreen);}}}// The AWT invokes the update() method in response to the repaint() method// calls that are made as a checker is dragged. The default implementation// of this method, which is inherited from the Container class, clears the// applet's drawing area to the background color prior to calling paint().// This clearing followed by drawing causes flicker. CheckerDrag overrides// update() to prevent the background from being cleared, which eliminates// the flicker.// AWT调用了update()方法来响应拖动棋子时所调用的repaint()方法 。该方法从// Container类继承的默认实现会在调用paint()之前,将applet的绘图区域清除// 为背景色,这种绘制之后的清除就导致了闪烁 。CheckerDrag重写了update()来// 防止背景被清除,从而消除了闪烁 。public void update (Graphics g){paint (g);}}
用JAVA编写一个小程序,不限类型好玩点的 。控制台小游戏 class YZDEL
{
public static void main(String[] args)
{
java.util.Random Shiji = new java.util.Random();
java.util.Scanner Shuru = new java.util.Scanner(System.in);
java.util.Scanner Nandu = new java.util.Scanner(System.in);
int YingxHP = 20;
int YingxMP = 0;
int MogHP = 20;
int MogMP = 0;
int MogDo = 0;
int fangyu = 0;
System.out.println("┏━━━━╮┏━━┣┣┓ ╮ ┓┏━┳━┳┓┓━┓ ");
System.out.println("┏┣━━━┓┏━━┣┣┓ ┃┏ ┃ ┃┓┃ ╯ ");
System.out.println("┣┣━━━┫╭━━┻╯╮ ┃╰┓┃ ┃╯┏━┣━━┓");
System.out.println("┣┣━━━┫┏━━━━┓╭━━┣━┛┗━┻━┻┛┃┃");
System.out.println("┏┣━━━┓┣━━━━┫ ┃┃┃┗╮╮┃┃┣━╯");
System.out.println(" ┛┗╯┗━━━━╯┗╯╰╰━━┛┛┗━╯╰━┛");
System.out.println();
System.out.println("作者:天狐空幻");
System.out.println();
System.out.println("说明:勇者的操作方式为以下所示:");
System.out.println("使用攻击需消耗1MP 伤害1HP");
System.out.println("使用蓄力可增加1MP 伤害0HP");
System.out.println("使用躲闪需消耗0MP 伤害0HP躲避攻击");
System.out.println("使用重击需消耗3MP 伤害2HP防御无效");
System.out.println("恶龙攻击力高,注意防御是取胜的关键");
System.out.println("恶龙MP达到4时可能会放出火焰无法躲避");
System.out.println("准备说明完毕,那么让我们来挑战恶龙吧!");
System.out.println("==================================");
//难度选择
System.out.println("请选择难度");
System.out.println("1.娱乐 2.挑战 3.噩梦");
int ND = Nandu.nextInt();
System.out.println("==================================");
while(true)
{
//HP,MP的显示
System.out.print("勇者:");
System.out.print(" HP ");
for(int x=YingxHP;x0;x--) System.out.print("*");
System.out.print(" " YingxHP);
System.out.println();
System.out.print("");
System.out.print(" MP ");
for(int x=YingxMP;x0;x--) System.out.print("*");
System.out.print(" " YingxMP);
System.out.println();
System.out.print("恶龙:");
System.out.print(" HP ");
for(int y=MogHP;y0;y--) System.out.print("*");
System.out.print(" " MogHP);
System.out.println();
System.out.print("");
System.out.print(" MP ");
for(int y=MogMP;y0;y--) System.out.print("*");
System.out.print(" " MogMP);
System.out.println();
System.out.println("==================================");
//胜利判定
if(YingxHP1) {System.out.println();System.out.println("勇者HP为0! 不..不可能..我怎么会..勇者倒下了 。再接再厉吧!~");System.out.println();break;}
if(MogHP1) {System.out.println();System.out.println("恶龙HP为0! 恶龙绝望的哀鸣中倒了下去 。勇者胜利了 。恭喜你挑战成功?。?);System.out.println();break;}
//角色输入判定
System.out.println("你要做什么:");
System.out.println("1.攻击 2.蓄力");
System.out.println("3.躲闪 4.重击");
System.out.println("____________________");
int Do = Shuru.nextInt();
//敌人输入判定
//娱乐难度
if(ND==1){
for(int i=1;i3;i) MogDo = Shiji.nextInt(4);}
//挑战难度
if(ND==2){
if(YingxMP==0MogMP==0) {MogDo = 0;}
else if(MogMP==0){for(int i=1;i3;i) MogDo = Shiji.nextInt(2);}
else if(YingxMP2MogMP2) MogDo = 2;
else if(MogMP4) {for(int i=1;i4;i) MogDo = Shiji.nextInt(3);}
else MogDo = 3;}
//噩梦难度
if(ND==3){
if(Do==1) MogDo=1;
if(Do==2MogMP0) MogDo=2;
if(Do==2MogMP==0) MogDo=0;
if(Do==3MogMP4) MogDo=0;
if(Do==3MogMP=4) MogDo=3;}//变态判定 。。
//战斗分析
//防御
if(Do==3) {fangyu=1;System.out.println("你灵巧的躲避攻击!");}
if(MogDo==1) {fangyu=1;System.out.println("恶龙进行防御!");}
//角色判定
if(Do==1YingxMP==0) {System.out.println("MP不足!");}
if(Do==1YingxMP0) {
if(fangyu==0) {MogHP=MogHP-1;YingxMP=YingxMP-1;System.out.println("你发动攻击!");}
if(fangyu==1) {YingxMP=YingxMP-1;System.out.println("你的攻击被格挡!");}}
if(Do==2) {YingxMP=YingxMP 1;System.out.println("你进行蓄力!");}
if(Do==4YingxMP3) {System.out.println("MP不足!");}
if(Do==4YingxMP2) {MogHP=MogHP-2;YingxMP=YingxMP-3;System.out.println("你发动重击!");}
if(Do 4) System.out.println("你不知所措...");
//敌人判定
if(MogDo==2MogMP==0) {System.out.println("恶龙在发呆!");}
if(MogDo==2MogMP0) {
if(fangyu==0) {YingxHP=YingxHP-2;MogMP=MogMP-1;System.out.println("恶龙发动攻击!");}
if(fangyu==1) {MogMP=MogMP-1;System.out.println("恶龙的攻击被躲开了!");}}
if(MogDo==0) {MogMP=MogMP 1;System.out.println("恶龙进行蓄力!");}
if(MogDo==3MogMP4) {System.out.println("恶龙在发呆!");}
if(MogDo==3MogMP3) {YingxHP=YingxHP-4;MogMP=MogMP-4;System.out.println("恶龙发动火焰吐吸!躲避不能!");}
//结束
fangyu = 0;
MogDo = 0;
System.out.println("____________________");
System.out.println();
System.out.println("==================================");}
}
}
【java实现好玩的代码 java实现好玩的代码是什么】java实现好玩的代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java实现好玩的代码是什么、java实现好玩的代码的信息别忘了在本站进行查找喔 。

    推荐阅读