Crappy Game of Peter's

import javax.swing.JFrame; public class Starter extends JFrame { public Starter() { add(new Pong()); setTitle("Board"); setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(500,500); setLocationRelativeTo(null); setVisible(true); setResizable(false); } public static void main(String[] args) { new Starter(); } } 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.*; import java.lang.Math; public class Pong extends JPanel implements Runnable, MouseListener { int die1 = 0; int die2 = 0; boolean ingame = false; private Dimension d; int BOARD_WIDTH=500; int BOARD_HEIGHT=500; int x = 0; BufferedImage img; String message = "Click Board to Start"; int point = 0; String message2 = "points: " + point; private Thread animator; public Pong() { 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); g.setColor(Color.white); g.fillRect(0, 0, d.width, d.height); /* g.setColor(Color.red); g.fillRect(0, 0, 50, 50); g.fillOval(x,20,20,20); */ Font small = new Font("Helvetica", Font.BOLD, 25); FontMetrics metr = this.getFontMetrics(small); g.setColor(Color.black); g.setFont(small); g.drawString(message, 10, 384); g.drawString(message2, 10, 284); if (ingame) { diceLogic(g); } 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: " + e.getKeyCode(); int key = e.getKeyCode(); if(key==39){ } } } int i; public void mousePressed(MouseEvent e) { int x = e.getX(); int y = e.getY(); ingame = true; openRoll(); openRollLogic(die1,die2); /* inGameLogic(); cashValue(); */ } public void openRoll() { die1 = makeRandomNumbers(1,6); die2 = makeRandomNumbers(1,6); message = "die1: " + die1 + " die2: " + die2; } public int makeRandomNumbers(int min, int max) { int rand = (int)(Math.random()*(max))+min; return rand; } public void openRollLogic(int die1, int die2){ int total = die1 + die2; if(total == 7 || total == 11){ message = "You win ;D"; } else{ if(total == 2 || total == 3 || total == 12){ message = "You suck :p"; point=0; message2 = "points: " + point; } else{ point++; message2 = "points: " + point; } } } public void diceLogic(Graphics g){ if(die1==1){ g.drawImage(img,0,0,128,128 ,0,0,64,64,null); } if(die1==2){ g.drawImage(img,0,0,128,128 ,64,0,128,64,null); } if(die1==3){ g.drawImage(img,0,0,128,128 ,128,0,192,64,null); } if(die1==4){ g.drawImage(img,0,0,128,128 ,0,64,64,128,null); } if(die1==5){ g.drawImage(img,0,0,128,128 ,64,64,128,128,null); } if(die1==6){ g.drawImage(img,0,0,128,128 ,128,64,192,128,null); } if(die2==1){ g.drawImage(img,192,0,320,128 ,0,0,64,64,null); } if(die2==2){ g.drawImage(img,192,0,320,128 ,64,0,128,64,null); } if(die2==3){ g.drawImage(img,192,0,320,128 ,128,0,192,64,null); } if(die2==4){ g.drawImage(img,192,0,320,128 ,0,64,64,128,null); } if(die2==5){ g.drawImage(img,192,0,320,128 ,64,64,128,128,null); } if(die2==6){ g.drawImage(img,192,0,320,128 ,128,64,192,128,null); } } 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.