用java开发小游戏
我是大二的软件工程学生,本学期要求用java组队开发一个类似于俄罗斯方块的小游戏,由于是第一次尝试自己动手开发项目,不知道该看些什么书,以及设计哪些技术,设想游戏以单机为...
我是大二的软件工程学生,本学期要求用java组队开发一个类似于俄罗斯方块的小游戏,由于是第一次尝试自己动手开发项目,不知道该看些什么书,以及设计哪些技术,设想游戏以单机为主,加入双人pk模式,请高手,牛人们帮忙指点一下~~~
话是没错...不过我们想做得有创意一点... 展开
话是没错...不过我们想做得有创意一点... 展开
6个回答
展开全部
我给你个华容道的游戏参照下吧。可以自己照着做下。多看例子想想就行了。
找个游戏做的不好,不要笑话啊。
import java.awt.*;
import javax.swing.JApplet.*;
import java.awt.event.*;
import javax.swing.*;
class People extends JButton implements FocusListener
{
Rectangle rect=null;
int left_x,left_y;//按钮左上角坐标.
int width,height; //按钮的宽和高.
String name;
int number;
public People(int number,String s,int x,int y,int w,int h,HuaRongRoad road)
{
super(s);
name=s;
this.number=number;
left_x=x;
left_y=y;
width=w;
height=h;
setBackground(Color.GREEN);
road.add(this);
addKeyListener(road);
setBounds(x,y,w,h);
addFocusListener(this);
rect=new Rectangle(x,y,w,h);
}
public void focusGained(FocusEvent e)
{
setBackground(Color.red);
}
public void focusLost(FocusEvent e)
{
setBackground(Color.GREEN);
}
}
public class HuaRongRoad extends JApplet implements KeyListener,ActionListener
{
People people[]=new People[10];
Rectangle left,right,above,below;//华容道的边界
JButton restart=new JButton("restart");
public void init()
{
getContentPane().setLayout(null);
getContentPane().add(restart);
restart.setBounds(5,5,80,25);
restart.addActionListener(this);
getContentPane().setBackground(Color.white);
people[0]=new People(0,"曹操",154,54,200,200,this);
people[1]=new People(1,"关羽",154,254,200,100,this);
people[2]=new People(2,"张飞",54,254,100,200,this);
people[3]=new People(3,"刘备",354,254,100,200,this);
people[4]=new People(4,"张辽",54,54,100,200,this);
people[5]=new People(5,"曹仁",354,54,100,200,this);
people[6]=new People(6,"兵 ",54,454,100,100,this);
people[7]=new People(7,"兵 ",354,454,100,100,this);
people[8]=new People(8,"兵 ",154,354,100,100,this);
people[9]=new People(9,"兵 ",254,354,100,100,this);
people[9].requestFocus();
people[0].setForeground(Color.white);
left=new Rectangle(49,49,5,510);
right=new Rectangle(454,49,5,510);
above=new Rectangle(49,49,410,5);
below=new Rectangle(49,554,410,5);
}
public void paint(Graphics g)
{ //华容道的边界
super.paint(g);
g.setColor(Color.cyan);
g.fillRect(49,49,5,510);
g.fillRect(454,49,5,510);
g.fillRect(49,49,410,5);
g.fillRect(49,554,410,5);
//
g.drawString("单击,按方向箭头移动",100,20);
g.setColor(Color.red);
g.drawString("曹操到达该位置",110,300);
}
public void keyPressed(KeyEvent e)
{
People man=(People)e.getSource();
man.rect.setLocation(man.getBounds().x,man.getBounds().y);
if(e.getKeyCode()==KeyEvent.VK_DOWN)
{
man.left_y=man.left_y+100; //向下前进50个单位
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
//判断是否和其他人或边界重叠,出现就退回50个单位
for(int i=0;i<10;i++)
{
if((man.rect.intersects(people[i].rect))&&(man.number!=i))
{
man.left_y=man.left_y-100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(man.rect.intersects(below))
{
man.left_y=man.left_y-100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(e.getKeyCode()==KeyEvent.VK_UP)
{
man.left_y=man.left_y-100; //向上前进50个单位
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
//判断是否和其他人或边界重叠,出现就退回50个单位
for(int i=0;i<10;i++)
{
if((man.rect.intersects(people[i].rect))&&(man.number!=i))
{
man.left_y=man.left_y+100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(man.rect.intersects(above))
{
man.left_y=man.left_y+100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(e.getKeyCode()==KeyEvent.VK_LEFT)
{
man.left_x=man.left_x-100; //向左前进50个单位
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
//判断是否和其他人或边界重叠,出现就退回50个单位
for(int i=0;i<10;i++)
{
if((man.rect.intersects(people[i].rect))&&(man.number!=i))
{
man.left_x=man.left_x+100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(man.rect.intersects(left))
{
man.left_x=man.left_x+100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
{
man.left_x=man.left_x+100; //向右进50个单位
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
//判断是否和其他人或边界重叠,出现就退回50个单位
for(int i=0;i<10;i++)
{
if((man.rect.intersects(people[i].rect))&&(man.number!=i))
{
man.left_x=man.left_x-100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(man.rect.intersects(right))
{
man.left_x=man.left_x-100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public void actionPerformed(ActionEvent e)
{
getContentPane().removeAll();
this.init();
}
}
找个游戏做的不好,不要笑话啊。
import java.awt.*;
import javax.swing.JApplet.*;
import java.awt.event.*;
import javax.swing.*;
class People extends JButton implements FocusListener
{
Rectangle rect=null;
int left_x,left_y;//按钮左上角坐标.
int width,height; //按钮的宽和高.
String name;
int number;
public People(int number,String s,int x,int y,int w,int h,HuaRongRoad road)
{
super(s);
name=s;
this.number=number;
left_x=x;
left_y=y;
width=w;
height=h;
setBackground(Color.GREEN);
road.add(this);
addKeyListener(road);
setBounds(x,y,w,h);
addFocusListener(this);
rect=new Rectangle(x,y,w,h);
}
public void focusGained(FocusEvent e)
{
setBackground(Color.red);
}
public void focusLost(FocusEvent e)
{
setBackground(Color.GREEN);
}
}
public class HuaRongRoad extends JApplet implements KeyListener,ActionListener
{
People people[]=new People[10];
Rectangle left,right,above,below;//华容道的边界
JButton restart=new JButton("restart");
public void init()
{
getContentPane().setLayout(null);
getContentPane().add(restart);
restart.setBounds(5,5,80,25);
restart.addActionListener(this);
getContentPane().setBackground(Color.white);
people[0]=new People(0,"曹操",154,54,200,200,this);
people[1]=new People(1,"关羽",154,254,200,100,this);
people[2]=new People(2,"张飞",54,254,100,200,this);
people[3]=new People(3,"刘备",354,254,100,200,this);
people[4]=new People(4,"张辽",54,54,100,200,this);
people[5]=new People(5,"曹仁",354,54,100,200,this);
people[6]=new People(6,"兵 ",54,454,100,100,this);
people[7]=new People(7,"兵 ",354,454,100,100,this);
people[8]=new People(8,"兵 ",154,354,100,100,this);
people[9]=new People(9,"兵 ",254,354,100,100,this);
people[9].requestFocus();
people[0].setForeground(Color.white);
left=new Rectangle(49,49,5,510);
right=new Rectangle(454,49,5,510);
above=new Rectangle(49,49,410,5);
below=new Rectangle(49,554,410,5);
}
public void paint(Graphics g)
{ //华容道的边界
super.paint(g);
g.setColor(Color.cyan);
g.fillRect(49,49,5,510);
g.fillRect(454,49,5,510);
g.fillRect(49,49,410,5);
g.fillRect(49,554,410,5);
//
g.drawString("单击,按方向箭头移动",100,20);
g.setColor(Color.red);
g.drawString("曹操到达该位置",110,300);
}
public void keyPressed(KeyEvent e)
{
People man=(People)e.getSource();
man.rect.setLocation(man.getBounds().x,man.getBounds().y);
if(e.getKeyCode()==KeyEvent.VK_DOWN)
{
man.left_y=man.left_y+100; //向下前进50个单位
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
//判断是否和其他人或边界重叠,出现就退回50个单位
for(int i=0;i<10;i++)
{
if((man.rect.intersects(people[i].rect))&&(man.number!=i))
{
man.left_y=man.left_y-100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(man.rect.intersects(below))
{
man.left_y=man.left_y-100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(e.getKeyCode()==KeyEvent.VK_UP)
{
man.left_y=man.left_y-100; //向上前进50个单位
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
//判断是否和其他人或边界重叠,出现就退回50个单位
for(int i=0;i<10;i++)
{
if((man.rect.intersects(people[i].rect))&&(man.number!=i))
{
man.left_y=man.left_y+100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(man.rect.intersects(above))
{
man.left_y=man.left_y+100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(e.getKeyCode()==KeyEvent.VK_LEFT)
{
man.left_x=man.left_x-100; //向左前进50个单位
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
//判断是否和其他人或边界重叠,出现就退回50个单位
for(int i=0;i<10;i++)
{
if((man.rect.intersects(people[i].rect))&&(man.number!=i))
{
man.left_x=man.left_x+100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(man.rect.intersects(left))
{
man.left_x=man.left_x+100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
{
man.left_x=man.left_x+100; //向右进50个单位
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
//判断是否和其他人或边界重叠,出现就退回50个单位
for(int i=0;i<10;i++)
{
if((man.rect.intersects(people[i].rect))&&(man.number!=i))
{
man.left_x=man.left_x-100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(man.rect.intersects(right))
{
man.left_x=man.left_x-100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public void actionPerformed(ActionEvent e)
{
getContentPane().removeAll();
this.init();
}
}
展开全部
哥们,你可以看一些关于魔兽世界的书,看看人家怎么能的,你设计个差不多的,但是不能要钱
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这种小东西一般学完J2SE就应该会做了吧
那就在界面里下点功夫,加一点好看的图片和背景,我写过,不难的
那就在界面里下点功夫,加一点好看的图片和背景,我写过,不难的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
就看几个稍稍复杂的编程例子就行了。
如果是想有锻炼度那你们就一直自己想着用自己所学的知识做,不行的话再下一个人家已经编好的看,如果是想快点编好并且有创意的话,你们就直接找几个看看,然后就是模仿人家的做,体会下人家的思想,并把它学会了,再把自己的创意加进去。这样锻炼度是小了一些,但是我敢肯定分会高些、、、、我的就是用了后面的方法的了高分的哦!
你的双人竞赛的想法还是不错的哦,而且实现也是不难的。
如果是想有锻炼度那你们就一直自己想着用自己所学的知识做,不行的话再下一个人家已经编好的看,如果是想快点编好并且有创意的话,你们就直接找几个看看,然后就是模仿人家的做,体会下人家的思想,并把它学会了,再把自己的创意加进去。这样锻炼度是小了一些,但是我敢肯定分会高些、、、、我的就是用了后面的方法的了高分的哦!
你的双人竞赛的想法还是不错的哦,而且实现也是不难的。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我给你个华容道的游戏参照下吧。可以自己照着做下。多看例子想想就行了。
找个游戏做的不好,不要笑话啊。
import
java.awt.*;
import
javax.swing.JApplet.*;
import
java.awt.event.*;
import
javax.swing.*;
class
People
extends
JButton
implements
FocusListener
{
Rectangle
rect=null;
int
left_x,left_y;//按钮左上角坐标.
int
width,height;
//按钮的宽和高.
String
name;
int
number;
public
People(int
number,String
s,int
x,int
y,int
w,int
h,HuaRongRoad
road)
{
super(s);
name=s;
this.number=number;
left_x=x;
left_y=y;
width=w;
height=h;
setBackground(Color.GREEN);
road.add(this);
addKeyListener(road);
setBounds(x,y,w,h);
addFocusListener(this);
rect=new
Rectangle(x,y,w,h);
}
public
void
focusGained(FocusEvent
e)
{
setBackground(Color.red);
}
public
void
focusLost(FocusEvent
e)
{
setBackground(Color.GREEN);
}
}
public
class
HuaRongRoad
extends
JApplet
implements
KeyListener,ActionListener
{
People
people[]=new
People[10];
Rectangle
left,right,above,below;//华容道的边界
JButton
restart=new
JButton("restart");
public
void
init()
{
getContentPane().setLayout(null);
getContentPane().add(restart);
restart.setBounds(5,5,80,25);
restart.addActionListener(this);
getContentPane().setBackground(Color.white);
people[0]=new
People(0,"曹操",154,54,200,200,this);
people[1]=new
People(1,"关羽",154,254,200,100,this);
people[2]=new
People(2,"张飞",54,254,100,200,this);
people[3]=new
People(3,"刘备",354,254,100,200,this);
people[4]=new
People(4,"张辽",54,54,100,200,this);
people[5]=new
People(5,"曹仁",354,54,100,200,this);
people[6]=new
People(6,"兵
",54,454,100,100,this);
people[7]=new
People(7,"兵
",354,454,100,100,this);
people[8]=new
People(8,"兵
",154,354,100,100,this);
people[9]=new
People(9,"兵
",254,354,100,100,this);
people[9].requestFocus();
people[0].setForeground(Color.white);
left=new
Rectangle(49,49,5,510);
right=new
Rectangle(454,49,5,510);
above=new
Rectangle(49,49,410,5);
below=new
Rectangle(49,554,410,5);
}
public
void
paint(Graphics
g)
{
//华容道的边界
super.paint(g);
g.setColor(Color.cyan);
g.fillRect(49,49,5,510);
g.fillRect(454,49,5,510);
g.fillRect(49,49,410,5);
g.fillRect(49,554,410,5);
//
g.drawString("单击,按方向箭头移动",100,20);
g.setColor(Color.red);
g.drawString("曹操到达该位置",110,300);
}
public
void
keyPressed(KeyEvent
e)
{
People
man=(People)e.getSource();
man.rect.setLocation(man.getBounds().x,man.getBounds().y);
if(e.getKeyCode()==KeyEvent.VK_DOWN)
{
man.left_y=man.left_y+100;
//向下前进50个单位
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
//判断是否和其他人或边界重叠,出现就退回50个单位
for(int
i=0;i<10;i++)
{
if((man.rect.intersects(people[i].rect))&&(man.number!=i))
{
man.left_y=man.left_y-100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(man.rect.intersects(below))
{
man.left_y=man.left_y-100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(e.getKeyCode()==KeyEvent.VK_UP)
{
man.left_y=man.left_y-100;
//向上前进50个单位
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
//判断是否和其他人或边界重叠,出现就退回50个单位
for(int
i=0;i<10;i++)
{
if((man.rect.intersects(people[i].rect))&&(man.number!=i))
{
man.left_y=man.left_y+100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(man.rect.intersects(above))
{
man.left_y=man.left_y+100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(e.getKeyCode()==KeyEvent.VK_LEFT)
{
man.left_x=man.left_x-100;
//向左前进50个单位
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
//判断是否和其他人或边界重叠,出现就退回50个单位
for(int
i=0;i<10;i++)
{
if((man.rect.intersects(people[i].rect))&&(man.number!=i))
{
man.left_x=man.left_x+100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(man.rect.intersects(left))
{
man.left_x=man.left_x+100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
{
man.left_x=man.left_x+100;
//向右进50个单位
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
//判断是否和其他人或边界重叠,出现就退回50个单位
for(int
i=0;i<10;i++)
{
if((man.rect.intersects(people[i].rect))&&(man.number!=i))
{
man.left_x=man.left_x-100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(man.rect.intersects(right))
{
man.left_x=man.left_x-100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
}
public
void
keyTyped(KeyEvent
e){}
public
void
keyReleased(KeyEvent
e){}
public
void
actionPerformed(ActionEvent
e)
{
getContentPane().removeAll();
this.init();
}
}
找个游戏做的不好,不要笑话啊。
import
java.awt.*;
import
javax.swing.JApplet.*;
import
java.awt.event.*;
import
javax.swing.*;
class
People
extends
JButton
implements
FocusListener
{
Rectangle
rect=null;
int
left_x,left_y;//按钮左上角坐标.
int
width,height;
//按钮的宽和高.
String
name;
int
number;
public
People(int
number,String
s,int
x,int
y,int
w,int
h,HuaRongRoad
road)
{
super(s);
name=s;
this.number=number;
left_x=x;
left_y=y;
width=w;
height=h;
setBackground(Color.GREEN);
road.add(this);
addKeyListener(road);
setBounds(x,y,w,h);
addFocusListener(this);
rect=new
Rectangle(x,y,w,h);
}
public
void
focusGained(FocusEvent
e)
{
setBackground(Color.red);
}
public
void
focusLost(FocusEvent
e)
{
setBackground(Color.GREEN);
}
}
public
class
HuaRongRoad
extends
JApplet
implements
KeyListener,ActionListener
{
People
people[]=new
People[10];
Rectangle
left,right,above,below;//华容道的边界
JButton
restart=new
JButton("restart");
public
void
init()
{
getContentPane().setLayout(null);
getContentPane().add(restart);
restart.setBounds(5,5,80,25);
restart.addActionListener(this);
getContentPane().setBackground(Color.white);
people[0]=new
People(0,"曹操",154,54,200,200,this);
people[1]=new
People(1,"关羽",154,254,200,100,this);
people[2]=new
People(2,"张飞",54,254,100,200,this);
people[3]=new
People(3,"刘备",354,254,100,200,this);
people[4]=new
People(4,"张辽",54,54,100,200,this);
people[5]=new
People(5,"曹仁",354,54,100,200,this);
people[6]=new
People(6,"兵
",54,454,100,100,this);
people[7]=new
People(7,"兵
",354,454,100,100,this);
people[8]=new
People(8,"兵
",154,354,100,100,this);
people[9]=new
People(9,"兵
",254,354,100,100,this);
people[9].requestFocus();
people[0].setForeground(Color.white);
left=new
Rectangle(49,49,5,510);
right=new
Rectangle(454,49,5,510);
above=new
Rectangle(49,49,410,5);
below=new
Rectangle(49,554,410,5);
}
public
void
paint(Graphics
g)
{
//华容道的边界
super.paint(g);
g.setColor(Color.cyan);
g.fillRect(49,49,5,510);
g.fillRect(454,49,5,510);
g.fillRect(49,49,410,5);
g.fillRect(49,554,410,5);
//
g.drawString("单击,按方向箭头移动",100,20);
g.setColor(Color.red);
g.drawString("曹操到达该位置",110,300);
}
public
void
keyPressed(KeyEvent
e)
{
People
man=(People)e.getSource();
man.rect.setLocation(man.getBounds().x,man.getBounds().y);
if(e.getKeyCode()==KeyEvent.VK_DOWN)
{
man.left_y=man.left_y+100;
//向下前进50个单位
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
//判断是否和其他人或边界重叠,出现就退回50个单位
for(int
i=0;i<10;i++)
{
if((man.rect.intersects(people[i].rect))&&(man.number!=i))
{
man.left_y=man.left_y-100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(man.rect.intersects(below))
{
man.left_y=man.left_y-100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(e.getKeyCode()==KeyEvent.VK_UP)
{
man.left_y=man.left_y-100;
//向上前进50个单位
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
//判断是否和其他人或边界重叠,出现就退回50个单位
for(int
i=0;i<10;i++)
{
if((man.rect.intersects(people[i].rect))&&(man.number!=i))
{
man.left_y=man.left_y+100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(man.rect.intersects(above))
{
man.left_y=man.left_y+100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(e.getKeyCode()==KeyEvent.VK_LEFT)
{
man.left_x=man.left_x-100;
//向左前进50个单位
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
//判断是否和其他人或边界重叠,出现就退回50个单位
for(int
i=0;i<10;i++)
{
if((man.rect.intersects(people[i].rect))&&(man.number!=i))
{
man.left_x=man.left_x+100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(man.rect.intersects(left))
{
man.left_x=man.left_x+100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
{
man.left_x=man.left_x+100;
//向右进50个单位
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
//判断是否和其他人或边界重叠,出现就退回50个单位
for(int
i=0;i<10;i++)
{
if((man.rect.intersects(people[i].rect))&&(man.number!=i))
{
man.left_x=man.left_x-100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
if(man.rect.intersects(right))
{
man.left_x=man.left_x-100;
man.setLocation(man.left_x,man.left_y);
man.rect.setLocation(man.left_x,man.left_y);
}
}
}
public
void
keyTyped(KeyEvent
e){}
public
void
keyReleased(KeyEvent
e){}
public
void
actionPerformed(ActionEvent
e)
{
getContentPane().removeAll();
this.init();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询