Merge remote-tracking branch 'arcade-controls/develop' into feature/controls
Conflicts: README.md control/GameControl.java main/Main.java main/Window.java model/GameModel.java view/GameView.java
This commit is contained in:
@@ -0,0 +1,7 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<classpath>
|
||||||
|
<classpathentry kind="src" path=""/>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
|
||||||
|
<classpathentry kind="lib" path="/home/janco/Documenten/github/Arcade/pi4j-core.jar"/>
|
||||||
|
<classpathentry kind="output" path="bin"/>
|
||||||
|
</classpath>
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
/bin/
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<projectDescription>
|
||||||
|
<name>Arcade-controls</name>
|
||||||
|
<comment></comment>
|
||||||
|
<projects>
|
||||||
|
</projects>
|
||||||
|
<buildSpec>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
</buildSpec>
|
||||||
|
<natures>
|
||||||
|
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||||
|
</natures>
|
||||||
|
</projectDescription>
|
||||||
@@ -1,2 +1,2 @@
|
|||||||
# Arcade
|
branch for the buttons and joystick for gpio raspberry
|
||||||
Arcade game for the fourth term at Avans Hogeschool Breda.
|
|
||||||
|
|||||||
+12
-31
@@ -1,52 +1,33 @@
|
|||||||
package control;
|
package control;
|
||||||
|
|
||||||
import java.awt.event.KeyEvent;
|
|
||||||
import java.awt.event.KeyListener;
|
|
||||||
import java.awt.event.MouseEvent;
|
|
||||||
import java.awt.event.MouseListener;
|
|
||||||
import java.awt.event.WindowEvent;
|
|
||||||
import java.awt.event.WindowFocusListener;
|
|
||||||
|
|
||||||
import model.GameModel;
|
import model.GameModel;
|
||||||
import view.GameView;
|
import view.GameView;
|
||||||
|
import control.button.ButtonEvent;
|
||||||
|
import control.button.ButtonListener;
|
||||||
|
import control.joystick.JoystickEvent;
|
||||||
|
import control.joystick.JoystickListener;
|
||||||
|
|
||||||
public class GameControl implements MouseListener, KeyListener, WindowFocusListener{
|
public class GameControl implements JoystickListener, ButtonListener{
|
||||||
|
|
||||||
GameModel model;
|
GameModel model;
|
||||||
GameView view;
|
GameView view;
|
||||||
|
|
||||||
|
|
||||||
public GameControl(GameModel model, GameView view)
|
public GameControl(GameModel model, GameView view)
|
||||||
{
|
{
|
||||||
this.model = model;
|
this.model = model;
|
||||||
this.view = view;
|
this.view = view;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void keyPressed(KeyEvent e) {}
|
@Override
|
||||||
|
public void buttonPressed(ButtonEvent e) {
|
||||||
|
|
||||||
public void keyReleased(KeyEvent e) {
|
|
||||||
if(e.getKeyCode() == KeyEvent.VK_ESCAPE)
|
|
||||||
{
|
|
||||||
System.exit(0);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void keyTyped(KeyEvent e) {}
|
@Override
|
||||||
|
public void buttonReleased(ButtonEvent e) {}
|
||||||
|
|
||||||
public void mouseClicked(MouseEvent e) {}
|
@Override
|
||||||
|
public void onJoystickMoved(JoystickEvent e) {
|
||||||
|
|
||||||
public void mouseEntered(MouseEvent e) {}
|
}
|
||||||
|
|
||||||
public void mouseExited(MouseEvent e) {}
|
|
||||||
|
|
||||||
public void mousePressed(MouseEvent e) {}
|
|
||||||
|
|
||||||
public void mouseReleased(MouseEvent e) {}
|
|
||||||
|
|
||||||
public void windowGainedFocus(WindowEvent e) {}
|
|
||||||
|
|
||||||
public void windowLostFocus(WindowEvent e) {}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,98 @@
|
|||||||
|
package control;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.BufferedWriter;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.OutputStreamWriter;
|
||||||
|
|
||||||
|
public class LedHandler {
|
||||||
|
private BufferedReader inp;
|
||||||
|
private BufferedWriter out;
|
||||||
|
private Process p;
|
||||||
|
|
||||||
|
public LedHandler(){
|
||||||
|
try {
|
||||||
|
p = Runtime.getRuntime().exec("sudo python led.py");
|
||||||
|
inp = new BufferedReader( new InputStreamReader(p.getInputStream()) );
|
||||||
|
out = new BufferedWriter( new OutputStreamWriter(p.getOutputStream()) );
|
||||||
|
|
||||||
|
//setLed(15, 100, 100, 100);
|
||||||
|
//strobo();
|
||||||
|
}
|
||||||
|
catch (Exception err) {
|
||||||
|
err.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void close(){
|
||||||
|
try {
|
||||||
|
inp.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
out.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
p.destroy();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the color of a specific led
|
||||||
|
* @param Pixel number of the led
|
||||||
|
* @param Red value
|
||||||
|
* @param Green value
|
||||||
|
* @param Blue value
|
||||||
|
* Remember: Button leds are GRB, normal ledstrip is RGB
|
||||||
|
*/
|
||||||
|
public void setLed(int led, int r, int g, int b) {
|
||||||
|
if(led<0)
|
||||||
|
return;
|
||||||
|
try {
|
||||||
|
System.out.println("Set the led with " + r);
|
||||||
|
out.write( "1|" + led + "|" + r + "|" + g + "|" + b + "\n" );
|
||||||
|
out.flush();
|
||||||
|
}
|
||||||
|
catch (Exception err) {
|
||||||
|
err.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Shows the (new) colors
|
||||||
|
*/
|
||||||
|
public void show(){
|
||||||
|
try {
|
||||||
|
out.write( "0\n" );
|
||||||
|
out.flush();
|
||||||
|
}
|
||||||
|
catch (Exception err) {
|
||||||
|
err.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void strobo(){
|
||||||
|
boolean on = true;
|
||||||
|
while(true){
|
||||||
|
if(on){
|
||||||
|
for(int i = 1; i < 66; i++){
|
||||||
|
setLed(i, 0, 0, 0);
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
for(int i = 1; i < 66; i++){
|
||||||
|
setLed(i, 255, 255, 255);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
on = !on;
|
||||||
|
show();
|
||||||
|
try {
|
||||||
|
Thread.sleep(100);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
package control.button;
|
||||||
|
|
||||||
|
import java.awt.Color;
|
||||||
|
|
||||||
|
import main.Window;
|
||||||
|
import control.LedHandler;
|
||||||
|
|
||||||
|
public class Button {
|
||||||
|
|
||||||
|
Color color;
|
||||||
|
int ledID;
|
||||||
|
int buttonID;
|
||||||
|
LedHandler led;
|
||||||
|
|
||||||
|
public Button(int buttonID, int ledID, LedHandler led)
|
||||||
|
{
|
||||||
|
color = new Color(255,255,255);
|
||||||
|
this.ledID = ledID;
|
||||||
|
this.buttonID = buttonID;
|
||||||
|
this.led = led;
|
||||||
|
setLed();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setLed()
|
||||||
|
{
|
||||||
|
if(Window.ON_RASP)
|
||||||
|
{
|
||||||
|
led.setLed(ledID, color.getGreen(), color.getRed(), color.getBlue());
|
||||||
|
led.show();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setColor(Color newColor)
|
||||||
|
{
|
||||||
|
color = newColor;
|
||||||
|
setLed();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Color getColor(){
|
||||||
|
return color;
|
||||||
|
}
|
||||||
|
public int getButtonID()
|
||||||
|
{
|
||||||
|
return buttonID;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package control.button;
|
||||||
|
|
||||||
|
public class ButtonEvent {
|
||||||
|
private Button button;
|
||||||
|
private Long now;
|
||||||
|
|
||||||
|
public ButtonEvent(Button button, Long now){
|
||||||
|
this.button = button;
|
||||||
|
this.now = now;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Button getButton() {
|
||||||
|
return button;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getNow() {
|
||||||
|
return now;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,136 @@
|
|||||||
|
package control.button;
|
||||||
|
|
||||||
|
import java.awt.Color;
|
||||||
|
import java.awt.event.KeyEvent;
|
||||||
|
import java.awt.event.KeyListener;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.pi4j.io.gpio.GpioController;
|
||||||
|
import com.pi4j.io.gpio.GpioFactory;
|
||||||
|
import com.pi4j.io.gpio.GpioPinDigitalInput;
|
||||||
|
import com.pi4j.io.gpio.PinState;
|
||||||
|
import com.pi4j.io.gpio.RaspiPin;
|
||||||
|
import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent;
|
||||||
|
import com.pi4j.io.gpio.event.GpioPinListenerDigital;
|
||||||
|
|
||||||
|
import main.Window;
|
||||||
|
import control.LedHandler;
|
||||||
|
|
||||||
|
public class ButtonHandler implements KeyListener{
|
||||||
|
|
||||||
|
List<ButtonListener> listeners;
|
||||||
|
static List<Button> buttons;
|
||||||
|
LedHandler led;
|
||||||
|
|
||||||
|
public ButtonHandler(LedHandler led)
|
||||||
|
{
|
||||||
|
this.led = led;
|
||||||
|
|
||||||
|
listeners = new ArrayList<ButtonListener>();
|
||||||
|
buttons = new ArrayList<Button>();
|
||||||
|
|
||||||
|
buttons.add(new Button(0, -1, led));
|
||||||
|
buttons.add(new Button(1, 2, led));
|
||||||
|
buttons.add(new Button(2, 1, led));
|
||||||
|
buttons.add(new Button(3, 0, led));
|
||||||
|
buttons.add(new Button(4, 3, led));
|
||||||
|
buttons.add(new Button(5, 4, led));
|
||||||
|
buttons.add(new Button(6, 5, led));
|
||||||
|
|
||||||
|
for(Button b : buttons)
|
||||||
|
{
|
||||||
|
b.setColor(new Color((int)(Math.random()*254+1),(int)(Math.random()*254+1),(int)(Math.random()*254+1)));
|
||||||
|
}
|
||||||
|
System.out.println(Window.ON_RASP);
|
||||||
|
if (Window.ON_RASP)
|
||||||
|
addGpioListeners();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addGpioListeners(){
|
||||||
|
ArrayList<GpioPinDigitalInput> inputpins = new ArrayList<GpioPinDigitalInput>();
|
||||||
|
final GpioController gpio = GpioFactory.getInstance();
|
||||||
|
|
||||||
|
inputpins.add(gpio.provisionDigitalInputPin(RaspiPin.GPIO_02, "1")); //button 1 to 6 + start button
|
||||||
|
inputpins.add(gpio.provisionDigitalInputPin(RaspiPin.GPIO_03, "2"));
|
||||||
|
inputpins.add(gpio.provisionDigitalInputPin(RaspiPin.GPIO_13, "3"));
|
||||||
|
inputpins.add(gpio.provisionDigitalInputPin(RaspiPin.GPIO_14, "4"));
|
||||||
|
inputpins.add(gpio.provisionDigitalInputPin(RaspiPin.GPIO_00, "5"));
|
||||||
|
inputpins.add(gpio.provisionDigitalInputPin(RaspiPin.GPIO_12, "6"));
|
||||||
|
inputpins.add(gpio.provisionDigitalInputPin(RaspiPin.GPIO_06, "0"));
|
||||||
|
|
||||||
|
|
||||||
|
for(GpioPinDigitalInput p:inputpins){
|
||||||
|
p.addListener(new GpioPinListenerDigital() {
|
||||||
|
@Override
|
||||||
|
public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent e) {
|
||||||
|
if(e.getState() == PinState.HIGH){
|
||||||
|
buttonRelease(buttons.get(Integer.parseInt(e.getPin().getName())));
|
||||||
|
System.out.println(e.getPin().getName() + " Released");
|
||||||
|
}else{
|
||||||
|
buttonPress(buttons.get(Integer.parseInt(e.getPin().getName())));
|
||||||
|
System.out.println(e.getPin().getName() + " Pressed");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addButtonListener(ButtonListener toAdd) {
|
||||||
|
listeners.add(toAdd);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void buttonPress(Button b) {
|
||||||
|
ButtonEvent e = new ButtonEvent(b, System.currentTimeMillis());
|
||||||
|
for (ButtonListener bt : listeners)
|
||||||
|
bt.buttonPressed(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void buttonRelease(Button b) {
|
||||||
|
ButtonEvent e = new ButtonEvent(b, System.currentTimeMillis());
|
||||||
|
for (ButtonListener bt : listeners)
|
||||||
|
bt.buttonReleased(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void keyPressed(KeyEvent e) {
|
||||||
|
switch(e.getKeyCode())
|
||||||
|
{
|
||||||
|
case KeyEvent.VK_0:
|
||||||
|
buttonPress(buttons.get(0));
|
||||||
|
break;
|
||||||
|
case KeyEvent.VK_1:
|
||||||
|
buttonPress(buttons.get(1));
|
||||||
|
break;
|
||||||
|
case KeyEvent.VK_2:
|
||||||
|
buttonPress(buttons.get(2));
|
||||||
|
break;
|
||||||
|
case KeyEvent.VK_3:
|
||||||
|
buttonPress(buttons.get(3));
|
||||||
|
break;
|
||||||
|
case KeyEvent.VK_4:
|
||||||
|
buttonPress(buttons.get(4));
|
||||||
|
break;
|
||||||
|
case KeyEvent.VK_5:
|
||||||
|
buttonPress(buttons.get(5));
|
||||||
|
break;
|
||||||
|
case KeyEvent.VK_6:
|
||||||
|
buttonPress(buttons.get(6));
|
||||||
|
break;
|
||||||
|
case KeyEvent.VK_ESCAPE:
|
||||||
|
if(!Window.ON_RASP)
|
||||||
|
System.exit(0);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void keyReleased(KeyEvent arg0) {}
|
||||||
|
public void keyTyped(KeyEvent arg0) {}
|
||||||
|
|
||||||
|
public static List<Button> getButtons()
|
||||||
|
{
|
||||||
|
return buttons;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package control.button;
|
||||||
|
|
||||||
|
public interface ButtonListener {
|
||||||
|
public void buttonPressed(ButtonEvent e);
|
||||||
|
public void buttonReleased(ButtonEvent e);
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package control.joystick;
|
||||||
|
|
||||||
|
public class Joystick {
|
||||||
|
|
||||||
|
public enum Position {
|
||||||
|
UP,
|
||||||
|
LEFT,
|
||||||
|
DOWN,
|
||||||
|
RIGHT,
|
||||||
|
UP_RIGHT,
|
||||||
|
DOWN_RIGHT,
|
||||||
|
UP_LEFT,
|
||||||
|
DOWN_LEFT,
|
||||||
|
CENTER
|
||||||
|
}
|
||||||
|
|
||||||
|
private Position pos;
|
||||||
|
|
||||||
|
public Joystick()
|
||||||
|
{
|
||||||
|
pos = Position.CENTER;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPosition(Position pos)
|
||||||
|
{
|
||||||
|
this.pos = pos;
|
||||||
|
}
|
||||||
|
public Position getPos() {
|
||||||
|
return pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package control.joystick;
|
||||||
|
|
||||||
|
public class JoystickEvent {
|
||||||
|
private Joystick joystick;
|
||||||
|
private Long now;
|
||||||
|
|
||||||
|
public JoystickEvent(Joystick joystick, Long now){
|
||||||
|
this.joystick = joystick;
|
||||||
|
this.now = now;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Joystick getJoystick() {
|
||||||
|
return joystick;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getNow() {
|
||||||
|
return now;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,175 @@
|
|||||||
|
package control.joystick;
|
||||||
|
|
||||||
|
import java.awt.event.KeyEvent;
|
||||||
|
import java.awt.event.KeyListener;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import main.Window;
|
||||||
|
|
||||||
|
import com.pi4j.io.gpio.GpioController;
|
||||||
|
import com.pi4j.io.gpio.GpioFactory;
|
||||||
|
import com.pi4j.io.gpio.GpioPinDigitalInput;
|
||||||
|
import com.pi4j.io.gpio.PinState;
|
||||||
|
import com.pi4j.io.gpio.RaspiPin;
|
||||||
|
import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent;
|
||||||
|
import com.pi4j.io.gpio.event.GpioPinListenerDigital;
|
||||||
|
|
||||||
|
import control.joystick.Joystick.Position;
|
||||||
|
|
||||||
|
public class JoystickHandler implements KeyListener{
|
||||||
|
|
||||||
|
List<JoystickListener> listeners;
|
||||||
|
Set<Integer> keys;
|
||||||
|
Joystick j;
|
||||||
|
|
||||||
|
public JoystickHandler()
|
||||||
|
{
|
||||||
|
listeners = new ArrayList<JoystickListener>();
|
||||||
|
keys = new HashSet<Integer>();
|
||||||
|
j = new Joystick();
|
||||||
|
if(Window.ON_RASP)
|
||||||
|
addGpioListeners();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addGpioListeners(){
|
||||||
|
ArrayList<GpioPinDigitalInput> inputpins = new ArrayList<GpioPinDigitalInput>();
|
||||||
|
final GpioController gpio = GpioFactory.getInstance();
|
||||||
|
|
||||||
|
inputpins.add(gpio.provisionDigitalInputPin(RaspiPin.GPIO_04, "UP")); //button 1 to 6 + start button
|
||||||
|
inputpins.add(gpio.provisionDigitalInputPin(RaspiPin.GPIO_05, "LEFT"));
|
||||||
|
inputpins.add(gpio.provisionDigitalInputPin(RaspiPin.GPIO_11, "RIGHT"));
|
||||||
|
inputpins.add(gpio.provisionDigitalInputPin(RaspiPin.GPIO_10, "DOWN"));
|
||||||
|
|
||||||
|
|
||||||
|
for(GpioPinDigitalInput p:inputpins){
|
||||||
|
p.addListener(new GpioPinListenerDigital() {
|
||||||
|
@Override
|
||||||
|
public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent e) {
|
||||||
|
if(e.getState() == PinState.HIGH){
|
||||||
|
keyReleased(new KeyEvent(
|
||||||
|
new java.awt.Component(){},
|
||||||
|
KeyEvent.KEY_RELEASED,
|
||||||
|
System.nanoTime(),
|
||||||
|
0,
|
||||||
|
stringToKeyevent(e.getPin().getName()),
|
||||||
|
KeyEvent.CHAR_UNDEFINED)
|
||||||
|
);
|
||||||
|
System.out.println(e.getPin().getName() + " Released");
|
||||||
|
}else{
|
||||||
|
keyPressed(new KeyEvent(
|
||||||
|
new java.awt.Component(){},
|
||||||
|
KeyEvent.KEY_PRESSED,
|
||||||
|
System.nanoTime(),
|
||||||
|
0,
|
||||||
|
stringToKeyevent(e.getPin().getName()),
|
||||||
|
KeyEvent.CHAR_UNDEFINED)
|
||||||
|
);
|
||||||
|
System.out.println(e.getPin().getName() + " Pressed");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addJoystickListener(JoystickListener toAdd) {
|
||||||
|
listeners.add(toAdd);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onJoystickMoved(Joystick j) {
|
||||||
|
JoystickEvent e = new JoystickEvent(j, System.currentTimeMillis());
|
||||||
|
for (JoystickListener yst : listeners)
|
||||||
|
yst.onJoystickMoved(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateJoystickPosition()
|
||||||
|
{
|
||||||
|
if(keys.contains(KeyEvent.VK_UP) && keys.contains(KeyEvent.VK_RIGHT))
|
||||||
|
{
|
||||||
|
j.setPosition(Position.UP_RIGHT);
|
||||||
|
}
|
||||||
|
else if(keys.contains(KeyEvent.VK_UP) && keys.contains(KeyEvent.VK_LEFT))
|
||||||
|
{
|
||||||
|
j.setPosition(Position.UP_LEFT);
|
||||||
|
}
|
||||||
|
else if(keys.contains(KeyEvent.VK_DOWN) && keys.contains(KeyEvent.VK_RIGHT))
|
||||||
|
{
|
||||||
|
j.setPosition(Position.DOWN_RIGHT);
|
||||||
|
}
|
||||||
|
else if(keys.contains(KeyEvent.VK_DOWN) && keys.contains(KeyEvent.VK_LEFT))
|
||||||
|
{
|
||||||
|
j.setPosition(Position.DOWN_LEFT);
|
||||||
|
}
|
||||||
|
|
||||||
|
else if(keys.contains(KeyEvent.VK_UP))
|
||||||
|
{
|
||||||
|
j.setPosition(Position.UP);
|
||||||
|
}
|
||||||
|
else if(keys.contains(KeyEvent.VK_LEFT))
|
||||||
|
{
|
||||||
|
j.setPosition(Position.LEFT);
|
||||||
|
}
|
||||||
|
else if(keys.contains(KeyEvent.VK_RIGHT))
|
||||||
|
{
|
||||||
|
j.setPosition(Position.RIGHT);
|
||||||
|
}
|
||||||
|
else if(keys.contains(KeyEvent.VK_DOWN))
|
||||||
|
{
|
||||||
|
j.setPosition(Position.DOWN);
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
{
|
||||||
|
j.setPosition(Position.CENTER);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private int stringToKeyevent(String s){
|
||||||
|
switch(s){
|
||||||
|
case "UP":
|
||||||
|
return KeyEvent.VK_UP;
|
||||||
|
case "DOWN":
|
||||||
|
return KeyEvent.VK_DOWN;
|
||||||
|
case "LEFT":
|
||||||
|
return KeyEvent.VK_LEFT;
|
||||||
|
case "RIGHT":
|
||||||
|
return KeyEvent.VK_RIGHT;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void keyPressed(KeyEvent e) {
|
||||||
|
if(e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyCode() == KeyEvent.VK_RIGHT)
|
||||||
|
{
|
||||||
|
Set<Integer> keysCopy = new HashSet<Integer>(keys);
|
||||||
|
keys.add(e.getKeyCode());
|
||||||
|
updateJoystickPosition();
|
||||||
|
|
||||||
|
if(!keys.equals(keysCopy))
|
||||||
|
{
|
||||||
|
onJoystickMoved(j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void keyReleased(KeyEvent e) {
|
||||||
|
if(e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyCode() == KeyEvent.VK_RIGHT)
|
||||||
|
{
|
||||||
|
Set<Integer> keysCopy = new HashSet<Integer>(keys);
|
||||||
|
|
||||||
|
keys.remove(e.getKeyCode());
|
||||||
|
updateJoystickPosition();
|
||||||
|
|
||||||
|
if(!keys.equals(keysCopy))
|
||||||
|
{
|
||||||
|
onJoystickMoved(j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void keyTyped(KeyEvent e) {}
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package control.joystick;
|
||||||
|
|
||||||
|
public interface JoystickListener {
|
||||||
|
public void onJoystickMoved(JoystickEvent e);
|
||||||
|
}
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
import sys
|
||||||
|
import time
|
||||||
|
from neopixel import *
|
||||||
|
|
||||||
|
# LED strip configuration:
|
||||||
|
LED_COUNT = 66 # Number of LED pixels.
|
||||||
|
LED_PIN = 18 # GPIO pin connected to the pixels (must support PWM!).
|
||||||
|
LED_FREQ_HZ = 700000 # LED signal frequency in hertz (usually 800khz)
|
||||||
|
LED_DMA = 5 # DMA channel to use for generating signal (try 5)
|
||||||
|
LED_BRIGHTNESS = 255 # Set to 0 for darkest and 255 for brightest
|
||||||
|
LED_INVERT = False # True to invert the signal (when using NPN transistor level shift)
|
||||||
|
|
||||||
|
def setLed(strip, l,r,g,b):
|
||||||
|
strip.setPixelColor(l,Color(r,g,b))
|
||||||
|
|
||||||
|
def strobetest(strip):
|
||||||
|
on = True
|
||||||
|
while True:
|
||||||
|
if on:
|
||||||
|
on = False
|
||||||
|
for i in range(strip.numPixels()):
|
||||||
|
strip.setPixelColor(i, Color(255,255,255))
|
||||||
|
else:
|
||||||
|
on = True
|
||||||
|
for i in range(strip.numPixels()):
|
||||||
|
strip.setPixelColor(i, Color(0,0,0))
|
||||||
|
strip.show()
|
||||||
|
time.sleep(100.0/1000)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
ledstrip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS)
|
||||||
|
ledstrip.begin()
|
||||||
|
s = sys.stdin.readline().strip()
|
||||||
|
while s not in ['break', 'quit']:
|
||||||
|
message = s.split("|")
|
||||||
|
returnms = 'e'
|
||||||
|
if(message[0] == '1'):
|
||||||
|
if (len(message) == 5):
|
||||||
|
try:
|
||||||
|
led = int(message[1])
|
||||||
|
r = int(message[2])
|
||||||
|
g = int(message[3])
|
||||||
|
b = int(message[4])
|
||||||
|
setLed(ledstrip, led,r,g,b)
|
||||||
|
returnms = 'a'
|
||||||
|
except ValueError:
|
||||||
|
returnms = 'e'
|
||||||
|
elif (message[0] == '0'):
|
||||||
|
ledstrip.show()
|
||||||
|
returnms = 'a'
|
||||||
|
s = sys.stdin.readline().strip()
|
||||||
|
|
||||||
+8
-1
@@ -3,7 +3,14 @@ package main;
|
|||||||
public class Main {
|
public class Main {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
new Window();
|
if(args.length != 1)
|
||||||
|
{
|
||||||
|
new Window(false);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
new Window(true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+30
-11
@@ -8,18 +8,22 @@ import javax.swing.JFrame;
|
|||||||
import model.GameModel;
|
import model.GameModel;
|
||||||
import view.GameView;
|
import view.GameView;
|
||||||
import control.GameControl;
|
import control.GameControl;
|
||||||
|
import control.LedHandler;
|
||||||
|
import control.button.ButtonHandler;
|
||||||
|
import control.joystick.JoystickHandler;
|
||||||
|
|
||||||
public class Window extends JFrame {
|
public class Window extends JFrame {
|
||||||
/**
|
|
||||||
*
|
public static boolean ON_RASP;
|
||||||
*/
|
|
||||||
private static final long serialVersionUID = 1L;
|
public Window(boolean ON_RASP)
|
||||||
|
|
||||||
public Window()
|
|
||||||
{
|
{
|
||||||
//Create window
|
//Create window
|
||||||
super("Arcade");
|
super("Arcade");
|
||||||
setSize(500,600);
|
setSize(1280, 1024);
|
||||||
|
|
||||||
|
Window.ON_RASP = ON_RASP;
|
||||||
|
System.out.println(ON_RASP);
|
||||||
|
|
||||||
//Set window close listener
|
//Set window close listener
|
||||||
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
|
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
|
||||||
@@ -33,15 +37,30 @@ public class Window extends JFrame {
|
|||||||
setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
|
setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
|
||||||
setUndecorated(true);
|
setUndecorated(true);
|
||||||
|
|
||||||
|
//Create Events
|
||||||
|
LedHandler led = null;
|
||||||
|
|
||||||
|
if(ON_RASP) //TODO REMOVE
|
||||||
|
{
|
||||||
|
led = new LedHandler();
|
||||||
|
}
|
||||||
|
|
||||||
|
ButtonHandler bth = new ButtonHandler(led);
|
||||||
|
JoystickHandler jsh = new JoystickHandler();
|
||||||
|
|
||||||
//Create Instances
|
//Create Instances
|
||||||
GameView view = new GameView();
|
GameView view = new GameView(led);
|
||||||
GameModel model = new GameModel(view);
|
GameModel model = new GameModel(view);
|
||||||
GameControl control = new GameControl(model, view);
|
GameControl control = new GameControl(model, view);
|
||||||
setContentPane(view);
|
setContentPane(view);
|
||||||
|
|
||||||
addKeyListener(control);
|
//Create EventListeners
|
||||||
addMouseListener(control);
|
if(!Window.ON_RASP){
|
||||||
addWindowFocusListener(control);
|
addKeyListener(bth);
|
||||||
|
addKeyListener(jsh);
|
||||||
|
}
|
||||||
|
bth.addButtonListener(control);
|
||||||
|
jsh.addJoystickListener(control);
|
||||||
|
|
||||||
//Display
|
//Display
|
||||||
pack();
|
pack();
|
||||||
|
|||||||
Executable
BIN
Binary file not shown.
+6
-1
@@ -1,5 +1,6 @@
|
|||||||
package view;
|
package view;
|
||||||
|
|
||||||
|
import java.awt.Color;
|
||||||
import java.awt.Graphics;
|
import java.awt.Graphics;
|
||||||
import java.awt.Graphics2D;
|
import java.awt.Graphics2D;
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
@@ -9,6 +10,7 @@ import javax.swing.JPanel;
|
|||||||
import javax.swing.Timer;
|
import javax.swing.Timer;
|
||||||
|
|
||||||
import model.Player;
|
import model.Player;
|
||||||
|
import control.LedHandler;
|
||||||
|
|
||||||
public class GameView extends JPanel implements ActionListener{
|
public class GameView extends JPanel implements ActionListener{
|
||||||
|
|
||||||
@@ -18,9 +20,11 @@ public class GameView extends JPanel implements ActionListener{
|
|||||||
private static final long serialVersionUID = 1939480784205689618L;
|
private static final long serialVersionUID = 1939480784205689618L;
|
||||||
Timer t;
|
Timer t;
|
||||||
Player player;
|
Player player;
|
||||||
|
LedHandler led;
|
||||||
|
|
||||||
public GameView()
|
public GameView(LedHandler led)
|
||||||
{
|
{
|
||||||
|
this.led=led;
|
||||||
t = new Timer(1000/30, this);
|
t = new Timer(1000/30, this);
|
||||||
t.start();
|
t.start();
|
||||||
}
|
}
|
||||||
@@ -29,6 +33,7 @@ public class GameView extends JPanel implements ActionListener{
|
|||||||
repaint();
|
repaint();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void paintComponent(Graphics g)
|
public void paintComponent(Graphics g)
|
||||||
{
|
{
|
||||||
super.paintComponent(g);
|
super.paintComponent(g);
|
||||||
|
|||||||
Reference in New Issue
Block a user