Java游戏之简单版雷霆战机
程序员文章站
2024-03-23 08:27:10
...
GameObject
package cn.sxt.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 width,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) {
super();
this.img = img;
this.x = x;
this.y = y;
}
public GameObject() {
}
public Rectangle getRect() {
return new Rectangle((int)x,(int)y,width,height);
}
}
package cn.sxt.game;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
public class Plane extends GameObject{
boolean left,up,right,down;
boolean live = true;
public void drawSelf(Graphics g) {
if(live) {
g.drawImage(img,(int)x,(int)y,null);
if(left) {
x-=speed;
}
if(right) {
x+=speed;
}if(up) {
y-=speed;
}
if(down) {
y+=speed;
}
}else {
}
}
public Plane(Image img,double x,double y) {
this.img = img;
this.x = x;
this.y = y;
this.speed =3;
this.width = img.getWidth(null);
this.height = img.getHeight(null);
}
//按下某个键,增加相应的方向
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;
}
}
}
喜欢我的可以关注我的微信公众号:
让我爱上它Computer
代码