User Tools

Site Tools


packperson

This is an old revision of the document!


Chase's Pack Person Game

Move Monster

MazeMaker

Frogger

Cell.java
import java.awt.*;
 
public class Cell
{
    public static final int NORTH=0, SOUTH=1, EAST=2, WEST=3;
 
    public static final int GAP = 5;
 
    private boolean[] canExit;// NSEW true if an exit
    private boolean visited;
    private int x, y, size;
 
    public Cell (int x, int y , int size)
    {
        this.x = x;
        this.y = y;
        this.size = size;
        canExit = new boolean[4];
        visited = false;
        setAllExits(true);    
    }
    public boolean canExit(int dir){
        return canExit(dir);
    }
    public void noExit(int dir) {
        canExit[dir] = false;
    }
    public void exit(int dir) {
        canExit[dir] = true;
    }
    public void setAllExits(boolean b){
        for(int i = 0; i < canExit.length; i++)
             canExit[i] = b;
    }
    public void draw(Graphics g)
    {
        g.setColor(Color.BLACK);
        g.fillRect(x, y, size, size);
        if( !visited){
            g.setColor(Color.YELLOW);
            g.fillRect(x+size/2, y+size/2, GAP,GAP);
        }
        g.setColor(Color.MAGENTA.darker());
        if(!canExit[NORTH]){
            g.fillRect(x, y, size, GAP);
        }
        if(!canExit[SOUTH]){
            g.fillRect(x, y+size-GAP, size, GAP);
        }
        if(!canExit[EAST]){
            g.fillRect(x+size-GAP, y, GAP, size);
        }
        if(!canExit[WEST]){
            g.fillRect(x, y, GAP, size);
        }
    }
}
Level.java
import java.awt.*;
 
 
public class Level
{
    private Cell[][] grid;
    private int rows, cols, top, left, size;
    public Level(int rows, int cols, int top, int left, int size)
    {
        this.rows = rows;
        this.cols = cols;
        this.top = top;
        this.left = left;
        grid = new Cell[rows][cols];
        for(int r=0; r<rows; r++)
           for(int c = 0; c < cols; c++){
               grid[r][c] = new Cell(left+c*size, top+r*size ,size);
           }
        for(int c =0; c < cols; c++){
             grid[0][c].noExit(Cell.NORTH);
             grid[rows-1][c].noExit(Cell.SOUTH);
            }
        for(int r=0; r < rows; r++){
            grid[r][0].noExit(Cell.WEST);
            grid[r][cols-1].noExit(Cell.EAST);
        }
    }
    public void noExit(int r, int c, int dir)
    {
        grid[r][c].noExit(dir);
    }
    public void draw(Graphics g)
    {
        for(Cell[] row:grid)
            for(Cell c : row)
                   c.draw(g);
    }
}
LevelTester.java
import javax.swing.*;
import java.awt.*;
 
 
public class LevelTester extends JPanel
{
    public static int WIDTH=800;
    public static int HEIGHT=600;
    public static final int NORTH=0, SOUTH=1, EAST=2, WEST=3;
 
    private Level lev1;
    public LevelTester(){
        lev1 = new Level(3, 7,  100,  50,  50);
        lev1.noExit(0,1,SOUTH);
        lev1.noExit(0,2,SOUTH);
        lev1.noExit(0,5,SOUTH);
        lev1.noExit(1,0,EAST);
        lev1.noExit(1,1,WEST);
        lev1.noExit(1,1,NORTH);
        lev1.noExit(1,2,NORTH);
        lev1.noExit(1,2,SOUTH);
        lev1.noExit(1,3,EAST);
        lev1.noExit(1,3,SOUTH);
        lev1.noExit(1,4,WEST);
        lev1.noExit(1,4,SOUTH);
        lev1.noExit(1,5,NORTH);
        lev1.noExit(1,5,EAST);
        lev1.noExit(1,6,WEST);
        lev1.noExit(2,2,NORTH);
        lev1.noExit(2,3,NORTH);
        lev1.noExit(2,4,NORTH);
    }
    public static void main(String[] args) {
        LevelTester app= new LevelTester();
        JFrame window = new JFrame("Level Tester");
        window.setSize(WIDTH, HEIGHT);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.getContentPane().add(app);
// remember to add Key Listeners for the game interface
        window.setVisible(true);
 
    }
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, getWidth(),getHeight());
        lev1.draw(g);
    }
}
packperson.1683577346.txt.gz · Last modified: 2023/05/08 16:22 by frchris

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki