Model of NXP:
For LEGO Digital Designer: http://www.box.net/shared/dglfry7c4s (NXP.lxf)
Video about this project: http://www.youtube.com/watch?v=0to-5yuVeI4
Source:
- Code: Select all
import lejos.nxt.*;
public class NXP_Pong {
//====================================Settings
private static final int joystickScale = 3; // if 2 the value can be -2 ~ 2
private static final boolean LREnabled = true;
private static final boolean UDEnabled = false;
private static final String[] menuList = {"Start", "About", "Quit"};
//---
private static final int[] notes = {1, 8, 3000, 5000, 5};
private static final byte font[][] = {
/* 0x21 */ {0x00, 0x04, 0x00, 0x04, 0x04, 0x04, 0x04, 0x00},
/* 0x22 */ {0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x0a, 0x00},
/* 0x23 */ {0x00, 0x0a, 0x1e, 0x0a, 0x1e, 0x14, 0x14, 0x00},
/* 0x24 */ {0x04, 0x0e, 0x14, 0x14, 0x0e, 0x06, 0x1e, 0x04},
/* 0x25 */ {0x00, 0x09, 0x15, 0x0a, 0x05, 0x07, 0x0a, 0x00},
/* 0x26 */ {0x00, 0x16, 0x09, 0x1b, 0x04, 0x0a, 0x04, 0x00},
/* 0x27 */ {0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x04, 0x00},
/* 0x28 */ {0x04, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x04},
/* 0x29 */ {0x04, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x04},
/* 0x2a */ {0x00, 0x00, 0x00, 0x0a, 0x04, 0x0e, 0x04, 0x00},
/* 0x2a */ {0x00, 0x00, 0x04, 0x04, 0x0e, 0x04, 0x04, 0x00},
/* 0x2b */ {0x02, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00},
/* 0x2c */ {0x00, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00},
/* 0x2d */ {0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
/* 0x2e */ {0x00, 0x04, 0x04, 0x04, 0x08, 0x08, 0x08, 0x00},
/* 0x2f */ {0x00, 0x04, 0x0a, 0x0a, 0x0a, 0x0a, 0x04, 0x00},
/* 0x30 */ {0x00, 0x08, 0x08, 0x08, 0x08, 0x0c, 0x08, 0x00},
/* 0x31 */ {0x00, 0x0e, 0x02, 0x04, 0x08, 0x0a, 0x04, 0x00},
/* 0x32 */ {0x00, 0x04, 0x0a, 0x08, 0x04, 0x0a, 0x04, 0x00},
/* 0x33 */ {0x00, 0x08, 0x1e, 0x0a, 0x0a, 0x0c, 0x08, 0x00},
/* 0x33 */ {0x00, 0x06, 0x0a, 0x08, 0x06, 0x02, 0x0c, 0x00},
/* 0x34 */ {0x00, 0x04, 0x0a, 0x0a, 0x06, 0x02, 0x0c, 0x00},
/* 0x35 */ {0x00, 0x04, 0x04, 0x04, 0x04, 0x08, 0x0e, 0x00},
/* 0x36 */ {0x00, 0x04, 0x0a, 0x0a, 0x04, 0x0a, 0x04, 0x00},
/* 0x37 */ {0x00, 0x06, 0x08, 0x0c, 0x0a, 0x0a, 0x04, 0x00},
/* 0x38 */ {0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x04, 0x00},
/* 0x39 */ {0x02, 0x04, 0x04, 0x00, 0x00, 0x00, 0x04, 0x00},
/* 0x3a */ {0x00, 0x00, 0x08, 0x04, 0x02, 0x04, 0x08, 0x00},
/* 0x3b */ {0x00, 0x00, 0x00, 0x0e, 0x00, 0x0e, 0x00, 0x00},
/* 0x3c */ {0x00, 0x00, 0x02, 0x04, 0x08, 0x04, 0x02, 0x00},
/* 0x3c */ {0x00, 0x04, 0x00, 0x04, 0x08, 0x0a, 0x04, 0x00},
/* 0x3d */ {0x00, 0x1e, 0x1f, 0x0b, 0x0b, 0x17, 0x1e, 0x00},
/* 0x3e */ {0x00, 0x11, 0x11, 0x0e, 0x0a, 0x0a, 0x04, 0x00},
/* 0x3f */ {0x00, 0x0e, 0x0a, 0x0a, 0x0e, 0x0a, 0x0e, 0x00},
/* 0x40 */ {0x00, 0x0e, 0x11, 0x01, 0x01, 0x11, 0x0e, 0x00},
/* 0x41 */ {0x00, 0x0e, 0x12, 0x12, 0x12, 0x12, 0x0e, 0x00},
/* 0x42 */ {0x00, 0x1e, 0x02, 0x02, 0x1e, 0x02, 0x1e, 0x00},
/* 0x43 */ {0x00, 0x02, 0x02, 0x02, 0x0e, 0x02, 0x0e, 0x00},
/* 0x44 */ {0x00, 0x0e, 0x11, 0x19, 0x01, 0x11, 0x0e, 0x00},
/* 0x45 */ {0x00, 0x12, 0x12, 0x12, 0x1e, 0x12, 0x12, 0x00},
/* 0x45 */ {0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00},
/* 0x46 */ {0x00, 0x0e, 0x0a, 0x08, 0x08, 0x08, 0x08, 0x00},
/* 0x47 */ {0x00, 0x11, 0x09, 0x07, 0x05, 0x09, 0x11, 0x00},
/* 0x48 */ {0x00, 0x0e, 0x02, 0x02, 0x02, 0x02, 0x02, 0x00},
/* 0x49 */ {0x00, 0x15, 0x15, 0x15, 0x1b, 0x1b, 0x1b, 0x00},
/* 0x4a */ {0x00, 0x12, 0x1a, 0x1a, 0x16, 0x16, 0x12, 0x00},
/* 0x4b */ {0x00, 0x0e, 0x11, 0x11, 0x11, 0x11, 0x0e, 0x00},
/* 0x4c */ {0x00, 0x02, 0x02, 0x0e, 0x0a, 0x0a, 0x0e, 0x00},
/* 0x4d */ {0x00, 0x1e, 0x09, 0x11, 0x11, 0x11, 0x0e, 0x00},
/* 0x4e */ {0x00, 0x12, 0x12, 0x0a, 0x1e, 0x12, 0x1e, 0x00},
/* 0x4e */ {0x00, 0x0e, 0x11, 0x1c, 0x03, 0x11, 0x0e, 0x00},
/* 0x4f */ {0x00, 0x04, 0x04, 0x04, 0x04, 0x04, 0x1f, 0x00},
/* 0x50 */ {0x00, 0x0c, 0x12, 0x12, 0x12, 0x12, 0x12, 0x00},
/* 0x51 */ {0x00, 0x04, 0x0a, 0x0a, 0x0a, 0x11, 0x11, 0x00},
/* 0x52 */ {0x00, 0x0a, 0x15, 0x15, 0x15, 0x15, 0x11, 0x00},
/* 0x53 */ {0x00, 0x11, 0x0a, 0x04, 0x04, 0x0a, 0x11, 0x00},
/* 0x54 */ {0x00, 0x04, 0x04, 0x04, 0x0a, 0x0a, 0x11, 0x00},
/* 0x55 */ {0x00, 0x1f, 0x01, 0x02, 0x04, 0x08, 0x1f, 0x00},
/* 0x56 */ {0x06, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x06},
/* 0x57 */ {0x00, 0x04, 0x04, 0x04, 0x02, 0x02, 0x02, 0x00},
/* 0x57 */ {0x0c, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x0c},
/* 0x58 */ {0x00, 0x00, 0x00, 0x00, 0x0a, 0x0a, 0x04, 0x00},
/* 0x59 */ {0x00, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
/* 0x5a */ {0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x04, 0x00},
/* 0x5b */ {0x00, 0x0e, 0x0a, 0x0e, 0x08, 0x06, 0x00, 0x00},
/* 0x5c */ {0x00, 0x06, 0x0a, 0x0a, 0x06, 0x02, 0x00, 0x00},
/* 0x5d */ {0x00, 0x0c, 0x02, 0x02, 0x0c, 0x00, 0x00, 0x00},
/* 0x5e */ {0x00, 0x0c, 0x0a, 0x0a, 0x0c, 0x08, 0x00, 0x00},
/* 0x5f */ {0x00, 0x0c, 0x02, 0x0e, 0x0a, 0x04, 0x00, 0x00},
/* 0x60 */ {0x00, 0x04, 0x04, 0x0e, 0x04, 0x0c, 0x00, 0x00},
/* 0x60 */ {0x06, 0x08, 0x0c, 0x0a, 0x0a, 0x0c, 0x00, 0x00},
/* 0x61 */ {0x00, 0x0a, 0x0a, 0x0a, 0x0e, 0x02, 0x00, 0x00},
/* 0x62 */ {0x00, 0x04, 0x04, 0x04, 0x04, 0x00, 0x04, 0x00},
/* 0x63 */ {0x02, 0x04, 0x04, 0x04, 0x04, 0x04, 0x00, 0x04},
/* 0x64 */ {0x00, 0x0a, 0x0a, 0x06, 0x0a, 0x02, 0x00, 0x00},
/* 0x65 */ {0x00, 0x04, 0x04, 0x04, 0x04, 0x04, 0x00, 0x00},
/* 0x66 */ {0x00, 0x15, 0x15, 0x15, 0x1f, 0x00, 0x00, 0x00},
/* 0x67 */ {0x00, 0x0a, 0x0a, 0x0a, 0x0e, 0x00, 0x00, 0x00},
/* 0x68 */ {0x00, 0x04, 0x0a, 0x0a, 0x04, 0x00, 0x00, 0x00},
/* 0x69 */ {0x02, 0x02, 0x06, 0x0a, 0x0a, 0x06, 0x00, 0x00},
/* 0x69 */ {0x08, 0x08, 0x0c, 0x0a, 0x0a, 0x0c, 0x00, 0x00},
/* 0x6a */ {0x00, 0x04, 0x04, 0x04, 0x0c, 0x00, 0x00, 0x00},
/* 0x6b */ {0x00, 0x06, 0x08, 0x04, 0x0c, 0x00, 0x00, 0x00},
/* 0x6c */ {0x00, 0x0c, 0x04, 0x04, 0x0e, 0x04, 0x00, 0x00},
/* 0x6d */ {0x00, 0x0e, 0x0a, 0x0a, 0x0a, 0x00, 0x00, 0x00},
/* 0x6e */ {0x00, 0x04, 0x0a, 0x0a, 0x0a, 0x00, 0x00, 0x00},
/* 0x6f */ {0x00, 0x0a, 0x15, 0x15, 0x15, 0x00, 0x00, 0x00},
/* 0x70 */ {0x00, 0x12, 0x0c, 0x0c, 0x12, 0x00, 0x00, 0x00},
/* 0x71 */ {0x02, 0x04, 0x04, 0x04, 0x04, 0x0a, 0x00, 0x00},
/* 0x72 */ {0x00, 0x0e, 0x02, 0x04, 0x0e, 0x00, 0x00, 0x00},
/* 0x72 */ {0x08, 0x04, 0x04, 0x04, 0x02, 0x04, 0x04, 0x08},
/* 0x73 */ {0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04},
/* 0x74 */ {0x04, 0x08, 0x08, 0x08, 0x10, 0x08, 0x08, 0x04},
/* 0x75 */ {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x14},
};
private static int objN = 5;
private static int[] objX = new int[objN], objY = new int[objN];
private static byte[][] objGraph = new byte[objN][800];
private static int[] objWidth = new int[objN], objHeight = new int[objN];
private static LightSensor buttonA = new LightSensor(SensorPort.S3);
private static TouchSensor buttonB = new TouchSensor(SensorPort.S1);
public static void main(String[] args) throws InterruptedException {
//---Initialize
buttonA.setFloodlight(false);
Motor LR = Motor.C;
Motor UD = Motor.B;
LR.setSpeed(80);
UD.setSpeed(80);
LR.regulateSpeed(true);
UD.regulateSpeed(true);
if (!LREnabled) {
LR.lock(1);
}
if (!UDEnabled) {
UD.lock(1);
}
int LRPower = 0, UDPower = 0;
int mode = 0, choseMode = 1;
//====================================GAME_Var
String temp;
int round = 0, win = 0;
boolean newRound = true;
int meX = 0, meY = 0, comX = 0, comY = 0, ballX = 0, ballY = 0, ballDX = 0, ballDY = 0, playRound = 0, playRoundChose = 3;
createObj(0, "011111111111011111111111111111111111111", 13, 3);
createObj(1, "111111111111111111111111110111111111110", 13, 3);
createObj(2, "010111010", 3, 3);
createObj(3, "0000110000001111110001111111100111111110111111111111111111110111111110011111111000111111000000110000", 10, 10);
createObj(4, "000011111111111111111110110000011111111111111111000111111111111111111111", 24, 3);
//---Start
if (Button.ENTER.isPressed()) {
Button.ENTER.waitForPressAndRelease();
}
do {
//---Control
if (LREnabled) {
float LRMoving = -Math.max(-45, Math.min(LR.getTachoCount(), 45));
if (LRMoving == 45) {
LRMoving -= 0.1;
}
LRPower = Math.round((float)((LRMoving+45)/(45/(joystickScale+0.5))+0.5))-joystickScale-1;
}
if (UDEnabled) {
float UDMoving = Math.max(-45, Math.min(UD.getTachoCount(), 45));
if (UDMoving == 45) {
UDMoving -= 0.1;
}
UDPower = Math.round((float)((UDMoving+45)/(45/(joystickScale+0.5))+0.5))-joystickScale-1;
}
while (mode == 0 && !Button.ESCAPE.isPressed()) {
//---Menu
LCD.clear();
LCD.bitBlt(55, 0, 45, 64, 17);
objX[3] = 34;
objY[3] = 15;
drawObj(3, 34, 15);
objX[4] = 14;
objY[4] = 33;
drawObj(4, 14, 33);
drawMenu(31, 52, choseMode);
LCD.refresh();
while (Button.readButtons() == 0);
if (Button.ENTER.isPressed()) {
Button.ENTER.waitForPressAndRelease();
mode = choseMode;
} else if (Button.RIGHT.isPressed()) {
Button.RIGHT.waitForPressAndRelease();
choseMode--;
if (choseMode < 1) {
choseMode = menuList.length;
}
} else if (Button.LEFT.isPressed()) {
Button.LEFT.waitForPressAndRelease();
choseMode++;
if (choseMode > menuList.length) {
choseMode = 1;
}
}
//---Initialize_Once
if (mode == 1) {
round = 1;
win = 0;
playRound = 0;
newRound = true;
} else if (mode == 2) {
about();
Button.ESCAPE.waitForPressAndRelease();
mode = 0;
}
}
//---Start
if (mode == 1) {
//---Settings
if (playRound == 0) {
while (!Button.ENTER.isPressed() && !Button.ESCAPE.isPressed()) {
LCD.clear();
drawText("No of Rounds:", 0, 5);
temp = new Integer(playRoundChose).toString();
drawText(temp, 26, 6);
while (!Button.ENTER.isPressed() && !Button.LEFT.isPressed() && !Button.RIGHT.isPressed() && !Button.ESCAPE.isPressed());
if (Button.LEFT.isPressed()) {
Button.LEFT.waitForPressAndRelease();
playRoundChose -= 2;
} else if (Button.RIGHT.isPressed()) {
Button.RIGHT.waitForPressAndRelease();
playRoundChose += 2;
}
playRoundChose = Math.max(1, Math.min(playRoundChose, 99));
}
if (Button.ESCAPE.isPressed()) {
Button.ESCAPE.waitForPressAndRelease();
mode = 0;
} else {
playRound = playRoundChose;
}
//---End
} else if (round > playRound) {
LCD.clear();
Thread.sleep(1000);
temp = new Integer(win).toString();
drawText("You win: "+temp, 4, 3);
Thread.sleep(1000);
temp = new Integer(playRound-win).toString();
drawText("COM win: "+temp, 4, 4);
Thread.sleep(1500);
if (win > (playRound-1)/2) {
drawText("You WIN! ", 12, 6);
Sound.playNote(notes, 1000, 50);
Sound.playNote(notes, 1200, 50);
Sound.playNote(notes, 1400, 50);
Sound.playNote(notes, 1600, 200);
} else {
drawText("You LOSE! ", 9, 6);
Sound.playNote(notes, 1200, 50);
Sound.playNote(notes, 1000, 100);
}
Thread.sleep(3000);
mode = 0;
//---Begin
} else if (newRound) {
newRound = false;
LCD.clear();
temp = new Integer(round).toString();
drawText("Round "+temp, 13, 5);
LCD.refresh();
Thread.sleep(1500);
meX = 25;
meY = 89;
comX = 25;
comY = 8;
ballX = 30+(int)(Math.floor(Math.random()*5)-2)*2;
ballY = 49+(int)(Math.floor(Math.random()*3)-1)*2;
ballDX = (int)Math.floor(Math.random()*2)+1;
if (Math.random() < 0.5) {
ballDX = -ballDX;
}
if (Math.random() < 0.5) {
ballDY = -2;
} else {
ballDY = 2;
}
LCD.clear();
drawObj(0, 25, 89);
drawObj(1, 25, 8);
drawObj(2, ballX, ballY);
LCD.refresh();
Thread.sleep(500);
//---Playing
} else {
meX = Math.max(0, Math.min(meX+LRPower, 52));
if (ballX-comX-5 == 0) {
comX = Math.max(0, Math.min(comX+(int)Math.floor(Math.random()*5)-2, 52));
} else {
comX = Math.max(0, Math.min(comX+Math.max(-3, Math.min(ballX-comX-5, 3)), 52));
}
if (ballX+ballDX <= 0 || ballX+ballDX >= 62) {
ballDX = -ballDX;
Sound.playNote(notes, 700, 20);
}
if (ballY+ballDY == 9 || ballY+ballDY == 87) {
int distance = 0;
if (ballY+ballDY == 87) {
distance = ballX+ballDX-meX-5;
} else {
distance = ballX+ballDX-comX-5;
}
if (Math.abs(distance) <= 6) {
ballDX += Math.round(distance/2);
ballDX = Math.max(-4, Math.min(ballDX, 4));
ballDY = -ballDY;
Sound.playNote(notes, 1000, 20);
}
}
ballX = ballX+ballDX;
ballY = ballY+ballDY;
//---Display
drawObj(0, meX, meY);
drawObj(1, comX, comY);
drawObj(2, ballX, ballY);
LCD.refresh();
Thread.sleep(50);
//---Calculate_WIN&LOSE
if (ballY <= 0 || ballY >= 97) {
if (ballY <= 0) {
drawText("Win", 25, 5);
Sound.playNote(notes, 400, 50);
win++;
} else {
drawText("Lose", 22, 5);
Sound.playNote(notes, 400, 50);
}
Thread.sleep(1000);
round++;
newRound = true;
}
//---
if (Button.ESCAPE.isPressed()) {
Button.ESCAPE.waitForPressAndRelease();
mode = 0;
}
}
}
//---
} while (!Button.ESCAPE.isPressed() && mode != 3);
//---End
LR.flt();
UD.flt();
LR.regulateSpeed(false);
UD.regulateSpeed(false);
LR.rotateTo(0, true);
UD.rotateTo(0);
}
//====================================
public static void about() {
LCD.clear();
drawText("NXT", 0, 1);
drawText("Pocket Game", 4, 2);
drawText("Author: Hong", 0, 4);
drawText("[Pong]", 0, 6);
drawText("Author: Hong", 0, 7);
}
//------------------------------------------------------------System
public static boolean buttonAIsPressed() {
return (buttonA.readValue() <= 16);
}
public static boolean buttonBIsPressed() {
return buttonB.isPressed();
}
public static int ceil(float v) {
return v == 0 ? 0 : (int)v+1;
}
public static int ceil(double v) {
return v == 0 ? 0 : (int)v+1;
}
public static void drawText(String str, int x, int y, boolean co) {
char[] chars = str.toCharArray();
for (int i = 0; i < chars.length; i++) {
int newX = x+i*5;
int newY = co ? y : y*8;
if (chars[i] >= 33) {
LCD.bitBlt(font[chars[i]-33], 8, 5, 0, 0, 99-newY-8, newX, 8, 5, 0x0000ff00);
}
}
LCD.refresh();
}
public static void drawText(String str, int x, int y) {
drawText(str, x, y, false);
}
public static void drawMenu(int x, int y, int choseMode) {
for (int i = 0; i < menuList.length; i++) {
drawText(menuList[i], x-(int)Math.floor(menuList[i].length()*5/2), y+i*14, true);
}
int newX = x-(int)Math.floor(menuList[choseMode-1].length()*5/2)-3;
LCD.bitBlt(99-y-(choseMode-1)*14-11, newX, 1, menuList[choseMode-1].length()*5+6, 0x00ff00ff);
LCD.bitBlt(99-y-(choseMode-1)*14+2, newX, 1, menuList[choseMode-1].length()*5+6, 0x00ff00ff);
}
public static void createObj(int id, String graph, int width, int height) {
char[] graphArray = graph.toCharArray();
objWidth[id] = width;
objHeight[id] = height;
int rows = ceil((float)width/8);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < height; j++) {
byte line = 0;
int tmpWidth = Math.min(8, width-i*8);
for (int k = 0; k < tmpWidth; k++) {
if (tmpWidth < 8) {
if (graphArray[j*width+k+(i*8)] == '1') {
line = (byte)(line+Math.pow(2, k));
}
} else {
if (k < 7) {
if ((graphArray[j*width+7+(i*8)] == '1' && graphArray[j*width+k+(i*8)] == '0') || (graphArray[j*width+7+(i*8)] == '0' && graphArray[j*width+k+(i*8)] == '1')) {
line = (byte)(line+Math.pow(2, k));
}
} else if (graphArray[j*width+7+(i*8)] == '1') {
line = (byte)(~line);
}
}
}
objGraph[id][i*height+height-j-1] = line;
}
}
}
public static void drawObj(int id, int x, int y) {
if (objX[id] != x || objY[id] != y) {
LCD.bitBlt(99-objY[id]-objHeight[id], objX[id], objHeight[id], objWidth[id], 0);
objX[id] = x;
objY[id] = y;
}
LCD.bitBlt(objGraph[id], objHeight[id], objWidth[id], 0, 0, 99-y-objHeight[id], x, objHeight[id], objWidth[id], 0x0000ff00);
}
}

