@Override
public void paint(Graphics g) {
// TODO Auto-generated method stub
super.paint(g);
g.drawImage(backgroundImage, 0, 0,this);
gava.DrawGava(g, this);
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
int key=e.getKeyCode();
switch (key) {
case KeyEvent.VK_W://跳跃
gava.Jump();break;
case KeyEvent.VK_A://向左移
gava.ToLeft();break;
case KeyEvent.VK_D://向右移
gava.ToRight();break;
case KeyEvent.VK_J://
gava.Shoot();break;
case KeyEvent.VK_F:
//gava.GetBullet(bullet);
default:
break;
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
public class Gava{
//子弹数组用于保存3种子弹
private Bullet[] bullet=new Bullet[3];
//当前子弹的序号(0~2)
private int bulletID=0;
//速度
public int speed=10;
//跳跃高度
public int Jumpheight=90;
//0向左 1向右
public int direction=1;
//血量
public int hp=100;
//图像数组 0右立 1右跑 2左立 3左跑 4跳上 5跳下
public BufferedImage []image;
//当前显示图片,初始为右立
private BufferedImage nowImage;
//储存发射出的子弹
public Vector<Bullet> bullets;
//坐标
int x;
int y;
//宽高
int width;
int heigth;
public Gava(int x,int y,int width,int height,BufferedImage []image) {
this.x=x;
this.y=y;
this.width=width;
this.height=height;
//创建基础子弹,其他设为null
this.bullet[0]=new Bullet(x, y, 40, 40, 5, 300, 5,Resource.bulletImages[0]);
this.bullet[1]=null;
this.bullet[2]=null;
bullets=new Vector<Bullet>(5, 5);
this.image=image;
nowImage=image[0];
}
//拾取武器,替换当前武器
public void GetBullet(Bullet bullet){
this.bullet[bulletID]=bullet;
}
//切换武器,改变当前武器的序号
public void SwitchBullet(){
bulletID++;
if(bulletID==3){
bulletID=0;
}
}
//向左走
public void ToLeft() {
//nowImage=image[]
direction=0;
x-=speed;
}
//向右走
public void ToRight() {
direction=1;
x+=speed;
}
//跳跃
public void Jump() {
public void UpDateBullet(){
for(int i=0;i<bullets.size();i++){
bullets.get(i).x+=bullets.get(i).speed;
}
}
public void DrawGava(Graphics g,JPanel panel){
g.drawImage(nowImage, x, y, width, height, (ImageObserver)panel);
for(int i=0;i<bullets.size();i++){
bullets.get(i).DrawBullet(g, panel);
}
}
}
public class Bullet{
//子弹的伤害
private int damageValue;
//射程,
public int range;
//射速,x轴每次移动距离
public int speed;
//子弹图片
public BufferedImage bulletImage;
//坐标
int x;
int y;
//宽高
int width;
int heigth;
@Override
public void paint(Graphics g) {
// TODO Auto-generated method stub
super.paint(g);
g.drawImage(backgroundImage, 0, 0,this);
gava.DrawGava(g, this);
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
int key=e.getKeyCode();
switch (key) {
case KeyEvent.VK_W://跳跃
gava.Jump();break;
case KeyEvent.VK_A://向左移
gava.ToLeft();break;
case KeyEvent.VK_D://向右移
gava.ToRight();break;
case KeyEvent.VK_J://
gava.Shoot();break;
case KeyEvent.VK_F:
//gava.GetBullet(bullet);
default:
break;
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
public class Gava{
//子弹数组用于保存3种子弹
private Bullet[] bullet=new Bullet[3];
//当前子弹的序号(0~2)
private int bulletID=0;
//速度
public int speed=10;
//跳跃高度
public int Jumpheight=90;
//0向左 1向右
public int direction=1;
//血量
public int hp=100;
//图像数组 0右立 1右跑 2左立 3左跑 4跳上 5跳下
public BufferedImage []image;
//当前显示图片,初始为右立
private BufferedImage nowImage;
//储存发射出的子弹
public Vector<Bullet> bullets;
//坐标
int x;
int y;
//宽高
int width;
int heigth;
public Gava(int x,int y,int width,int height,BufferedImage []image) {
this.x=x;
this.y=y;
this.width=width;
this.height=height;
//创建基础子弹,其他设为null
this.bullet[0]=new Bullet(x, y, 40, 40, 5, 300, 5,Resource.bulletImages[0]);
this.bullet[1]=null;
this.bullet[2]=null;
bullets=new Vector<Bullet>(5, 5);
this.image=image;
nowImage=image[0];
}
//拾取武器,替换当前武器
public void GetBullet(Bullet bullet){
this.bullet[bulletID]=bullet;
}
//切换武器,改变当前武器的序号
public void SwitchBullet(){
bulletID++;
if(bulletID==3){
bulletID=0;
}
}
//向左走
public void ToLeft() {
//nowImage=image[]
direction=0;
x-=speed;
}
//向右走
public void ToRight() {
direction=1;
x+=speed;
}
//跳跃
public void Jump() {
public void UpDateBullet(){
for(int i=0;i<bullets.size();i++){
bullets.get(i).x+=bullets.get(i).speed;
}
}
public void DrawGava(Graphics g,JPanel panel){
g.drawImage(nowImage, x, y, width, height, (ImageObserver)panel);
for(int i=0;i<bullets.size();i++){
bullets.get(i).DrawBullet(g, panel);
}
}
}
public class Bullet{
//子弹的伤害
private int damageValue;
//射程,
public int range;
//射速,x轴每次移动距离
public int speed;
//子弹图片
public BufferedImage bulletImage;
//坐标
int x;
int y;
//宽高
int width;
int heigth;