Skip to content

Commit da4b27f

Browse files
authored
Add files via upload
1 parent 28acea6 commit da4b27f

File tree

6 files changed

+636
-0
lines changed

6 files changed

+636
-0
lines changed

src/Games/Tetris/BlockGrid.java

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package Games.Tetris;
2+
3+
import java.awt.*;
4+
5+
public class BlockGrid
6+
{
7+
public static final int BLOCK_SIDE = 25;
8+
public static final Color BACKGROUND_COLOR = Color.WHITE;
9+
10+
private Color[][] blocks;
11+
private Color currentColor = BACKGROUND_COLOR;
12+
13+
public BlockGrid(int width, int height)
14+
{
15+
blocks = new Color[width][height];
16+
}
17+
18+
public void drawSquare(int x, int y)
19+
{
20+
try
21+
{
22+
blocks[x][y] = currentColor;
23+
}
24+
catch(Exception ex)
25+
{
26+
}
27+
}
28+
29+
public void drawSquare(Point p)
30+
{
31+
drawSquare(p.x, p.y);
32+
}
33+
34+
public void setColor(Color color)
35+
{
36+
currentColor = color;
37+
}
38+
39+
public int getWidth()
40+
{
41+
return blocks.length;
42+
}
43+
44+
public int getHeight()
45+
{
46+
return blocks[0].length;
47+
}
48+
49+
public int getGraphicsWidth()
50+
{
51+
return getWidth()*BLOCK_SIDE;
52+
}
53+
54+
public int getGraphicsHeight()
55+
{
56+
return getHeight()*BLOCK_SIDE;
57+
}
58+
59+
public void clear()
60+
{
61+
blocks = new Color[blocks.length][blocks[0].length];
62+
}
63+
64+
public void draw(Graphics g)
65+
{
66+
for(int i = 0; i < blocks.length; i++)
67+
{
68+
for(int j = 0; j < blocks[0].length; j++)
69+
{
70+
if(blocks[i][j] != null)
71+
g.setColor(blocks[i][j]);
72+
else
73+
g.setColor(BACKGROUND_COLOR);
74+
g.fillRect(i*BLOCK_SIDE, j*BLOCK_SIDE, BLOCK_SIDE-1, BLOCK_SIDE-1);
75+
g.setColor(Color.BLACK);
76+
g.drawRect(i*BLOCK_SIDE, j*BLOCK_SIDE, BLOCK_SIDE-1, BLOCK_SIDE-1);
77+
}
78+
}
79+
}
80+
81+
public String toString()
82+
{
83+
String ret = "";
84+
for(int j = 0; j < blocks[0].length; j++)
85+
{
86+
if(j != 0)
87+
ret+="\n";
88+
for(int i = 0; i < blocks.length; i++)
89+
{
90+
if(blocks[i][j] != null)
91+
ret+="#";
92+
else
93+
ret+="-";
94+
}
95+
}
96+
return ret;
97+
}
98+
99+
public static void main(String[] args)
100+
{
101+
BlockGrid grid = new BlockGrid(10,20);
102+
TetrisBlock.RIGHT_L.setLocation(0,2).draw(grid);
103+
// TetrisBlock.RIGHT_L.setLocation(1,15).rotateLeft().draw(grid);
104+
System.out.println(grid);
105+
}
106+
}

src/Games/Tetris/PlayTetris.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package Games.Tetris;
2+
3+
import javax.swing.*;
4+
import java.awt.*;
5+
6+
public class PlayTetris extends JFrame
7+
{
8+
public PlayTetris()
9+
{
10+
setTitle("Tetris");
11+
setIconImage(Toolkit.getDefaultToolkit().getImage(Games.Tetris.PlayTetris.class.getResource("/Games/Tetris/icon/TetrisIcon.png")));
12+
getContentPane().setLayout(new FlowLayout());
13+
add(new TetrisComponent(10,20));
14+
pack();
15+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
16+
setVisible(true);
17+
}
18+
19+
public static void main(String[] args)
20+
{
21+
new PlayTetris();
22+
}
23+
}

