- UID
- 3
- 帖子
- 924
- 主题
- 28
- 精华
- 5
- 积分
- 5325
- 历练
- 10
- 声望
- 119
- 人气
- 141
- 经验
- 2790
- 金钱
- 6124
- 注册时间
- 2009-7-25
- 最后登录
- 2017-9-6
- 在线时间
- 376 小时
- 阅读权限
- 200
TA的每日心情 | 衰 2010-9-25 00:07 |
---|
签到天数: 3 天 [LV.2]偶尔看看I - 精华
- 5
- 积分
- 5325
- 历练
- 10
- 声望
- 119
- 人气
- 141
|
这个帖子我竟然不知道发到哪里~~就扔水区了~呵呵~管他呢~
关于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;
}
}
|
-
1
查看全部评分
-
|