Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Sam Mulford
- Intro to CS Period 3
- May 2018
- Description: Tetris
- */
- package programs;
- import java.awt.*;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import javax.swing.ImageIcon;
- import java.util.*;
- public class Tetris extends JoeApplet implements KeyListener
- {
- int currentLevel =0;
- Font good = new Font("Monospaced", Font.PLAIN, 30);
- //String path = "H:\\workspace\\SaveFiles\\";
- //Image man = new ImageIcon(path + "man.png").getImage();
- int score=0, num;
- int key;
- int dummy =0, buddy;
- 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;
- int man, man1;
- Random gen = new Random();
- SolidObject floor = new SolidObject();
- int[] test= new int[100];
- public void init()
- {
- addKeyListener(this);
- }
- public void menuScreen(Graphics art)
- {
- setSize(1260, 800);
- setBackground(Color.BLACK);
- //art.drawImage(man, 600, 400, this);
- art.setColor(Color.WHITE);
- art.setFont(good);
- art.drawString("Tetris! Press Space to play!", 350, 300);
- }
- public void paint(Graphics art)
- {
- switch(currentLevel)
- {
- case 0:
- menuScreen(art);
- break;
- case 1:
- game(art);
- break;
- case 2:
- gameOver(art);
- break;
- default:
- System.out.println("ERROR");
- }
- }
- public void game(Graphics art)
- {
- setSize(300, 400);
- setBackground(Color.BLACK);
- art.setColor(Color.YELLOW);
- art.drawRect(0, 375, 299, 20);
- floor.makeSolidObject(0, 375, 299, 20);
- line(art);
- num = 1 + gen.nextInt(7);
- }
- public void gameOver(Graphics art)
- {
- setSize(1260, 800);
- setBackground(Color.BLACK);
- art.setColor(Color.WHITE);
- art.drawString("Game Over!", 350, 300);
- }
- public void line(Graphics art)
- {
- art.setColor(Color.CYAN);
- dummy=1;
- art.fillRect(lineX, lineY, 20, 80);
- if(lineY<289)
- {
- lineY++;
- }
- if(lineY==289)
- {
- int[] test = {lineX, lineY};
- art.clearRect(lineX, lineY, getWidth(), getHeight());
- art.setColor(Color.GRAY);
- art.fillRect(test[0], test[1], 20, 80);
- dummy=2;
- art.setColor(Color.ORANGE);
- art.fillRect(lx, ly, 20, 60);
- art.fillRect(lx, ly, 40, 20);
- if(ly<289+20)
- {
- ly++;
- }
- if(ly==289+20)
- {
- dummy=3;
- art.setColor(Color.MAGENTA);
- art.fillRect(tx, ty, 20, 60);
- art.fillRect(tx, ty+20, 40, 20);
- if(ty<289+20)
- {
- ty++;
- }
- }
- if(ty==289+20)
- {
- dummy=4;
- art.setColor(Color.BLUE);
- art.fillRect(jx, jy, 20, 60);
- art.fillRect(jx-20, jy, 40, 20);
- if(jy<289+20)
- {
- jy++;
- }
- if(jy==289+20)
- {
- dummy = 5;
- art.setColor(Color.YELLOW);
- art.fillRect(boxX, boxY, 40, 40);
- if(boxY<289+40)
- {
- boxY++;
- }
- }
- if(boxY==289+40)
- {
- dummy = 6;
- art.setColor(Color.GREEN);
- art.fillRect(sx, sy, 40, 20);
- art.fillRect(sx-20, sy-20, 40, 20);
- if(sy<289+60)
- {
- sy++;
- }
- if(sy==289+60)
- {
- dummy = 7;
- art.setColor(Color.RED);
- art.fillRect(zx, zy, 40, 20);
- art.fillRect(zx-20, zy+20, 40, 20);
- if(zy<289+40)//fix
- {
- zy++;
- }
- }
- }
- }
- }
- }
- @Override
- public void keyPressed(KeyEvent e)
- {
- // TODO Auto-generated method stub
- int key = e.getKeyCode();
- switch(currentLevel)
- {
- case 0:
- if(key==e.VK_SPACE)
- {
- currentLevel=1;
- }
- break;
- case 1:
- lineControl(e);
- lControl(e);
- tControl(e);
- jControl(e);
- boxControl(e);
- sControl(e);
- zControl(e);
- break;
- }
- }
- public void lineControl(KeyEvent e)
- {
- key = e.getKeyCode();
- if(dummy==1)
- {
- if(key==e.VK_D || key==e.VK_RIGHT && lineY!=289)
- {
- lineX+=20;
- }
- if(key==e.VK_A || key==e.VK_LEFT && lineY!=289)
- {
- lineX-=20;
- }
- if(lineY<289)
- {
- if(key==e.VK_S || key==e.VK_DOWN)
- {
- lineY=289;
- }
- }
- }
- }
- public void lControl(KeyEvent e)
- {
- key = e.getKeyCode();
- if(dummy==2)
- {
- if(key==e.VK_D || key==e.VK_RIGHT && ly!=289+20)
- {
- lx+=20;
- }
- if(key==e.VK_A || key==e.VK_LEFT && ly!=289+20)
- {
- lx-=20;
- }
- if(ly<289)
- {
- if(key==e.VK_S || key==e.VK_DOWN)
- {
- ly=289+20;
- }
- }
- }
- }
- public void tControl(KeyEvent e)
- {
- key = e.getKeyCode();
- if(dummy==3)
- {
- if(key==e.VK_D || key==e.VK_RIGHT && ty!=289+20)
- {
- tx+=20;
- }
- if(key==e.VK_A || key==e.VK_LEFT && ty!=289+20)
- {
- tx-=20;
- }
- if(ty<289)
- {
- if(key==e.VK_S || key==e.VK_DOWN)
- {
- ty=289+20;
- }
- }
- }
- }
- public void jControl(KeyEvent e)
- {
- key = e.getKeyCode();
- if(dummy==4)
- {
- if(key==e.VK_D || key==e.VK_RIGHT && jy!=289+20)
- {
- jx+=20;
- }
- if(key==e.VK_A || key==e.VK_LEFT && jy!=289+20)
- {
- jx-=20;
- }
- if(jy<289)
- {
- if(key==e.VK_S || key==e.VK_DOWN)
- {
- jy=289+20;
- }
- }
- }
- }
- public void boxControl(KeyEvent e)
- {
- key = e.getKeyCode();
- if(dummy==5)
- {
- if(key==e.VK_D || key==e.VK_RIGHT && boxY!=289+40)
- {
- boxX+=20;
- }
- if(key==e.VK_A || key==e.VK_LEFT && boxY!=289+40)
- {
- boxX-=20;
- }
- if(boxY<289+40)
- {
- if(key==e.VK_S || key==e.VK_DOWN)
- {
- boxY=289+40;
- }
- }
- }
- }
- public void sControl(KeyEvent e)
- {
- key = e.getKeyCode();
- if(dummy==6)
- {
- if(key==e.VK_D || key==e.VK_RIGHT && sy!=289+60)
- {
- sx+=20;
- }
- if(key==e.VK_A || key==e.VK_LEFT && sy!=289+60)
- {
- sx-=20;
- }
- if(sy<289+60)
- {
- if(key==e.VK_S || key==e.VK_DOWN)
- {
- sy=289+60;
- }
- }
- }
- }
- public void zControl(KeyEvent e)
- {
- key = e.getKeyCode();
- if(dummy==7)
- {
- if(key==e.VK_D || key==e.VK_RIGHT && zy!=289+40)
- {
- zx+=20;
- }
- if(key==e.VK_A || key==e.VK_LEFT && zy!=289+40)
- {
- zx-=20;
- }
- if(zy<289+40)
- {
- if(key==e.VK_S || key==e.VK_DOWN)
- {
- zy=289+40;
- }
- }
- }
- }
- @Override
- public void keyReleased(KeyEvent e) {
- // TODO Auto-generated method stub
- }
- @Override
- public void keyTyped(KeyEvent e) {
- // TODO Auto-generated method stub
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement