仙剑之十里坡
标题:
java_swing贪吃蛇教程[上]
[打印本页]
作者:
七夕小雨
时间:
2011-5-17 00:04
标题:
java_swing贪吃蛇教程[上]
这个帖子我竟然不知道发到哪里~~就扔水区了~呵呵~管他呢~
http://player.youku.com/player.php/sid/XMjY3NTQ0MDI0/v.swf
关于java Swing的基本开发教程,以贪吃蛇游戏为实例,但本教程并不是0基础教程,需要有一定的java基础才可以明白。
这一讲 三个类的源代码
snackMain类
package game;
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();
}
}
snckaWin类
package game;
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{
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>();
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);
}
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);
repaint();
}
if (e.getSource()==stopGame) {
System.exit(0);
}
}
public void move(int x,int y){
if (minYes(x, y)) {
list.get(0).setX(list.get(0).getX()+x);
list.get(0).setY(list.get(0).getY()+y);
repaint();
}else {
}
}
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;
}
return true;
}
public void keyPressed(KeyEvent e) {
if (start) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
move(0, -1);
break;
case KeyEvent.VK_DOWN:
move(0, 1);
break;
case KeyEvent.VK_LEFT:
move(-1, 0);
break;
case KeyEvent.VK_RIGHT:
move(1, 0);
break;
default:
break;
}
}
}
public void keyReleased(KeyEvent arg0) {}
public void keyTyped(KeyEvent arg0) {}
}
snackAct类
package game;
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 07:33
……
看到代码就头痛...学c++学得内伤了
作者:
七千
时间:
2011-5-17 12:46
于是小雨- -你最近就在鼓捣这玩意么..
作者:
黄泉
时间:
2011-5-17 12:51
标题:
null
完全不明白…茫然中…
作者:
御剑天河
时间:
2011-5-17 18:22
不知道神马玩意.........
作者:
BlackFeather
时间:
2011-5-17 19:32
满是代码啊
欢迎光临 仙剑之十里坡 (http://palslp.com/BBS/)
Powered by Discuz! X2.5