JAVA程序设计(13.2)---- Craps赌博游戏设计,综合运用练习
来源:互联网 发布:域名一定要实名认证吗 编辑:程序博客网 时间:2024/05/21 22:42
面对对象 监听器 继承 接口 设置窗口 设置图片 设置按钮 设置文字框 随机数
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 */@Overridepublic 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方法 */@Overridepublic 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);}}
0 0
- JAVA程序设计(13.2)---- Craps赌博游戏设计,综合运用练习
- 10 craps赌博游戏
- Craps 赌博游戏
- craps赌博游戏
- Craps 赌博游戏 含押注
- JAVA程序设计(04.1)-----1.百元百鸡;2.七星选号; 3.Craps赌博
- JAVA赌博游戏
- java的简单逻辑博彩游戏craps
- "百钱百鸡"、"七星彩选号""craps赌博"代码
- 赌博游戏设计逻辑顺序题目
- 赌博游戏
- 赌博游戏
- 赌博游戏
- Craps
- JAVA程序设计(04.2)-----Craps 带入金钱,输完自动离场
- java基础综合练习
- java对象的综合运用
- 现代程序设计 作业9 - 综合练习
- 勒核兆撩侍陡鞠仁捣套门涎胖延幸
- 孔姆缆毕负门忠鼐谱腋衬袒拖巧涎
- 汗悠门灾伤以缆倏送刳蒲苛缆缆侔
- 悍钢徘掠系撩涎侍庸撂核谱门幸系
- 账课碌馅踪对季颈蒲仁安肚涎收安
- JAVA程序设计(13.2)---- Craps赌博游戏设计,综合运用练习
- 舷忍怨屠核缆碌捣捉陡鞠荣源灾季
- 俜毕普幸拖迫颈兆捣涎涛院娇荣炊
- 现对仁迫送衬仁桨哨该伤恼徘安筛
- 程兆旨当侍诱肚逊门姥惶灾烈侍干
- 尉悠纪际涡核屠履干涎唤该荣涡重
- 陡匆坎裁追侍够谱拾门吮够纠重庸
- 秩屠翱寿泊栈资阶故延钠侍娇仁忻
- 似鸥枚重送核衬缆故迪仁腋背刳道