本文主要是介绍【汇智学堂】-JAVA小游戏(井字游戏-菜鸟版),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
游戏执行的效果图:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;import javax.swing.JFrame;
import javax.swing.JOptionPane; /*** * 程序入口**/
public class ticktacktoe { public static void main(String[] args) { new DrawSee2();}
}
class DrawSee2 extends JFrame {private static final long serialVersionUID = 2L;private Graphics jg;private static final int w2=200;//格子的宽度private static final int x1=100;//起点X坐标private static final int y1=100;//起点Y坐标private static final int x2=700;//终点X坐标private static final int y2=700;//终点Y坐标private int[][] position; // 存放游戏数据的二维数组public Color c=Color.RED; private int isEnd2=0;/*** DrawSee构造方法*/public DrawSee2() {setBounds(300, 50, 800, 800);//设置画布大小 this.setTitle("雷雷的井子游戏"); setVisible(true);//二维数组初始化position=new int[3][3];for(int i=0;i<3;i++){for(int j=0;j<3;j++) {position[i][j]=0;}} try { Thread.sleep(500);} catch (Exception e) {e.printStackTrace();} // 获取专门用于在窗口界面上绘图的对象jg = this.getGraphics();// 绘制游戏区域paintComponents(jg);// 添加鼠监听事件,当鼠标点击时触发this.addMouseListener(new MouseAdapter() {// 定义鼠标点击事件响应过程@Overridepublic void mouseClicked(MouseEvent e) {// 如果游戏结束,返回,不执行后面的代码 if(isEnd2==1) { JOptionPane.showMessageDialog(null,"游戏结束", "消息提示", JOptionPane.INFORMATION_MESSAGE);return;} //获取鼠标点击的那一点的x,y坐标int x = e.getX(), y = e.getY(); int dx = (x - x1) / w2, dy = (y - y1) / w2; int newx=dx*w2+x1,newy=dy*w2+y1;paintComponents2(jg,newx,newy,200,200);//将棋子落在准确的位置if(c==Color.RED) {position[dx][dy]=1;//记录落下的是红色的子} if(c==Color.green) {position[dx][dy]=2;//记录落下的是红色的子}winOrNot();//判断输赢 }}); }public void paintComponents(Graphics g) {try { // 设置线条颜色为红色g.setColor(Color.RED); // 绘制棋盘 for(int i=0;i<4;i++) {g.drawLine(x1, y1+i*w2, x2, y1+i*w2);//画横线g.drawLine(x1+i*w2, y1, x1+i*w2, y2);//画竖线}} catch (Exception e) {e.printStackTrace();}}public void paintComponents2(Graphics g,int x,int y,int width,int height) {try { // 设置线条颜色为红色g.setColor(c); if(c==Color.RED) { c=Color.green;} else { c=Color.red;} // 绘制棋盘 g.fillOval(x, y, width, height);} catch (Exception e) {e.printStackTrace();}}public void winOrNot() {if(position[0][0]==position[1][0]&&position[1][0]==position[2][0]) {if(position[0][0]!=0) {isEnd2=1;if(c!=Color.red) {JOptionPane.showMessageDialog(null,"红方获胜", "消息提示", JOptionPane.INFORMATION_MESSAGE);}if(c!=Color.green) {JOptionPane.showMessageDialog(null,"绿方获胜", "消息提示", JOptionPane.INFORMATION_MESSAGE);}}}if(position[0][1]==position[1][1]&&position[1][1]==position[2][1]) {if(position[0][1]!=0) {isEnd2=1;if(c!=Color.red) {JOptionPane.showMessageDialog(null,"红方获胜", "消息提示", JOptionPane.INFORMATION_MESSAGE);}if(c!=Color.green) {JOptionPane.showMessageDialog(null,"绿方获胜", "消息提示", JOptionPane.INFORMATION_MESSAGE);}}}if(position[0][2]==position[1][2]&&position[1][2]==position[2][2]) {if(position[0][2]!=0) {isEnd2=1;if(c!=Color.red) {JOptionPane.showMessageDialog(null,"红方获胜", "消息提示", JOptionPane.INFORMATION_MESSAGE);}if(c!=Color.green) {JOptionPane.showMessageDialog(null,"绿方获胜", "消息提示", JOptionPane.INFORMATION_MESSAGE);}}}if(position[0][0]==position[0][1]&&position[0][1]==position[0][2]) {if(position[0][0]!=0) {isEnd2=1;if(c!=Color.red) {JOptionPane.showMessageDialog(null,"红方获胜", "消息提示", JOptionPane.INFORMATION_MESSAGE);}if(c!=Color.green) {JOptionPane.showMessageDialog(null,"绿方获胜", "消息提示", JOptionPane.INFORMATION_MESSAGE);}}}if(position[1][0]==position[1][1]&&position[1][1]==position[1][2]) {if(position[1][0]!=0) {isEnd2=1;if(c!=Color.red) {JOptionPane.showMessageDialog(null,"红方获胜", "消息提示", JOptionPane.INFORMATION_MESSAGE);}if(c!=Color.green) {JOptionPane.showMessageDialog(null,"绿方获胜", "消息提示", JOptionPane.INFORMATION_MESSAGE);}}}
if(position[2][0]==position[2][1]&&position[2][1]==position[2][2]) {if(position[2][0]!=0) {isEnd2=1;if(c!=Color.red) {JOptionPane.showMessageDialog(null,"红方获胜", "消息提示", JOptionPane.INFORMATION_MESSAGE);}if(c!=Color.green) {JOptionPane.showMessageDialog(null,"绿方获胜", "消息提示", JOptionPane.INFORMATION_MESSAGE);}}}if(position[0][0]==position[1][1]&&position[1][1]==position[2][2]) {if(position[0][0]!=0) {isEnd2=1;if(c!=Color.red) {JOptionPane.showMessageDialog(null,"红方获胜", "消息提示", JOptionPane.INFORMATION_MESSAGE);}if(c!=Color.green) {JOptionPane.showMessageDialog(null,"绿方获胜", "消息提示", JOptionPane.INFORMATION_MESSAGE);}}}if(position[2][0]==position[1][1]&&position[1][1]==position[0][2]) {if(position[2][0]!=0) {isEnd2=1;if(c!=Color.red) {JOptionPane.showMessageDialog(null,"红方获胜", "消息提示", JOptionPane.INFORMATION_MESSAGE);}if(c!=Color.green) {JOptionPane.showMessageDialog(null,"绿方获胜", "消息提示", JOptionPane.INFORMATION_MESSAGE);}}}}
}
这篇关于【汇智学堂】-JAVA小游戏(井字游戏-菜鸟版)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!