Advertisement
Guest User

Untitled

a guest
May 24th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.21 KB | None | 0 0
  1. /*
  2. Sam Mulford
  3. Intro to CS Period 3
  4. May 2018
  5. Description: Tetris
  6. */
  7. package programs;
  8. import java.awt.*;
  9. import java.awt.event.KeyEvent;
  10. import java.awt.event.KeyListener;
  11. import javax.swing.ImageIcon;
  12. import java.util.*;
  13.  
  14. public class Tetris extends JoeApplet implements KeyListener
  15. {
  16. int currentLevel =0;
  17. Font good = new Font("Monospaced", Font.PLAIN, 30);
  18. //String path = "H:\\workspace\\SaveFiles\\";
  19. //Image man = new ImageIcon(path + "man.png").getImage();
  20. int score=0, num;
  21. int key;
  22. int dummy =0, buddy;
  23. int lineX=0, lineY=0, lx=0, ly=0, tx=0, ty=0, jx=20, jy=0, boxX=0, boxY=0, zx=20, zy=20, sx=20, sy=20;
  24. int man, man1;
  25. Random gen = new Random();
  26. SolidObject floor = new SolidObject();
  27. int[] test= new int[100];
  28.  
  29. public void init()
  30. {
  31. addKeyListener(this);
  32. }
  33. public void menuScreen(Graphics art)
  34. {
  35. setSize(1260, 800);
  36. setBackground(Color.BLACK);
  37. //art.drawImage(man, 600, 400, this);
  38. art.setColor(Color.WHITE);
  39. art.setFont(good);
  40. art.drawString("Tetris! Press Space to play!", 350, 300);
  41. }
  42. public void paint(Graphics art)
  43. {
  44. switch(currentLevel)
  45. {
  46. case 0:
  47. menuScreen(art);
  48. break;
  49. case 1:
  50. game(art);
  51. break;
  52. case 2:
  53. gameOver(art);
  54. break;
  55. default:
  56. System.out.println("ERROR");
  57. }
  58. }
  59. public void game(Graphics art)
  60. {
  61. setSize(300, 400);
  62. setBackground(Color.BLACK);
  63. art.setColor(Color.YELLOW);
  64. art.drawRect(0, 375, 299, 20);
  65. floor.makeSolidObject(0, 375, 299, 20);
  66.  
  67. line(art);
  68. num = 1 + gen.nextInt(7);
  69.  
  70.  
  71. }
  72. public void gameOver(Graphics art)
  73. {
  74. setSize(1260, 800);
  75. setBackground(Color.BLACK);
  76. art.setColor(Color.WHITE);
  77. art.drawString("Game Over!", 350, 300);
  78.  
  79.  
  80. }
  81. public void line(Graphics art)
  82. {
  83.  
  84. art.setColor(Color.CYAN);
  85. dummy=1;
  86. art.fillRect(lineX, lineY, 20, 80);
  87. if(lineY<289)
  88. {
  89. lineY++;
  90. }
  91. if(lineY==289)
  92. {
  93.  
  94. int[] test = {lineX, lineY};
  95. art.clearRect(lineX, lineY, getWidth(), getHeight());
  96. art.setColor(Color.GRAY);
  97. art.fillRect(test[0], test[1], 20, 80);
  98.  
  99.  
  100.  
  101. dummy=2;
  102. art.setColor(Color.ORANGE);
  103. art.fillRect(lx, ly, 20, 60);
  104. art.fillRect(lx, ly, 40, 20);
  105. if(ly<289+20)
  106. {
  107. ly++;
  108. }
  109. if(ly==289+20)
  110. {
  111. dummy=3;
  112. art.setColor(Color.MAGENTA);
  113. art.fillRect(tx, ty, 20, 60);
  114. art.fillRect(tx, ty+20, 40, 20);
  115. if(ty<289+20)
  116. {
  117. ty++;
  118. }
  119. }
  120. if(ty==289+20)
  121. {
  122. dummy=4;
  123. art.setColor(Color.BLUE);
  124. art.fillRect(jx, jy, 20, 60);
  125. art.fillRect(jx-20, jy, 40, 20);
  126.  
  127. if(jy<289+20)
  128. {
  129. jy++;
  130. }
  131. if(jy==289+20)
  132. {
  133. dummy = 5;
  134. art.setColor(Color.YELLOW);
  135. art.fillRect(boxX, boxY, 40, 40);
  136. if(boxY<289+40)
  137. {
  138. boxY++;
  139. }
  140. }
  141. if(boxY==289+40)
  142. {
  143. dummy = 6;
  144. art.setColor(Color.GREEN);
  145. art.fillRect(sx, sy, 40, 20);
  146. art.fillRect(sx-20, sy-20, 40, 20);
  147. if(sy<289+60)
  148. {
  149. sy++;
  150. }
  151. if(sy==289+60)
  152. {
  153. dummy = 7;
  154. art.setColor(Color.RED);
  155. art.fillRect(zx, zy, 40, 20);
  156. art.fillRect(zx-20, zy+20, 40, 20);
  157. if(zy<289+40)//fix
  158. {
  159. zy++;
  160. }
  161. }
  162. }
  163. }
  164. }
  165.  
  166. }
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175. @Override
  176. public void keyPressed(KeyEvent e)
  177. {
  178. // TODO Auto-generated method stub
  179. int key = e.getKeyCode();
  180. switch(currentLevel)
  181. {
  182. case 0:
  183. if(key==e.VK_SPACE)
  184. {
  185. currentLevel=1;
  186. }
  187. break;
  188. case 1:
  189.  
  190. lineControl(e);
  191. lControl(e);
  192. tControl(e);
  193. jControl(e);
  194. boxControl(e);
  195. sControl(e);
  196. zControl(e);
  197.  
  198.  
  199. break;
  200. }
  201.  
  202. }
  203. public void lineControl(KeyEvent e)
  204. {
  205. key = e.getKeyCode();
  206. if(dummy==1)
  207. {
  208. if(key==e.VK_D || key==e.VK_RIGHT && lineY!=289)
  209. {
  210. lineX+=20;
  211. }
  212. if(key==e.VK_A || key==e.VK_LEFT && lineY!=289)
  213. {
  214. lineX-=20;
  215. }
  216. if(lineY<289)
  217. {
  218. if(key==e.VK_S || key==e.VK_DOWN)
  219. {
  220. lineY=289;
  221. }
  222. }
  223.  
  224. }
  225. }
  226. public void lControl(KeyEvent e)
  227. {
  228. key = e.getKeyCode();
  229. if(dummy==2)
  230. {
  231. if(key==e.VK_D || key==e.VK_RIGHT && ly!=289+20)
  232. {
  233. lx+=20;
  234. }
  235. if(key==e.VK_A || key==e.VK_LEFT && ly!=289+20)
  236. {
  237. lx-=20;
  238. }
  239. if(ly<289)
  240. {
  241. if(key==e.VK_S || key==e.VK_DOWN)
  242. {
  243. ly=289+20;
  244. }
  245. }
  246. }
  247. }
  248. public void tControl(KeyEvent e)
  249. {
  250. key = e.getKeyCode();
  251. if(dummy==3)
  252. {
  253. if(key==e.VK_D || key==e.VK_RIGHT && ty!=289+20)
  254. {
  255. tx+=20;
  256. }
  257. if(key==e.VK_A || key==e.VK_LEFT && ty!=289+20)
  258. {
  259. tx-=20;
  260. }
  261. if(ty<289)
  262. {
  263. if(key==e.VK_S || key==e.VK_DOWN)
  264. {
  265. ty=289+20;
  266. }
  267. }
  268. }
  269. }
  270. public void jControl(KeyEvent e)
  271. {
  272. key = e.getKeyCode();
  273. if(dummy==4)
  274. {
  275. if(key==e.VK_D || key==e.VK_RIGHT && jy!=289+20)
  276. {
  277. jx+=20;
  278. }
  279. if(key==e.VK_A || key==e.VK_LEFT && jy!=289+20)
  280. {
  281. jx-=20;
  282. }
  283. if(jy<289)
  284. {
  285. if(key==e.VK_S || key==e.VK_DOWN)
  286. {
  287. jy=289+20;
  288. }
  289. }
  290. }
  291. }
  292. public void boxControl(KeyEvent e)
  293. {
  294. key = e.getKeyCode();
  295. if(dummy==5)
  296. {
  297. if(key==e.VK_D || key==e.VK_RIGHT && boxY!=289+40)
  298. {
  299. boxX+=20;
  300. }
  301. if(key==e.VK_A || key==e.VK_LEFT && boxY!=289+40)
  302. {
  303. boxX-=20;
  304. }
  305. if(boxY<289+40)
  306. {
  307. if(key==e.VK_S || key==e.VK_DOWN)
  308. {
  309. boxY=289+40;
  310. }
  311. }
  312. }
  313. }
  314. public void sControl(KeyEvent e)
  315. {
  316. key = e.getKeyCode();
  317. if(dummy==6)
  318. {
  319. if(key==e.VK_D || key==e.VK_RIGHT && sy!=289+60)
  320. {
  321. sx+=20;
  322. }
  323. if(key==e.VK_A || key==e.VK_LEFT && sy!=289+60)
  324. {
  325. sx-=20;
  326. }
  327. if(sy<289+60)
  328. {
  329. if(key==e.VK_S || key==e.VK_DOWN)
  330. {
  331. sy=289+60;
  332. }
  333. }
  334. }
  335. }
  336. public void zControl(KeyEvent e)
  337. {
  338. key = e.getKeyCode();
  339. if(dummy==7)
  340. {
  341. if(key==e.VK_D || key==e.VK_RIGHT && zy!=289+40)
  342. {
  343. zx+=20;
  344. }
  345. if(key==e.VK_A || key==e.VK_LEFT && zy!=289+40)
  346. {
  347. zx-=20;
  348. }
  349. if(zy<289+40)
  350. {
  351. if(key==e.VK_S || key==e.VK_DOWN)
  352. {
  353. zy=289+40;
  354. }
  355. }
  356. }
  357. }
  358. @Override
  359. public void keyReleased(KeyEvent e) {
  360. // TODO Auto-generated method stub
  361.  
  362. }
  363. @Override
  364. public void keyTyped(KeyEvent e) {
  365. // TODO Auto-generated method stub
  366.  
  367. }
  368.  
  369. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement