GameObject.类
package cn.qym.game;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
public class GameObject {
Image img;
double x,y;
int speed;
int width,height;
public void drawSelf(Graphics g) {
g.drawImage(img, (int)x, (int)y, null);
}
public GameObject(Image img, double x, double y, int speed, int weight, int height) {
super();
this.img = img;
this.x = x;
this.y = y;
this.speed = speed;
this.width = width;
this.height = height;
}
public GameObject(Image img, double x, double y, int speed) {
super();
this.img = img;
this.x = x;
this.y = y;
this.speed = speed;
}
public GameObject(){
}
/*
*返回物体所在矩形,便于后续碰撞检测
*/
public Rectangle getRect(){
return new Rectangle((int)x, (int)y, width, height);
}
}
GameUtil类
package cn.qym.game;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
public class GameUtil {
private GameUtil() {
}
public static Image getImage(String path) {
BufferedImage bi =null;
try {
URL u=GameUtil.class.getClassLoader().getResource(path);
bi=ImageIO.read(u);
} catch(IOException e) {
e.printStackTrace();
}
return bi;
}
}
MyGameFrame类
package cn.qym.game;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
public class MyGameFrame extends JFrame {
Image bg =GameUtil.getImage("images/bg.jpg");
Image planeImg=GameUtil.getImage("images/plane.png");
Plane plane =new Plane(planeImg,250,250);
public void paint(Graphics g) {
g.drawImage(bg, 0, 0, null);
plane.drawSelf(g);
}
class PaintThread extends Thread{
}
public void run() {
while (true) {
repaint();
try {
Thread.sleep(40);
}catch(InterruptedException e) {
e.printStackTrace();
}
}
}
class KeyMonitor extends KeyAdapter{
public void keyPressed(KeyEvent e) {
plane.addDirection(e);
}
public void keyReleased(KeyEvent e) {
plane.minusDirection(e);
}
}
public void launchFrame() {
this.setTitle("钱宇明作品");
this.setVisible(true);
this.setLocation(300, 300);
this.setSize(500, 500);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
new PaintThread().start();
addKeyListener(new KeyMonitor());
}
public static void main(String[]args) {
MyGameFrame f =new MyGameFrame();
f.launchFrame();
}
}
Plane类
package cn.qym.game;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
public class Plane extends GameObject {
boolean left,up,right,down;
int speed =3;
public void drawSelf(Graphics g) {
g.drawImage(img, (int)x, (int)y, null);
if(left) {
x-=speed;
}if(right) {
x+=speed;
}if(up) {
y-=speed;
}if(down) {
y+=speed;
}
}
public Plane(Image img,double x,double y) {
this.img=img;
this.x=x;
this.y=y;
}
public void addDirection(KeyEvent e) {
switch(e.getKeyCode()) {
case KeyEvent.VK_LEFT:
left = true;
break;
case KeyEvent.VK_UP:
up=true;
break;
case KeyEvent.VK_RIGHT:
right=true;
break;
case KeyEvent.VK_DOWN:
down=true;
break;
}
}
public void minusDirection(KeyEvent e) {
switch(e.getKeyCode()) {
case KeyEvent.VK_LEFT:
left = false;
break;
case KeyEvent.VK_UP:
up=false;
break;
case KeyEvent.VK_RIGHT:
right=false;
break;
case KeyEvent.VK_DOWN:
down=false;
break;
}
}
}
全都定义好了,完全按着视频来的,为什么我按上下左右实现不了飞机的移动。