導航:首頁 > 編程語言 > javaweb開發源代碼

javaweb開發源代碼

發布時間:2025-05-07 19:18:51

㈠ 用java web小游戲源代碼。期末結課老師讓做,急用,謝了

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.JFrame;

@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.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;

@SuppressWarnings("serial")
public class ControlSnake extends JPanel implements ActionListener {
Random rand;

ArrayList<Point> list, 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<Point>();
listBody = new ArrayList<Point>();

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.BLACK);
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;
}
}

/*貪吃蛇代碼*/

閱讀全文

與javaweb開發源代碼相關的資料

熱點內容
兒童編程的機構有哪些內容 瀏覽:613
視頻文件格式怎麼變 瀏覽:988
asp首頁文件 瀏覽:330
直接吹還是走程序 瀏覽:725
gta4收音機文件在哪裡 瀏覽:145
cad哪個字體文件好用 瀏覽:570
描述網站地圖文件的基本格式 瀏覽:932
虎撲下載的視頻在哪個文件夾 瀏覽:92
怎麼提高網站的轉化率 瀏覽:823
東莞小太陽幼兒園app 瀏覽:347
linux文件差異 瀏覽:163
公司系統編程怎麼編 瀏覽:932
數據文件結構已更改是什麼意思 瀏覽:821
優值卡app怎麼用 瀏覽:722
雲中歌電視劇有幾個版本 瀏覽:40
語音數據包怎麼安裝 瀏覽:659
tpk是什麼文件 瀏覽:238
word辦公兩個文件找不同 瀏覽:611
網站文字怎麼改成自己的字 瀏覽:192
蘋果手機突然黑屏開不了機為什麼 瀏覽:52

友情鏈接