⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gamestartframe.java

📁 简单的用Java做的小游戏主要是用了自己的框架来
💻 JAVA
字号:
package frame;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Rectangle;

import javax.swing.JFrame;

import assistant.CreateMapRect;
import assistant.CreateRole;
import assistant.FrameKeyEvent;
import assistant.PublicVar;
import assistant.MapRect;
import assistant.Music;
import assistant.TrackerMainImage;

import role.BaseRole;
import role.Dragon;


/**
 * 主窗体类
 * @author lovo
 *
 */
public class GameStartFrame extends JFrame implements Runnable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -3794300301467896183L;

	/** 地图的X坐标 */
	public static int mapX = 0;//0
	
	/** 地图的Y坐标 */
	public static int mapY = -2200;//-2200
	
	/** 取得双缓冲画布 */
	private Image memoryImage;

	/** 产生双缓冲画笔 */
	private Graphics memoryGraphics;
	
	/** 媒体追踪器tracker */
	private MediaTracker tracker;
	
	/**地图图像*/
	public static Image mapImage;

	/**地图是否向右滚动*/
	public static boolean isRightScroll=true;//true
	
	/**地图是否向上滚动*/
	public static boolean isUpScroll=false;
	
	/**进入BOSS地图区域*/
	public static MapRect enterBoss;
	
	/**音乐*/
	public static Music playMusic;

	/**
	 * 构造方法
	 *
	 */
	public GameStartFrame() {
		/* 设置窗体大小 */
		this.setSize(650, 550);

		/* 设置窗体可见 */
		this.setVisible(true);
		
		/* 设置点击关闭,程序结束 */
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);

		/* 设置窗口不可改变 */
		this.setResizable(false);
		/*设置窗体居中*/
		this.setLocationRelativeTo(null);
		
		/*构建媒体追踪器对象*/
		this.tracker=new MediaTracker(this);
		
		/*媒体追踪器*/
		TrackerMainImage.trackerImage(this.tracker,this);

		/* 创建画布 */
		memoryImage = this.createImage(650, 550);
		
		/* 得到画笔对象 */
		memoryGraphics = memoryImage.getGraphics();
		
		/*注册键盘事件*/
		this.addKeyListener(new FrameKeyEvent());
		
		//播放背景音乐
		playMusic=new Music(Music.PLAY_BACKGROUND);
		
		PublicVar.roleList.clear();
		PublicVar.roleList.add(PublicVar.chip);
		
		PublicVar.roleList.add(new Dragon(200,200,PublicVar.RIGHT));

//		PublicVar.roleList.add(new FlyMonkey(610,100,PublicVar.LEFT));
//		
//		PublicVar.roleList.add(new Fire(610,100,PublicVar.LEFT,60));
		
		
		
		/*启动线程*/
		Thread thread=new Thread(this);
		thread.start();
		
	}
	
	
	/**
	 * 重写线程run()方法,完成业务逻辑
	 */
	public void run() {
		while(PublicVar.isStart){

//			repaint();//窗体重绘
			
			this.update(this.getGraphics());
			
			CreateMapRect.createRect();//创建地图区域
			
			CreateRole.createRole();//创建角色
			
			if(PublicVar.isBoss){//如果进入Boss,则跳出循环
				break;
			}
			
			PublicVar.chipDie();
			
			try {
				Thread.sleep(20);//线程休眠
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
		playMusic.stopLoopMusic();//停止播放背景音乐
		this.dispose();//窗体卸载
		
		//如果到达Boss区域
		if(PublicVar.isBoss){
			new BossFrame();//新建BOSS窗体
		}
		else{
			new LoseFrame();
		}
	}
	
	/**
	 * 重写update方法,绘制图形
	 * @param Graphics 画笔对象
	 */
	public void update(Graphics g){
		
		/*绘制地图*/
		memoryGraphics.drawImage(GameStartFrame.mapImage, 
				GameStartFrame.mapX,GameStartFrame.mapY,null);
		
		/*绘制松鼠临时区域*/
		memoryGraphics.setColor(Color.white);
		Rectangle mapRects = PublicVar.chip.tempMapRect;//地图区域
		memoryGraphics.drawRect(mapRects.x,mapRects.y,
				mapRects.width,mapRects.height);//绘制松鼠地图区域
		
//		绘制松鼠碰撞区域
		Rectangle hitRect = PublicVar.chip.tempHitRect;//碰撞区域
		memoryGraphics.drawRect(hitRect.x,hitRect.y,
				hitRect.width,hitRect.height);//绘制松鼠碰撞区域
		
		memoryGraphics.setColor(Color.yellow);
		
		
		/*绘制角色区域*/
		for(int i=0;i<PublicVar.roleList.size();i++){//循环遍历角色集合
			BaseRole role = PublicVar.roleList.get(i);
			role.drawMyself(memoryGraphics, this);//绘制角色
			memoryGraphics.drawRect(role.getRect().x,role.getRect().y,
					role.getRect().width,role.getRect().height);
		}
		
		/*绘制地图区域*/
		memoryGraphics.setColor(Color.green);
		for(int i=0;i<PublicVar.rectList.size();i++){
			MapRect mapRect=PublicVar.rectList.get(i);
			memoryGraphics.drawRect(mapRect.x,mapRect.y,mapRect.width,mapRect.height);//绘制地图区域
		}
		
		PublicVar.drawInfo(memoryGraphics);
		
		/**将内存中的图像画在画布上*/
		g.drawImage(memoryImage, 0, 0, this);
		
		
	}

	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO 自动生成方法存根
		new GameStartFrame();
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -