代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
双人贪吃蛇游戏
作者:
彧福
/ 发布于
2015/5/11
/
737
package snake; import java.awt.*; import java.awt.event.*; public class SnakeGuide implements ItemListener, AdjustmentListener { static SnakeGuide snakeGuide; static Runnable game; static Thread gamerun; Frame framestart; // 开始 Panel pannum, pancho, panw, panh, panbtn; Checkbox cbsin, cbdou, cbcro, cbucr; // 单选:单人,双人;可穿墙,不穿墙 Scrollbar slw, slh; // 滚动条:宽度,高度 Label lbnum, lbcro, lbwid, lbhei; // 标签:人数,模式,宽度,高度 Label lbhp1, lbhp2, lbhp3, lbhp4, lbhp5; TextField tfw, tfh; // 文本框:宽度,高度 Button btncfm, btnccl; // 按钮:确认,取消 Frame frameend; // 结束 Label msg; // 询问 Panel panyn; Button btny, btnn; // 是,否 static boolean num; static boolean choice; static int w, h; public SnakeGuide() { num = true; choice = true; init(); } public void init() { num = true; choice = true; w = 60; h = 40; // 实例化 lbhp1 = new Label("......wasd或者8456,空格暂停"); lbhp2 = new Label("......黄色的是普通食物,一个五分,会使蛇身变长"); lbhp3 = new Label("......红色是奖励食物,吃到的分数是倒计时所剩的时间"); lbhp4 = new Label("......绿色是草,分数随机"); lbhp5 = new Label("......黑色是岩石,撞上就结束啦"); pannum = new Panel(); lbnum = new Label("人数"); CheckboxGroup cbgnum = new CheckboxGroup(); cbsin = new Checkbox("单人", cbgnum, true); cbsin.addItemListener(this); cbdou = new Checkbox("双人", cbgnum, false); cbdou.addItemListener(this); pannum.add(lbnum); pannum.add(cbsin); pannum.add(cbdou); pancho = new Panel(); lbcro = new Label("模式"); CheckboxGroup cbgcho = new CheckboxGroup(); cbcro = new Checkbox("可穿墙", cbgcho, true); cbcro.addItemListener(this); cbucr = new Checkbox("不可穿墙", cbgcho, false); cbucr.addItemListener(this); pancho.add(lbcro); pancho.add(cbcro); pancho.add(cbucr); panw = new Panel(); lbwid = new Label("宽度"); slw = new Scrollbar(Scrollbar.HORIZONTAL, 60, 5, 10, 65); slw.addAdjustmentListener(this); tfw = new TextField("60", 5); tfw.setEditable(false); panw.add(lbwid); panw.add(tfw); panh = new Panel(); lbhei = new Label("高度"); slh = new Scrollbar(Scrollbar.HORIZONTAL, 40, 3, 10, 43); slh.addAdjustmentListener(this); tfh = new TextField("40", 5); tfh.setEditable(false); panh.add(lbhei); panh.add(tfh); panbtn = new Panel(); btncfm = new Button("确定"); btncfm.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { framestart.setVisible(false); framestart.dispose(); game = new Game(num, choice, w, h); gamerun = new Thread(game); gamerun.start(); } }); btnccl = new Button("退出"); btnccl.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { framestart.setVisible(false); framestart.dispose(); System.exit(0); } }); panbtn.add(btncfm); panbtn.add(btnccl); framestart = new Frame("选项设置"); framestart.setLayout(new GridLayout(12, 1)); framestart.add(lbhp1); framestart.add(lbhp2); framestart.add(lbhp3); framestart.add(lbhp4); framestart.add(lbhp5); framestart.add(pannum); framestart.add(pancho); framestart.add(panw); framestart.add(slw); framestart.add(panh); framestart.add(slh); framestart.add(panbtn); framestart.setSize(400, 360); framestart.setResizable(false); framestart.setLocationRelativeTo(null); framestart.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { framestart.setVisible(false); framestart.dispose(); System.exit(0); } }); msg = new Label("是否继续?"); panyn = new Panel(); btny = new Button("是"); btnn = new Button("否"); btny.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { frameend.setVisible(false); frameend.dispose(); framestart.setVisible(true); } }); btnn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { frameend.setVisible(false); frameend.dispose(); System.exit(0); } }); frameend = new Frame(); frameend.setLayout(new BorderLayout()); frameend.add(BorderLayout.NORTH, msg); panyn.add(btny); panyn.add(btnn); frameend.add(BorderLayout.SOUTH, panyn); frameend.setSize(100, 100); frameend.setResizable(false); frameend.setLocationRelativeTo(null); frameend.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { frameend.setVisible(false); frameend.dispose(); System.exit(0); } }); framestart.setVisible(true); frameend.setVisible(false); } public void adjustmentValueChanged(AdjustmentEvent eve) { if (eve.getSource() == slw) { tfw.setText(Integer.toString(((Scrollbar) eve.getSource()) .getValue())); w = ((Scrollbar) eve.getSource()).getValue(); } if (eve.getSource() == slh) { tfh.setText(Integer.toString(((Scrollbar) eve.getSource()) .getValue())); h = ((Scrollbar) eve.getSource()).getValue(); } } public void itemStateChanged(ItemEvent eve) { if (eve.getSource() == cbsin) { num = true; lbhp1.setText("......wasd或者8456,空格暂停"); } if (eve.getSource() == cbdou) { num = false; lbhp1.setText("......player1:wasd,player2:8456或者ijkl,空格暂停"); } if (eve.getSource() == cbcro) { choice = true; } if (eve.getSource() == cbucr) { choice = false; } } public static void main(String[] args) { snakeGuide = new SnakeGuide(); } } 2. [文件] Food.java ~ 7KB 下载(8) ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 package snake; import java.awt.Color; import java.awt.Point; import javax.swing.JOptionPane; import snake.Snake.Body; public class Food { protected static int num = 0; public static int result = 0; protected static Node general = new Food.Node(1); protected static Node special = new Food.Node(2); protected static Node grass = new Food.Node(3); protected static Node rock = new Food.Node(4); Food() { num = 0; result = 0; } static class Node { Color c; Point p; int score; public Node(int i) { if (i == 1) { this.c = Color.YELLOW; this.p = null; this.score = 5; } if (i == 2) { this.c = Color.RED; this.p = null; this.score = 100; } if (i == 3) { this.c = Color.GREEN; this.p = null; this.score = (int) (100 * Math.random() - 50); } if (i == 4) { this.c = Color.BLACK; this.p = null; this.score = 0; } } } public void setGeneralFood(Snake snake1, Snake snake2) { general = new Node(1); general.p = snake1.head.p; if (snake2 == null) { Body temp1 = snake1.head; while (temp1 != null) { if (temp1.p.equals(general.p)) { general.p = new Point( (int) ((Map.width - 1) * 2.0 * (Math.random() - 0.5)), (int) ((Map.height - 1) * 2.0 * (Math.random() - 0.5))); temp1 = snake1.head; } else temp1 = temp1.next; } } else { Body temp = snake1.head; while (temp != null) { if (temp.p.equals(general.p)) { general.p = new Point( (int) ((Map.width - 1) * 2.0 * (Math.random() - 0.5)), (int) ((Map.height - 1) * 2.0 * (Math.random() - 0.5))); temp = snake1.head; } else if (temp == snake1.tail) temp = snake2.head; else temp = temp.next; } } } private static void setSpecialFood(Snake snake1, Snake snake2) { special = new Node(2); special.p = snake1.head.p; if (snake2 == null) { Body temp1 = snake1.head; while (temp1 != null) { if (temp1.p.equals(special.p)) { special.p = new Point( (int) ((Map.width - 1) * 2.0 * (Math.random() - 0.5)), (int) ((Map.height - 1) * 2.0 * (Math.random() - 0.5))); temp1 = snake1.head; } else temp1 = temp1.next; } } else { Body temp = snake1.head; while (temp != null) { if (temp.p.equals(special.p)) { special.p = new Point( (int) ((Map.width - 1) * 2.0 * (Math.random() - 0.5)), (int) ((Map.height - 1) * 2.0 * (Math.random() - 0.5))); temp = snake1.head; } else if (temp == snake1.tail) temp = snake2.head; else temp = temp.next; } } } private static void setGrassFood(Snake snake1, Snake snake2) { grass = new Node(3); grass.p = snake1.head.p; if (snake2 == null) { Body temp1 = snake1.head; while (temp1 != null) { if (temp1.p.equals(grass.p)) { grass.p = new Point( (int) ((Map.width - 1) * 2.0 * (Math.random() - 0.5)), (int) ((Map.height - 1) * 2.0 * (Math.random() - 0.5))); temp1 = snake1.head; } else temp1 = temp1.next; } } else { Body temp = snake1.head; while (temp != null) { if (temp.p.equals(grass.p)) { grass.p = new Point( (int) ((Map.width - 1) * 2.0 * (Math.random() - 0.5)), (int) ((Map.height - 1) * 2.0 * (Math.random() - 0.5))); temp = snake1.head; } else if (temp == snake1.tail) temp = snake2.head; else temp = temp.next; } } } private static void setRockFood(Snake snake1, Snake snake2) { rock = new Node(4); rock.p = snake1.head.p; if (snake2 == null) { Body temp1 = snake1.head; while (temp1 != null) { if (temp1.p.equals(rock.p)) { rock.p = new Point( (int) ((Map.width - 1) * 2.0 * (Math.random() - 0.5)), (int) ((Map.height - 1) * 2.0 * (Math.random() - 0.5))); temp1 = snake1.head; } else temp1 = temp1.next; } } else { Body temp = snake1.head; while (temp != null) { if (temp.p.equals(rock.p)) { rock.p = new Point( (int) ((Map.width - 1) * 2.0 * (Math.random() - 0.5)), (int) ((Map.height - 1) * 2.0 * (Math.random() - 0.5))); temp = snake1.head; } else if (temp == snake1.tail) temp = snake2.head; else temp = temp.next; } } } public static void drawFood() { StdDraw.setPenColor(general.c); StdDraw.filledSquare(general.p.x, general.p.y, 0.5); if (special.p != null) { StdDraw.setPenColor(special.c); StdDraw.filledCircle(special.p.x, special.p.y, 0.5); } if (grass.p != null) { StdDraw.setPenColor(grass.c); StdDraw.filledCircle(grass.p.x, grass.p.y, 0.5); } if (rock.p != null) { StdDraw.setPenColor(rock.c); StdDraw.filledSquare(rock.p.x, rock.p.y, 0.5); } } public void eatFood(Snake snake1, Snake snake2) { if (snake1.head.p.equals(Food.general.p)) { Body temp = new Body(1); temp.p = new Point(snake1.tail.p.x, snake1.tail.p.y); temp.last = snake1.tail; snake1.tail.next = temp; snake1.tail = temp; result += Food.general.score; num++; setGeneralFood(snake1, snake2); if (num % 5 == 0) setSpecialFood(snake1, snake2); if (num % 3 == 0) setGrassFood(snake1, snake2); if (num % 2 == 0) setRockFood(snake1, snake2); } if (snake1.head.p.equals(Food.special.p)) { result += Food.special.score; Food.special.p = null; } if (snake1.head.p.equals(Food.grass.p)) { result += Food.grass.score; Food.grass.p = null; } if (snake2 != null) { if (snake2.head.p.equals(Food.general.p)) { Body temp = new Body(2); temp.p = new Point(snake2.tail.p.x, snake2.tail.p.y); temp.last = snake2.tail; snake2.tail.next = temp; snake2.tail = temp; result += Food.general.score; num++; setGeneralFood(snake1, snake2); if (num % 5 == 0) setSpecialFood(snake1, snake2); if (num % 3 == 0) setGrassFood(snake1, snake2); if (num % 2 == 0) setRockFood(snake1, snake2); } if (snake2.head.p.equals(Food.special.p)) { result += Food.special.score; Food.special.p = null; } if (snake2.head.p.equals(Food.grass.p)) { result += Food.grass.score; Food.grass.p = null; } } if (Food.special.p != null) { Food.special.score--; if (Food.special.score == 0) { Food.special.p = null; } } } public void sleep(Snake snake1, Snake snake2) { int sum = result; if (snake2 != null) sum += result; StdDraw.show(250 - sum > 50 ? 250 - sum : 50); } public void pause() { JOptionPane.showMessageDialog(null, "днЭ?жа..МЬа??П"); } } package snake; public class Game implements Runnable { boolean choice; boolean num; int w, h; public Game(boolean num, boolean choice, int w, int h) { super(); this.choice = choice; this.num = num; this.w = w; this.h = h; } public void run() { Food food = new Food(); if (num) { Map.drawmap(w, h); Snake snake1 = new Snake(1); snake1.drawSnake(); food.setGeneralFood(snake1, null); while (snake1.alive(choice, null)) { char in = 'n'; if (StdDraw.hasNextKeyTyped()) { switch (StdDraw.nextKeyTyped()) { case 'w': case '8': in = 'u'; break; case 'a': case '4': in = 'l'; break; case 's': case '5': in = 'd'; break; case 'd': case '6': in = 'r'; break; case ' ': food.pause(); break; default: in = 'n'; break; } } food.eatFood(snake1, null); snake1.move(in, choice); Map.draw(choice, snake1, null); food.sleep(snake1, null); } StdDraw.show(1000); StdDraw.show(); StdDraw.clear(); StdDraw.text(0, 2, "Your score is " + Food.result); StdDraw.show(); } else { Map.drawmap(w, h); Snake snake1 = new Snake(1); Snake snake2 = new Snake(2); snake1.drawSnake(); snake2.drawSnake(); food.setGeneralFood(snake1, null); while (snake1.alive(choice, snake2) && snake2.alive(choice, snake1)) { char in0 = 'n', in1 = 'n'; if (StdDraw.hasNextKeyTyped()) { switch (StdDraw.nextKeyTyped()) { case 'w': in0 = 'u'; break; case 'a': in0 = 'l'; break; case 's': in0 = 'd'; break; case 'd': in0 = 'r'; break; case 'i': case '8': in1 = 'u'; break; case 'j': case '4': in1 = 'l'; break; case 'k': case '5': in1 = 'd'; break; case 'l': case '6': in1 = 'r'; break; case ' ': food.pause(); break; default: in0 = in1 = 'n'; break; } } food.eatFood(snake1, snake2); snake1.move(in0, choice); snake2.move(in1, choice); Map.draw(choice, snake1, snake2); food.sleep(snake1, snake2); } StdDraw.show(1000); StdDraw.show(); StdDraw.clear(); if (snake1.alive(choice, snake2)) StdDraw.text(0, 2, "Play1 Win! The score is " + Food.result); else if (snake2.alive(choice, snake1)) StdDraw.text(0, 2, "Play2 Win! The score is " + Food.result); else StdDraw.text(0, 2, "No one Win! "); StdDraw.show(); } SnakeGuide.snakeGuide.frameend.setVisible(true); } } package snake; import java.awt.Color; public class Map { static int height, width; static double[] x = { -width, -width, width, width }; static double[] y = { -height, height, height, -height }; public static void draw(boolean b, Snake snake1, Snake snake2) { StdDraw.clear(); StdDraw.setPenColor(Color.lightGray); StdDraw.filledSquare(0, 0, 50); Food.drawFood(); snake1.drawSnake(); if (snake2 != null) snake2.drawSnake(); if (b) StdDraw.setPenColor(Color.ORANGE); else StdDraw.setPenColor(Color.BLACK); StdDraw.setPenRadius(0.02); StdDraw.polygon(x, y); StdDraw.setPenRadius(); StdDraw.setPenColor(Color.red); StdDraw.textLeft((double) -width, (double) height + 1, "score:" + Food.result); StdDraw.text(0.0, (double) height + 1, "Grade:" + (Food.result / 10 > 25 ? 25 : Food.result / 10)); if (Food.special.p != null) StdDraw.textRight((double) width, (double) height + 1, "time:" + Food.special.score); // if(Food.grass.p!=null) // StdDraw.text(10.0, (double)height+1, "grass:"+Food.grass.score); StdDraw.setPenColor(Food.general.c); StdDraw.filledSquare(-width, height + 2.3, 0.5); StdDraw.textLeft(0.5 - width, height + 2.3, "general"); StdDraw.setPenColor(Food.special.c); StdDraw.filledSquare(-width / 2, height + 2.3, 0.5); StdDraw.textLeft(0.5 - width / 2, height + 2.3, "special"); StdDraw.setPenColor(Food.grass.c); StdDraw.filledSquare(0, height + 2.3, 0.5); StdDraw.textLeft(0.5, height + 2.3, "grass"); StdDraw.setPenColor(Food.rock.c); StdDraw.filledSquare(width / 2, height + 2.3, 0.5); StdDraw.textLeft(0.5 + width / 2, height + 2.3, "rock"); } public static void drawmap(int wt, int ht) { width = wt / 2; height = ht / 2; x[0] = -width; x[1] = -width; x[2] = width; x[3] = width; y[0] = -height; y[1] = height; y[2] = height; y[3] = -height; int w = width * 600 / (height + 1); int h = 600; StdDraw.setCanvasSize(w, h); StdDraw.setXscale(-width, width); StdDraw.setYscale(-height, height + 2); } } package snake; import java.awt.*; public class Snake { protected char direction = 'l'; protected Body head = null; protected Body tail = null; protected boolean life = true; protected static class Body { Point p; Color c; Body last; Body next; public Body(int i) { if (i == 1) this.c = Color.CYAN; else this.c = Color.PINK; } } Snake(int i) { head = new Body(i); if (i == 1) head.c = Color.BLUE; else head.c = Color.RED; head.p = new Point(-1, 2 * i - 3); head.next = new Body(i); head.next.p = new Point(0, 2 * i - 3); head.next.next = new Body(i); head.next.next.p = new Point(1, 2 * i - 3); tail = head; while (tail.next != null) { tail.next.last = tail; tail = tail.next; } } private void draw(Body temp) { StdDraw.setPenColor(temp.c); StdDraw.filledCircle(temp.p.x, temp.p.y, 0.6); } public void drawSnake() { Body temp = head; do { draw(temp); temp = temp.next; } while (temp != null); } public boolean alive(boolean b, Snake another) { Body temp = head.next; if (another != null) { if (head.p.equals(another.head.p)) { life = false; another.life = false; return false; } } do { if (head.p.equals(temp.p)) { life = false; return life; } temp = temp.next; } while (temp != null); if (!b) { if (head.p.x >= Map.width || head.p.x <= -Map.width || head.p.y >= Map.height || head.p.y <= -Map.height) { life = false; return life; } } if (another != null) { temp = another.head; do { if (head.p.equals(temp.p)) { life = false; return life; } temp = temp.next; } while (temp != null); } if (head.p.equals(Food.rock.p)) life = false; return life; } public void move(char in, boolean b) { if (direction == 'u' || direction == 'd') { if (in == 'l') { moveleft(b); return; } if (in == 'r') { moveright(b); return; } movestill(b); return; } else { if (in == 'u') { moveup(b); return; } if (in == 'd') { movedown(b); return; } movestill(b); return; } } private void moveup(boolean b) { Body temp = tail; while (temp != head) { temp.p = temp.last.p; temp = temp.last; } if (head.next.p.y + 1 >= Map.height && b) head.p = new Point(head.next.p.x, -Map.height + 1); else head.p = new Point(head.next.p.x, head.next.p.y + 1); direction = 'u'; return; } private void movedown(boolean b) { Body temp = tail; while (temp != head) { temp.p = temp.last.p; temp = temp.last; } if (head.next.p.y - 1 <= -Map.height && b) head.p = new Point(head.next.p.x, Map.height - 1); else head.p = new Point(head.next.p.x, head.next.p.y - 1); direction = 'd'; return; } private void moveleft(boolean b) { Body temp = tail; while (temp != head) { temp.p = temp.last.p; temp = temp.last; } if (head.next.p.x - 1 <= -Map.width && b) head.p = new Point(Map.width - 1, head.next.p.y); else head.p = new Point(head.next.p.x - 1, head.next.p.y); direction = 'l'; return; } private void moveright(boolean b) { Body temp = tail; while (temp != head) { temp.p = temp.last.p; temp = temp.last; } if (head.next.p.x + 1 >= Map.width && b) head.p = new Point(-Map.width + 1, head.next.p.y); else head.p = new Point(head.next.p.x + 1, head.next.p.y); direction = 'r'; return; } private void movestill(boolean b) { switch (direction) { case 'u': moveup(b); return; case 'd': movedown(b); return; case 'l': moveleft(b); return; case 'r': moveright(b); return; } } }
试试其它关键字
贪吃蛇
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
彧福
贡献的其它代码
(
11
)
.
数字的加减乘除
.
获取json数据所有的节点路径
.
使用xlrd读取excel并返回json
.
NxN方块排序,可自动运行
.
双人贪吃蛇游戏
.
实现单链表
.
打电话
.
广播实例
.
统计多台linux的CPU使用率
.
wifi 掉线自动重连
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3