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

📄 gameeditor.java

📁 控制移到机器人的例子程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        return(x/this.maze.cellsize);	    
	}
	public int getYCell(int y)
	{
	    return(this.maze.height - 1 - (y/this.maze.cellsize));
	}
	
	public void goldClick(java.awt.event.MouseEvent event)
	{
	    int mouse_x = event.getX()+5; int mouse_y=event.getY()+5;
	    int dx = mouse_x - this.maze_x;
	    int dy = mouse_y- this.maze_y;
	    
	    if (this.nearCenter(dx) && this.nearCenter(dy) &&
	        this.inXBounds(dx) && this.inYBounds(dy)) {
	        //in this case, gold goes in a single cell//
	        int cx=this.getXCell(dx); int cy=this.getYCell(dy);
	        if (this.maze.removeGold(cx,cy))
	            return;
	            else this.maze.addGold(cx,cy);	    
	    }  // endif
	    else if (this.nearEdge(dx) && this.inXBounds(dx) &&
	             this.inYBounds(dy)) {
	        //put gold on a vertical wall//
	        int cx=this.getXCell(dx); int cy=this.getYCell(dy);
	        if (this.maze.removeGold(cx,cy,cx-1,cy))
	            return;
	            else this.maze.addGold(cx,cy,cx-1,cy);
	    } // end  else if (this.nearEdge(dx... //
	    else if (this.nearEdge(dy) && this.inXBounds(dx) &&
	             this.inYBounds(dy)) {
	        // put gold on a horizontal wall //
	        int cx=this.getXCell(dx); int cy=this.getYCell(dy);
	        if (this.maze.removeGold(cx,cy,cx,cy+1))
	            return;
	            else this.maze.addGold(cx,cy,cx,cy+1);
	    } // end else if (this.nearEdge(dy... //
	} // goldClick() //
	public void portClick(java.awt.event.MouseEvent event)
	{
	    int mouse_x = event.getX();
	    int mouse_y = event.getY();
	    int dx = mouse_x - this.maze_x;
	    int dy = mouse_y - this.maze_y;
	    
	    // if the click's near the center of a cell...
	    if (this.nearCenter(dx) &&
	        this.nearCenter(dy)  &&
            this.inXBounds(dx) &&
            this.inYBounds(dy)
           ) {
                int cx = this.getXCell(dx);
                int cy = this.getYCell(dy);
                if (this.maze.removePort(cx,cy)) return;
                else {
                    /*System.out.println("adding port!\n");*/
                    this.maze.addPort(cx,cy);
                }
           } //endif   
	} // portClick() //
	public void robotClick(java.awt.event.MouseEvent event)
	{
	    int mouse_x = event.getX();
	    int mouse_y = event.getY();
	    int dx = mouse_x - this.maze_x;
	    int dy = mouse_y - this.maze_y;
	    
	    // if the click's near the center of a cell...
	    if (this.nearCenter(dx) &&
	        this.nearCenter(dy)  &&
            this.inXBounds(dx) &&
            this.inYBounds(dy)
           ) {
                int cx = this.getXCell(dx);
                int cy = this.getYCell(dy);
                if (myLocation != null) {
                    this.maze.removeRobot(myLocation[0],myLocation[1],
                            myLocation[2],0);
                }
                myLocation = new int[] {cx,cy,roboDirection};
                this.maze.addRobot(cx,cy,roboDirection,0);
           } //endif   
	} // robotClick() //
	public void wallClick(java.awt.event.MouseEvent event)
	{
        int mouse_x = event.getX() + 5;
        int mouse_y = event.getY() + 5;
        int dx = mouse_x - this.maze_x;
        int dy = mouse_y - this.maze_y;
        int cell_x, cell_y;
        
        if (this.nearEdge(dx) && this.inXBounds(dx) &&
            this.inYBounds(dy)) {
                // we've clicked for a vertical wall!
                int cx = this.getXCell(dx);
                int cy = this.getYCell(dy);
                if (maze.walls[cx][cy][1] == 0) {
                    // System.out.println("adding wall");
                    this.maze.addWall(cx,cy,1);
                }
                    else {
                        // System.out.println("removing wall");
                        this.maze.removeWall(cx,cy,1);
                    }
        } else if (this.nearEdge(dy) &&
            this.inXBounds(dx) && this.inYBounds(dy)) {
                // we've clicked for a horizontal wall!
                int cx = this.getXCell(dx);
                int cy = this.getYCell(dy);
                if (maze.walls[cx][cy][0] == 0)                  
                    this.maze.addWall(cx,cy,0);
                    else this.maze.removeWall(cx,cy,0);
        } //endelseif
	} // wallClick() //

	void buttonWalls_MouseClick(java.awt.event.MouseEvent event)
	{
		// to do: code goes here.
		this.edit_state= GameEditor.WALLS;
		button_walls.setBackground(Color.yellow);
		button_robotN.setBackground(new Color(12632256));
		button_robotW.setBackground(new Color(12632256));
		button_robotS.setBackground(new Color(12632256));
		button_robotE.setBackground(new Color(12632256));				
		button_golds.setBackground(new Color(12632256));
		button_ports.setBackground(new Color(12632256));
	} // buttonWalls_MouseClick() //

	void buttonGolds_MouseClick(java.awt.event.MouseEvent event)
	{
		this.edit_state= GameEditor.GOLDS;
		button_golds.setBackground(Color.yellow);
		button_robotN.setBackground(new Color(12632256));
		button_robotW.setBackground(new Color(12632256));
		button_robotS.setBackground(new Color(12632256));
		button_robotE.setBackground(new Color(12632256));				
		button_walls.setBackground(new Color(12632256));
		button_ports.setBackground(new Color(12632256));
	} //buttonGolds_MouseClick() //

	void buttonRobotN_MouseClick(java.awt.event.MouseEvent event)
	{
		this.edit_state= GameEditor.ROBOTS;
		this.roboDirection = 0;
		button_robotN.setBackground(Color.yellow);
		button_robotW.setBackground(new Color(12632256));
		button_robotS.setBackground(new Color(12632256));
		button_robotE.setBackground(new Color(12632256));		
		button_walls.setBackground(new Color(12632256));
		button_golds.setBackground(new Color(12632256));
		button_ports.setBackground(new Color(12632256));
	} //buttonRobots_MouseClick() //
	
	void buttonRobotW_MouseClick(java.awt.event.MouseEvent event)
	{
		this.edit_state= GameEditor.ROBOTS;
		this.roboDirection = 1;
		button_robotW.setBackground(Color.yellow);
		button_robotN.setBackground(new Color(12632256));
		button_robotS.setBackground(new Color(12632256));
		button_robotE.setBackground(new Color(12632256));		
		button_walls.setBackground(new Color(12632256));
		button_golds.setBackground(new Color(12632256));
		button_ports.setBackground(new Color(12632256));
	}

	void buttonRobotS_MouseClick(java.awt.event.MouseEvent event)
	{
		this.edit_state= GameEditor.ROBOTS;
		this.roboDirection = 2;
		button_robotS.setBackground(Color.yellow);
		button_robotW.setBackground(new Color(12632256));
		button_robotN.setBackground(new Color(12632256));
		button_robotE.setBackground(new Color(12632256));		
		button_walls.setBackground(new Color(12632256));
		button_golds.setBackground(new Color(12632256));
		button_ports.setBackground(new Color(12632256));
	}

	void buttonRobotE_MouseClick(java.awt.event.MouseEvent event)
	{
		this.edit_state= GameEditor.ROBOTS;
		this.roboDirection = 3;
		button_robotN.setBackground(new Color(12632256));
		button_robotW.setBackground(new Color(12632256));
		button_robotS.setBackground(new Color(12632256));
		button_robotE.setBackground(Color.yellow);		
		button_walls.setBackground(new Color(12632256));
		button_golds.setBackground(new Color(12632256));
		button_ports.setBackground(new Color(12632256));
	}
	

	void buttonSave_MouseClick(java.awt.event.MouseEvent event)
	{
		// serialize the maze into a file!
		/* create a GameWorld */
		myGameWorld = new GameWorld(this.maze.walls);
		myGameWorld.height = this.maze.height;
		myGameWorld.width = this.maze.width;
		myGameWorld.teamHeadCount = Integer.parseInt(headCountField.getText());
		myGameWorld.myLocation = this.myLocation;
		myGameWorld.nuggets = maze.golds;
		myGameWorld.ports = maze.ports;

		/* now save this GameWorld we just created */
		FileDialog f = new FileDialog(this, "Save Maze",
		    FileDialog.SAVE);
		f.show();
		String filename = f.getFile();
		String directory = f.getDirectory();
		if (filename != null) {
		    try {
		        System.out.println("we are printing!");
		        FileOutputStream fos = new FileOutputStream(directory+filename);
		        GZIPOutputStream gzos = new GZIPOutputStream(fos);
		        ObjectOutputStream out = new ObjectOutputStream(gzos);
		        out.writeObject(this.myGameWorld);
		        out.flush();
		        out.close();
		    } // endtry
		    catch (IOException e) { System.out.println(e); }
		} // endif
	} // buttonSave_MouseClick() //

	void buttonLoad_MouseClick(java.awt.event.MouseEvent event)
	{
	    // read in the file and then set the parent_frame in this object
	    // accordingly. then refresh.
	    FileDialog f = new FileDialog(this, "Load Maze", FileDialog.LOAD);
	    f.show();
	    String filename = f.getFile();
	    String directory = f.getDirectory();
	    if (filename != null) {
	        try {
	            this.maze.wipeMaze(); 
	            this.maze = new MazeGraphics(this, 2, 2, this.maze_x, this.maze_y);
	            
	            FileInputStream fis = new FileInputStream(directory+filename);
	            GZIPInputStream gzis = new GZIPInputStream(fis);
	            ObjectInputStream in = new ObjectInputStream(gzis);
	            this.myGameWorld = (GameWorld)in.readObject();
	            in.close();
	            this.maze.parent_frame = this;
                this.moveButtons();
	            this.maze.refresh();
	            
                maze = 
	            new MazeGraphics(this, myGameWorld.width, myGameWorld.height,
	                             this.maze_x,this.maze_y);
	            
	            /* set the depth field... */
	            headCountField.setText(Integer.toString(myGameWorld.teamHeadCount));
	                                    
	            /* now add all the walls of this new maze */
	            for (int i=0; i< myGameWorld.width; i++) {
	                for (int j=0; j < myGameWorld.height; j++) {
	                    for (int k=0; k < 4; k++) {
	                        if (myGameWorld.maze[i][j][k] == 1)
	                            maze.addWall(i,j,k);
	                    }
	                }
	            }
	            /* now add the init robot position to this maze */
	            int[] myLocation = myGameWorld.myLocation;
                if (myLocation != null) maze.addRobot(myLocation[0],myLocation[1],myLocation[2],0);

	            /* now add the nuggets into maze just by putting gold */
	            int[] nuggetPos;
	            if (myGameWorld.nuggets != null) {
	                System.out.println("Number of golds: " + myGameWorld.nuggets.size());
	                for (int i=0; i < myGameWorld.nuggets.size(); i++) {
	                    nuggetPos = (int [])myGameWorld.nuggets.elementAt(i);
	                    if (nuggetPos.length == 2) {
    	                    maze.addGold(nuggetPos[0],nuggetPos[1]);
    	                } else {
    	                    maze.addGold(nuggetPos[0],nuggetPos[1],nuggetPos[2],nuggetPos[3]);
    	                }
	                }
	            }		
	            
	            /* and add the ports by putting ports */
	            int[] portPos;
	            if (myGameWorld.ports != null) {
	                System.out.println("Number of ports: "+myGameWorld.ports.size());
	                for (int i=0; i < myGameWorld.ports.size(); i++) {
	                    portPos = (int[]) myGameWorld.ports.elementAt(i);
	                    maze.addPort(portPos[0],portPos[1]);
	                }
	            }
	            
                this.moveButtons();
                this.maze.refresh();
	            
	            
	        } catch (Exception e) {
	            this.maze.refresh();
                System.out.println("Exception during load:");
                System.out.println(e);
            }
	    } //endif
   	}// buttonLoad_MouseClick() //


	class SymAction implements java.awt.event.ActionListener
	{
		public void actionPerformed(java.awt.event.ActionEvent event)
		{
		}
	}

	void buttonPorts_MouseClick(java.awt.event.MouseEvent event)
	{
		this.edit_state= GameEditor.PORTS;
		button_robotS.setBackground(new Color(12632256));
		button_robotW.setBackground(new Color(12632256));
		button_robotN.setBackground(new Color(12632256));
		button_robotE.setBackground(new Color(12632256));		
		button_walls.setBackground(new Color(12632256));
		button_golds.setBackground(new Color(12632256));
	    button_ports.setBackground(Color.yellow);
	}
}

⌨️ 快捷键说明

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