-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTetris.java
More file actions
167 lines (144 loc) · 4.03 KB
/
Tetris.java
File metadata and controls
167 lines (144 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import java.lang.*;
import java.awt.*;
import java.awt.event.*;
/**
* Tetris oyununun applet versiyonu
* Sistemden iki parametre alýnýyor: X ve Y
* Bu parametreler oyunun alanýný belirliyor
* YAZAR: Hüseyin Yaðlý huseyinyagli@yahoo.com
*/
public class Tetris extends java.applet.Applet implements Runnable {
/** Oyun durumu */
TetrisOyun Oyun;
/** Oyunu kaydetmeye yarayan Memento sýnýfý */
Memento Kayit;
/** Oyunu çalýþtýran görev */
private volatile Thread OyunCalistir;
/** Oyun alan genisligi */
int xs,ys;
int x,y;
/** Puanýn yazýlacaðý kutu */
Yazi_Decorator Skor;
/** Oyunun þu anki durumunu gösteren kutu */
Yazi_Decorator Durum;
/** Oyunun durum göstergeleri */
boolean OyunBitti;
boolean Mola;
/**
* java.applet.Applet sýnýfýndaki init fonksiyonunun
* yerini alýyor. Deðiþkenler ilklenir, resize() ve getfocus() çaðrýlýr
* ayrýca oyun tarafýndan kullanýlacak tuþlar için görevler atanýr
*/
public void init()
{
System.out.println("Applet: "+toString());
try {
x=Integer.parseInt(getParameter("X"));
} catch (NullPointerException e) { x=10; }
catch (NumberFormatException e) { x=10; }
try {
y=Integer.parseInt(getParameter("Y"));
} catch (NullPointerException e) { y=20; }
catch (NumberFormatException e) { y=20; }
Oyun = new TetrisOyun(x,y);
Skor = new Yazi_Decorator(new Kutu(80,16),"Skor: ");
Durum = new Yazi_Decorator(new Kutu(95,16),"Durum: ");
Kayit = null;
xs=x*12+16; // Oyun alaný
ys=y*12+4 // Oyun alaný
+60; // Skor ve Durum
resize(xs,ys);
requestFocus();
/** Basýlacak tuþlar için görev atama */
addKeyListener(new KeyListener(){
public void keyPressed(KeyEvent e){
int Tus = e.getKeyCode();
switch (Tus)
{
/** Baþtan baþlama */
case KeyEvent.VK_R:
OyunBitti = false;
Oyun = new TetrisOyun(x,y);
if(Kayit != null)
Kayit.HedefDegistir(Oyun);
if(!Mola && !OyunBitti) stop();
start(); repaint(); break;
/** Oyunu þu anki haliyle kaydetme */
case KeyEvent.VK_K:
Kayit = new Memento(Oyun);
break;
/** Oyunu en son kaydedilen haliyle yükleme */
case KeyEvent.VK_Y:
if(Kayit != null) Kayit.Yukle();
repaint(); break;
/** Oyunu duraklatma */
case KeyEvent.VK_P:
if(Mola && !OyunBitti){ start(); Mola = false; }
else{ stop(); Mola = true; }
repaint(); break;
}
if(!OyunBitti && !Mola)
{
switch (Tus)
{
case KeyEvent.VK_LEFT : Oyun.SolaGit(); repaint(); break;
case KeyEvent.VK_RIGHT: Oyun.SagaGit(); repaint(); break;
case KeyEvent.VK_UP : Oyun.SagaDon(); repaint(); break;
case KeyEvent.VK_DOWN : OyunBitti = Oyun.Adim(true); repaint(); break;
case KeyEvent.VK_Z : Oyun.SolaDon(); repaint(); break;
case KeyEvent.VK_X : Oyun.SagaDon(); repaint(); break;
case KeyEvent.VK_SPACE: Oyun.Dus(); repaint(); break;
}
}
}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
});
}
// Oyuna baslama
public void start()
{
if(OyunCalistir == null)
{
OyunCalistir = new Thread(this);
OyunCalistir.start();
}
}
// Oyunu durdurma
public void stop()
{
if(OyunCalistir != null && OyunCalistir.isAlive())
OyunCalistir.interrupt();
OyunCalistir = null;
}
public void destroy()
{
}
public void run()
{
OyunBitti = false;
Mola = false;
while(!OyunBitti && !Mola)
{
OyunBitti = Oyun.Adim(false);
repaint();
try {Thread.sleep(500);} catch (InterruptedException e){ return; }
}
}
public void paint(Graphics g)
{
Oyun.Ciz(g,2,2);
Skor.YaziBelirle((new Integer(Oyun.Skor())).toString());
Skor.Ciz(g,5,ys-40);
if(OyunBitti)
Durum.YaziBelirle("Bitti");
else if(Mola)
Durum.YaziBelirle("Mola");
else
Durum.YaziBelirle("Oyunda");
Durum.Ciz(g,90,ys-40);
g.drawString("TETRIS",xs,20);
g.drawString("Hüseyin",xs,50);
g.drawString("Yaðlý",xs,65);
}
}