package ui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import building.EnemyBulletBuilding;
import building.EnemyPlaneBuilding;
import building.MyBulletBuilding;
import entity.EnemyBullet;
import entity.EnemyPlane;
import entity.MyBullet;
import entity.MyPlane;
import entity.Prop;
import main.Main;
import tools.Config;
import tools.Player;
public class GamePlay extends JPanel implements KeyListener
{
private Random random = new Random(System.currentTimeMillis());
private MyPlane myPlane;
private int smallEnemyPlaneShowTime;
private int middleEnemyPlaneShowTime;
private int maxEnemyPlaneShowTime;
private int myBulletDuration;
private int score;
private int level;
private Player backgoroundMusic;
private GamePlayBackground gamePlayBackground;
//volatile被设计用来修饰被不同线程访问和修改的变量
//敌方子弹的链表
private volatile List<EnemyBullet> enemyBullets;
//敌方飞机的链表
private volatile List<EnemyPlane> enemyPlanes;
//我方子弹的链表
private volatile List<MyBullet> myBullets;
//礼物链表
private volatile List<Prop> props;
//分数链表
private volatile List<Integer> scoreList;
public GamePlay() throws UnsupportedAudioFileException, IOException, LineUnavailableException
{
init();
initMusic();
}
private void initMusic() throws UnsupportedAudioFileException, IOException, LineUnavailableException
{
backgoroundMusic = new Player(Config.BACKGROUND_MUSIC);
}
private void init()
{
this.addKeyListener(this);
this.setSize(Config.GAME_WIDTH, Config.GAME_HEIGHT);
this.setOpaque(false);
}
public void startGame()
{
smallEnemyPlaneShowTime = Config.ENEMYPLANE_SMALL_SHOW_TIME;
middleEnemyPlaneShowTime = Config.ENEMYPLANE_MIDDLE_SHOW_TIME;
maxEnemyPlaneShowTime = Config.ENEMYPLANE_MAX_SHOW_TIME;
Config.ENEMYPLANE_SMALL_SPEED = Config.ENEMYPLANE_SMALL_SPEED_START;
Config.ENEMYPLANE_MIDDLE_SPEED = Config.ENEMYPLANE_MIDDLE_SPEED_START;
Config.ENEMYPLANE_MAX_SPEED = Config.ENEMYPLANE_MAX_SPEED_START;
myBulletDuration = -1;
ImageIcon imageIcon = new ImageIcon(Config.MYPLANE_SMALL_IMAGE);
myPlane = new MyPlane(this, (Config.GAME_WIDTH - imageIcon.getIconWidth()) / 2, Config.GAME_HEIGHT - imageIcon.getIconHeight() - Config.GAME_UP_HEIGHT);
gamePlayBackground = new GamePlayBackground(this, new ImageIcon(Config.GAME_PLAY_BACKGROUND_IMAGE));
enemyPlanes = new ArrayList<EnemyPlane>();
enemyBullets = new ArrayList<EnemyBullet>();
myBullets = new ArrayList<MyBullet>();
props = new ArrayList<Prop>();
score = 0;
level = 1;
backgoroundMusic.loop();
new Thread(new PaintThead()).start();
}
private void drawStr(Graphics g)
{
String string = "生命值:" + myPlane.getBlood();
g.setFont(new Font("微软雅黑", Font.BOLD, 30));
g.setColor(Color.BLUE);
g.drawString(string, Config.BLOOD_POSX, Config.BLOOD_POSY);
}
private void drawScore(Graphics g, int posx, int posy)
{
Graphics2D g2d = (Graphics2D)g;
scoreList = new ArrayList<Integer>();
int temp = score;
//为了使刚开始的0也加入到链表中
while ((temp / 10) != 0)
{
scoreList.add(temp % 10);
temp /= 10;
}
scoreList.add(temp % 10);
//设置分数的其实位置
int posX = posx;
int posY = posy;
//画分数
ImageIcon imageIcon;
for (int i = 0; i < scoreList.size(); i++)
{
switch (scoreList.get(i)) {
case 0:
imageIcon = new ImageIcon(Config.NUM_0);
g2d.drawImage(imageIcon.getImage(), posX, posY, imageIcon.getIconWidth() / 2, imageIcon.getIconHeight() / 2, GamePlay.this);
posX -= imageIcon.getIconWidth() / 2;
break;
case 1:
imageIcon = new ImageIcon(Config.NUM_1);
g2d.drawImage(imageIcon.getImage(), posX, posY, imageIcon.getIconWidth() / 2, imageIcon.getIconHeight() / 2, GamePlay.this);
posX -= imageIcon.getIconWidth() / 2;
break;
case 2:
imageIcon = new ImageIcon(Config.NUM_2);
g2d.drawImage(imageIcon.getImage(), posX, posY, imageIcon.getIconWidth() / 2, imageIcon.getIconHeight() / 2, GamePlay.this);
posX -= imageIcon.getIconWidth() / 2;
break;
case 3:
imageIcon = new ImageIcon(Config.NUM_3);
g2d.drawImage(imageIcon.getImage(), posX, posY, imageIcon.getIconWidth() / 2, imageIcon.getIconHeight() / 2, GamePlay.this);
posX -= imageIcon.getIconWidth() / 2;
break;
case 4:
imageIcon = new ImageIcon(Config.NUM_4);
g2d.drawImage(imageIcon.getImage(), posX, posY, imageIcon.getIconWidth() / 2, imageIcon.getIconHeight() / 2, GamePlay.this);
posX -= imageIcon.getIconWidth() / 2;
break;
case 5:
imageIcon = new ImageIcon(Config.NUM_5);
g2d.drawImage(imageIcon.getImage(), posX, posY, imageIcon.getIconWidth() / 2, imageIcon.getIconHeight() / 2, GamePlay.this);
posX -= imageIcon.getIconWidth() / 2;
break;
case 6:
imageIcon = new ImageIcon(Config.NUM_6);
g2d.drawImage(imageIcon.getImage(), posX, posY, imageIcon.getIconWidth() / 2, imageIcon.getIconHeight() / 2, GamePlay.this);
posX -= imageIcon.getIconWidth() / 2;
break;
case 7:
imageIcon = new ImageIcon(Config.NUM_7);
g2d.drawImage(imageIcon.getImage(), posX, posY, imageIcon.getIconWidth() / 2, imageIcon.getIconHeight() / 2, GamePlay.this);
posX -= imageIcon.getIconWidth() / 2;
break;
case 8:
imageIcon = new ImageIcon(Config.NUM_8);
g2d.drawImage(imageIcon.getImage(), posX, posY, imageIcon.getIconWidth() / 2, imageIcon.getIconHeight() / 2, GamePlay.this);
posX -= imageIcon.getIconWidth() / 2;
break;
case 9:
imageIcon = new ImageIcon(Config.NUM_9);
g2d.drawImage(imageIcon.getImage(), posX, posY, imageIcon.getIconWidth() / 2, imageIcon.getIconHeight() / 2, GamePlay.this);
posX -= imageIcon.getIconWidth() / 2;
break;
}
}
}
private void stopGame()
{
backgoroundMusic.stop();
}
private class PaintThead implements Runnable
{
@Override
public void run()
{
while (myPlane.isLive())
{
for (int i = 0; i < props.size(); i++)
{
Prop prop = props.get(i);
if (!prop.isLive())
{
synchronized (props)
{
props.remove(i);
}
}
else
{
propsAndMyPlane(i);
}
}
//对敌方的子弹进行检测
for (int i = 0; i < enemyBullets.size(); i++)
{
EnemyBullet enemyBullet = enemyBullets.get(i);
if (!enemyBullet.isLive())
{
synchronized (enemyBullets)
{
enemyBullets.remove(i);
}
}
else
{
enemyBulletsAndMyPlane(i);
enemyBulletsAndMyBullets(i);
}
}
//对我方的子弹进行检测
for (int i = 0; i < myBullets.size(); i++)
{
MyBullet myBullet = myBullets.get(i);
if (!myBullet.isLive())
{
synchronized (myBullets)
{
myBullets.remove(i);
}
}
}
//对敌方的飞机进行检测
for (int i = 0; i < enemyPlanes.size(); i++)
{
EnemyPlane enemyPlane = enemyPlanes.get(i);
if (!enemyPlane.isLive())
{
synchronized (enemyPlanes)
{
enemyPlanes.remove(i);
}
}
else
{
enemyPlaneAndMyBullets(i);
enemyPlaneAndMyPlane(i);
Random random = new Random();
//100以内的随机数大于90 并且 敌方飞机正好正对着我方飞机 将该敌方飞机设置开枪
if (random.nextInt(100) > 80 && enemyPlane.isLive() && (enemyPlane.getX() < myPlane.getX() + (myPlane.getWidth() / 2)) && (enemyPlane.getX() > myPlane.getX() - (myPlane.getWidth() / 2)))
没有合适的资源?快使用搜索试试~ 我知道了~
Java课程设计 - 王牌战机
共159个文件
png:71个
class:29个
java:24个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 16 浏览量
2023-11-04
01:04:09
上传
评论
收藏 5.57MB ZIP 举报
温馨提示
该资源内项目源码是个人的课程设计作业,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到94.5分,放心下载使用! 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 -------- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
资源推荐
资源详情
资源评论
收起资源包目录
Java课程设计 - 王牌战机 (159个子文件)
GamePlay.class 9KB
GamePlay$PaintThead.class 7KB
MainFrame.class 6KB
Config.class 6KB
MyPlane.class 4KB
GameButton.class 4KB
EnemyPlane.class 3KB
EnemyBullet.class 3KB
GameTopScore.class 3KB
MysqlConnection.class 3KB
MyBullet.class 2KB
Prop.class 2KB
EnemyPlaneBuilding.class 2KB
GameMenu.class 2KB
GameLoading.class 2KB
MiddleEnemyPlane.class 1KB
SmallEnemyPlane.class 1KB
MaxEnemyPlane.class 1KB
Player.class 1KB
GamePlayBackground.class 1KB
EnemyBulletBuilding.class 1KB
MyBulletBuilding.class 1KB
MainFrame$startGameClass.class 1KB
Score.class 718B
MiddleEnemyPlane$DrawKilled.class 688B
SmallEnemyPlane$DrawKilled.class 682B
MaxEnemyPlane$DrawKilled.class 670B
GameBackground.class 665B
Main.class 432B
.classpath 295B
Thumbs.db 78KB
Thumbs.db 59KB
Thumbs.db 48KB
Thumbs.db 22KB
Thumbs.db 20KB
Thumbs.db 9KB
EnemyMaxPlaneExplode.gif 68KB
EnemyMiddlePlaneExplode.gif 10KB
MyPlaneExplode.gif 10KB
MyPlane.gif 6KB
Loading.gif 6KB
EnemySmallPlaneExplode.gif 5KB
GamePlay.java 15KB
Config.java 7KB
MainFrame.java 6KB
MyPlane.java 5KB
GameButton.java 3KB
EnemyPlane.java 3KB
EnemyBullet.java 2KB
EnemyPlaneBuilding.java 2KB
GameMenu.java 2KB
GameTopScore.java 2KB
MysqlConnection.java 2KB
Prop.java 2KB
MyBullet.java 2KB
GameLoading.java 1KB
EnemyBulletBuilding.java 1KB
MyBulletBuilding.java 1KB
Player.java 1KB
GamePlayBackground.java 1KB
MaxEnemyPlane.java 1KB
MiddleEnemyPlane.java 1KB
SmallEnemyPlane.java 999B
GameBackground.java 482B
Score.java 277B
Main.java 139B
Background.jpg 88KB
GamePlayBackground - 副本.jpg 88KB
StartBackground.jpg 43KB
bg_menu.jpg 34KB
bg_02.jpg 26KB
bg_01.jpg 25KB
bg1_startcg.jpg 24KB
GamePlayBackground.png 1.31MB
GamePlayBackground.png 66KB
EnemyMaxPlane.png 52KB
EnemyPlane_three_2.png 52KB
EnemyPlane_two_3.png 31KB
EnemyMiddlePlane.png 31KB
EnemyPlane_two_1.png 30KB
StartBackgound.png 28KB
GameOver.png 27KB
MyOrangePlane_three.png 23KB
MyMiddlePlane.png 21KB
3.png 21KB
2.png 21KB
1.png 20KB
0.png 20KB
敌方飞机7.png 20KB
MyPinkPlane_three.png 18KB
MyMaxPlane.png 18KB
MyOrangePlane_two.png 18KB
Prop - 副本.png 18KB
MyPinkPlane_one.png 16KB
MyPinkPlane_two.png 16KB
MySmallPlane.png 16KB
MyOrangePlane_one.png 15KB
MyMaxBullet.png 15KB
敌方飞机11.png 15KB
PinkBullet_three.png 15KB
共 159 条
- 1
- 2
资源评论
机智的程序员zero
- 粉丝: 2012
- 资源: 4223
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功