src/Games/Tetris/TetrisBlock.java

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
package Games.Tetris;
2+
3+
import java.awt.Color;
4+
import java.util.Vector;
5+
6+
public class TetrisBlock
7+
{
8+
public static final TetrisBlock LONG = new TetrisBlock(
9+
new boolean[][]{{true, true, true, true}}, Color.RED);
10+
11+
public static final TetrisBlock LEFT_L = new TetrisBlock(new boolean[][]{
12+
{true, true, true}, {true, false, false}}, Color.GREEN);
13+
14+
public static final TetrisBlock TEE = new TetrisBlock(
15+
new boolean[][]{{true, true, true}, {false, true, false}}, Color.BLUE);
16+
17+
public static final TetrisBlock RIGHT_L = new TetrisBlock(new boolean[][]{
18+
{true, true, true}, {false, false, true}}, Color.CYAN);
19+
20+
public static final TetrisBlock SQUARE = new TetrisBlock(new boolean[][]{
21+
{true, true}, {true, true}}, Color.BLACK);
22+
23+
private boolean[][] blocks;
24+
private Color color;
25+
26+
// location of the bottum left hand corner.
27+
private int x, y;
28+
29+
private TetrisBlock(boolean[][] blocks, Color color)
30+
{
31+
this.blocks = blocks;
32+
this.color = color;
33+
}
34+
35+
private TetrisBlock(boolean[][] blocks, Color color, int x, int y)
36+
{
37+
this(blocks, color);
38+
this.x = x;
39+
this.y = y;
40+
}
41+
42+
public static TetrisBlock getRandomBlock()
43+
{
44+
switch((int)(Math.random()*5))
45+
{
46+
case 0: return LONG;
47+
case 1: return LEFT_L;
48+
case 2: return TEE;
49+
case 3: return RIGHT_L;
50+
case 4: return SQUARE;
51+
}
52+
return null;
53+
}
54+
55+
public int getX()
56+
{
57+
return x;
58+
}
59+
60+
public int getY()
61+
{
62+
return y;
63+
}
64+
65+
public int getWidth()
66+
{
67+
return blocks.length;
68+
}
69+
70+
public int getHeight()
71+
{
72+
return blocks[0].length;
73+
}
74+
75+
public TetrisBlock setLocation(int x, int y)
76+
{
77+
return new TetrisBlock(blocks, color, x, y);
78+
}
79+
80+
public TetrisBlock moveDown()
81+
{
82+
return new TetrisBlock(blocks, color, x, y+1);
83+
}
84+
85+
public TetrisBlock moveLeft()
86+
{
87+
return new TetrisBlock(blocks, color, x-1, y);
88+
}
89+
90+
public TetrisBlock moveRight()
91+
{
92+
return new TetrisBlock(blocks, color, x+1, y);
93+
}
94+
95+
public boolean overlaps(int x, int y)
96+
{
97+
for(int i = 0; i < blocks.length; i++)
98+
{
99+
for(int j = 0; j < blocks[0].length; j++)
100+
{
101+
if(blocks[i][j])
102+
{
103+
if((this.x+i == x) && (this.y-blocks[0].length+1+j == y))
104+
return true;
105+
}
106+
}
107+
}
108+
return false;
109+
}
110+
111+
public boolean overlaps(TetrisBlock other)
112+
{
113+
for(int i = 0; i < blocks.length; i++)
114+
{
115+
for(int j = 0; j < blocks[0].length; j++)
116+
{
117+
if(blocks[i][j])
118+
{
119+
if(other.overlaps(x+i, y-blocks[0].length+1+j))
120+
return true;
121+
}
122+
}
123+
}
124+
return false;
125+
}
126+
127+
public TetrisBlock turnRight()
128+
{
129+
boolean[][] newBlocks = new boolean[blocks[0].length][blocks.length];
130+
for(int i = 0; i < blocks.length; i++)
131+
{
132+
for(int j = 0; j < blocks[0].length; j++)
133+
{
134+
newBlocks[blocks[0].length-1-j][i] = blocks[i][j];
135+
}
136+
}
137+
return new TetrisBlock(newBlocks, color, x, y);
138+
}
139+
140+
public TetrisBlock turnLeft()
141+
{
142+
boolean[][] newBlocks = new boolean[blocks[0].length][blocks.length];
143+
for(int i = 0; i < blocks.length; i++)
144+
{
145+
for(int j = 0; j < blocks[0].length; j++)
146+
{
147+
newBlocks[j][blocks.length-1-i] = blocks[i][j];
148+
}
149+
}
150+
return new TetrisBlock(newBlocks, color, x, y);
151+
}
152+
153+
public String toString()
154+
{
155+
String ret = "";
156+
for(int j = 0; j < blocks[0].length; j++)
157+
{
158+
ret+=(j!=0)?"\n":"";
159+
for(int i = 0; i < blocks.length; i++)
160+
{
161+
if(blocks[i][j])
162+
ret+="#";
163+
else
164+
ret+=" ";
165+
}
166+
}
167+
return ret;
168+
}
169+
170+
public static TetrisBlock getSingleBlock(int x, int y, Color color)
171+
{
172+
return new TetrisBlock(new boolean[][]{{true}}, color, x, y);
173+
}
174+
175+
public Vector<TetrisBlock> getSingleBlocks()
176+
{
177+
Vector<TetrisBlock> parts = new Vector();
178+
for(int i = 0; i < blocks.length; i++)
179+
{
180+
for(int j = 0; j < blocks[0].length; j++)
181+
{
182+
if(blocks[i][j])
183+
parts.add(getSingleBlock(x+i,y-blocks[0].length+1+j, color));
184+
}
185+
}
186+
return parts;
187+
}
188+
189+
public void draw(BlockGrid grid)
190+
{
191+
grid.setColor(color);
192+
for(int i = 0; i < blocks.length; i++)
193+
{
194+
for(int j = 0; j < blocks[0].length; j++)
195+
{
196+
if(blocks[i][j])
197+
grid.drawSquare(x+i,y-blocks[0].length+1+j);
198+
}
199+
}
200+
}
201+
202+
public static void main(String[] args)
203+
{
204+
TetrisBlock a = TetrisBlock.SQUARE.setLocation(2,2);
205+
TetrisBlock b = TetrisBlock.SQUARE.setLocation(5,2);
206+
System.out.println(a.overlaps(b));
207+
}
208+
}

0 commit comments

Comments
 (0)