توضیحات :
سورس کد پیاده سازی برنامه بازی Dot Game – نقطه بازی
همراه با فایلهای .jar , .java
بخشهایی از سورس کد این پروژه :
coordinate of the center of the sprite
public Sprite() {
// Initialize all the fields
shape = new Polygon();
width = 0;
height = 0;
x = 0;
y = 0;
color = Color.BLACK;
}
public void render(Graphics g) {
// The render method is responsible for positioning the sprite at the proper location
g.setColor(color);
Polygon renderedShape = new Polygon();
for (int i = 0; i < shape.npoints; i++) {
int renderedx = shape.xpoints[i] + x + width / 2;
int renderedy = shape.ypoints[i] + y + height / 2;
renderedShape.addPoint(renderedx, renderedy);
}
g.fillPolygon(renderedShape);
}
public boolean containsPoint(int x, int y) {
// This returns true only if the point (x, y) is contained within the visible shape of the sprite
return shape.contains(x – this.x – width / 2, y – this.y – height / 2);
}
}
class ConnectionSprite extends Sprite {
public static final int HORZ_CONN = 1;
public static final int VERT_CONN = 2;
boolean connectionMade; // Tracks wether the ConnectionSprite has been clicked on
public ConnectionSprite() {
// Initialize all the fields
super();
connectionMade = false;
color = Color.WHITE;
}
public static ConnectionSprite createConnection(int type, int x, int y) {
ConnectionSprite conn = new ConnectionSprite();
if (type == ConnectionSprite.HORZ_CONN) {
conn.width = Dots.DOT_GAP;
conn.height = Dots.DOT_SIZE;
} else if (type == ConnectionSprite.VERT_CONN) {
conn.width = Dots.DOT_SIZE;
conn.height = Dots.DOT_GAP;
} else {
return null;
}
conn.x = x;
conn.y = y;
conn.shape.addPoint(-conn.width / 2, -conn.height / 2);
conn.shape.addPoint(-conn.width / 2, conn.height / 2);
conn.shape.addPoint(conn.width / 2, conn.height / 2);
conn.shape.addPoint(conn.width / 2, -conn.height / 2);
return conn;
}
}
class BoxSprite extends Sprite {
ConnectionSprite[] horizontalConnections; // The ConnectionSprites that are the top and bottom borders of the box
ConnectionSprite[] verticalConnections; // The ConnectionSprites that are the left and right borders of the box
int player; // Tracks the player that closed the box
public BoxSprite() {
super();
color = Color.WHITE; // Initially the box should be the same color as the background
horizontalConnections = new ConnectionSprite[2];
verticalConnections = new ConnectionSprite[2];
width = Dots.DOT_GAP;
height = Dots.DOT_GAP;
shape.addPoint(-width / 2, -height / 2);
shape.addPoint(-width / 2, height / 2);
shape.addPoint(width / 2, height / 2);
shape.addPoint(width / 2, -height / 2);
}
public boolean isBoxed() {
boolean boxed = true;
for (int i = 0; i < 2; i++) {
if (!horizontalConnections[i].connectionMade || !verticalConnections[i].connectionMade) {
boxed = false;
}
}
return boxed;
}
public static BoxSprite createBox(int x, int y, ConnectionSprite[] horizontalConnections, ConnectionSprite[] verticalConnections) {
BoxSprite box = new BoxSprite();
box.player = 0;
box.x = x;
box.y = y;
box.horizontalConnections = horizontalConnections;
box.verticalConnections = verticalConnections;
return box;
}
}
public class Dots extends JFrame implements MouseMotionListener, MouseListener {
public static final int DOT_NUMBER = 15; // The number of dots on each side of the square game board
public static final int DOT_GAP = 24; // The space between each dot
public static final int DOT_SIZE = 4; // The length of the sides of the square dot
public static final int PLAYER_ONE = 1;
public static final int PLAYER_TWO = 2;
public static final Color PLAYER_ONE_COLOR = Color.ORANGE; // The color of player1’s boxes
public static final Color PLAYER_TWO_COLOR = Color.GREEN; // The color of player2’s boxes
private ConnectionSprite[] horizontalConnections; // Array for all the ConnectionSprites that horizontally connect dots
private ConnectionSprite[] verticalConnections; // Array for all the ConnectionSprites that vertically connect dots
private BoxSprite[] boxes; // Array for all the BoxSprites
private Sprite[] dots; // Array for all the dots
private Dimension dim; // Window dimensions
private int clickx; // Holds the x coordinate of mouse click
private int clicky; // Holds the y coordinate of mouse click
private int mousex; // Holds the x coordinate of the mouse location
private int mousey; // Holds the y coordinate of the mouse location
private int centerx; // x coordinate of the center of the gameboard
private int centery; // y coordinate of the center of the gameborad
private int side; // Length of the sides of the square gameboard
private int space; // Length of 1 dot + 1 connection
private int activePlayer; // Holds the current player
public Dots() {
super(“Connect the Dots”);
setSize(500, 600);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addMouseListener(this);
addMouseMotionListener(this);
loadProperties();
loadDots();
startNewGame();
setVisible(true);
}
private void loadProperties() {
// Initialize fields
clickx = 0;
clicky = 0;
mousex = 0;
mousey = 0;
dim = getSize();
centerx = dim.width / 2;
centery = (dim.height – 100) / 2;
side = DOT_NUMBER * DOT_SIZE + (DOT_NUMBER – 1) * DOT_GAP; // There is one less connection than dot per side
space = DOT_SIZE + DOT_GAP;
}
private void loadConnections() {
horizontalConnections = new ConnectionSprite[(DOT_NUMBER – 1) * DOT_NUMBER];
verticalConnections = new ConnectionSprite[(DOT_NUMBER – 1) * DOT_NUMBER];
for (int i = 0; i < horizontalConnections.length; i++) {
int colsx = i % (DOT_NUMBER – 1);
int rowsx = i / (DOT_NUMBER – 1);
int horx = centerx – side / 2 + DOT_SIZE + colsx * space;
int hory = centery – side / 2 + rowsx * space;
horizontalConnections[i] = ConnectionSprite.createConnection(ConnectionSprite.HORZ_CONN, horx, hory);
int colsy = i % DOT_NUMBER;
تصاویری از محیط این برنامه :
=================================
|
دیدگاهتان را بنویسید