📄 gamecanvas.java~208~
字号:
package fourconnect;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
public class gamecanvas extends GameCanvas implements Runnable {
private int m_x = 0, m_y = 0;
public static int map[][] = new int[6 + 5][7 + 5];
private int server = 1, client = 2;
private int serverorclient;// 客户机或则服务器
private int TURN = 0;
private int PIECE = 0;
private Thread thread;
private Image background;
private Image p1;
private Image p2;
private Image rediswin;
private Image greeniswin;
private Image noiswin;
private Sprite make;
public static boolean isgame = true;
private boolean play = true;
private boolean redwin = false;
private boolean greenwin = false;
private boolean nowin = false;
public static boolean b_redaction=true;
public static boolean b_greenaction=false;
public static boolean B_ACTION;
private boolean b_serverorclient;
public Server ser = null;
public Client cli = null;
public gamecanvas(Display d, String type) {
super(true);
for (int i = 0; i < 6; i++)
for (int j = 0; j < 7; j++) {
map[i][j] = 0;
// System.out.println(i+" "+j+" :"+map[i][j]);
}
super.setFullScreenMode(true);
try {
background = Image.createImage("/background.png");
p1 = Image.createImage("/p1.png");
p2 = Image.createImage("/p2.png");
rediswin = Image.createImage("/redwin.png");
greeniswin = Image.createImage("/greenwin.png");
noiswin = Image.createImage("/nowin.png");
make = new Sprite(Image.createImage("/make.png"), 25, 25);
make.setPosition(0, 35);
b_serverorclient = type.equals("server");
if (type.equals("server")) {
ser = new Server();
ser.start();
// ser.send1.send("1212");
make.setFrame(1);
serverorclient = server;
b_greenaction=false;//服务器不能先走
this.start();
d.setCurrent(this);
} else {
cli = new Client();
cli.start();
// sendstatus();
b_redaction=true;//客户机先走
make.setFrame(0);
serverorclient = client;
this.start();
d.setCurrent(this);
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("21212");
}
}
public void start() {
thread = new Thread(this);
thread.start();
}
public void run() {
Graphics g = getGraphics();
while (isgame) {
drawimage(g);
if(b_serverorclient){
if(b_greenaction){
keypress();
}
}else {
if(b_redaction){
keypress();
}
}keypress();
win();
try {
thread.sleep(200);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void drawimage(Graphics g) {
g.drawImage(background, 0, 0, g.TOP | g.LEFT);
g.setColor(0xFF000000);
if(b_greenaction){
g.drawString("TURN TO GREEN !",40,2,g.TOP|g.LEFT);
} else {
// g.drawString("TURN TO RED!",20,2,g.TOP|g.LEFT);
}
if(b_redaction){
g.drawString("TURN TO RED !",40,2,g.TOP|g.LEFT);
}else {
// g.drawString("TURN TO GREEN!",10,2,g.TOP|g.LEFT);
}
int r = 0;
for (int i = 0; i < 6; i++)
for (int j = 0; j < 7; j++) {
// if(b_serverorclient){
// map[i][j]=ser.status[r];
// r++;
// }else {map[i][j]=cli.status[r];r++;}
if (map[i][j] == server) {
g.drawImage(p1, j * 25, i * 25 + 70, g.TOP | g.LEFT);
}
if (map[i][j] == client) {
g.drawImage(p2, j * 25, i * 25 + 70, g.TOP | g.LEFT);
}
}
make.move(m_x, m_y);
m_x = 0;
m_y = 0;
make.paint(g);
try{
if (redwin) {
g.drawImage(rediswin, 0, 5, g.TOP | g.LEFT);
if (b_serverorclient) {
ser.stop();
}
else {
cli.stop();
}
isgame = false;
}
else if (greenwin) {
g.drawImage(greeniswin, 0, 5, g.TOP | g.LEFT);
if (b_serverorclient) {
ser.stop();
}
else {
cli.stop();
}
isgame = false;
}
else if (nowin) {
g.drawImage(noiswin, 0, 15, g.TOP | g.LEFT);
if (b_serverorclient) {
ser.stop();
}
else {
cli.stop();
}
isgame = false;
}
}catch(Exception e){e.printStackTrace();System.out.println("wewewew");}
flushGraphics();
}
public void keypress() {
int keystates = getKeyStates();
if ((keystates & LEFT_PRESSED) != 0) {
if (make.getX() == 0) {
make.setPosition(150, 35);
} else {
m_x = -25;
m_y = 0;
}
}
if ((keystates & RIGHT_PRESSED) != 0) {
if (make.getX() == 150) {
make.setPosition(0, 35);
} else {
m_x = 25;
m_y = 0;
}
}
if ((keystates & FIRE_PRESSED) != 0) {
// 落棋
// switch (serverorclient) {
// case 1:
// serverorclient = client;
// make.setFrame(1);
// break;
// case 2:
// serverorclient = server;
// make.setFrame(0);
// break;
// }
// sendstatus();
// cli.send1.send("004");
switch (make.getX() / 25) {
case 0:
for (int i = 5; i >= 0; i--) {
if (map[i][0] == 0) {
map[i][0] = serverorclient;
PIECE += 1;
sendstatus();
// if(b_serverorclient){
// ser.send1.send(String.valueOf(map[i][0]));
// }else{cli.send1.send(String.valueOf(map[i][0]));}
break;
}
}
break;
case 1:
for (int i = 5; i >= 0; i--) {
if (map[i][1] == 0) {
map[i][1] = serverorclient;
// if(b_serverorclient){
// ser.send1.send(String.valueOf(map[i][1]));
// }else{cli.send1.send(String.valueOf(map[i][1]));}
PIECE += 1;
sendstatus();
break;
}
}
break;
case 2:
for (int i = 5; i >= 0; i--) {
if (map[i][2] == 0) {
map[i][2] = serverorclient;
// if(b_serverorclient){
// ser.send1.send(String.valueOf(map[i][2]));
// }else{cli.send1.send(String.valueOf(map[i][2]));}
PIECE += 1;
sendstatus();
break;
}
}
break;
case 3:
for (int i = 5; i >= 0; i--) {
if (map[i][3] == 0) {
map[i][3] = serverorclient;
// if(b_serverorclient){
// ser.send1.send(String.valueOf(map[i][3]));
// }else{cli.send1.send(String.valueOf(map[i][3]));}
PIECE += 1;
sendstatus();
break;
}
}
break;
case 4:
for (int i = 5; i >= 0; i--) {
if (map[i][4] == 0) {
map[i][4] = serverorclient;
// if(b_serverorclient){
// ser.send1.send(String.valueOf(map[i][4]));
// }else{cli.send1.send(String.valueOf(map[i][4]));}
PIECE += 1;
sendstatus();
break;
}
}
break;
case 5:
for (int i = 5; i >= 0; i--) {
if (map[i][5] == 0) {
map[i][5] = serverorclient;
// if(b_serverorclient){
// ser.send1.send(String.valueOf(map[i][5]));
// }else{cli.send1.send(String.valueOf(map[i][5]));}
PIECE += 1;
sendstatus();
break;
}
}
break;
case 6:
for (int i = 5; i >= 0; i--) {
if (map[i][6] == 0) {
map[i][6] = serverorclient;
// if(b_serverorclient){
// ser.send1.send(String.valueOf(map[i][6]));
// }else{cli.send1.send(String.valueOf(map[i][6]));}
PIECE += 1;
sendstatus();
break;
}
}
break;
}
if(b_serverorclient){
b_greenaction=false;
b_redaction=true;
ser.send1.send("REDACTION");
}else {
b_greenaction=true;
b_redaction=false;
cli.send1.send("GREENACTION");
}
/** *** */
}
}
/** **************************************** */
public void sendstatus() {
String str = "A";
for (int i = 0; i < 6; i++)
for (int j = 0; j < 7; j++) {
// if(b_serverorclient){
// ser.send1.send(String.valueOf(map[i][j]));
// }else{
// cli.send1.send(String.valueOf(map[i][j]));
// }
str = str + String.valueOf(map[i][j]);
}
if (b_serverorclient) {
ser.send1.send(str);
} else {
cli.send1.send(str);
}
}
/** ************************************** */
public void win() {
if ((PIECE == 21) && (!redwin) && (!greenwin)) {
nowin = true;
}
for (int i = 0; i < 6; i++)
for (int j = 0; j < 7; j++) {
// System.out.println(map[i][j]);
// if((map[i][j]!=0)&&(greenwin=false)&&(redwin=false)){
// nowin=true;
// }
if (map[i][j] == 1) {// 绿色胜利
if ((map[i][j + 1] == 1) && (map[i][j + 2] == 1)
&& (map[i][j + 3] == 1)) {
System.out.println("win~~~~~~heng~~~1~~~");
greenwin = true;//isgame=false;
}
if ((map[i + 1][j] == 1) && (map[i + 2][j] == 1)
&& (map[i + 3][j]) == 1) {
System.out.println("win~~~~~shu~~~~1~~~");
greenwin = true;//isgame=false;
}
if ((map[i + 1][j + 1] == 1) && (map[i + 2][j + 2] == 1)
&& (map[i + 3][j + 3] == 1)) {
System.out.println("win~~~~~xie~~~1~la~~'\'~~");
greenwin = true;//isgame=false;
}
if ((j >= 3) && (map[i + 1][j - 1] == 1)
&& (map[i + 2][j - 2] == 1)
&& (map[i + 3][j - 3] == 1)) {
System.out.println("win~~~~~shu~~1~~pei~'/'~~");
greenwin = true;//isgame=false;
}
}
if (map[i][j] == 2) {// 红色胜利
if ((map[i][j + 1] == 2) && (map[i][j + 2] == 2)
&& (map[i][j + 3] == 2)) {
System.out.println("win~~~~~~heng~2~~~~~");
redwin = true;//isgame=false;
}
if ((map[i + 1][j] == 2) && (map[i + 2][j] == 2)
&& (map[i + 3][j]) == 2) {
System.out.println("win~~~~~shu~~~2~~~~");
redwin = true;//isgame=false;
}
if ((map[i + 1][j + 1] == 2) && (map[i + 2][j + 2] == 2)
&& (map[i + 3][j + 3] == 2)) {
System.out.println("win~~~~~xie~~~2~la~~'\'~~");
redwin = true;//isgame=false;
}
if ((j >= 3) && (map[i + 1][j - 1] == 2)
&& (map[i + 2][j - 2] == 2)
&& (map[i + 3][j - 3] == 2)) {
System.out.println("win~~~~~shu~~2~~pei~'/'~~");
redwin = true;//isgame=false;
}
}
}
// System.out.println("/////////////////");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -