(附素材)飞机大战

news/2025/2/12 1:49:54/

public class Constant {public static final int GAME_WIDTH = 400;public static final int GAME_HEIGHT = 400;
}
import java.awt.*;
/*** 爆炸类* @author Administrator**/
public class Explode {double x,y;//爆炸的位置static Image[] imgs = new Image[16];static {for(int i=0;i<16;i++) {imgs[i] = GameUtil.getImage("images/explode/e"+(i+1)+".gif");imgs[i].getWidth(null);}}int count;public void draw(Graphics g) {if(count<=15) {g.drawImage(imgs[count],(int)x,(int)y,null);count++;}}public Explode(double x,double y) {this.x = x;this.y = y;}
}
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
/*** 游戏物体的父类* @author Administrator**/
public class GameObject {Image img;double y;double x;int speed=20;int width,height;public void drawSelf(Graphics g) {g.drawImage(img, (int)x,(int)y,null);}public GameObject(Image img, double y, double x, int speed, int width, int height) {super();this.img = img;this.y = y;this.x = x;this.speed = speed;this.width = width;this.height = height;}public GameObject(Image img, double y, double x) {super();this.img = img;this.y = y;this.x = x;}public GameObject() {}/*** 返回物体所在的矩形,便于后续的碰撞检测* @return*/public Rectangle getRect() {return new Rectangle((int)x,(int)y,width,height);}
}

 

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;import javax.imageio.ImageIO;class GameUtil {private GameUtil() {//工具类最好将构造器私有}
/*** 指定路径的图片对象	* @param path* @return*/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;}
}

