craps

/** * CRAPs * * Josh Wang * yeboi */ import java.awt.event.MouseListener; import java.awt.event.MouseEvent; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Toolkit; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import javax.swing.Timer; import java.util.ArrayList; import java.util.Iterator; import java.util.Random; import javax.swing.ImageIcon; import javax.swing.JPanel; import javax.imageio.*; import java.awt.image.*; import java.io.*; public class craps extends JPanel implements Runnable, MouseListener{ boolean ingame = false; private Dimension d; int BOARD_WIDTH=400; int BOARD_HEIGHT=300; BufferedImage img; String message0 = ""; String message1 = ""; String message2 = ""; String title = "CRAPS!"; String display0 = ""; String display1 = ""; String start = "Set bet and then click to playgame!"; String gameOver = ""; private Thread animator; int die1; int die2; int openRoll; int pointRoll; int cash = 100; int bet = 10; boolean openRolled = false; boolean win = false; boolean noCash = false; String money = "Cash: " + Integer.toString(cash); String wager = "Bet: " + Integer.toString(bet); public craps(){ addKeyListener(new TAdapter()); addMouseListener(this); setFocusable(true); d = new Dimension(BOARD_WIDTH, BOARD_HEIGHT); setBackground(Color.black); try { img = ImageIO.read(this.getClass().getResource("dice.png")); } catch (IOException e) { System.out.println("Image could not be read"); // System.exit(1); } if (animator == null || !ingame) { animator = new Thread(this); animator.start(); } setDoubleBuffered(true); } public void paint(Graphics g) { super.paint(g); Font small = new Font("Helvetica", Font.BOLD, 23); Font medium = new Font("Helvetica", Font.BOLD, 35); Font large = new Font("Helvetica", Font.BOLD, 70); Font button = new Font("Courier", Font.PLAIN, 15); FontMetrics metr = this.getFontMetrics(small); int upx[] = {105,125,115}; int upy[] = {345,345,325}; int downx[] = {105,125,115}; int downy[] = {350,350,370}; g.setColor(Color.black); g.fillPolygon(upx,upy,3); g.fillPolygon(downx,downy,3); g.fillRect(10,334,38,28); g.fillRect(58,334,38,28); g.setColor(Color.red); if(openRolled == false){ g.fillPolygon(upx,upy,3); g.fillPolygon(downx,downy,3); } g.setFont(small); g.drawString(message0, 205, 300); g.drawString(message1, 155, 410); g.drawString(start,60,450); g.setFont(medium); g.drawString(display1,180,165); g.drawString(gameOver,25,380); g.setColor(Color.cyan); if(openRolled == false){ g.fillRect(10,334,38,28); g.fillRect(58,334,38,28); } g.setFont(small); g.drawString(message2, 135, 450); g.setFont(medium); g.drawString(display0,180,165); g.drawString(money,300,360); String wager = "Bet: " + Integer.toString(bet); g.drawString(wager,130,360); g.setFont(large); g.drawString(title,115,100); g.setColor(Color.black); g.setFont(button); g.drawString("MIN",16,352); g.drawString("MAX",64,352); if (ingame) { g.drawImage(img,168,200,232,264,(die1-1)%3*64,(die1-1)/3*64,(die1-1)%3*64+64,(die1-1)/3*64+64,null); g.drawImage(img,268,200,332,264,(die2-1)%3*64,(die2-1)/3*64,(die2-1)%3*64+64,(die2-1)/3*64+64,null); } Toolkit.getDefaultToolkit().sync(); g.dispose(); } private class TAdapter extends KeyAdapter { public void keyReleased(KeyEvent e) { int key = e.getKeyCode(); } public void keyPressed(KeyEvent e) { //System.out.println( e.getKeyCode()); // message = "Key Pressed: "33 + e.getKeyCode(); int key = e.getKeyCode(); if(key==39){ } } } public void mousePressed(MouseEvent e) { int x = e.getX(); int y = e.getY(); ingame = true; if(x > 10 && x < 48 && y > 334 && y < 362) if(openRolled == true){} else bet = 0; else if (x > 58 && x < 96 && y > 334 && y < 362) if(openRolled == true){} else bet = cash; else if (x > 105 && x < 125 && y > 325 && y < 345 && bet < cash) if(openRolled == true){} else bet++; else if (x > 105 && x < 125 && y > 350 && y < 370 && bet > 0) if(openRolled == true){} else bet--; else{ start = ""; if(noCash == true) System.exit(1); roll(); if(noCash == false){ if(openRolled == true && win == false){ gameLogic(); } if(openRolled == false){ openRollLogic(); } if(win == true){ openRolled = false; win = false; } }//if you have cash } } public void roll(){ if(cash == 0){ message0 = ""; message1 = ""; message2 = ""; display0 = ""; display1 = ""; money = ""; noCash = true; gameOver = "You are out of cash money!"; } else{ die1 = rng(1,6); die2 = rng(1,6); } } public int rng(int min,int max){ int rand = (int) (Math.random() * (max)) + min; return rand; } public void openRollLogic(){ openRoll = die1 + die2; if (openRoll == 7 || openRoll == 11){ message0 = die1 + " + " + die2 + " = " + openRoll; message1 = " You're winner! "; message2 = " Set your bet!"; display0 = ""; display1 = "Natural!"; cash += bet; money = "Cash: " + Integer.toString(cash); win = true; } else if(openRoll == 2 || openRoll == 3 || openRoll == 12){ message0 = die1 + " + " + die2 + " = " + openRoll; message1 = " You are lost."; message2 = " Set your bet!"; display0 = ""; display1 = " Craps!"; cash -= bet; money = "Cash: " + Integer.toString(cash); win = true; } else{ message0 = die1 + " + " + die2 + " = " + openRoll; message1 = "Point established!"; message2 = " Click to rolling again!"; display0 = "Point: " + openRoll; display1 = ""; } openRolled = true; } public void gameLogic(){ pointRoll = die1 + die2; message0 = die1 + " + " + die2 + " = " + pointRoll; message1 = " Nothing happen!"; message2 = " Click to rolling again!"; display0 = "Point: " + openRoll; if (die1 + die2 == 7){ message0 = die1 + " + " + die2 + " = " + pointRoll; message1 = " You are lost."; message2 = " Set your bet!"; display0 = ""; display1 = "No pass!"; cash -= bet; money = "Cash: " + Integer.toString(cash); win = true; } if (die1 + die2 == openRoll){ message0 = die1 + " + " + die2 + " = " + pointRoll; message1 = " WinRAR!"; message2 = " Set your bet!"; display0 = ""; display1 = " Pass!"; cash += bet; money = "Cash: " + Integer.toString(cash); win = true; } } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mouseClicked(MouseEvent e) { } public void run() { long beforeTime, timeDiff, sleep; beforeTime = System.currentTimeMillis(); int animationDelay = 20; long time = System.currentTimeMillis(); while (true) {//infinite loop // spriteManager.update(); repaint(); try { time += animationDelay; Thread.sleep(Math.max(0,time - System.currentTimeMillis())); }catch (InterruptedException e) { System.out.println(e); }//end catch }//end while loop }//end of run }//end of class

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.