使用java的网页游戏源代码。期末,老师让我急着做,谢谢。

导入Java . awt . borderlayout;

导入Java . awt . dimension;

导入Java . awt . toolkit;

导入javax . swing . jframe;

@SuppressWarnings("serial ")

公共类MainClass扩展JFrame {

ControlSnake控件;

工具包;

维度dimen

公共静态void main(String[] args) {

新主类(“我的蛇”);

}

公共主类(字符串){

超级用户;

control = new control snake();

control . setfocusable(true);

kit = toolkit . getdefaulttoolkit();

dimen = kit . getscreensize();

添加(控制);

set layout(new BorderLayout());

setLocation(dimen.width / 3,dimen . height/3);//dimen.width/3,dimen.height/3

setSize(FWIDTH,FH height);

setDefaultCloseOperation(JFrame。EXIT _ ON _ CLOSE);

setResizable(false);

set visible(true);

}

public static final int FWIDTH = 315;

公共静态最终int FHEIGHT = 380

}

导入Java . awt . color;

导入Java . awt . dimension;

导入Java . awt . graphics;

导入Java . awt . point;

导入Java . awt . event . action event;

导入Java . awt . event . action listener;

导入Java . awt . event . key adapter;

导入Java . awt . event . key event;

导入Java . util . ArrayList;

导入Java . util . random;

导入javax . swing . joptionpane;

导入javax . swing . jpanel;

导入javax . swing . timer;

@SuppressWarnings("serial ")

公共类控件Snake扩展JPanel实现ActionListener {

随机rand

ArrayList & lt点& gtlist,listBody

字符串str,str 1;

静态布尔键;

int x,y,dx,dy,fx,fy,flag

内部蛇身;

int速度;

公共控件Snake() {

蛇身= 1;

Str = "上下左右方向键控制P键暂停...";

Str1 = "当前长度为:"+snake body;

key = true

flag = 1;

速度= 700;

rand = new Random();

list = new ArrayList & lt点& gt();

listBody = new ArrayList & lt点& gt();

x = 5;

y = 5;

list.add(新点(x,y));

list body . add(list . get(0));

dx = 10;

dy = 0;

FX = rand . nextint(30)* 10+5;// 2

fy = rand . nextint(30)* 10+5;// 2

setBackground(颜色。黑色);

setSize(新维度(318,380));

最终计时器时间=新计时器(速度,这个);

time . start();

addkey listener(new key adapter(){

公共void按键(按键事件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(标志% 2 == 0) {

time . start();

}

flag++;

}

}

});

}

公共空白油漆(图形g) {

g.setColor(颜色。白色);

g.fillRect(0,0,400,400);

g.setColor(颜色。深灰);

g .画线(3,3,305,3);

g .拉线(3,3,3,305);

g .拉线(305,3,305,305);

g .拉线(3,305,305,305);

g.setColor(颜色。粉色);

for(int I = 0;我& ltlist body . size();i++) {

g.fillRect(listBody.get(i))。x,listBody.get(i)。y,9,9);

}

g.fillRect(x,y,9,9);

g.setColor(颜色。橙色);

g.fillRect(fx,fy,9,9);

g.setColor(颜色。深灰);

Str1 = "当前长度为:"+snake body;

g .抽绳(str,10,320);

g .拉带(str1,10,335);

}

public void action performed(action event e){

x+= dx;

y+= dy;

if (makeOut() == false) {

Joptionpane。显示消息对话框(空,“重新开始...”);

速度= 700;

蛇身= 1;

x = 5;

y = 5;

list . clear();

list.add(新点(x,y));

list body . clear();

list body . add(list . get(0));

dx = 10;

dy = 0;

}

addPoint(x,y);

if(x = = FX & amp;& ampy == fy) {

速度= (int)(速度* 0.8);//速度增加参数

如果(速度& lt200) {

速度= 100;

}

FX = rand . nextint(30)* 10+5;// 2

fy = rand . nextint(30)* 10+5;// 2

snake body++;// 2

} // 2

repaint();

}

公共void添加点(int xx,int yy) {

//动态记录最近50步内移动的坐标。

//并画出最新的蛇身。

if(list . size()& lt;100) {//蛇身最长长度为100。

list.add(新点(xx,YY));

}否则{

list . remove(0);

list.add(新点(xx,YY));

}

if (snakeBody == 1) {

list body . remove(0);

listBody.add(0,list . get(list . size()-1));

}否则{

list body . clear();

if(list . size()& lt;蛇身){

for(int I = list . size()-1;我& gt0;我- ) {

list body . add(list . get(I));

}

}否则{

for(int I = list . size()-1;list body . size()& lt;蛇身;我- ) {

list body . add(list . get(I));

}

}

}

}

公共布尔makeOut() {

如果((x & lt3 | | y & lt3)| |(x & gt;305 | | y & gt305)) {

返回false

}

for(int I = 0;我& ltlist body . size()-1;i++) {

for(int j = I+1;j & ltlist body . size();j++) {

if (listBody.get(i)。equals(listBody.get(j))) {

返回false

}

}

}

返回true

}

}

/*蛇代码*/