目录
大家好,我是小刘!
本篇文章将向各位介绍一个Java游戏项目,本次项目可以应用与毕业设计以及简历等!!!
运行结果如下
接下来就让我们进入代码的实现吧
首先新建一个项目,然后在项目里面新建一个存放图片的文件夹,将本次项目所需要的图片存放进去,图片资源在文章最后。(按照小刘下面这个方式去创建就可以啦,!!一定要在项目中去创建!!)
首先是程序的主类GameWin
package ld1;
import Game.*;
import Game.Utils.GameUtils;
import Game.Utils.PlaneObj;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class GameWin extends JFrame {
public static int state=0;
public static int score=0;
Image offScreenImage=null;
int width=600;
int height=600;
int count=1;
int enemyCount=0;
BgObj bgobj=new BgObj(GameUtils.bgImg,0,-2000,2);
public PlaneObj planeObj=new PlaneObj(GameUtils.planeImg,290,550,20,30,0,this);
public BossObj bossObj=null;
public void lunch(){
//设置窗口是否可见
this.setVisible(true);
//设置窗口大小
this.setSize(width,height);
//设置窗口位置
this.setLocationRelativeTo(null);
//设置窗口标题
this.setTitle("飞机大战");
GameUtils.gameObjList.add(bgobj);
GameUtils.gameObjList.add(planeObj);
this.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if(e.getButton()==1 && state==0){
state=1;
repaint();
}
}
});
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode()==32){
switch (state){
case 1:
state=2;
break;
case 2:
state=1;
break;
default:break;
}
}
}
});
while (true){
if (state==1){
createObj();
repaint();
}
try {
Thread.sleep(25);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void paint(Graphics g) {
if (offScreenImage==null){
offScreenImage=createImage(width,height);
}
Graphics gImage= offScreenImage.getGraphics();
gImage.fillRect(0,0,width,height);
if (state==0){
gImage.drawImage(GameUtils.bgImg,0,0,null);
gImage.drawImage(GameUtils.bossImg,220,120,null);
gImage.drawImage(GameUtils.explodeImg,270,350,null);
GameUtils.drawWord(gImage,"点击开始游戏",Color.yellow,40,180,300);
}
if (state==1){
GameUtils.gameObjList.addAll(GameUtils.explodeObjList);
for (int i=0;i<GameUtils.gameObjList.size();i++){
GameUtils.gameObjList.get(i).paintSelf(gImage);
}
GameUtils.gameObjList.removeAll(GameUtils.removeObjList);
}
if (state==3){
gImage.drawImage(GameUtils.explodeImg,bossObj.getX()+30,planeObj.getY()-50,null);
GameUtils.drawWord(gImage,"GAME OVER",Color.red,50,180,300);
}
if (state==4){
gImage.drawImage(GameUtils.explodeImg,planeObj.getX()-35,bossObj.getY(),null);
GameUtils.drawWord(gImage,"游戏通关",Color.green,50,180,300);
}
GameUtils.drawWord(gImage,score+"分",Color.green,40,30,100);
g.drawImage(offScreenImage,0,0,null);
count++;
System.out.println(GameUtils.gameObjList.size());
}
void createObj(){
if (count%15==0) {
GameUtils.shellObjList.add(new ShellObj(GameUtils.shellImg, planeObj.getX() + 3, planeObj.getY() - 16, 14, 29, 5, this));
GameUtils.gameObjList.add(GameUtils.shellObjList.get(GameUtils.shellObjList.size() - 1));
}
if (count%15==0) {
GameUtils.enemyObjList.add(new EnemyObj(GameUtils.enemyImg,(int)(Math.random()*12)*50,0,32,24,5,this));
GameUtils.gameObjList.add(GameUtils.enemyObjList.get(GameUtils.shellObjList.size()-1));
enemyCount++;
}
if (count%15==0 && bossObj!=null) {
GameUtils.bulletObjList.add(new BulletObj(GameUtils.bulletImg, bossObj.getX()+76, bossObj.getY()+85,15,25,5,this ));
GameUtils.gameObjList.add(GameUtils.bulletObjList.get(GameUtils.bulletObjList.size()-1));
}
if (enemyCount>100 && bossObj==null){
bossObj=new BossObj(GameUtils.bossImg,250,35,155,100,5,this);
GameUtils.gameObjList.add(bossObj);
}
}
public static void main(String[] args) {
GameWin gameWin=new GameWin();
gameWin.lunch();
}
}
游戏父类GameObj
package Game;
import ld1.GameWin;
import java.awt.*;
public class GameObj {
Image img;
int x;
int y;
int width;
int height;
double speed;
GameWin frame;
public Image getImg() {
return img;
}
public void setImg(Image img) {
this.img = img;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public double getSpeed() {
return speed;
}
public void setSpeed(double speed) {
this.speed = speed;
}
public GameWin getFrame() {
return frame;
}
public void setFrame(GameWin frame) {
this.frame = frame;
}
public GameObj(){
}
public GameObj(int x, int y) {
this.x = x;
this.y = y;
}
public GameObj(Image img, int x, int y, double speed) {
this.img = img;
this.x = x;
this.y = y;
this.speed = speed;
}
public GameObj(Image img, int x, int y, int width, int height, double speed, GameWin frame) {
this.img = img;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.speed = speed;
this.frame = frame;
}
public void paintSelf(Graphics gImage){
gImage.drawImage(img,x,y,null);
}
public Rectangle getRec(){
return new Rectangle(x,y,width,height);
}
}
工具类GameUtils
package Game.Utils;
import Game.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
public class GameUtils {
public static Image bgImg=Toolkit.getDefaultToolkit().getImage("imgs/bgImg.png");
public static Image bossImg=Toolkit.getDefaultToolkit().getImage("imgs/boss.png");
public static Image explodeImg=Toolkit.getDefaultToolkit().getImage("imgs/e6.png");
public static Image planeImg=Toolkit.getDefaultToolkit().getImage("imgs/planeImg.png");
public static Image shellImg=Toolkit.getDefaultToolkit().getImage("imgs/shellImg.png");
public static Image bulletImg=Toolkit.getDefaultToolkit().getImage("imgs/bulletImg.png");
public static Image enemyImg=Toolkit.getDefaultToolkit().getImage("imgs/enemyImg.png");
public static List<GameObj> gameObjList=new ArrayList<>();
public static List<GameObj> removeObjList=new ArrayList<>();
public static List<BulletObj> bulletObjList=new ArrayList<>();
public static List<ShellObj> shellObjList=new ArrayList<>();
public static List<EnemyObj> enemyObjList=new ArrayList<>();
public static List<ExplodeObj> explodeObjList=new ArrayList<>();
public static void drawWord(Graphics gImage,String str,Color color,int size,int x,int y){
gImage.setColor(color);
gImage.setFont(new Font("仿宋",Font.BOLD,size));
gImage.drawString(str,x,y);
}
}
飞机类PlaneObj
package Game.Utils;
import Game.GameObj;
import ld1.GameWin;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class PlaneObj extends GameObj {
@Override
public Image getImg() {
return super.getImg();
}
public PlaneObj() {
super();
}
public PlaneObj(Image img, int x, int y, int width, int height, double speed, GameWin frame) {
super(img, x, y, width, height, speed, frame);
this.getFrame().addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
PlaneObj.super.setX(e.getX() - 19);
PlaneObj.super.setY(e.getY() - 20);
}
});
}
@Override
public void paintSelf(Graphics gImage) {
super.paintSelf(gImage);
if (this.getFrame().bossObj!=null&&this.getRec().intersects(this.getFrame().bossObj.getRec())){
GameWin.state=3;
}
}
@Override
public Rectangle getRec() {
return super.getRec();
}
}
背景类BgObj
package Game;
import java.awt.*;
public class BgObj extends GameObj{
public BgObj(){
super();
}
public BgObj(Image img, int x, int y, double speed) {
super(img, x, y, speed);
}
public void paintSelf(Graphics gImage) {
super.paintSelf(gImage);
y+=speed;
if (y>=0){
y=-2000;
}
}
}
反派boss类BossObj
package Game;
import Game.Utils.GameUtils;
import ld1.GameWin;
import java.awt.*;
public class BossObj extends GameObj{
int life=10;
public BossObj(Image img, int x, int y, int width, int height, double speed, GameWin frame) {
super(img, x, y, width, height, speed, frame);
}
@Override
public void paintSelf(Graphics gImage) {
super.paintSelf(gImage);
if (x>550 || x<-50){
speed=-speed;
}
x+=speed;
for (ShellObj shellObj: GameUtils.shellObjList){
if (this.getRec().intersects(shellObj.getRec())){
shellObj.setX(-100);
shellObj.setY(100);
GameUtils.removeObjList.add(shellObj);
life--;
}
if (life <= 0) {
GameWin.state=4;
}
}
gImage.setColor(Color.white);
gImage.fillRect(20,40,100,10);
gImage.setColor(Color.red);
gImage.fillRect(20,40,life*100/10,10);
}
@Override
public Rectangle getRec() {
return super.getRec();
}
}
子弹类BulletObj
package Game;
import Game.Utils.GameUtils;
import ld1.GameWin;
import java.awt.*;
public class BulletObj extends GameObj{
public BulletObj(Image img, int x, int y, int width, int height, double speed, GameWin frame) {
super(img, x, y, width, height, speed, frame);
}
@Override
public void paintSelf(Graphics gImage) {
super.paintSelf(gImage);
y+=speed;
if (this.getRec().intersects(this.frame.planeObj.getRec())){
GameWin.state=3;
}
if (y>600){
this.x=-300;
this.y=300;
GameUtils.removeObjList.add(this);
}
}
@Override
public Rectangle getRec() {
return super.getRec();
}
}
敌机类EnemyObj
package Game;
import Game.Utils.GameUtils;
import ld1.GameWin;
import java.awt.*;
public class EnemyObj extends GameObj{
public EnemyObj() {
super();
}
public EnemyObj(Image img, int x, int y, int width, int height, double speed, GameWin frame) {
super(img, x, y, width, height, speed, frame);
}
@Override
public void paintSelf(Graphics gImage) {
super.paintSelf(gImage);
y+=speed;
if (this.getRec().intersects(this.frame.planeObj.getRec())){
GameWin.state=3;
}
if (y>600){
this.x=-200;
this.y=200;
GameUtils.removeObjList.add(this);
}
for (ShellObj shellObj: GameUtils.shellObjList) {
if (this.getRec().intersects(shellObj.getRec())){
ExplodeObj explodeObj=new ExplodeObj(x,y);
GameUtils.explodeObjList.add(explodeObj);
GameUtils.removeObjList.add(explodeObj);
shellObj.setX(-100);
shellObj.setY(100);
this.x=-200;
this.y=200;
GameUtils.removeObjList.add(shellObj);
GameUtils.removeObjList.add(this);
GameWin.score++;
}
}
}
@Override
public Rectangle getRec() {
return super.getRec();
}
}
爆炸类ExplodeObj
package Game;
import java.awt.*;
public class ExplodeObj extends GameObj{
static Image[] pic=new Image[16];
int explodeCount=0;
static{
for (int i = 0; i < pic.length ; i++) {
pic[i]=Toolkit.getDefaultToolkit().getImage("imgs/explode/e1"+i+".png");
}
}
public ExplodeObj(int x,int y){
super(x,y);
}
@Override
public void paintSelf(Graphics gImage) {
if (explodeCount<16){
super.img=pic[explodeCount];
super.paintSelf(gImage);
explodeCount++;
}
}
}
最后是己方子弹ShellObj类
package Game;
import Game.Utils.GameUtils;
import ld1.GameWin;
import java.awt.*;
public class ShellObj extends GameObj{
@Override
public Image getImg() {
return super.getImg();
}
public ShellObj() {
super();
}
public ShellObj(Image img, int x, int y, int width, int height, double speed, GameWin frame) {
super(img, x, y, width, height, speed, frame);
}
@Override
public void paintSelf(Graphics gImage) {
super.paintSelf(gImage);
y-=speed;
if (y<0){
this.x=-100;
this.y=100;
GameUtils.removeObjList.add(this);
}
}
@Override
public Rectangle getRec() {
return super.getRec();
}
}
图片资源
最后希望大家多多点赞支持小刘,下一个项目是贪吃蛇游戏的实现,大家也可以支持一下!
背景图bgImg.png
敌方boss图boss.png
敌方子弹图bulletImg.png
敌方飞机图enemyImg.png
飞机图planeImg.png
子弹图shellImg.png
下面是爆炸图1-16