import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
public class tetris {
public static void main(String[] args){
new gameMain();
}
}
class gameMain extends JFrame implements KeyListener{
int key, move, score=0;
boolean up, down, left, right, rot, end=false;
int[][] stopBox=new int[10][30];//0null 1red 2blue 3green 4yellow
int[][] movingBox=new int[10][30];//0null 1red 2blue 3green 4yellow
int[][] tempBox=new int[10][30];//0null 1red 2blue 3green 4yellow
float sin=(float)Math.sin(Math.PI/2), cos=(float)Math.cos(Math.PI/2);
Graphics bufferGraphics;
Image offscreen;
int time=0;
int[][][] figure={
{{0,0,0, 0, 1, 1, 1, 1}, {0,0,0, 0, 0, 0, 0, 0}},
{{0,0,0, 0, 0, 2, 2, 2}, {0,0,0, 0, 0, 2, 0, 0}},
{{0,0,0, 0, 0, 3, 3,0}, {0,0,0, 0, 0, 3, 3,0}},
{{0,0,0, 0, 4, 4, 0,0}, {0,0,0, 0, 0, 4, 4,0}}
};
gameMain(){
init();
while(true){
process();
drawing();
if(end){
break;
}
sleep();
if(time<1000){
time++;
}
else{
time=0;
}
}
drawing();
}
private void sleep(){
try{
if(down==false){
Thread.sleep(20);
}
else{
Thread.sleep(5);
}
}catch(Exception e){
}
}
private void process(){
rotation();
falling();
sideMoving();
trans();
score();
end();
}
private void making(){
int sort=(int)(Math.random()*4);
for(int a=0; a<figure[sort].length;a++){
for(int b=0; b<figure[sort][a].length;b++){
movingBox[a+5][b]=figure[sort][a][b];
}
}
}
private void trans(){
if(downJudge()==true){
for(int a=0; a<10; a++){
for(int b=0; b<30; b++){
if(movingBox[a][b]!=0){
stopBox[a][b]=movingBox[a][b];
movingBox[a][b]=0;
}
}
}
making();
}
}
private void drawing(){
repaint();
}
public void init()
{
setTitle("TETRIS");
setSize(200, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setLocation(300, 100);
setVisible(true);
offscreen = createImage(200,600);
bufferGraphics = offscreen.getGraphics();
addKeyListener(this);
making();
}
public void paint(Graphics g)
{
if(bufferGraphics==null){
return;
}
bufferGraphics.clearRect(0,0,200,600);
bufferGraphics.setColor(Color.BLACK);
if(end==true){
bufferGraphics.drawString("game over", 100, 100);
return;
}
bufferGraphics.drawString("Score "+score, 100, 100);
for(int a=0; a<10;a++){
for(int b=0; b<30;b++){
if(stopBox[a][b]!=0){
if(stopBox[a][b]==1){
bufferGraphics.setColor(Color.RED);
}
else if(stopBox[a][b]==2){
bufferGraphics.setColor(Color.BLUE);
}
else if(stopBox[a][b]==3){
bufferGraphics.setColor(Color.GREEN);
}
else if(stopBox[a][b]==4){
bufferGraphics.setColor(Color.YELLOW);
}
bufferGraphics.fill3DRect(a*20, b*20, 20, 20, true);
}
else if(movingBox[a][b]!=0){
if(movingBox[a][b]==1){
bufferGraphics.setColor(Color.RED);
}
else if(movingBox[a][b]==2){
bufferGraphics.setColor(Color.BLUE);
}
else if(movingBox[a][b]==3){
bufferGraphics.setColor(Color.GREEN);
}
else if(movingBox[a][b]==4){
bufferGraphics.setColor(Color.YELLOW);
}
bufferGraphics.fill3DRect(a*20, b*20, 20, 20, true);
}
}
}
g.drawImage(offscreen,0,0,this);
}
public void update(Graphics g)
{
paint(g);
}
private void end(){
for(int a=0; a<5;a++){
for(int b=0; b<10;b++){
if(stopBox[b][a]!=0){
end=true;
}
}
}
}
private void score(){
if(time==0){
int count;
for(int a=0; a<30;a++){
count=0;
for(int b=0; b<10;b++){
if(stopBox[b][a]!=0){
count++;
}
}
if(count==10){
score++;
lineDelete(a);
}
}
}
}
private void lineDelete(int line){
for(int b=0; b<10;b++){
stopBox[b][line]=0;
}
for(int a=line-1; a>=0;a--){
for(int b=0; b<10;b++){
if(stopBox[b][a]!=0){
tempBox[b][a+1]=stopBox[b][a];
stopBox[b][a]=0;
}
}
}
for(int a=line; a>=0;a--){
for(int b=0; b<10;b++){
if(tempBox[b][a]!=0){
stopBox[b][a]=tempBox[b][a];
tempBox[b][a]=0;
}
}
}
}
private boolean out(int x, int y){
if(x>=0 && x<10 && y>=0 && y<30){
return false;
}
return true;
}
private void rotation(){
int centx=0, centy=0;
for(int a=0; a<10;a++){
for(int b=0; b<30;b++){
if(movingBox[a][b]!=0){
centx+=a;
centy+=b;
}
}
}
centx/=4;
centy/=4;
if(up==true){
rot=true;
for(int a=0; a<10;a++){
for(int b=0; b<30;b++){
if(movingBox[a][b]!=0){
if(out((int)(cos*(a-centx)-sin*(b-centy))+centx,(int)(sin*(a-centx)+cos*(b-centy))+centy)==false){
tempBox[(int)(cos*(a-centx)-sin*(b-centy))+centx][(int)(sin*(a-centx)+cos*(b-centy))+centy]=movingBox[a][b];
}
else{
rot=false;
}
}
}
}
if(rot!=false){
rot=rotJudge();
}
if(rot==false){
for(int a=0; a<10;a++){
for(int b=0; b<30;b++){
if(tempBox[a][b]!=0){
tempBox[a][b]=0;
}
}
}
}
else{
for(int a=0; a<10;a++){
for(int b=0; b<30;b++){
if(movingBox[a][b]!=0){
movingBox[a][b]=0;
}
}
}
for(int a=0; a<10;a++){
for(int b=0; b<30;b++){
if(tempBox[a][b]!=0){
movingBox[a][b]=tempBox[a][b];
tempBox[a][b]=0;
}
}
}
}
up=false;
}
}
private void falling(){
if(time==0){
for(int a=0; a<10;a++){
for(int b=0; b<30;b++){
if(movingBox[a][b]!=0){
tempBox[a][b+1]=movingBox[a][b];
movingBox[a][b]=0;
}
}
}
for(int a=0; a<10;a++){
for(int b=0; b<30;b++){
if(tempBox[a][b]!=0){
movingBox[a][b]=tempBox[a][b];
tempBox[a][b]=0;
}
}
}
}
}
private void sideMoving(){
if(right==true){
move=1;right=false;
}
else if(left==true){
move=-1;left=false;
}
if(sideJudge(move)==move){
for(int a=0; a<10;a++){
for(int b=0; b<30;b++){
if(movingBox[a][b]!=0){
tempBox[a+move][b]=movingBox[a][b];
movingBox[a][b]=0;
}
}
}
for(int a=0; a<10;a++){
for(int b=0; b<30;b++){
if(tempBox[a][b]!=0){
movingBox[a][b]=tempBox[a][b];
tempBox[a][b]=0;
}
}
}
move=0;
}
}
private boolean rotJudge(){
for(int a=0; a<10;a++){
for(int b=0; b<30;b++){
if(tempBox[a][b]!=0 && stopBox[a][b]!=0){
return false;
}
}
}
return true;
}
private int sideJudge(int mode){//-1 left 1right
for(int a=0; a<10;a++){
for(int b=0; b<30;b++){
if(movingBox[a][b]!=0 && out(a+mode, b)==true){
return 0;
}
else if(out(a+mode, b)==false && movingBox[a][b]!=0 && stopBox[a+mode][b]!=0){
return 0;
}
}
}
return mode;
}
private boolean downJudge(){
for(int a=0; a<10;a++){
for(int b=1; b<30;b++){
if(movingBox[a][b-1]!=0 && stopBox[a][b]!=0){
return true;
}
else if(b==29 && movingBox[a][b]!=0){
return true;
}
}
}
return false;
}
@Override
public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub
key=arg0.getKeyCode();
switch(key){
case 37:
left=true;
break;
case 38:
up=true;
break;
case 39:
right=true;
break;
case 40:
down=true;
break;
}
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
key=arg0.getKeyCode();
switch(key){
case 37:
left=false;
break;
case 38:
up=false;
break;
case 39:
right=false;
break;
case 40:
down=false;
break;
}
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
}
쓰레드도 처리도 안했내
코드라고는... 연봉 2000이상은 못받을 듯
윗님 정말 궁금해서 그런데 왜 그렇다는 거임?
단순 게임에 멀티쓰레드는 왜씀? 하나만으로도 잘 돌아가는데용
얌마 코드가 읽어보면 니가 만든 세계관이 보여야 되는데 테트리스는 안보이고 루프만 보이잖니. 쫌 읽으면 ... 아 이거 말되네... 다른 부분은 이러저러하게 할수있겠네... 이런게 되야 나중에 유지보수도 쉽고 그게 되야 돈도 잘 받는다
병신
아 그거는 주석 달아놓으면 되는뎅
얌마 진짜 잘된 코드는 주석이 없어도 모든게 보이는 코드야. 그게 안되면 넌 그저 이름없는 땔감
야이미친 / ㅄ 개솔하구 자빠졌네 주석 없어도 모든게 보이는 코드가 어딨냐..ㅋㅋㅋ 실력 알 수 있는 것 중 하나가 주석을 어떻게 써먹는가 인데ㅋㅋ
주석 어케 쓰는지만 봐도 그사람 코딩실력은 대충 보인다 근데 주석없이 보든게 보이는 코드?ㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋ
if문이 뭔지는 아냐?ㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋ
어서 먹다버린 찌꺼기 같은 생키가 와서 훈수질이냐 프로그래밍의 ㅍ자도 모르는게
야이미친// 시분할이잖아 밥팅아
혼자 힘으로 짤거라서 안볼겁니다 `ㅡ`
그건 그렇고 내코드는 존나 개밥으로 줘도 안먹겠노 ㅠㅠ
자바의 탈을 쓴 c언어. 아, c로도 저렇게는 안짬. 이런놈 회사에 받아주면 진짜 큰일난다.
ㅅㅂㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋ그러고보니 이새키 객체지향 좆까하고 만들었넼ㅋㅋㅋㅋㅋㅋ
아놔콘다 안드의노예/로직이 쉬워서 클래스 안 나누고 그냥 짠거임요.
괜찮구만 뭘.. 리팩토링 공부해봐.. 그럼 너도 좋고 남도 좋고... 나중에도 좋고.. 보수도 좋고
근데 글쓴이는 직장인임?