Total members 11894 |It is currently Sat Nov 23, 2024 8:56 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Applet Dice Game:
Fruit machine simulation - this applet will generate 3 random numbers, in the 3 colored boxes to simulate the rolling of 3 dice.
The player can type in numbers in the 3 text fields below the dice as guesses for what will be thrown.
Each time the button is is pressed, the 3 die are "rolled" and their scores are matched with the guesses in the text fields. The player is told if there are 0, 1, 2 or 3 matches

Code:

import java
.awt.Color;
import java.awt.Font;
import java.awt.Graphics;

/**
 * Simple class to demonstrate a Die
 * 
  
 *
 */
public class Die {

    private int value;
    private int posX, posY;
    private Font font;
    
    
/**
     * Create a new die at the given position and 
     * set the initial value to 1
     * 
     * @param posX
     * @param posY
     */
    public Die(int posX, int posY) {
        this.posX = posX;
        this.posY = posY;
        value = 1;
        font = new Font("Courier", Font.BOLD, 60);
    }
    
    
/**
     * Set the top left position to display the die
     * @param posX
     * @param posY
     */
    public void setPos(int posX, int posY){
        this.posX = posX;
        this.posY = posY;        
    
}

    /**
     * @return the value
     */
    public int getValue() {
        return value;
    }

    /**
     * @param value the value to set
     */
    public void setValue(int value) {
        this.value = value;
    }

    /**
     * @param font the font to set
     */
    public void setFont(Font font) {
        this.font = font;
    }
    
    
/**
     * Roll the dice to get a random value between 1& 6
     */
    public void roll(){
        value = (int)(Math.random()*+ 1);
    }
    
    public void display
(Graphics g){
        g.setColor(Color.yellow);
        g.fillRect(posX, posY, 80, 80);
        g.setColor(Color.black);
        g.drawRect(posX, posY, 80, 80);
        g.setFont(font);
        g.drawString(""+value, posX + 20, posY+60);    
    
}
}

 


Code:

import java
.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class DiceRoller extends Applet implements ActionListener{
    
    
    private Die d1
,d2;
    private Button btnRoll;
    
    
    public void init
(){
        setSize(220,200);
        setBackground(Color.green);
        
        
        d1 
= new Die(20,40);
        d2 = new Die(120,40);
        
        
        btnRoll 
= new Button("ROLL DICE");
        btnRoll.addActionListener(this);
        add(btnRoll);
    }
    
    
/**
     * Display the current values of the 2 dice
     */
    public void paint(Graphics g){
        d1.display(g);
        d2.display(g);
    }

    /**
     * When the button is clicked roll the dice.
     * 
     */
    public void actionPerformed(ActionEvent ae) {
        d1.roll();
        d2.roll();
        repaint();        
    
}
}

 





Attachments:
File comment: I need help with the codes that will make the game running smoothly! Thks
DiceRoller1.txt [841 Bytes]
Downloaded 1180 times
File comment: I need help with the codes that will make the game running smoothly! Thks
Die.txt [1.36 KiB]
Downloaded 841 times
Author:
Newbie
User avatar Posts: 2
Have thanks: 0 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : Applet Dice Game Fruit Machine simulation
 Applet Game To Shot a Random Ball     -  
 ROLL DICE and HORSE RACE projects help     -  
 ping pong game - java-Sticker-ball game (modified v1.1)     -  
 fill all Squares of game board - 2d Java Game Example     -  
 Rotar Machine     -  
 Rotor Machine Java     -  
 clients cannot connect to server on another machine     -  
 how to connect client on one machine with server     -  
 machine learning java libraries     -  
 Support Vector Machine coding on Matlab...     -  



Topic Tags

Java Applet
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com