이 포스팅은 단순 코드 저장 목적으로 작성되었던 글입니다. Github 놔두고 왜 여기다 썼지
1. 벽돌깨기
Main.java — 기본 창 띄워줌
package brickBracker; import javax.swing.JFrame; public class Main { public static void main(String[] args) { JFrame obj = new JFrame(); Gameplay gamePlay = new Gameplay(); obj.setBounds(10, 10, 700, 600); obj.setTitle("Breakout Ball"); obj.setResizable(false); obj.setVisible(true); obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); obj.add(gamePlay); } }
Gameplay.java — 전체적인 게임로직
package brickBracker; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.JPanel; import javax.swing.Timer; public class Gameplay extends JPanel implements KeyListener, ActionListener { private boolean play = false; private int score = 0; private int totalBricks = 21; private Timer timer; private int delay = 8; private int playerX = 310; private int ballposX = 120; private int ballposY = 350; private int ballXdir = -1; private int ballYdir = -2; private Mapgenerator map; public Gameplay() { map = new Mapgenerator(3, 7); addKeyListener(this); setFocusable(true); setFocusTraversalKeysEnabled(false); timer = new Timer(delay, this); timer.start(); } public void paint(Graphics g) { g.setColor(Color.black); g.fillRect(1, 1, 692, 592); map.draw((Graphics2D) g); g.setColor(Color.yellow); g.fillRect(0, 0, 3, 592); g.fillRect(0, 0, 692, 3); g.fillRect(0, 0, 3, 592); g.setColor(Color.white); g.setFont(new Font("serif", Font.BOLD, 25)); g.drawString("" + score, 590, 30); g.setColor(Color.green); g.fillRect(playerX, 550, 100, 8); g.setColor(Color.yellow); g.fillOval(ballposX, ballposY, 20, 20); if (totalBricks <= 0) { play = false; ballXdir = 0; ballYdir = 0; g.setColor(Color.red); g.setFont(new Font("serif", Font.BOLD, 30)); g.drawString("블록을 모두 제거하였습니다 ", 190, 300); g.setFont(new Font("serif", Font.BOLD, 20)); g.drawString("Enter키를 눌러 다시 시작하세요. ", 230, 350); } if (ballposY > 570) { play = false; ballXdir = 0; ballYdir = 0; g.setColor(Color.red); g.setFont(new Font("serif", Font.BOLD, 30)); g.drawString("모든 블록을 제거하지 못했습니다.", 190, 300); g.setFont(new Font("serif", Font.BOLD, 20)); g.drawString("Enter키를 눌러 다시 시작하세요. ", 230, 350); } g.dispose(); } @Override public void actionPerformed(ActionEvent e) { timer.start(); if (play) { if (new Rectangle(ballposX, ballposY, 20, 20).intersects(new Rectangle(playerX, 550, 100, 8))) { //블록 만들기 ballYdir = -ballYdir; } A: for (int i = 0; i < map.map.length; i++) { for (int j = 0; j < map.map[0].length; j++) { if (map.map[i][j] > 0) { int brickX = j * map.brickWidth + 80; int brickY = i * map.brickHeight + 50; int brickWidth = map.brickWidth; int brickHeight = map.brickHeight; Rectangle rect = new Rectangle(brickX, brickY, brickWidth, brickHeight); Rectangle ballRect = new Rectangle(ballposX, ballposY, 20, 20); Rectangle brickRect = rect; if (ballRect.intersects(brickRect)) { map.setBrickValue(0, i, j); totalBricks--; score += 5; if (ballposX + 19 <= brickRect.x || ballposX + 1 >= brickRect.x + brickRect.width) { ballXdir = -ballXdir; } else { ballYdir = -ballYdir; } break A; }}}} ballposX += ballXdir; ballposY += ballYdir; if (ballposX < 0) { ballXdir = -ballXdir; } if (ballposY < 0) { ballYdir = -ballYdir; } if (ballposX > 670) { ballXdir = -ballXdir; } } repaint(); } @Override public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_RIGHT) { if (playerX >= 600) { playerX = 600; } else { moveRight(); } } if (e.getKeyCode() == KeyEvent.VK_LEFT) { if (playerX < 10) { playerX = 10; } else { moveLeft(); } } if (e.getKeyCode() == KeyEvent.VK_ENTER) { if (!play) { play = true; ballposX = 120; ballposY = 350; ballXdir = -1; ballYdir = -2; score = 0; totalBricks = 21; map = new Mapgenerator(3, 7); repaint(); } } } public void moveRight() { play = true; playerX += 20; } public void moveLeft() { play = true; playerX -= 20; } @Override public void keyReleased(KeyEvent e) { // TODO Auto-generated method stub } @Override public void keyTyped(KeyEvent e) { // TODO Auto-generated method stub } }
Mapgenerator.java —블록들과 공 위치 지정&표시
package brickBracker; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Graphics2D; public class Mapgenerator { public int map[][]; public int brickWidth; public int brickHeight; public Mapgenerator(int row, int col) { map = new int[row][col]; for(int i=0; i<map.length; i++) { for(int j=0; j<map[0].length; j++) { map[i][j] = 1; } } brickWidth = 540/col; brickHeight = 150/row; } public void draw(Graphics2D g) { for(int i=0; i<map.length; i++) { for(int j=0; j<map[0].length; j++) { if(map[i][j]> 0) { g.setColor(Color.white); g.fillRect(j*brickWidth + 80, i*brickWidth + 50, brickWidth, brickHeight); g.setStroke(new BasicStroke(3)); g.setColor(Color.black); g.drawRect(j*brickWidth + 80, i*brickWidth + 50, brickWidth, brickHeight); } } } } public void setBrickValue(int value, int row, int col) { map[row][col] = value; } }
2. 테트리스
package Tetris; import java.awt.Color; import java.awt.Graphics; import java.awt.Point; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.util.ArrayList; import java.util.Collections; import javax.swing.JFrame; import javax.swing.JPanel; public class Tetirs extends JPanel { private static final long serialVersionUID = -8715353373678321308L; private final Point[][][] Tetraminos = { // I-Piece { { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(3, 1) }, { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(1, 3) }, { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(3, 1) }, { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(1, 3) } }, // J-Piece { { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(2, 0) }, { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(2, 2) }, { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(0, 2) }, { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(0, 0) } }, // L-Piece { { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(2, 2) }, { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(0, 2) }, { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(0, 0) }, { new Point(1, 0), new Point(1, 1), new Point(1, 2), new Point(2, 0) } }, // O-Piece { { new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1) }, { new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1) }, { new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1) }, { new Point(0, 0), new Point(0, 1), new Point(1, 0), new Point(1, 1) } }, // S-Piece { { new Point(1, 0), new Point(2, 0), new Point(0, 1), new Point(1, 1) }, { new Point(0, 0), new Point(0, 1), new Point(1, 1), new Point(1, 2) }, { new Point(1, 0), new Point(2, 0), new Point(0, 1), new Point(1, 1) }, { new Point(0, 0), new Point(0, 1), new Point(1, 1), new Point(1, 2) } }, // T-Piece { { new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(2, 1) }, { new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(1, 2) }, { new Point(0, 1), new Point(1, 1), new Point(2, 1), new Point(1, 2) }, { new Point(1, 0), new Point(1, 1), new Point(2, 1), new Point(1, 2) } }, // Z-Piece { { new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(2, 1) }, { new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(0, 2) }, { new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(2, 1) }, { new Point(1, 0), new Point(0, 1), new Point(1, 1), new Point(0, 2) } } }; private final Color[] tetraminoColors = { Color.cyan, Color.blue, Color.orange, Color.yellow, Color.green, Color.pink, Color.red }; private Point pieceOrigin; private int currentPiece; private int rotation; private ArrayList<Integer> nextPieces = new ArrayList<Integer>(); private long score; private Color[][] well; // Creates a border around the well and initializes the dropping piece private void init() { well = new Color[12][24]; for (int i = 0; i < 12; i++) { for (int j = 0; j < 23; j++) { if (i == 0 || i == 11 || j == 22) { well[i][j] = Color.GRAY; } else { well[i][j] = Color.BLACK; } } } newPiece(); } // Put a new, random piece into the dropping position public void newPiece() { pieceOrigin = new Point(5, 2); rotation = 0; if (nextPieces.isEmpty()) { Collections.addAll(nextPieces, 0, 1, 2, 3, 4, 5, 6); Collections.shuffle(nextPieces); } currentPiece = nextPieces.get(0); nextPieces.remove(0); } // Collision test for the dropping piece private boolean collidesAt(int x, int y, int rotation) { for (Point p : Tetraminos[currentPiece][rotation]) { if (well[p.x + x][p.y + y] != Color.BLACK) { return true; } } return false; } // Rotate the piece clockwise or counterclockwise public void rotate(int i) { int newRotation = (rotation + i) % 4; if (newRotation < 0) { newRotation = 3; } if (!collidesAt(pieceOrigin.x, pieceOrigin.y, newRotation)) { rotation = newRotation; } repaint(); } // Move the piece left or right public void move(int i) { if (!collidesAt(pieceOrigin.x + i, pieceOrigin.y, rotation)) { pieceOrigin.x += i; } repaint(); } // Drops the piece one line or fixes it to the well if it can't drop public void dropDown() { if (!collidesAt(pieceOrigin.x, pieceOrigin.y + 1, rotation)) { pieceOrigin.y += 1; } else { fixToWell(); } repaint(); } // Make the dropping piece part of the well, so it is available for // collision detection. public void fixToWell() { for (Point p : Tetraminos[currentPiece][rotation]) { well[pieceOrigin.x + p.x][pieceOrigin.y + p.y] = tetraminoColors[currentPiece]; } clearRows(); newPiece(); } public void deleteRow(int row) { for (int j = row-1; j > 0; j--) { for (int i = 1; i < 11; i++) { well[i][j+1] = well[i][j]; } } } // Clear completed rows from the field and award score according to // the number of simultaneously cleared rows. public void clearRows() { boolean gap; int numClears = 0; for (int j = 21; j > 0; j--) { gap = false; for (int i = 1; i < 11; i++) { if (well[i][j] == Color.BLACK) { gap = true; break; } } if (!gap) { deleteRow(j); j += 1; numClears += 1; } } switch (numClears) { case 1: score += 100; break; case 2: score += 300; break; case 3: score += 500; break; case 4: score += 800; break; } } // Draw the falling piece private void drawPiece(Graphics g) { g.setColor(tetraminoColors[currentPiece]); for (Point p : Tetraminos[currentPiece][rotation]) { g.fillRect((p.x + pieceOrigin.x) * 26, (p.y + pieceOrigin.y) * 26, 25, 25); } } @Override public void paintComponent(Graphics g) { // Paint the well g.fillRect(0, 0, 26*12, 26*23); for (int i = 0; i < 12; i++) { for (int j = 0; j < 23; j++) { g.setColor(well[i][j]); g.fillRect(26*i, 26*j, 25, 25); } } // Display the score g.setColor(Color.WHITE); g.drawString("" + score, 19*12, 25); // Draw the currently falling piece drawPiece(g); } public static void main(String[] args) { JFrame f = new JFrame("Tetris 태인"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setSize(12*26+10, 26*23+25); f.setVisible(true); final Tetirs game = new Tetirs(); game.init(); f.add(game); // Keyboard controls f.addKeyListener(new KeyListener() { public void keyTyped(KeyEvent e) { } public void keyPressed(KeyEvent e) { switch (e.getKeyCode()) { case KeyEvent.VK_UP: game.rotate(-1); break; case KeyEvent.VK_DOWN: game.rotate(+1); break; case KeyEvent.VK_LEFT: game.move(-1); break; case KeyEvent.VK_RIGHT: game.move(+1); break; case KeyEvent.VK_SPACE: game.dropDown(); game.score += 1; break; } } public void keyReleased(KeyEvent e) { } }); // Make the falling piece drop every second new Thread() { @Override public void run() { while (true) { try { Thread.sleep(1000); game.dropDown(); } catch ( InterruptedException e ) {} } } }.start(); } }
jar file로 export하는 법
File -> Export -> JAVA -> Runnable JAR file -> Next ->java파일 선택 -> 경로지정 -> next
댓글