package com.jsf;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Shape;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Ellipse2D;
import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class JSFGameFrame extends JFrame implements Serializable , KeyListener , MouseMotionListener {
/**
* 飞机大战 全屏版 (还有点小问题,没时间修改 ,有空再来改吧,呵呵,哪位有心情也可以帮忙改一下哎 O(∩_∩)O)
* 英文 f键为开火 空格键为 退出程序 复制到eclipse中就可以运行的
*/
private static final long serialVersionUID = 1L;
private Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
private JSFGamePanel jsfgp ;
private int fontsize = 100 ;
private int myx = d.width/2-fontsize ;
private int myy = d.height*4/5-fontsize ;
private int zdx ;
private int zdy ;
private int zdw ;
private int zdh ;
private String zddirection ;
private ZiDan myzidan = null ;
private List<ZiDan> myzidans = null ;
private Random r ;
private int ex ;
private int ey ;
private int efontsize ;
private int escore ;
private Enemy e = null ;
private List<Enemy> es =null ;
private Shape shape ;
private long score = 0 ;
public JSFGameFrame() {
super();
this.setUndecorated(true);
this.setSize(d);
this.getContentPane().setBackground(Color.BLACK);
jsfgp = new JSFGamePanel();
Thread th = new Thread(jsfgp);
th.start();
this.addKeyListener(this);
this.addMouseMotionListener(this);
this.add(jsfgp);
this.setVisible(true);
}
class JSFGamePanel extends JPanel implements Serializable ,Runnable{
/**
* 飞机大战游戏界面
*/
private static final long serialVersionUID = 1L;
private Graphics g ;
public JSFGamePanel() {
super();
this.setBackground(null);
myzidans = new LinkedList<ZiDan>();
es = new LinkedList<Enemy>();
r = new Random(System.currentTimeMillis());
this.getEnemys();
}
@Override
public void run() {
for(;;){
if(myzidans.size()!=0){
for (int i = 0 ; i < myzidans.size() ; i++ ) {
myzidans.get(i).setZdy(myzidans.get(i).getZdy()-5);
if(myzidans.get(i).getZdy()==0){
myzidans.remove(myzidans.get(i));
}
}
}
// if(es!=null){
for (int i = 0; i < es.size(); i++) {
es.get(i).setEy(es.get(i).getEy()+1);
if(es.get(i).getEy()==d.height){
es.remove(i);
this.getEnemy();
}
}
// }
for (int i = 0; i < es.size(); i++) {
shape = new Ellipse2D.Double(es.get(i).getEx(),es.get(i).getEy(),es.get(i).getEfontsize(),es.get(i).getEfontsize()/2);
for (int j = 0; j < myzidans.size(); j++) {
if(shape.contains(myzidans.get(j).getZdx(), myzidans.get(j).getZdy())){
score = score + es.get(i).getEscore();
es.remove(i);
this.getEnemy();
}
}
shape = null ;
}
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.repaint();
}
}
@Override
public void paint(Graphics g) {
super.paint(g);
this.g = g ;
this.paintScore();
g.setFont(new Font("Microsoft yahei",Font.BOLD,fontsize));
g.setColor(Color.ORANGE);
g.drawString("士",myx, myy);
if(myzidans!=null){
this.paintMyZiDans();
}
if(es.size()!=0&&es!=null){
this.paintEnemys();
}
}
public void paintScore(){
g.setColor(Color.WHITE);
g.drawString("分数为:"+score, 100, 100);
}
public void paintMyZiDans(){
g.setColor(Color.WHITE);
if(myzidans!=null&&myzidans.size()!=0){
for (int i = 0 ; i < myzidans.size() ; i++ ) {
g.fill3DRect(myzidans.get(i).getZdx(), myzidans.get(i).getZdy(), myzidans.get(i).getZdw(), myzidans.get(i).getZdh(), true);
}
}
}
public void paintEnemys(){
g.setColor(Color.RED);
if(es!=null&&es.size()!=0){
for (int i = 0; i < es.size(); i++) {
g.setFont(new Font("",Font.BOLD,es.get(i).getEfontsize()));
g.fill3DRect(es.get(i).getEx(), es.get(i).getEy(), 30, 10, true);
}
}
}
public void getEnemys(){
for (int i = 0; i < 5; i++) {
this.getEnemy();
}
}
public void getEnemy(){
this.getEnemyData();
e = new Enemy(ex, ey-d.height, efontsize, escore);
es.add(e);
}
int tempex = 0 ;
int tempey = 0 ;
int tempefontsize = 0 ;
public void getEnemyData(){
for(;;){
tempex = r.nextInt(d.width);
tempey = r.nextInt(d.height);
tempefontsize = r.nextInt(fontsize);
if(tempex>100 || tempex<d.width-100 && tempey>100 || tempey<d.height-100 && tempefontsize>20){
ex = tempex ;
ey = tempey ;
efontsize = tempefontsize ;
escore = tempefontsize ;
break ;
}
}
}
public void getMyZiDan(){
this.getMyZiDanData();
myzidan = new ZiDan(zdx , zdy , zdw , zdh , zddirection );
myzidans.add(myzidan);
}
public void getMyZiDanData(){
zdx = myx + fontsize/2;
zdy = myy -fontsize ;
zdw = 5 ;
zdh = 15 ;
zddirection = "u";
}
}
public static void main(String[] args) {
new JSFGameFrame();
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_SPACE){System.exit(0);}
if(e.getKeyCode()==KeyEvent.VK_F){
this.jsfgp.getMyZiDan();
}
}
@Override
public void keyTyped(KeyEvent e) {}
@Override
public void keyReleased(KeyEvent e) {}
@Override
public void mouseDragged(MouseEvent e) {
myx = e.getX()-fontsize/2 ;
myy = e.getY();
this.repaint();
}
@Override
public void mouseMoved(MouseEvent e) {
myx = e.getX() -fontsize/2 ;
myy = e.getY();
this.repaint();
}
class Enemy implements Serializable{
/**
* 敌人飞机类
*/
private static final long serialVersionUID = 1L;
private int ex ;
private int ey ;
private int efontsize ;
private int escore ;
public Enemy() {
super();
// TODO Auto-generated constructor stub
}
public Enemy(int ex, int ey, int efontsize , int escore) {
super();
this.ex = ex;
this.ey = ey;
this.efontsize = efontsize;
this.escore = escore;
}
public int getEfontsize() {
return efontsize;
}
public void setEfontsize(int efontsize) {
this.efontsize = efontsize;
}
public int getEx() {
return ex;
}
public void setEx(int ex) {
this.ex = ex;
}
public int getEy() {
return ey;
}
public void setEy(int ey) {
this.ey = ey;
}
public int getEscore() {
return escore;
}
public void setEscore(int escore) {
this.escore = escore;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + getOuterType().hashCode();
result = prime * result + efontsize;
result = prime * result + escore;
result = prime * result + ex;
result = prime * result + ey;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Enemy other = (Enemy) obj;
if (!getOuterType().equals(other.getOuterType()))
return false;
if (efontsize != other.efontsize)
return false;
if (escore != other.escore)
return false;
if (ex != other.ex)
return false;
if (ey != other.ey)
return false;
return true;
}
private JSFGameFrame getOuterType() {
return JSFGameFrame.this;
}
@Override
public String toString() {
return "Enemy [ex=" + ex + ", ey=" + ey + ", efontsize="
+ efontsize + ", escore=" + escore + "]";
}
}
class ZiDan implements Serializable{
private static final long serialVersionUID = 1L;
/**
* 子弹类
*/
private int zdx ;
private int zdy ;
private int zdw ;
private int zdh ;
private String zddirection ; //L R U D
public ZiDan() {
super();
// TODO Auto-generated constructor stub
}
public ZiDan(int zdx, int zdy, int zdw, int zdh, String zddirection) {
super();
this.zdx = zdx;
this.zdy = zdy;
this.zdw = zdw;
this.zdh = zdh;
this.zddirection = zddirection;
}
public int getZdx() {
return zdx;
}
public void setZdx(int zdx) {
this.zdx = zdx;
}
public int getZdy() {
return zdy;
}
public void setZdy(int zdy) {
this.zdy = zdy;
}
public int getZdw() {
return zdw;
}
public void setZdw(int zdw) {
this.zdw = zdw;
}
public int getZdh() {
return zdh;
}
public void setZdh(int zdh) {
this.zdh = zdh;
}
public String getZddirection() {
return zddirection;
}
public void setZddirection(String zddirection) {
this.zddirection = zddirection;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + getOuterType().hashCode();
result = prime * result
+ ((zddirection == null) ? 0 : zddirection.hashCode());
result = prime * result + zdh;
result = prime * result + zdw;
result = prime * result + zdx;
result = prime * result + zdy;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ZiDan other = (ZiDan) obj;
if (!getOuterType().equals(other.getOuterType()))
return false;
if (zddirection == null) {
if (other.zddirection != null)
return false;
} else if (!zddirection.equals(other.zddirection))
return false;
if (zdh != other.zdh)
return false;
if (zdw != other.zdw)
return false;
if (zdx != other.zdx)
return false;
if (zdy != other.zdy)
return false;
return true;
}
private JSFGameFrame getOuterType() {
return JSFGameFrame.this;
}
@Override
public String toString() {
return "ZiDan [zdx=" + zdx + ", zdy=" + zdy + ", zdw=" + zdw
+ ", zdh=" + zdh + ", zddirection=" + zddirection + "]";
}
}
}
java swing 飞机 空战 全屏 复制 粘贴 可运行
最新推荐文章于 2019-05-12 13:46:47 发布