import java.awt.*;
import java.awt.event.*;
import java.util.Date;/*** 飞机游戏的主窗口* @author Administrator**/
public class MyGameFrame extends Frame{Image planeImg = GameUtil.getImage("images/plane.png");Image bg = GameUtil.getImage("images/bg.jpg");Plane plane = new Plane(planeImg,10,100);Shell[] shells = new Shell[50];Explode bao;Date startTime = new Date();Date endTime;int period;//游戏持续的时间 @Override//自动被系统调用//帮助我们做 窗口的绘制,自动被动用,g这个变量相当于画笔,我们想画的内容都是通过g这个画笔来画的public void paint(Graphics g) {//清除黑色背景g.clearRect(0, 0, this.getWidth(), this.getHeight());Color   c =  g.getColor();g.drawImage(bg, 0,0,null);plane.drawSelf(g);//飞机和炮弹的碰撞检测for(int i=0;i<shells.length;i++){shells[i].draw(g);boolean peng = shells[i].getRect().intersects(plane.getRect());if(peng) {plane.live =false;if(bao==null) {bao = new Explode(plane.x,plane.y);endTime = new Date();period = (int)(endTime.getTime()- startTime.getTime())/1000;}bao.draw(g);}if(!plane.live) {g.setColor(Color.white);Font f = new Font("宋体",Font.BOLD,50);g.setFont(f);g.drawString("你坚持了"+period+"秒",50 , 100);}}g.setColor(c);}//这个线程可以帮组我们反复地重画窗口class PaintThread extends Thread{@Overridepublic void run() {while(true) {repaint();//重画try {Thread.sleep(40);} catch (InterruptedException e) {e.printStackTrace();}}}}//定义键盘监听的内部类class   KeyMonitor extends  KeyAdapter  {@Overridepublic void keyPressed(KeyEvent e) {plane.addDirection(e);}@Overridepublic void keyReleased(KeyEvent e) {plane.minusDirection(e);}}/*** 初始化窗口*/public void launchFrame() {this.setTitle("飞机大战游戏");//我们这个窗口默认是不可见的,我们在这里使它可见this.setVisible(true);this.setSize(Constant.GAME_WIDTH, Constant.GAME_HEIGHT);	this.setLocation(100, 100);	//关闭窗口this.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {System.exit(0);}});new PaintThread().start();			//启动重画窗口的线程addKeyListener(new KeyMonitor());   //给窗口增加键盘的监听for(int i=0;i<shells.length;i++) {shells[i] = new Shell();}}public static void main(String[] args) {MyGameFrame f = new MyGameFrame();f.launchFrame();}//双缓冲private Image offSreenImage = null;public void update(Graphics g) {if(offSreenImage == null) {offSreenImage = this.createImage(Constant.GAME_WIDTH,Constant.GAME_HEIGHT);}Graphics gOff = offSreenImage.getGraphics();paint(gOff);g.drawImage(offSreenImage, 0, 0, null);} }
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;}			}}public Plane(Image img,double x, double y) {this.img = img;this.x = x;this.y = y;this.speed = 5;this.width = img.getWidth(null);this.height = img.getHeight(null);}//按下public void addDirection(KeyEvent e) {System.out.println("####"+e.getKeyCode());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) {System.out.println("####"+e.getKeyCode());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;}}
}
import java.awt.Color;
import java.awt.Graphics;public class Shell   extends  GameObject {double  degree;public  Shell(){x = 200;y = 200;width=10;height = 10;speed = 3;degree = Math.random()*Math.PI*2;}public  void   draw(Graphics  g){Color   c =  g.getColor();g.setColor(Color.YELLOW);g.fillOval((int)x,(int) y, width, height);//炮弹沿着任意角度去飞x += speed*Math.cos(degree);y += speed*Math.sin(degree);if(x<0||x>Constant.GAME_WIDTH-width){degree  = Math.PI - degree;}if(y<30||y>Constant.GAME_HEIGHT-height){degree  = - degree;}g.setColor(c);}}

 

背景素材: 

飞机素材:

爆炸素材:

(需将后缀名改为gif格式)(其实质是利用了连载做出了效果)

 


http://www.ppmy.cn/news/246778.html

相关文章

AB压测工具的介绍及安装

前言 今天我要和大家聊聊AB压测工具&#xff0c;如果你对网站性能测试感兴趣或有需要&#xff0c;那么这篇文章一定会帮到你。 我曾经也因为缺少良好的压力测试工具而苦恼&#xff0c;直到我发现了AB压测工具。它可以帮助我们测试网站在高并发情况下的性能表现&#xff0c;让…

飞机大战编程

飞机大战没有封装的前的代码 import pygame from pygame.locals import * import random import time class HeroBullet():#定义一个战机子弹的类def __init__(self,x,y,windows):self.xxself.yyself.windowswindowsself.picpygame.image.load(D:\\python使用软件\\IT研究院-P…

经典游戏----飞机大战

由于我们是用Python来制作游戏,我们需要安装一些必要的模块,如:pygame 安装方法: pip install pygame 目录 1 构建游戏窗口界面 2 基础设置类 3 在屏幕上绘制飞船 4 模块的重构(让代码简洁) 5 飞船移动(响应按键) 我们首先定义一个游戏界面并设置一个游戏对象 def run_ga…

python版飞机大战

python版飞机大战 用python几百行代码搞定飞机大站游戏。 我们利用pygame包进行飞机大战的游戏开发&#xff0c;所以大家首先得安装好pygame包&#xff0c;本游戏一共封装了8个类&#xff0c;大家可以在GitHub上下载https://github.com/Young157/aircraft-battle 定义玩家飞…

python——飞机大战小游戏

目录 1、导入模块 2、窗口操作 3、事件操作 4、长按事件 5、添加游戏背景 6、添加英雄飞机 7、获取飞机的图片矩形 8、基本游戏窗口 9、添加游戏窗口图片 10、英雄飞机登场 11、英雄飞机装备子弹并发射 1、enemy_plane 2、game_main 3、game_map 4、game_score …

飞机大战小游戏(超详细)

偷学Python之最后的项目二&#xff1a;飞机大战小游戏(超详细) 古之立大事者&#xff0c;不惟有超世之才&#xff0c;亦必有坚忍不拔之志。——苏轼 甜甜先说 这次用Python中的pygame模块来完成一个飞机大战的小游戏&#xff1b;基本思路是通过方向键来控制飞机的左右移动射击…

简单制作飞机大战游戏。

飞机大战&#xff08;初级&#xff09;(一) 因为最近有做这个的实训&#xff0c;每天一点点的学习。因为自己也是个初学者&#xff0c;希望自己所做的这些也能帮助简单学习java的朋友&#xff0c;这是第一次在这上面去分享这些&#xff0c;一方面希望自己能进步&#xff0c;能交…

Python 飞机大战搞怪版本

python 飞机大战搞怪版本 (飞机为迷你亚索&#xff0c;外星人为迷你小诺手&#xff0c;由于时间关系和图片素材较难寻找&#xff0c;仅仅做了简易版&#xff0c;没有贴上背景图片。由于篇幅原因&#xff0c;对于函数讲解较为简略&#xff0c;可以自行搜索相应函数的用法) 主要…