java 双人贪吃蛇
关注:72 答案:2 mip版
解决时间 2021-02-02 15:15
提问者眼泪↓流到叻心里
2021-02-01 19:45
有人能给我一个程序参考下么?自己写的程序有问题。希望有人能给我一份代码参考一下。
最佳答案
二级知识专家久愛不厭
2021-02-01 21:18
package game;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class HsGame extends JFrame {
HsGame(String str) {
// 设置主面版
this.setTitle(str);
this.setSize(910, 630);
this.setLocation(50, 50);
MyPanel mp = new MyPanel();
this.add(mp);
this.addKeyListener(mp);
new Thread(mp).start();
}
// 主线程
public static void main(String[] args) {
HsGame m = new HsGame("贪吃蛇竞技版");
m.setVisible(true);
}
}
class MyPanel extends JPanel implements Runnable, KeyListener {
int[][] s1 = new int[180][120];
int[][] s2 = new int[180][120];
int x1 = 40;
int y1 = 60;
int x2 = 140;
int y2 = 60;
// 设置线程休眠时间
int sleep = 50;
// 设置初始方向
int fangxiang1 = 2;
int fangxiang2 = 0;
// 构造函数
MyPanel() {
// 初始化界面
for (int ix = 0; ix < 180; ix++) {
for (int iy = 0; iy < 120; iy++) {
s1[ix][iy] = 0;
s2[ix][iy] = 0;
}
}
// 设置边界
// 上下边界
for (int ix = 0; ix < 180; ix++) {
s1[ix][0] = 1;
s1[ix][119] = 1;
s2[ix][0] = 1;
s2[ix][119] = 1;
}
// 左右边界
for (int iy = 0; iy < 120; iy++) {
s1[0][iy] = 1;
s1[179][iy] = 1;
s2[0][iy] = 1;
s2[179][iy] = 1;
}
}
// 画板
public void paint(Graphics g) {
// 显示玩家一
g.setColor(Color.red);
for (int ix = 0; ix < 180; ix++) {
for (int iy = 0; iy < 120; iy++) {
if (s1[ix][iy] == 1) {
g.fillRect(ix * 5, iy * 5, 5, 5);
}
}
}
// 显示玩家二
g.setColor(Color.blue);
for (int ix = 0; ix < 180; ix++) {
for (int iy = 0; iy < 120; iy++) {
if (s2[ix][iy] == 1) {
g.fillRect(ix * 5, iy * 5, 5, 5);
}
}
}
// 画出边界
g.setColor(Color.black);
for (int i = 0; i < 180; i++) {
g.fillRect(i * 5, 0, 5, 5);
g.fillRect(i * 5, 119 * 5, 5, 5);
}
for (int i = 0; i < 120; i++) {
g.fillRect(0, i * 5, 5, 5);
g.fillRect(179 * 5, i * 5, 5, 5);
}
}
public void run() {
while (true) {
switch (fangxiang1) {
case 0:
x1--;
break;
case 1:
y1--;
break;
case 2:
x1++;
break;
case 3:
y1++;
}
switch (fangxiang2) {
case 0:
x2--;
break;
case 1:
y2--;
break;
case 2:
x2++;
break;
case 3:
y2++;
}
// 判断死活
if (s1[x1][y1] == 1 || s2[x1][y1] == 1) {
// 弹出player2胜利对话框
JOptionPane.showMessageDialog(null,
"player 2 win!",
"Game Over !", JOptionPane.ERROR_MESSAGE);
// 结束游戏
System.exit(0);
} else {
s1[x1][y1] = 1;
}
if (s2[x2][y2] == 1 || s1[x2][y2] == 1) {
// 弹出player1胜利对话框
JOptionPane.showMessageDialog(null,
"player 1 win!",
"Game Over !", JOptionPane.ERROR_MESSAGE);
// 结束游戏
System.exit(0);
} else {
s2[x2][y2] = 1;
}
try {
Thread.sleep(sleep);
} catch (Exception e) {
e.printStackTrace();
}
// 重画
this.repaint();
}
}
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
// 玩家一控制
if (e.getKeyCode() == 65) {
if (fangxiang1 != 2) {
fangxiang1 = 0;
System.out.println("player 1 left");
}
}
if (e.getKeyCode() == 87) {
if (fangxiang1 != 3) {
fangxiang1 = 1;
System.out.println("player 1 up");
}
}
if (e.getKeyCode() == 68) {
if (fangxiang1 != 0) {
fangxiang1 = 2;
System.out.println("player 1 right");
}
}
if (e.getKeyCode() == 83) {
if (fangxiang1 != 1) {
fangxiang1 = 3;
System.out.println("player 1 down");
}
}
// 玩家二控制
if (e.getKeyCode() == 37) {
if (fangxiang2 != 2) {
fangxiang2 = 0;
System.out.println("player 2 left");
}
}
if (e.getKeyCode() == 38) {
if (fangxiang2 != 3) {
fangxiang2 = 1;
System.out.println("player 2 up");
}
}
if (e.getKeyCode() == 39) {
if (fangxiang2 != 0) {
fangxiang2 = 2;
System.out.println("player 2 right");
}
}
if (e.getKeyCode() == 40) {
if (fangxiang2 != 1) {
fangxiang2 = 3;
System.out.println("player 2 down");
}
}
}
public void keyReleased(KeyEvent e) {
}
}
全部回答
1楼全球变暖丶我心却变冷
2021-02-01 21:39
自己写着玩的,很简单,你试一试哦...
主要用了javax.swing.timer这个类:
import java.awt.*;
import javax.swing.*;
@suppresswarnings("serial")
public class mainclass extends jframe {
controlsnake control;
toolkit kit;
dimension dimen;
public static void main(string[] args) {
new mainclass("my snake");
}
public mainclass(string s) {
super(s);
control = new controlsnake();
control.setfocusable(true);
kit = toolkit.getdefaulttoolkit();
dimen = kit.getscreensize();
add(control);
setlayout(new borderlayout());
setlocation(dimen.width / 3, dimen.height / 3);// dimen.width/3,dimen.height/3
setsize(fwidth, fheight);
setdefaultcloseoperation(jframe.exit_on_close);
setresizable(false);
setvisible(true);
}
public static final int fwidth = 315;
public static final int fheight = 380;
}
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.timer;
import java.util.random;
@suppresswarnings("serial")
public class controlsnake extends jpanel implements actionlistener {
random rand;
arraylistlist, listbody;
string str, str1;
static boolean key;
int x, y, dx, dy, fx, fy, flag;
int snakebody;
int speed;
public controlsnake() {
snakebody = 1;
str = "上下左右方向键控制 p键暂停...";
str1 = "现在的长度为:" + snakebody;
key = true;
flag = 1;
speed = 700;
rand = new random();
list = new arraylist();
listbody = new arraylist();
x = 5;
y = 5;
list.add(new point(x, y));
listbody.add(list.get(0));
dx = 10;
dy = 0;
fx = rand.nextint(30) * 10 + 5;// 2
fy = rand.nextint(30) * 10 + 5;// 2
setbackground(color.white);
setsize(new dimension(318, 380));
final timer time = new timer(speed, this);
time.start();
addkeylistener(new keyadapter() {
public void keypressed(keyevent e) {
if (e.getkeycode() == 37) {
dx = -10;
dy = 0;
} else if (e.getkeycode() == 38) {
dx = 0;
dy = -10;
} else if (e.getkeycode() == 39) {
dx = 10;
dy = 0;
} else if (e.getkeycode() == 40) {
dx = 0;
dy = 10;
} else if (e.getkeycode() == 80) {
if (flag % 2 == 1) {
time.stop();
}
if (flag % 2 == 0) {
time.start();
}
flag++;
}
}
});
}
public void paint(graphics g) {
g.setcolor(color.white);
g.fillrect(0, 0, 400, 400);
g.setcolor(color.dark_gray);
g.drawline(3, 3, 305, 3);
g.drawline(3, 3, 3, 305);
g.drawline(305, 3, 305, 305);
g.drawline(3, 305, 305, 305);
g.setcolor(color.pink);
for (int i = 0; i < listbody.size(); i++) {
g.fillrect(listbody.get(i).x, listbody.get(i).y, 9, 9);
}
g.fillrect(x, y, 9, 9);
g.setcolor(color.orange);
g.fillrect(fx, fy, 9, 9);
g.setcolor(color.dark_gray);
str1 = "现在的长度为:" + snakebody;
g.drawstring(str, 10, 320);
g.drawstring(str1, 10, 335);
}
public void actionperformed(actionevent e) {
x += dx;
y += dy;
if (makeout() == false) {
joptionpane.showmessagedialog(null, "重新开始......");
speed = 700;
snakebody = 1;
x = 5;
y = 5;
list.clear();
list.add(new point(x, y));
listbody.clear();
listbody.add(list.get(0));
dx = 10;
dy = 0;
}
addpoint(x, y);
if (x == fx && y == fy) {
speed = (int) (speed * 0.8);//速度增加参数
if (speed < 200) {
speed = 100;
}
fx = rand.nextint(30) * 10 + 5;// 2
fy = rand.nextint(30) * 10 + 5;// 2
snakebody++;// 2
} // 2
repaint();
}
public void addpoint(int xx, int yy) {
// 动态的记录最新发生的50步以内的移动过的坐标
// 并画出最新的snakebody
if (list.size() < 100) {//蛇身长度最长为100
list.add(new point(xx, yy));
} else {
list.remove(0);
list.add(new point(xx, yy));
}
if (snakebody == 1) {
listbody.remove(0);
listbody.add(0, list.get(list.size() - 1));
} else {
listbody.clear();
if (list.size() < snakebody) {
for (int i = list.size() - 1; i > 0; i--) {
listbody.add(list.get(i));
}
} else {
for (int i = list.size() - 1; listbody.size() < snakebody; i--) {
listbody.add(list.get(i));
}
}
}
}
public boolean makeout() {
if ((x < 3 || y < 3) || (x > 305 || y > 305)) {
return false;
}
for (int i = 0; i < listbody.size() - 1; i++) {
for (int j = i + 1; j < listbody.size(); j++) {
if (listbody.get(i).equals(listbody.get(j))) {
return false;
}
}
}
return true;
}
}
我要举报
如以上问答内容为低俗/色情/暴力/不良/侵权的信息,可以点下面链接进行举报,我们会做出相应处理,感谢你的支持!
→点此我要举报以上信息!←
推荐资讯
大家都在看