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

📄 client.java

📁 简单的单机游戏
💻 JAVA
字号:
package game;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.swing.JDialog;
import javax.swing.JOptionPane;

import game.Way;

public class Client extends Frame{
	public static final int BG_WIDTH=800;
	public static final int BG_HEIGHT=600;
	
	People myPeople=new People(70,70,true,People.Direction.STOP,this);
	People endPeople=new People(670,570,false,People.Direction.STOP,this);
	private static Random r=new Random();
	int randomNumber=r.nextInt(8);
	//Way wall1=new Way(200,300,220,5,this);//画一条路
	//Way wall2=new Way(500,200,3,255,this);//画一条路
	//Blood b=new Blood();
	//enum Location{222,111,55,444,555};//随机位置
	Explode e=new Explode(60,60,this);

	Way way1=new Way(100,500,490,1,this);
	List<Way> wayList1=new ArrayList<Way>();
	List<Way> wayList2=new ArrayList<Way>();
	List<Missile> missiles=new ArrayList<Missile>();
	List<Explode> explodes=new ArrayList<Explode>();
	List<People> monster=new ArrayList<People>();//画出多个monster
	Image offScreenImage=null;
	Date date=new Date();
	public void paint(Graphics g) {
		
		g.drawString("金币值(my money):"+myPeople.getMoney()+"⊙ o ⊙", 10, 50);
		g.drawString("当前时间(current time):"+date.getYear()+"年"+date.getMonth()+"月"+date.getDay()+"日", 410, 50);
		g.drawString("GO!", 75, 75);
		g.drawString("END!", 700, 530);
		
		if(monster.size()<=0){
			for(int i=0;i<5;i++){
				monster.add(new People(70+40*(i+1),50+20*(i+2),false,People.Direction.R,this));//画出多(10)个敌人的坦克
			}
		}
		
		for(int i=0;i<missiles.size();i++){
			Missile m=missiles.get(i);
			m.hitPeoples(monster);
			m.hitPeople(myPeople);
			//m.hitWay(wall1);
			//m.hitWay(wall2);
			//m.hitPeople(enemyPeople);
			/*if(!m.isAlive())	missiles.remove(m);
			else	m.draw(g);
			//在Missile.java中已经清除,故不需*/
			m.draw(g);
		}
		
		for(int i=0;i<explodes.size();i++){
			Explode e=explodes.get(i);
			e.draw(g);
		}
		/*if(b.isAlive()==false){//若血块死了,让其复活
			b.setAlive(true);
		}
		b.draw(g);*/
		//myPeople.hitWay(wall);
		myPeople.draw(g);
		//myPeople.eat(b);
		//enemyPeople.draw(g);
		//wall1.draw(g);//画一条路
		//wall2.draw(g);//画一条路
		way1.draw(g);
		
		for(int i=0;i<wayList1.size();i++){
			Way way1=wayList1.get(i);
			way1.draw(g);
		}
		for(int i=0;i<wayList2.size();i++){
			Way way2=wayList2.get(i);
			way2.draw(g);
		}
		for(int i=0;i<monster.size();i++){
			People t=monster.get(i);
			t.onWay(way1);
			//t.hitWay(wall2);
			t.collidesWithPeoples(monster);
			t.draw(g);
			//JOptionPane.showMessageDialog("");
			//JOptionPane.showInputDialog("OOIIIII");
		}
	}
	
	public void update(Graphics g) {
		if(offScreenImage==null){
			offScreenImage=this.createImage(BG_WIDTH, BG_HEIGHT);
		}
		Graphics gOffScreen=offScreenImage.getGraphics();
		Color c=gOffScreen.getColor();
		gOffScreen.setColor(Color.WHITE);
		gOffScreen.fillRect(0, 0,BG_WIDTH, BG_HEIGHT);
		gOffScreen.setColor(c);//刷新背景色
		paint(gOffScreen);
		g.drawImage(offScreenImage, 0,0,null);
	}

	public void lauchFrame() throws InterruptedException{
		for(int i=0,j=9;i<9;i++,j--){
			monster.add(new People(170+40*(i+1),250+20*(j-2),false,People.Direction.D,this));//画出多(10)个敌人的坦克
		}
		for(int i=0;i<=11;i++){
			wayList1.add(new Way(100,70+40*(i+1),500,2,this));//横向画出10条路
			wayList2.add(new Way(100+40*(i+1),100,2,400,this));//纵画出
		}
		
		this.setLocation(200,100);
		this.setSize(BG_WIDTH, BG_HEIGHT);
		this.setTitle("迷宫游戏");//改变窗口的标题
		
		//以下窗口关闭时间使用匿名类
		this.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e) {
				System.exit(0);//窗口正常关闭
			}
			
		});
		this.setResizable(false);//不允许改变窗口的大小
		this.setBackground(Color.WHITE);//刷新窗口背景色
		this.addKeyListener(new KeyMonitor());
		setVisible(true);//置于最前面
		//new Thread(new PaintThread()).start();//窗口打开时,启动重画线程
		
		if(1==1){
			JOptionPane.showInputDialog("请输入角色名!");
			new Thread(new PaintThread()).start();//窗口打开时,启动重画线程
			//new Thread(new PaintThread()).sleep(1100);
			//Thread.sleep(500);
			}
		
		if(myPeople.collidesWithPeoples(monster)){
			JOptionPane.showMessageDialog(new MYDialog(null), this);
		}
		//new Thread().start();
		
		//this.addComponentListener(new MYDialog(null));
		/*this.addWindowListener(new WindowAdapter(){
			public void windowOpen(WindowEvent e) {
				e.WINDOW_OPENED;//窗口正常关闭
			}
			
		});*/
	}
	
	public static void main(String[] args) throws InterruptedException{
		Client client=new Client();
	      client.lauchFrame();
	}
	
	private class   MYDialog   extends   JDialog   {   
        public   MYDialog(Frame   owner) throws InterruptedException {  
        super(owner,   "title",   true);  
        if(1==1){
			JOptionPane.showInputDialog("交易货物!");
			new Thread(new PaintThread()).wait();//窗口打开时,启动重画线程
			//new Thread(new PaintThread()).sleep(1100);
			//Thread.sleep(500);
			}
        }   
        
	}
	/**
	 * 使用内部类
	 *创建重画线程
	 **/
	private class PaintThread implements Runnable{

		public void run() {
			while(true){
				repaint();
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			
		}
		
	}
	/**
	 * 使用内部类
	 *创建控制坦克的键盘监视器
	 * 可以使玩家向四个方向运动
	 **/
	private class KeyMonitor extends KeyAdapter{
		
		public void keyReleased(KeyEvent e) {
			myPeople.keyReleased(e);
		}

		public void keyPressed(KeyEvent e) {
			myPeople.keyPressed(e);
		}
		
	}
	/*
	private class WindowMonitor extends WindowAdapter{
		
		public void keyReleased(KeyEvent e) {
			myPeople.keyReleased(e);
		}

		public void keyPressed(KeyEvent e) {
			myPeople.keyPressed(e);
		}
		
	}*/
	
}

⌨️ 快捷键说明

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