Hello All! I am trying to make a Bop It Phidget game and I am using an LCD Text
ID: 3533244 • Letter: H
Question
Hello All!
I am trying to make a Bop It Phidget game and I am using an LCD Text as my output, and my inputs are a touch sensor, force sensor, slider, and rotation sensor.
I basically want to command the user to either "Touch It, Force It, Slide It, or Turn It" each action corresponding to each type of sensor.
Currently I am stuck trying to figure out when the user does not follow the command how to end the game.... Or If they get 10 in a row correct it says they win the game?!
Here is my code so far...
[code]
import com.phidgets.*;
import com.phidgets.event.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.Random;
public class BopIt extends JFrame
{
TextLCDPhidget lcd;
private boolean slideIt=false;
private boolean touchIt=false;
private boolean turnIt=false;
private boolean forceIt=false;
private int count=1;
private Random randomNumber = new Random();
private int random;
private Timer t;
public BopIt()
{
try{
lcd = new TextLCDPhidget();
lcd.openAny();
lcd.waitForAttachment();
lcd.setBrightness(120);
lcd.setBacklight(true);
lcd.setCursorBlink(false);
lcd.setCursor(false);
//add slide it
if(touchIt || turnIt || forceIt)
lcd.setDisplayString(0,("YOU WON"));
//add slide it
else
lcd.setDisplayString(0,("YOU LOSE"));
lcd.close();
lcd=null;
}
catch(PhidgetException e)
{
System.out.println("Phidget Exception Generate");
e.printStackTrace();
}
setSize(1400,800);
getContentPane().setBackground(new Color(0,153,203));
ImageIcon image = new ImageIcon("/Users/lyates93/Documents/Phidget Project/bopit.jpg");
JLabel imagelabel = new JLabel(image);
add(imagelabel);
Timer t = new Timer(3500, new TimerListener());
t.setDelay(5000);
t.start();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void paint(Graphics g)
{
super.paint(g);
Font myFont = new Font("Serif", Font.BOLD, 40);
g.setFont(myFont);
g.drawString("We are going to play Bop It!", 30,100);
if(random==0)
{
g.drawString("Slide It!",200,400);
}
if(random==1)
{
g.drawString("Touch It!",200,400);
}
if(random==2)
{
g.drawString("Turn It!",200,400);
}
if(random==3)
{
g.drawString("Force It!",200,400);
}
if(forceIt && turnIt && touchIt)
{
t.stop();
}
}
private class TimerListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//get new action, add one more number for slider
random = randomNumber.nextInt(4);
repaint();
count++;
if(count==10)
t.stop();
else if(forceIt==false || turnIt==false || touchIt==false || slideIt==false)
t.stop();
}
}
public static void main(String[] args)
{
new BopIt();
}
public void sensorChanged(SensorChangeEvent se)
{
int port = se.getIndex();
int value = se.getValue();
if(port == 7 && value<500)
{
slideIt=true;
}
else if(port == 7 && value>900)
{
slideIt=false;
}
if(port == 4 && value<500)
{
touchIt=true;
}
else if(port == 4 && value>500)
{
touchIt=false;
}
if(port == 5 && value>55)
{
turnIt=true;
}
else if(port == 5 && value<55)
{
turnIt=false;
}
if(port == 6 && value>75)
{
forceIt=true;
}
else if(port == 6 && value<75)
{
forceIt=false;
}
}
}
[/code]
Any help would feel great!
Explanation / Answer
You can place a condition statement whenever user has to follow a command and if the user does not follow it, use System.exit() to end the game.
Also place a counter, which will increment for each correct answer and will reset to zero when a false answer comes. If the counter reaches 10, the user will win the game
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.