面对对象 监听器 继承 接口 设置窗口 设置图片 设置按钮 设置文字框 随机数
Craps赌博游戏 投2颗色子 ,首轮 7 ,11为玩家胜利 , 2,3,12为庄家胜利;之后玩家投到7就输 ,投到和首轮一样的点数 就赢;
1.先做色子
package com.lovo.homework; import java.awt.Graphics; import java.awt.Image; import javax.swing.ImageIcon; /** * 类:骰子 * @author Abe * 属性:点数 图片 */ public class Dice { private int point; private static Image[] images = new Image[6]; static { //静态加载图片 for (int i = 0; i < images.length; i++) { ImageIcon icon = new ImageIcon("Dice/" + (i + 1) + ".jpg"); images[i] = icon.getImage(); } } /** * 构造器 */ public Dice() { this.point = (int)(Math.random() * 6 + 1); } /** * 绘制一颗骰子 * xy坐标 图标大小135*135 */ public void draw(Graphics g, int i , int x, int y) { g.drawImage(Dice.images[i], x, y, 135, 135, null); } public int getPoint() { return point; } public static Image[] getImages() { return images; } }
2.窗口 以及主程序
package com.lovo.homework; import java.awt.Button; import java.awt.Font; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.Timer; /** * 类:窗口 * @author Abe * 父类:JFrame * 接口:监听器 * 属性:很多 */ @SuppressWarnings("serial") public class MyFrame extends JFrame implements ActionListener { private Button b1 = new Button("开始"); private Button b2 = new Button("停止"); private Button b3 = new Button("再来一局!"); public JLabel jl1 = new JLabel(); public JLabel jl2 = new JLabel(); public JLabel jl3 = new JLabel(); private Timer timer = null; private Dice p1; private Dice p2; private int round = 0; private int first = 0; private boolean gameOver = false; /** * 接口 :重写事件监听器 * @author Abe */ @Override public void actionPerformed(ActionEvent e) { Object obj = e.getSource(); if (!gameOver) { if (obj == b1) { timer.start(); jl3.setText("第" + (round +1) + "轮"); repaint(); } else if (obj == b2) { timer.stop(); if (round == 0) { first = p1.getPoint() + p2.getPoint(); jl1.setText("首次投出的点数为:" + first); round++; switch (first) { case 2: case 3: case 12: jl2.setText("玩家首轮投出了" + first + "点,庄家胜出!!"); gameOver = true; break; case 7: case 11: jl2.setText("玩家首轮投出了" + first + "点,玩家赢了!"); gameOver = true; } } else if (first == p1.getPoint() + p2.getPoint()) { round++; jl2.setText("玩家投出了和第一轮一样的" + first + "点,玩家胜出!!"); gameOver = true; } else if (p1.getPoint() + p2.getPoint() == 7) { round++; jl2.setText("玩家投出了7点,庄家赢了!"); gameOver = true; } else { round++; jl2.setText("玩家这次投出了" + (p1.getPoint() + p2.getPoint()) + "点,游戏继续~"); } } else if (obj == timer) { p1 = new Dice(); p2 = new Dice(); repaint(); } } if (obj == b3 && gameOver == true){ gameOver = false; round = 0; jl1.setText(""); jl2.setText(""); } } /** *构造器 */ public MyFrame() { this.setTitle("Craps娱乐机"); this.setSize(800, 600); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setResizable(false); this.setLocationRelativeTo(null); this.setLayout(null); jl1.setBounds(50, 250, 300, 50); jl1.setFont(new Font("微软雅黑", Font.BOLD, 24)); jl2.setBounds(50, 350, 500, 50); jl2.setFont(new Font("微软雅黑", Font.BOLD, 24)); jl3.setBounds(50, 150, 100, 50); jl3.setFont(new Font("微软雅黑", Font.BOLD, 24)); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); timer = new Timer(100, this); b1.setBounds(275, 500, 100, 50); b2.setBounds(425, 500, 100, 50); b3.setBounds(670, 25, 100, 50); this.add(b1); this.add(b2); this.add(b3); this.add(jl1); this.add(jl2); this.add(jl3); } /** * 重写:paint方法 */ @Override public void paint(Graphics g) { super.paint(g); if (p1 != null && p2 != null) { p1.draw(g, p1.getPoint() - 1, 240, 100); p2.draw(g, p2.getPoint() - 1, 425, 100); } } }
窗口还有点大…… 懒得调整了……
3.
package com.lovo.homework; /** * 类:执行器? * @author Abe */ public class CrapsTest { public static void main(String[] args) { new MyFrame().setVisible(true); } }
时间: 2024-12-16 08:58:43