仙剑之十里坡
标题:
java_swing贪吃蛇教程[下]
[打印本页]
作者:
七夕小雨
时间:
2011-5-17 17:03
标题:
java_swing贪吃蛇教程[下]
http://player.youku.com/player.php/sid/XMjY3Njg0NjQw/v.swf
关于java Swing的基本开发教程,以贪吃蛇游戏为实例,但本教程并不是0基础教程,需要有一定的java基础才可以明白。
最终完成的 三个类
snackMain.class
package snack;
import javax.swing.*;
public class snackMain extends JFrame{
public snackMain() {
snackWin win = new snackWin();
add(win);
setTitle("贪吃蛇1.0v---七夕小雨作品");
setSize(435,390);
setLocation(200, 200);
setVisible(true);
}
public static void main(String[] args) {
new snackMain();
}
}
snackWin.class
package snack;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;
public class snackWin extends JPanel implements ActionListener,KeyListener,Runnable{
JButton newGame,stopGame;
int fenShu=0,Speed=0;
boolean start = false;
Random r = new Random();
int rx = 0,ry=0;
List<snackAct> list = new ArrayList<snackAct>();
int temp =0,tempeat1=0,tempeat2=0;
JDialog dialog = new JDialog();
JLabel label = new JLabel("你挂了你的分数是"+fenShu);
JButton ok = new JButton("好吧= .=");
Thread nThread;
public snackWin() {
newGame = new JButton("开始");
stopGame =new JButton("退出");
this.setLayout(new FlowLayout(FlowLayout.LEFT));
newGame.addActionListener(this);
stopGame.addActionListener(this);
this.addKeyListener(this);
this.add(newGame);
this.add(stopGame);
ok.addActionListener(this);
dialog.setLayout(new GridLayout(2,1));
dialog.add(label);
dialog.add(ok);
dialog.setSize(200,200);
dialog.setLocation(200, 200);
dialog.setVisible(false);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawRect(10, 40, 400, 300);
g.drawString("分数:"+fenShu, 150, 20);
g.drawString("速度:"+Speed, 150, 35);
g.setColor(new Color(0,255,0));
if (start) {
g.fillRect(10+rx*10, 40+ry*10, 10, 10);
g.setColor(new Color(255,0,0));
for (int i = 0; i < list.size(); i++) {
g.fillRect(10+list.get(i).getX()*10, 40+list.get(i).getY()*10, 10, 10);
}
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource()==newGame) {
newGame.setEnabled(false);
start = true;
rx = r.nextInt(40);ry = r.nextInt(30);
snackAct tempAct = new snackAct();
tempAct.setX(20);
tempAct.setY(15);
list.add(tempAct);
requestFocus(true);
nThread = new Thread(this);
nThread.start();
repaint();
}
if (e.getSource()==stopGame) {
System.exit(0);
}
if (e.getSource()==ok) {
list.clear();
fenShu = 0;
Speed = 0;
start = false;
newGame.setEnabled(true);
dialog.setVisible(false);
repaint();
}
}
private void eat() {
if (rx==list.get(0).getX()&&ry==list.get(0).getY()) {
rx = r.nextInt(40);ry = r.nextInt(30);
snackAct tempAct = new snackAct();
tempAct.setX(list.get(list.size()-1).getX());
tempAct.setY(list.get(list.size()-1).getY());
list.add(tempAct);
fenShu +=100 +10*Speed;
tempeat1 +=1;
if (tempeat1-tempeat2>=10) {
tempeat2 =tempeat1;
if (Speed <=9) {
Speed+=1;
}
}
}
}
public void otherMove(){
snackAct tempAct = new snackAct();
for (int i = 0; i < list.size(); i++) {
if (i==1) {
list.get(i).setX(list.get(0).getX());
list.get(i).setY(list.get(0).getY());
}else if (i>1) {
tempAct = list.get(i-1);
list.set(i-1, list.get(i));
list.set(i, tempAct);
}
}
}
public void move(int x,int y){
if (minYes(x, y)) {
otherMove();
list.get(0).setX(list.get(0).getX()+x);
list.get(0).setY(list.get(0).getY()+y);
eat();
repaint();
}else {//死亡方法
nThread = null;
label.setText("你挂了你的分数是"+fenShu);
dialog.setVisible(true);
}
}
public boolean minYes(int x,int y){
if (!maxYes(list.get(0).getX()+x, list.get(0).getY()+y)) {
return false;
}
return true;
}
public boolean maxYes(int x,int y){
if (x<0||x>=40||y<0||y>=30) {
return false;
}
for (int i = 0; i < list.size(); i++) {
if (i>1&&list.get(0).getX()==list.get(i).getX()&&list.get(0).getY()==list.get(i).getY()) {
return false;
}
}
return true;
}
public void keyPressed(KeyEvent e) {
if (start) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
move(0, -1);
temp =1;
break;
case KeyEvent.VK_DOWN:
move(0, 1);
temp =2;
break;
case KeyEvent.VK_LEFT:
move(-1, 0);
temp =3;
break;
case KeyEvent.VK_RIGHT:
move(1, 0);
temp =4;
break;
default:
break;
}
}
}
public void keyReleased(KeyEvent arg0) {}
public void keyTyped(KeyEvent arg0) {}
public void run() {
while (start) {
switch (temp) {
case 1:
move(0, -1);
break;
case 2:
move(0, 1);
break;
case 3:
move(-1, 0);
break;
case 4:
move(1, 0);
break;
default:
move(1, 0);
break;
}
fenShu +=10*Speed;
repaint();
try {
Thread.sleep(500-(50*Speed));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
snackAct.class
package snack;
public class snackAct {
private int x;
private int y;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
作者:
景雪
时间:
2011-5-17 17:24
有木有CSS教程神马的
作者:
御剑天河
时间:
2011-5-17 18:22
不知道神马玩意.........
作者:
慕容晓佳
时间:
2011-5-17 18:46
小雨哥做贪吃蛇。。。
作者:
林间御风
时间:
2011-5-17 21:30
话说JAVA 对我手机一点用处没有- =
作者:
woaizhong
时间:
2011-5-22 10:38
路过看看先留下记号的啊,以后有时间再来看的啊……
欢迎光临 仙剑之十里坡 (http://palslp.com/BBS/)
Powered by Discuz! X2.5