Created
December 12, 2012 07:54
-
-
Save anonymous/4265925 to your computer and use it in GitHub Desktop.
JABBATRIS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.awt.Color; | |
| import java.awt.Font; | |
| import java.awt.event.KeyAdapter; | |
| import java.awt.event.KeyEvent; | |
| import java.io.FileInputStream; | |
| import java.io.FileNotFoundException; | |
| import java.io.IOException; | |
| import java.io.InputStream; | |
| import java.util.ArrayList; | |
| import java.util.Random; | |
| import javax.swing.ImageIcon; | |
| import javax.swing.JFrame; | |
| import javax.swing.JLabel; | |
| import sun.audio.AudioPlayer; | |
| import sun.audio.AudioStream; | |
| public class TetrisMain { | |
| public static final int TIME = 25; | |
| //public static final int FALLSPD = 20; //20(slowest)-2(fastest) | |
| public static final int gridx = 11; | |
| public static final int gridy = 19; | |
| public static final int notypes = 7; | |
| public static void main(String args[]) { | |
| Random r = new Random(); | |
| JFrame frame = new JFrame("javaTetris"); | |
| frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
| frame.setSize(225,420); | |
| frame.setLayout(null); | |
| frame.setResizable(false); | |
| frame.getContentPane().setBackground(Color.BLACK); | |
| frame.setLocationRelativeTo(null); | |
| frame.setVisible(true); | |
| Font gamefont = new Font("Consolas", Font.ITALIC, 10); | |
| int highscore = 0; | |
| while(true) { //program loop | |
| frame.getContentPane().removeAll(); | |
| JLabel highscore1 = new JLabel(); highscore1.setForeground(Color.yellow); | |
| highscore1.setText(""+highscore); | |
| highscore1.setFont(gamefont); | |
| highscore1.setBounds(123, 47, 225, 50); | |
| frame.add(highscore1); | |
| JLabel titlescreen = new JLabel(); | |
| titlescreen.setIcon(new ImageIcon("img\\title.gif")); | |
| titlescreen.setBounds(0,-25,255,420); | |
| frame.add(titlescreen); | |
| frame.getContentPane().repaint(); | |
| TitleListener titlekey = new TitleListener(frame); | |
| frame.addKeyListener(titlekey); | |
| InputStream in = null; | |
| AudioStream as = null; | |
| try { | |
| in = new FileInputStream("snd\\titlescreen.wav"); | |
| as = new AudioStream(in); | |
| } catch (FileNotFoundException e) { | |
| } catch (IOException e) { | |
| } | |
| AudioPlayer.player.start(as); | |
| long init = System.currentTimeMillis(); | |
| long titlecount = init; | |
| while (titlekey.cont) { | |
| try { | |
| Thread.sleep(100); | |
| } catch (InterruptedException e) { | |
| e.printStackTrace(); | |
| } | |
| titlecount = System.currentTimeMillis() - init; | |
| //System.out.println(titlecount); | |
| if (titlecount >= 28100) { | |
| init = System.currentTimeMillis(); | |
| try { | |
| in = new FileInputStream("snd\\titlescreen.wav"); | |
| as = new AudioStream(in); | |
| } catch (FileNotFoundException e) { | |
| } catch (IOException e) { | |
| } | |
| AudioPlayer.player.start(as); | |
| } | |
| if (titlekey.trip) titlescreen.setIcon(new ImageIcon("img\\title0.gif")); | |
| } | |
| AudioPlayer.player.stop(as); | |
| frame.removeKeyListener(titlekey); | |
| GameManager game = new GameManager(gridx,gridy); | |
| JLabel staticdisp = new JLabel(); | |
| staticdisp.setText("SCORE: SPEED: NEXT:"); | |
| staticdisp.setBounds(11, 355, 225, 50); | |
| staticdisp.setForeground(Color.yellow); | |
| staticdisp.setFont(gamefont); | |
| JLabel pointdisp = new JLabel(); | |
| pointdisp.setText("0"); | |
| pointdisp.setBounds(55, 355, 225, 50); | |
| pointdisp.setForeground(Color.yellow); | |
| pointdisp.setFont(gamefont); | |
| JLabel spddisp = new JLabel(); | |
| spddisp.setText("1"); | |
| spddisp.setBounds(130, 355, 225, 50); | |
| spddisp.setForeground(Color.yellow); | |
| spddisp.setFont(gamefont); | |
| JLabel nxtdisp = new JLabel(); | |
| nxtdisp.setIcon(new ImageIcon("img\\1.png")); | |
| nxtdisp.setBounds(195,365,30,30); | |
| ArrayList<JLabel> dispblock = new ArrayList<JLabel>(); | |
| KeyListener klisten = new KeyListener(game, frame); | |
| frame.addKeyListener(klisten); | |
| game.player.generateNew(game.grid,r.nextInt(notypes)+1); | |
| int nextsto = r.nextInt(notypes)+1; | |
| changenext(nxtdisp,nextsto); | |
| titlecount = 0; | |
| int counter = 0; | |
| int fallspd = 20; | |
| int speedup = 0; | |
| int points = 0; | |
| InputStream ins = null; | |
| AudioStream asd = null; | |
| try { | |
| ins = new FileInputStream("snd\\mains.wav"); | |
| asd = new AudioStream(ins); | |
| } catch (FileNotFoundException e) { | |
| } catch (IOException e) { | |
| } | |
| init = System.currentTimeMillis(); | |
| AudioPlayer.player.start(asd); | |
| while(true) { //game loop | |
| titlecount = System.currentTimeMillis() - init; | |
| //System.out.println(titlecount); | |
| if (titlecount >= 62400) { //CHECKTIME | |
| try { | |
| ins = new FileInputStream("snd\\mains.wav"); | |
| asd = new AudioStream(ins); | |
| } catch (FileNotFoundException e) { | |
| } catch (IOException e) { | |
| } | |
| init = System.currentTimeMillis(); | |
| } | |
| AudioPlayer.player.start(asd); | |
| if (game.player.hitcheck(game.grid)) { //player piece hit static | |
| game.convertplayerstatic(); | |
| if (game.player.generateNew(game.grid,nextsto)) { | |
| game.viewPlayer(); | |
| frame.removeKeyListener(klisten); | |
| //print(game.output()); | |
| display(game.output(),frame,dispblock,titlekey.trip); | |
| frame.getContentPane().repaint(); | |
| break; | |
| } | |
| nextsto = r.nextInt(notypes)+1; | |
| } else { | |
| if (counter >= fallspd) { //change this variable based on game speed | |
| game.player.fall(); | |
| counter = 0; | |
| } | |
| game.player.event(game.grid); //player event (refresh every cycle) | |
| } | |
| counter++; | |
| game.viewPlayer(); | |
| int stopoints = game.clearlines(); | |
| if (stopoints > 0) { | |
| speedup++; | |
| if (speedup == 20) { | |
| if (fallspd > 1) { | |
| fallspd--; | |
| } | |
| speedup = 0; | |
| } | |
| points = points + (21-fallspd)*stopoints; | |
| } | |
| try { | |
| Thread.sleep(TIME); | |
| } catch (InterruptedException e) { | |
| e.printStackTrace(); | |
| } | |
| dispblock.clear(); | |
| frame.getContentPane().removeAll(); | |
| frame.add(spddisp); | |
| spddisp.setText(""+(21-fallspd)); | |
| pointdisp.setText(""+points); | |
| frame.add(pointdisp); | |
| frame.add(staticdisp); | |
| changenext(nxtdisp,nextsto); | |
| frame.add(nxtdisp); | |
| display(game.output(),frame,dispblock,titlekey.trip); | |
| //print(game.output()); | |
| frame.getContentPane().repaint(); | |
| } //end game loop | |
| AudioPlayer.player.stop(asd); | |
| frame.remove(staticdisp); frame.remove(nxtdisp); frame.remove(spddisp); frame.remove(pointdisp); | |
| JLabel gameoverdisp = new JLabel(); | |
| gameoverdisp.setText("GAME OVER (SPACE)"); | |
| gameoverdisp.setBounds(65, 355, 225, 50); | |
| gameoverdisp.setForeground(Color.yellow); | |
| gameoverdisp.setFont(gamefont); | |
| frame.add(gameoverdisp); | |
| frame.getContentPane().repaint(); | |
| titlekey.cont = true; | |
| frame.addKeyListener(titlekey); | |
| int count = 0; | |
| InputStream ends = null; | |
| AudioStream endsa = null; | |
| try { | |
| ends = new FileInputStream("snd\\end.wav"); | |
| endsa = new AudioStream(ends); | |
| } catch (FileNotFoundException e) { | |
| } catch (IOException e) { | |
| } | |
| AudioPlayer.player.start(endsa); | |
| while (titlekey.cont) { | |
| count++; | |
| try { | |
| Thread.sleep(100); | |
| } catch (InterruptedException e) { | |
| e.printStackTrace(); | |
| } | |
| if (count == 5) { | |
| frame.remove(gameoverdisp); | |
| frame.getContentPane().repaint(); | |
| } | |
| if (count == 10) { | |
| frame.add(gameoverdisp); | |
| frame.getContentPane().repaint(); | |
| count = 0; | |
| } | |
| } | |
| AudioPlayer.player.stop(endsa); | |
| frame.removeKeyListener(titlekey); | |
| highscore = Math.max(highscore, points); | |
| } //end program loop | |
| } | |
| public static void changenext(JLabel nxtdisp,int nextsto) { | |
| String sto = "img\\" + nextsto + ".png"; | |
| nxtdisp.setIcon(new ImageIcon(sto)); | |
| } | |
| //frame.getContentPane().removeAll(); | |
| //frame.getContentPane().repaint(); | |
| public static void display(ArrayList<String> output, JFrame frame, ArrayList<JLabel> dispblock, boolean trip) { | |
| for (int y = 0; y < output.size(); y++) { | |
| String sto = output.get(y); | |
| for (int x = 0; x < sto.length();x++) { | |
| if (sto.substring(x,x+1).equals("1") || sto.substring(x,x+1).equals("9")) { | |
| dispblock.add(new JLabel()); | |
| if (trip) { | |
| dispblock.get(dispblock.size()-1).setIcon(new ImageIcon("img\\block0.gif")); | |
| } else { | |
| dispblock.get(dispblock.size()-1).setIcon(new ImageIcon("img\\block0.png")); | |
| } | |
| dispblock.get(dispblock.size()-1).setBounds(5+(x*19), 5+(y*19), 18, 18); | |
| frame.add(dispblock.get(dispblock.size()-1)); | |
| } | |
| } | |
| } | |
| } | |
| //placeholder text-based gui | |
| //NOTE--gui should not shot y row 0 | |
| public static void print(ArrayList<String> output) { | |
| System.out.println("\n\n\n\n\n\n\n\n\n\n\n"); | |
| for (int i = 0; i < output.size(); i++) { | |
| String sto = output.get(i); | |
| for (int j = 0; j < sto.length();j++) { | |
| if (sto.substring(j,j+1).equals("0")) { | |
| System.out.print(" "); | |
| } else { | |
| System.out.print("X"); | |
| } | |
| } | |
| System.out.print(output.get(i)); | |
| System.out.println(); | |
| } | |
| } | |
| public static class KeyListener extends KeyAdapter { | |
| private GameManager game; | |
| public KeyListener(GameManager game, JFrame frame) { | |
| this.game = game; | |
| } | |
| public void keyPressed(KeyEvent event) { | |
| if (event.getKeyCode() == 39)game.getPlayerInput("RIGHT"); //moveright | |
| if (event.getKeyCode() == 37)game.getPlayerInput("LEFT"); //moveleft | |
| if (event.getKeyCode() == 38)game.getPlayerInput("ROTATE"); //rotate | |
| if (event.getKeyCode() == 40)game.getPlayerInput("DOWN"); | |
| } | |
| } | |
| public static class TitleListener extends KeyAdapter { | |
| public boolean cont; | |
| public boolean trip; | |
| public TitleListener(JFrame frame) { | |
| cont = true; | |
| } | |
| public void keyPressed(KeyEvent event) { | |
| if (event.getKeyCode() == 32)cont = false; | |
| if (event.getKeyCode() == 90)trip = true; | |
| } | |
| } | |
| } | |
| public class PlayerManager { | |
| public int gridx,gridy; | |
| private GameManager game; | |
| private int type; | |
| private int status; | |
| private boolean leftqueue,rightqueue,rotatequeue,downqueue; | |
| public int ax,ay; | |
| public int bx,by; | |
| public int cx,cy; | |
| public int dx,dy; | |
| public PlayerManager(int gridx,int gridy,GameManager game) { | |
| this.gridx = gridx; | |
| this.gridy = gridy; | |
| this.game = game; | |
| } | |
| //return true is game over switch on game | |
| public boolean generateNew(int[][] grid,int rand) { | |
| status = 0; | |
| type = rand; | |
| if (type == 1) { //+ type | |
| ax = (gridx/2)-1;ay = 0; // abc | |
| bx = (gridx/2);by = 0; // d | |
| cx = (gridx/2)+1;cy = 0; | |
| dx = (gridx/2);dy = 1; | |
| } else if (type == 2) { //box type | |
| ax = (gridx/2);ay = 0; // ab | |
| bx = (gridx/2)+1;by = 0; // cd | |
| cx = (gridx/2);cy = 1; | |
| dx = (gridx/2)+1;dy = 1; | |
| } else if (type == 3) { //- type | |
| ax = (gridx/2)-1;ay = 0; // abcd | |
| bx = (gridx/2);by = 0; // | |
| cx = (gridx/2)+1;cy = 0; | |
| dx = (gridx/2)+2;dy = 0; | |
| } else if (type == 4) { //L type | |
| ax = (gridx/2)-1;ay = 0; // abc | |
| bx = (gridx/2);by = 0; // d | |
| cx = (gridx/2)+1;cy = 0; | |
| dx = (gridx/2)-1;dy = 1; | |
| } else if (type == 5) { //BL type | |
| ax = (gridx/2)-1;ay = 0; // abc | |
| bx = (gridx/2);by = 0; // d | |
| cx = (gridx/2)+1;cy = 0; | |
| dx = (gridx/2)+1;dy = 1; | |
| } else if (type == 6) { //z type | |
| ax = (gridx/2)-1;ay = 0; // ab | |
| bx = (gridx/2);by = 0; // cd | |
| cx = (gridx/2);cy = 1; | |
| dx = (gridx/2)+1;dy = 1; | |
| } else if (type == 7) { //s type | |
| ax = (gridx/2);ay = 0; // ab | |
| bx = (gridx/2)+1;by = 0; // cd | |
| cx = (gridx/2)-1;cy = 1; | |
| dx = (gridx/2);dy = 1; | |
| } | |
| if (game.isActive(ax, ay) || game.isActive(bx, by) || game.isActive(cx, cy) || game.isActive(dx,dy) | |
| || game.isActive(ax, ay+1) || game.isActive(bx, by+1) || game.isActive(cx, cy+1) || game.isActive(dx,dy+1)) { | |
| return true; | |
| } else { | |
| return false; | |
| } | |
| } | |
| public void fall() { | |
| ay++; | |
| by++; | |
| cy++; | |
| dy++; | |
| } | |
| public boolean hitcheck(int[][] grid) { | |
| if ((ay == gridy-1) || (by == gridy-1) || (cy == gridy-1) || (dy == gridy-1)) { | |
| return true; | |
| } else if (!(grid[ax][ay+1] == 0) && !(grid[ax][ay+1] == 9)) { | |
| return true; | |
| } else if (!(grid[bx][by+1] == 0) && !(grid[bx][by+1] == 9)) { | |
| return true; | |
| } else if (!(grid[cx][cy+1] == 0) && !(grid[cx][cy+1] == 9)) { | |
| return true; | |
| } else if (!(grid[dx][dy+1] == 0) && !(grid[dx][dy+1] == 9)) { | |
| return true; | |
| } else { | |
| return false; | |
| } | |
| } | |
| public void event(int[][] grid) { | |
| if (leftqueue) { | |
| leftevent(grid); | |
| } else if (rightqueue) { | |
| rightevent(grid); | |
| } else if (rotatequeue) { | |
| rotateevent(grid); | |
| } else if (downqueue) { | |
| downevent(grid); | |
| } | |
| leftqueue = false; | |
| rightqueue = false; | |
| rotatequeue = false; | |
| downqueue = false; | |
| } | |
| public void downevent(int[][] grid) { | |
| if ((ay+1<gridy) && (by+1<gridy) && (cy+1<gridy) && (dy+1<gridy) && canrotate(ax,ay+1,bx,by+1,cx,cy+1,dx,dy+1)) { | |
| ay++; | |
| by++; | |
| cy++; | |
| dy++; | |
| } | |
| } | |
| public void leftevent(int[][] grid) { | |
| boolean leftedgecheck = true; | |
| for(int y = 0; y < gridy; y++) { | |
| if (grid[0][y] == 9) { | |
| leftedgecheck = false; | |
| } | |
| } | |
| if (leftedgecheck && (grid[ax-1][ay] == 1 || grid[bx-1][by] == 1 || grid[cx-1][cy] == 1 || grid[dx-1][dy] == 1)) { | |
| leftedgecheck = false; | |
| } | |
| if (leftedgecheck) { | |
| ax--; | |
| bx--; | |
| cx--; | |
| dx--; | |
| } | |
| } | |
| public void rightevent(int[][] grid) { | |
| boolean rightedgecheck = true; | |
| for(int y = 0; y < gridy; y++) { | |
| if (grid[gridx-1][y] == 9) { | |
| rightedgecheck = false; | |
| } | |
| } | |
| if (rightedgecheck && (grid[ax+1][ay] == 1 || grid[bx+1][by] == 1 || grid[cx+1][cy] == 1 || grid[dx+1][dy] == 1)) { | |
| rightedgecheck = false; | |
| } | |
| if (rightedgecheck) { | |
| ax++; | |
| bx++; | |
| cx++; | |
| dx++; | |
| } | |
| } | |
| public void actionRight() { | |
| leftqueue = false; | |
| rotatequeue = false; | |
| downqueue = false; | |
| rightqueue = true; | |
| } | |
| public void actionLeft() { | |
| rightqueue = false; | |
| rotatequeue = false; | |
| downqueue = false; | |
| leftqueue = true; | |
| } | |
| public void actionRotate() { | |
| rotatequeue = true; | |
| rightqueue = false; | |
| downqueue = false; | |
| leftqueue = false; | |
| } | |
| public void actionDown() { | |
| rotatequeue = false; | |
| rightqueue = false; | |
| downqueue = true; | |
| leftqueue = false; | |
| } | |
| public void rotateevent(int[][] grid) { | |
| //status 0,1,2,3 | |
| if (status == 0) { | |
| status0(grid); | |
| } else if (status == 1) { | |
| status1(grid); | |
| } else if (status == 2) { | |
| status2(grid); | |
| } else if (status == 3) { | |
| status3(grid); | |
| } | |
| } | |
| private boolean canrotate(int ax, int ay, int bx, int by, int cx, int cy, int dx, int dy) { | |
| if ( (ax > -1) && (bx > -1) && (cx > -1) && (dx > -1) && | |
| (ax < gridx) && (bx < gridx) && (cx < gridx) && (dx < gridx) && | |
| (ay > -1) && (by > -1) && (cy > -1) && (dy > -1) && | |
| (ay < gridy) && (by < gridy) && (cy < gridy) && (dy < gridy) && | |
| (!game.isStatic(ax,ay)) && (!game.isStatic(bx,by)) && (!game.isStatic(cx,cy)) && (!game.isStatic(dx,dy)) ) { | |
| return true; | |
| } | |
| return false; | |
| } | |
| private void status0(int[][] grid) { | |
| if (type == 1) { | |
| if (/*DNC*/canrotate(ax+1,ay-1,bx,by,cx-1,cy+1,dx-1,dy-1)/*DNC*/) { | |
| status = 1; | |
| ax = ax + 1; ay = ay - 1; | |
| cx = cx - 1; cy = cy + 1; | |
| dx = dx - 1; dy = dy - 1; | |
| } | |
| } else if (type == 3) { | |
| if (/*DNC*/canrotate(ax+1,ay-1,bx,by,cx-1,cy+1,dx-2,dy+2)/*DNC*/) { | |
| status = 1; | |
| ax = ax + 1; ay = ay - 1; | |
| cx = cx - 1; cy = cy + 1; | |
| dx = dx - 2; dy = dy + 2; | |
| } | |
| } else if (type == 4) { | |
| if (/*DNC*/canrotate(ax,ay,bx-1,by+1,cx-2,cy+2,dx-1,dy-1)/*DNC*/) { | |
| status = 1; | |
| bx = bx - 1; by = by + 1; | |
| cx = cx - 2; cy = cy + 2; | |
| dx = dx - 1; dy = dy - 1; | |
| } | |
| } else if (type == 5) { | |
| if (/*DNC*/canrotate(ax+2,ay-2,bx+1,by-1,cx,cy,dx-1,dy-1)/*DNC*/) { | |
| status = 1; | |
| ax = ax+2; ay = ay-2; | |
| bx = bx+1; by = by-1; | |
| dx = dx-1; dy = dy-1; | |
| } | |
| } else if (type == 6) { | |
| if (/*DNC*/canrotate(ax+2,ay,bx+1,by+1,cx,cy,dx-1,dy+1)/*DNC*/) { | |
| status = 1; | |
| ax = ax + 2; | |
| bx = bx + 1; by = by + 1; | |
| dx = dx - 1; dy = dy + 1; | |
| } | |
| } else if (type == 7) { | |
| if (/*DNC*/canrotate(ax+1,ay+1,bx,by+2,cx+1,cy-1,dx,dy)/*DNC*/) { | |
| status = 1; | |
| ax = ax + 1; ay = ay + 1; | |
| by = by + 2; | |
| cx = cx + 1; cy = cy - 1; | |
| } | |
| } | |
| } | |
| private void status1(int[][] grid) { | |
| if (type == 1) { | |
| if (/*DNC*/canrotate(ax+1,ay-1,bx,by,cx-1,cy+1,dx-1,dy-1)/*DNC*/) { | |
| status = 2; | |
| ax = ax + 1; ay = ay + 1; | |
| cx = cx - 1; cy = cy - 1; | |
| dx = dx + 1; dy = dy - 1; | |
| } | |
| } else if (type == 3) { | |
| if (/*DNC*/canrotate(ax-1,ay+1,bx,by,cx+1,cy-1,dx+2,dy-2)/*DNC*/) { | |
| status = 0; | |
| ax = ax - 1; ay = ay + 1; | |
| cx = cx + 1; cy = cy - 1; | |
| dx = dx + 2; dy = dy - 2; | |
| } | |
| } else if (type == 4) { | |
| if (/*DNC*/canrotate(ax,ay,bx-1,by-1,cx-2,cy-2,dx+1,dy-1)/*DNC*/) { | |
| status = 2; | |
| bx = bx - 1; by = by - 1; | |
| cx = cx - 2; cy = cy - 2; | |
| dx = dx + 1; dy = dy - 1; | |
| } | |
| } else if (type == 5) { | |
| if (/*DNC*/canrotate(ax+2,ay+2,bx+1,by+1,cx,cy,dx+1,dy-1)/*DNC*/) { | |
| status = 2; | |
| ax = ax+2; ay = ay+2; | |
| bx = bx+1; by = by+1; | |
| dx = dx+1; dy = dy-1; | |
| } | |
| } else if (type == 6) { | |
| if (/*DNC*/canrotate(ax-2,ay,bx-1,by-1,cx,cy,dx+1,dy-1)/*DNC*/) { | |
| status = 0; | |
| ax = ax - 2; | |
| bx = bx - 1; by = by - 1; | |
| dx = dx + 1; dy = dy - 1; | |
| } | |
| } else if (type == 7) { | |
| if (/*DNC*/canrotate(ax-1,ay-1,bx,by-2,cx-1,cy+1,dx,dy)/*DNC*/) { | |
| status = 0; | |
| ax = ax - 1; ay = ay - 1; | |
| by = by - 2; | |
| cx = cx - 1; cy = cy + 1; | |
| } | |
| } | |
| } | |
| private void status2(int[][] grid) { | |
| if (type == 1) { | |
| if (/*DNC*/canrotate(ax-1,ay+1,bx,by,cx+1,cy-1,dx+1,dy+1)/*DNC*/) { | |
| status = 3; | |
| ax = ax - 1; ay = ay + 1; | |
| cx = cx + 1; cy = cy - 1; | |
| dx = dx + 1; dy = dy + 1; | |
| } | |
| } else if (type == 4) { | |
| if (/*DNC*/canrotate(ax,ay,bx+1,by-1,cx+2,cy-2,dx+1,dy+1)/*DNC*/) { | |
| status = 3; | |
| bx = bx + 1; by = by - 1; | |
| cx = cx + 2; cy = cy - 2; | |
| dx = dx + 1; dy = dy + 1; | |
| } | |
| } else if (type == 5) { | |
| if (/*DNC*/canrotate(ax-2,ay+2,bx-1,by-1,cx,cy,dx+1,dy+1)/*DNC*/) { | |
| status = 3; | |
| ax = ax-2; ay = ay+2; | |
| bx = bx-1; by = by+1; | |
| dx = dx+1; dy = dy+1; | |
| } | |
| } | |
| } | |
| private void status3(int[][] grid) { | |
| if (type == 1) { | |
| if (/*DNC*/canrotate(ax-1,ay-1,bx,by,cx+1,cy+1,dx-1,dy+1)/*DNC*/) { | |
| status = 0; | |
| ax = ax - 1; ay = ay - 1; | |
| cx = cx + 1; cy = cy + 1; | |
| dx = dx - 1; dy = dy + 1; | |
| } | |
| } else if (type == 4) { | |
| if (/*DNC*/canrotate(ax,ay,bx+1,by+1,cx+2,cy+2,dx-1,dy+1)/*DNC*/) { | |
| status = 0; | |
| bx = bx + 1; by = by + 1; | |
| cx = cx + 2; cy = cy + 2; | |
| dx = dx - 1; dy = dy + 1; | |
| } | |
| } else if (type == 5) { | |
| if (/*DNC*/canrotate(ax-2,ay-2,bx-1,by-1,cx,cy,dx-1,dy+1)/*DNC*/) { | |
| status = 0; | |
| ax = ax-2; ay = ay-2; | |
| bx = bx-1; by = by-1; | |
| dx = dx-1; dy = dy+1; | |
| } | |
| } | |
| } | |
| } | |
| import java.io.FileInputStream; | |
| import java.io.FileNotFoundException; | |
| import java.io.IOException; | |
| import java.io.InputStream; | |
| import java.util.ArrayList; | |
| import sun.audio.AudioPlayer; | |
| import sun.audio.AudioStream; | |
| public class GameManager { | |
| public int grid[][]; | |
| public PlayerManager player; | |
| private int gridx; | |
| private int gridy; | |
| public GameManager(int x,int y) { | |
| grid = new int[x][y]; | |
| gridx = x; | |
| gridy = y; | |
| player = new PlayerManager(gridx,gridy,this); | |
| } | |
| public void getPlayerInput(String key) { | |
| if (key.equals("RIGHT")) { | |
| player.actionRight(); | |
| } else if (key.equals("LEFT")) { | |
| player.actionLeft(); | |
| } else if (key.equals("ROTATE")) { | |
| player.actionRotate(); | |
| } else if (key.equals("DOWN")) { | |
| player.actionDown(); | |
| } | |
| } | |
| //0 - inactive | |
| //1 - static | |
| //9 - player controlled | |
| //checks all y rows if all blocks are 1 status | |
| //if satisfy conditions, converts all to 0's | |
| public int clearlines() { | |
| int retpoints = 0; | |
| for(int y = 0; y < gridy; y++) { | |
| boolean clearthisline = true; | |
| for (int x = 0; x < gridx; x++) { | |
| if (grid[x][y] != 1) { //checks if this line is in need of clearing | |
| clearthisline = false; | |
| break; | |
| } | |
| } | |
| if(clearthisline){ | |
| InputStream ends = null; | |
| AudioStream endsa = null; | |
| try { | |
| ends = new FileInputStream("snd\\linebrk.wav"); | |
| endsa = new AudioStream(ends); | |
| } catch (FileNotFoundException e) { | |
| } catch (IOException e) { | |
| } | |
| AudioPlayer.player.start(endsa); | |
| retpoints++; | |
| for (int x = 0; x < gridx; x++) { | |
| setInactive(x,y); | |
| } | |
| //this goes backwards | |
| //moves all lines above clearedline down by one | |
| for (int iy = y-1; iy > 0; iy--) { // all 1's above break line down by one sq | |
| for (int ix = 0; ix < gridx; ix++) { | |
| if (grid[ix][iy] == 1) { | |
| grid[ix][iy] = 0; | |
| grid[ix][iy+1] = 1; | |
| } | |
| } | |
| } | |
| } | |
| } | |
| return retpoints; | |
| } | |
| public boolean isStatic(int x, int y) { | |
| return (grid[x][y] == 1); | |
| } | |
| public boolean isActive(int x, int y) { | |
| return !(grid[x][y] == 0); | |
| } | |
| public void setActive(int x,int y) { | |
| grid[x][y] = 2; | |
| } | |
| public void setInactive(int x,int y) { | |
| grid[x][y] = 0; | |
| } | |
| //outputs view of current game | |
| //arraylist of strings, every element is new line | |
| public ArrayList<String> output() { | |
| ArrayList<String> ret = new ArrayList<String>(); | |
| for(int y = 0; y < gridy; y++) { | |
| String add = ""; | |
| for(int x = 0; x < gridx; x++){ | |
| add = add + grid[x][y]; | |
| } | |
| ret.add(add); | |
| } | |
| return ret; | |
| } | |
| public void viewPlayer() { | |
| for(int y = 0; y < gridy; y++) { | |
| for(int x = 0; x < gridx; x++){ | |
| if (grid[x][y] == 9) { | |
| grid[x][y] = 0; | |
| } | |
| } | |
| } | |
| grid[player.ax][player.ay] = 9; | |
| grid[player.bx][player.by] = 9; | |
| grid[player.cx][player.cy] = 9; | |
| grid[player.dx][player.dy] = 9; | |
| } | |
| public void convertplayerstatic() { | |
| for(int y = 0; y < gridy; y++) { | |
| for(int x = 0; x < gridx; x++){ | |
| if (grid[x][y] == 9) { | |
| grid[x][y] = 1; | |
| } | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment