java雷霆战机小游戏代码,雷霆战机编程
本文实例为大家分享了爪哇岛实现雷霆战机的具体代码,供大家参考,具体内容如下
GameFame.java
包裹cn .tx;导入javax。挥棒。*;导入Java。awt。*;导入Java。awt。形象。缓冲图像;导入Java。util。随机;导入Java。util。向量;类游戏框架扩展了JFrame { HeroPlane heroPlane//定义子弹的集合向量项目符号项目符号=新向量();//敌机集合vectorenemy飞机敌人s=new Vector();游戏框架框架;公共游戏框架(){ frame=this//创建英雄机英雄飞机=新英雄飞机();英雄飞机。start();//设置窗体的宽高this.setSize(450,730);//标题this.setTitle(雷霆战机);这个。setresizable(false);这个。setdefaultcloseoperation(窗口常量.EXIT _ ON _ CLOSE);这个。setlocationrelativeto(null);//窗口可见这个。设置可见(真);new Thread(new Runnable(){ @ Override public void run(){ while(true){ repaint();试试{线程。睡眠(10);} catch(中断异常e){ e . printstacktrace();} } } }).start();//产生敌机的线程新线程(new Runnable() { //创建随机数的对象Random r=new Random();@ Override public void run(){ while(true){//启动敌机敌机敌机=新敌机(r . nextint(500),0,frame);敌机。start();//添加敌机的时候,让x轴随机敌人。添加(敌机);试试{线程。睡眠(500);} catch(中断异常e){ e . printstacktrace();} } } }).start();}//*在窗口上画,内容,绘画这个画笔的方法在窗口初始化的时候会默认的执行//@ param g public void paint(Graphics g){//system。出去。println(绘制画板);//两背景缓冲图像image=(缓冲图像)this。创建图像(这。getsize().width,this.getSize().身高);//高效缓存的画笔图形bi=图像。获取图形();//画背景
bi.drawImage(new ImageIcon("img/MAP02_01.png").getImage(),0,0,null); //画战斗机 bi.drawImage(heroPlane.img, heroPlane.x,heroPlane.y, heroPlane.width,heroPlane.heigth,null); //飞机发射炮弹 for (int i = 0; i < bullets.size(); i++) { System.out.println(bullets); Bullet bullet = bullets.get(i); if(bullet.y > 0) bi.drawImage(bullet.image, bullet.x,bullet.y -= bullet.speed, bullet.width,bullet.height, null); else bullets.remove(bullet); } //画敌机 for (int i = 0; i < enemys.size(); i++) { System.out.println(enemys); EnemyPlane ep = enemys.get(i); if(ep.y < 730 ) bi.drawImage(ep.img, ep.x,ep.y += ep.speed, ep.width,ep.heigth,null); else enemys.remove(ep); } //生效 g.drawImage(image,0,0,null); } public static void main (String[]args){ GameFrame frame = new GameFrame(); Player player = new Player(frame); frame.addKeyListener(player); } }HeroPlane
package cn.tx;import javax.swing.*;import java.awt.*; public class HeroPlane extends Thread{ //英雄机在画板上的位置 int x=200, y=600; int width = 50, heigth = 50; //飞机的速度 int speed = 10; Image img = new ImageIcon("img/10011.png").getImage(); //定义方向键的标志 boolean up,down,left,right; public HeroPlane() { } public HeroPlane(int x, int y, int width, int heigth, Image img) { this.x = x; this.y = y; this.width = width; this.heigth = heigth; } @Override public void run() { while (true){ if (up){ y -= speed; } if (down){ y += speed; } if (left){ x -= speed; } if (right){ x += speed; } try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } }}
Player
package cn.tx;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;//定义一个玩家public class Player extends KeyAdapter { GameFrame frame; HeroPlane heroPlane; public Player(GameFrame frame) { this.frame=frame; } public void keyPressed(KeyEvent e) { int keyCode = e.getKeyCode(); //38、40、37、39 switch (keyCode){ case 38: frame.heroPlane.up = true; break; case 40: frame. heroPlane.down = true; break; case 37: frame. heroPlane.left = true; break; case 39: frame. heroPlane.right = true; break; case 66: addBullut(); break; } } @Override public void keyReleased(KeyEvent e) { int keyCode = e.getKeyCode(); //38、40、37、39 switch (keyCode){ case 38: frame.heroPlane.up = false; break; case 40: frame. heroPlane.down = false; break; case 37: frame. heroPlane.left = false; break; case 39: frame. heroPlane.right = false; break; } } public void addBullut(){ frame.bullets.add(new Bullet( frame.heroPlane.x+5, frame.heroPlane.y - 20)); }}
EnemyPlane
package cn.tx; import javax.swing.*;import java.awt.*; public class EnemyPlane extends Thread { public GameFrame gf; //子弹的坐标,大小速度 public int x, y; public int width = 50; public int heigth = 50; public int speed = 2; public Image img = new ImageIcon("img/10021.png").getImage(); public EnemyPlane(int x, int y, GameFrame gf) { super(); this.x = x; this.y = y; this.gf = gf; } public EnemyPlane(int x, int y, int width, int heigth, GameFrame gf) { super(); this.x = x; this.y = y; this.width = width; this.heigth = heigth; this.gf = gf; } //玛丽飞翔的逻辑;移动的逻辑都在这里。 public void run() { while (true) { //向左走 if (hit()) { System.out.println("hit................"); this.speed = 0; this.img = new ImageIcon("img/300350.png").getImage(); try { this.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } gf.enemys.remove(this); break; } if (this.y >= 760) { break; } try { this.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } } } //检测碰撞 public boolean hit() { // swing在水中,人家已经提供了 Rectangle myrect = new Rectangle(this.x, this.y, this.width, this.heigth); Rectangle rect = null; for (int i = 0; 1 < gf.bullets.size(); i++) { Bullet bullet = gf.bullets.get(i); System.out.println("test hit"); rect = new Rectangle(bullet.x, bullet.y - 1, bullet.width, bullet.height); //碰撞检测 if (myrect.intersects(rect)) { return true; } } return false; } @Override public String toString() { return "EnemyPlane{" + "x=" + x + ", y=" + y + ", width=" + width + ", height=" + heigth + }; }}
Bullet
package cn.tx;import javax.swing.*;import java.awt.*; public class Bullet { //在面板上的坐标 int x, y; int width= 50,height = 50; //定义飞机默认速度 int speed = 5; Image image = new ImageIcon("img/30022.png").getImage(); public Bullet(int x, int y) { this.x = x; this.y = y; } public Bullet(int x, int y, int width, int height) { this.x = x; this.y = y; this.width = width; this.height = height; }}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持盛行IT。
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。