import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.applet.*;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JTextField;
@SuppressWarnings("serial")
public class Tetris extends JFrame{
GameState g = new GameState();
Screen s = new Screen();
private static int resX = 1024;
private static int resY = 768;
public static void main(String[] args)
{
DisplayMode dm = new DisplayMode(resX,resY,16,DisplayMode.REFRESH_RATE_UNKNOWN);
Tetris g = new Tetris();
g.run(dm);
}
public void run(DisplayMode dm)
{
try{
s.setFullScreen(dm, this);
}catch(Exception e){}
}
public void paint(Graphics g)
{
Movement m = new Movement();
g.setColor(Color.WHITE);
g.fillRect(0, 0, resX, resY);
m.game(g);
}
}
:rolleyes:
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.applet.*;
import java.util.Random;
public class GameState extends Applet implements KeyListener
{
int xMovement;
int yMovement;
Color pieceColor = Color.BLACK;
Random rand = new Random();
public GameState()
{
[COLOR="#FF0000"]addKeyListener(this);[/COLOR]
xMovement = 0;
yMovement = 0;
}
public boolean grid[][] = new boolean[20][10];
public boolean gamestate = true;
GamePiece p;
public void setGrid(){
for(int r = 0;r < grid.length; ++r){
for(int c = 0; c < grid[r].length; ++c){
grid[r][c] = false;
}
}
}
public void drawPiece(Graphics g, Cordinate[] a ,Color c)
{
for(int i = 0;i < 4;++i)
{
g.setColor(c);
g.fill3DRect((a[i].x*20)+300, (a[i].y*20), 20, 20,true);
}
}
public void erasePiece(Graphics g, Cordinate[] a)
{
for(int i = 0;i < 4;++i)
{
g.setColor(Color.WHITE);
g.fillRect((a[i].x*20)+ 300, (a[i].y*20), 20, 20);
}
}
[COLOR="#FF0000"]public void keyPressed(KeyEvent e)
{
System.out.print("pressed!!!");
int keyCode = e.getKeyCode();
if(keyCode == KeyEvent.VK_A)
{
xMovement += 1;
System.out.print("Worked!!!!");
e.consume();
}
else if(keyCode == KeyEvent.VK_D)
{
xMovement -= 1;
e.consume();
}
else if(keyCode == KeyEvent.VK_S)
{
yMovement += 1;
e.consume();
}
else if(keyCode == KeyEvent.VK_SPACE)
{
yMovement += 1;
e.consume();
}
else if(keyCode == KeyEvent.VK_ESCAPE)
{
System.out.print("DSFASDDFSDS!!!");
gamestate = false;
e.consume();
}
}
public void keyReleased(KeyEvent e)
{
System.out.print("pressed!!!");
e.consume();
}
public void keyTyped(KeyEvent e)
{
System.out.print("pressed!!!");
e.consume();
}[/COLOR]
}
:rolleyes:
import java.awt.event.KeyEvent;
import java.awt.*;
import java.awt.event.KeyListener;
import java.util.Random;
public class Movement extends Thread{
GameState s = new GameState();
GamePiece p;
private Cordinate beenSet[] = new Cordinate[4];
public void game(Graphics g)
{
System.out.println("Test");
while(s.gamestate)
{
switch ((new Random()).nextInt(8)+1)
{
case 1 : p = new Piece1(); break;
case 2 : p = new Piece2(); break;
case 3 : p = new Piece3(); break;
case 4 : p = new Piece4(); break;
case 5 : p = new Piece5(); break;
case 6 : p = new Piece6(); break;
case 7 : p = new Piece7();
}
while(p.dropping)
{
refresh();
s.drawPiece(g, p.cord, p.pieceColor);
try{
Thread.sleep(200);
}catch(Exception e){System.out.println("Thread Error");}
s.yMovement += 1;
if(p.dropping){
s.erasePiece(g, p.cord);
}
}
}
}
public GamePiece getPiece()
{
return p;
}
public void refresh()
{
int k = 0;
GamePiece p = getPiece();
for(int i = 0;i < 4; ++i)
{
p.cord[i].x += s.xMovement;
p.cord[i].y += s.yMovement;
}
if((p.cord[0].y >= 31) || (p.cord[1].y >= 31) || (p.cord[2].y >= 31) || (p.cord[3].y >= 31))
{
p.dropping = false;
}
s.yMovement = 0;
s.xMovement = 0;
for(int r = 0;r < s.grid.length; ++r)
{
for(int c = 0; c < s.grid[r].length; ++c)
{
for(int i = 0;i < 4; ++i)
{
if((p.cord[i].x==r)&&(p.cord[i].y==c))
{
if((s.grid[r][c] == true)&&(r!=beenSet[0].x)&&(c!= beenSet[0].y)&&(r!=beenSet[1].x)&&(c!= beenSet[1].y)&&(r!=beenSet[2].x)&&(c!= beenSet[2].y)&&(r!=beenSet[3].x)&&(c!= beenSet[3].y)){
p.dropping = false;
}
else if (s.grid[r][c] != true)
{
//s.grid[r][c] = true;
//beenSet[k].x = r;
//beenSet[k].y = c;
k++;
if (k == 4)
k = 0;
}
else{
}
}
}
}
}
}
}
:rolleyes:
import java.awt.*;
import javax.swing.JFrame;
public class Screen{
private GraphicsDevice vc;
public Screen()
{
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
vc = env.getDefaultScreenDevice();
}
public void setFullScreen(DisplayMode dm, JFrame window)
{
window.setUndecorated(true);
window.setResizable(false);
vc.setFullScreenWindow(window);
if(dm != null && vc.isDisplayChangeSupported())
{
try{
vc.setDisplayMode(dm);
}catch(Exception e){}
}
}
public Window getFullScreenWindow()
{
return vc.getFullScreenWindow();
}
public void restoreScreen()
{
Window w = vc.getFullScreenWindow();
if(w != null)
w.dispose();
vc.setFullScreenWindow(null);
}
}
:rolleyes:
import java.awt.Color;
public abstract class GamePiece
{
protected Cordinate cord[];
Color pieceColor;
public boolean dropping = true;
public GamePiece()
{
this.cord = new Cordinate[4];
for(int i = 0; i < 4; ++i)
{
this.cord[i] = new Cordinate();
}
}
}
:rolleyes:
public class Cordinate {
int x;
int y;
public void set(int x, int y)
{
this.x = x;
this.y = y;
}
public void swap()
{
int temp = y;
this.y = this.x;
this.x = temp;
}
}
:rolleyes:
import java.awt.Color;
public class Piece1 extends GamePiece
{
public Piece1()
{
cord[0].set(0, 0);
cord[1].set(0, 1);
cord[2].set(0, 2);
cord[3].set(0, 3);
pieceColor = Color.RED;
}
public void rotate()
{
for (int i = 0; i < 4; ++i)
{
this.cord[i].swap();
}
}
}
:rolleyes:
import java.awt.Color;
public class Piece2 extends GamePiece
{
public Piece2()
{
cord[0].set(0, 0);
cord[1].set(0, 1);
cord[2].set(1, 0);
cord[3].set(1, 1);
pieceColor = Color.BLUE;
}
public void rotate()
{
}
}
:rolleyes:
import java.awt.Color;
public class Piece3 extends GamePiece
{
public Piece3()
{
cord[0].set(0, 0);
cord[1].set(0, 1);
cord[2].set(1, 1);
cord[3].set(2, 1);
pieceColor = Color.YELLOW;
}
public void rotate()
{
for (int i = 0; i < 4; ++i)
{
this.cord[i].swap();
if (cord[i].y == 0)
{
cord[i].y = 1;
}
else
{
cord[i].y = 0;
}
}
}
}
:rolleyes:
import java.awt.Color;
public class Piece4 extends GamePiece
{
private int state;
public Piece4()
{
cord[0].set(0, 1);
cord[1].set(1, 1);
cord[2].set(1, 0);
cord[3].set(2, 0);
pieceColor = Color.GREEN;
state = 0;
}
public void rotate()
{
++state;
if (state == 0) // set to state 0
{
cord[0].set(0, 1);
cord[3].set(2, 0);
}
if (state == 1) //set to state 1
{
cord[0].set(0, 0);
cord[3].set(1, 2);
}
}
}
:rolleyes:
import java.awt.Color;
public class Piece5 extends GamePiece
{
private int state;
public Piece5()
{
cord[0].set(0, 0);
cord[1].set(0, 1);
cord[2].set(0, 2);
cord[3].set(1, 2);
pieceColor = Color.ORANGE;
state = 0;
}
public void rotate()
{
++state;
switch (state)
{
case 0 : for (int i = 0; i < 4; ++i)
{
cord[0].set(0, 0);
cord[1].set(0, 1);
cord[2].set(0, 2);
cord[3].set(1, 2);
}
case 1 : for (int i = 0; i < 4; ++i)
{
cord[0].set(0, 1);
cord[1].set(1, 1);
cord[2].set(2, 1);
cord[3].set(2, 0);
}
case 2 : for (int i = 0; i < 4; ++i)
{
cord[0].set(1, 2);
cord[1].set(1, 1);
cord[2].set(1, 0);
cord[3].set(0, 0);
}
case 3 : for (int i = 0; i < 4; ++i)
{
cord[0].set(2, 0);
cord[1].set(1, 0);
cord[2].set(0, 0);
cord[3].set(0, 1);
}
}
}
}
:rolleyes:
import java.awt.Color;
public class Piece6 extends GamePiece
{
private int state;
public Piece6()
{
cord[0].set(1, 0);
cord[1].set(1, 1);
cord[2].set(1, 2);
cord[3].set(0, 2);
pieceColor = Color.CYAN;
state = 0;
}
public void rotate()
{
++state;
switch (state)
{
case 0 : for (int i = 0; i < 4; ++i)
{
cord[0].set(1, 0);
cord[1].set(1, 1);
cord[2].set(1, 2);
cord[3].set(0, 2);
}
case 1 : for (int i = 0; i < 4; ++i)
{
cord[0].set(0, 0);
cord[1].set(1, 0);
cord[2].set(2, 0);
cord[3].set(2, 1);
}
case 2 : for (int i = 0; i < 4; ++i)
{
cord[0].set(0, 2);
cord[1].set(0, 1);
cord[2].set(0, 0);
cord[3].set(1, 0);
}
case 3 : for (int i = 0; i < 4; ++i)
{
cord[0].set(2, 1);
cord[1].set(1, 1);
cord[2].set(0, 1);
cord[3].set(0, 0);
}
}
}
}
import java.awt.Color;
public class Piece7 extends GamePiece
{
private int state;
public Piece7()
{
cord[0].set(0, 1);
cord[1].set(1, 0);
cord[2].set(1, 1);
cord[3].set(2, 1);
pieceColor = Color.PINK;
state = 0;
}
public void rotate()
{
++state;
switch (state)
{
case 0 : for (int i = 0; i < 4; ++i)
{
cord[0].set(0, 1);
cord[1].set(1, 0);
cord[2].set(1, 1);
cord[3].set(2, 1);
}
case 1 : for (int i = 0; i < 4; ++i)
{
cord[0].set(1, 2);
cord[1].set(0, 1);
cord[2].set(1, 1);
cord[3].set(1, 0);
}
case 2 : for (int i = 0; i < 4; ++i)
{
cord[0].set(2, 0);
cord[1].set(1, 1);
cord[2].set(1, 0);
cord[3].set(0, 0);
}
case 3 : for (int i = 0; i < 4; ++i)
{
cord[0].set(0, 0);
cord[1].set(1, 1);
cord[2].set(0, 1);
cord[3].set(0, 2);
}
}
}
}