نستكمل معاً لعبة tetries التى تكلمنا عنها فى مشاركة سابقة .
هنا نحن أمام مرحلة تحريك الشكل - شكل المربع - على المسار الأفقى أثناء حركته رأسياً لإسفل .
كتبتُ دالة الحركة الأفقية و هى كالتالى :
--------------------------------------
العمود المبدأى الذى يقف عليه الشكل قيمته = 4
و بما أن الشكل مربع إذن هو يأخذ أربع خانات . خانتين فى الصف الأول و خانتين فى الصف الثانى .
فإذا أردنا الحركة يميناً علينا :
بزيادة قيمة العمود مرة بمقدار 2 أى 4+2 - حتى تُرسم أول خانة فى العمود ذات القيمة التى = 6
ولرسم الخانة الثانية نزد قيمة العمود بمقدار 3 أى 4+3 - حتى تُرسم ثانى خانة فى العمود ذات القيمة التى = 7
ثم نزيد قيمة الصف لرسم الخانتين الآخرتين فى نفس الأعمدة.
و هكذا إذا أردنا الحركة يساراً و لكن نطرح .
هل تفكيرى سليم أم لا ؟!
و بكل بساطة استعملنا ال KeyListener لجعل الأسم اليمنية و اليسارية تُحرِك الشكل عند الضغط عليها
و لكن لم تستجب لإى شئ
انظروا معنا و تأملوا المشهد - الدالة التى أضيفت فى الفئة MainFrameTetries
اسمها moveSquareHorizontal()
------------------------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
enum Action {
START, HELP, EXIT;
}
class MainGame extends JFrame {
private JPanel mainPanel, buttonsPanel;
private Box mainButtonsBox;
private JButton[] buttons;
private Action[] buttonsNames;
private Action action;
private MainFrameTetries mainFrameTetries;
public MainGame() {
createContainer();
buttonsNames = Action.values();
for (int i = 0; i < buttonsNames.length; ++i) {
action = buttonsNames;
createButtons(action);
}
getContentPane().add(mainPanel);
//////////////////////////////////////////////////
}
public static void main(String[] args) {
MainGame mainGame = new MainGame();
mainGame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainGame.setSize(new Dimension(600, 300));
mainGame.setLocationRelativeTo(null);
mainGame.setExtendedState(MAXIMIZED_BOTH);
mainGame.setVisible(true);
}
private void createContainer() { // for createing container
/////////////////////MainPanel/////////////////////////////
mainPanel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(30, 30, 30, 30);
//////////////////////Box&Panel fro buttons/////////////////////////////////////
mainButtonsBox = Box.createHorizontalBox();
buttonsPanel = new JPanel(new GridLayout(3, 1));
mainButtonsBox.setBorder(BorderFactory.createLineBorder(Color.BLUE, 5));
mainButtonsBox.setPreferredSize(new Dimension(400, 200));
mainButtonsBox.add(buttonsPanel);
///////////////////////////////////////////////////////////////////////////////
mainPanel.add(mainButtonsBox, c);
//////////////////////////////////////////////////
mainFrameTetries = new MainFrameTetries(this);
mainFrameTetries.setVisible(false);
mainPanel.add(mainFrameTetries);
}
private void createButtons(Action action) {
buttons = new JButton[buttonsNames.length];
for (int i = 0; i < 1; ++i) {
buttons = new JButton(action.name());
buttons.addActionListener(listenerFor(action));
buttonsPanel.add(buttons);
}
}
private ActionListener listenerFor(Action action) {
switch (action) {
case START: // START BUTTON
return new ActionListener() {
public void actionPerformed(ActionEvent e) {
mainFrameTetries.setVisible(true);
mainButtonsBox.setVisible(false);
}
};
case HELP: // HELP BUTTON
return new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "HELP");
}
};
case EXIT: // EXIT BUTTON
return new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "EXIT");
}
};
}
return null;
}
}
-----------------------------------------------------------------------------------------------
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Random;
import javax.swing.BorderFactory;
import javax.swing.Timer;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
class MainFrameTetries extends JPanel implements ActionListener {
private int x, y;
private int currentRow = 0, currentColumn = 4;
private Timer timer;
private int rows = 20, columns = 10;
private JPanel[][] panel;
private Random random;
private Color randomColor;
private MyKeyListener myKeyListener;
public MainFrameTetries(MainGame mainGame) {
myKeyListener = new MyKeyListener();
random = new Random();
createMainPanel(mainGame);
creatwTetrisPanel();
randomColor = randomColor();
timer = new Timer(600, this);
timer.start();
}
private void createMainPanel(MainGame mainGame) {
setPreferredSize(new Dimension(200, 400));
setSize(getPreferredSize());
setLayout(new GridLayout(rows, columns));
System.out.println(getPreferredSize().height);
x = mainGame.getWidth() / 2 - getWidth() / 2;
y = mainGame.getHeight() / 2 - getHeight() / 2;
setLocation(x, y);
}
private void creatwTetrisPanel() {
panel = new JPanel[rows][columns];
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < columns; ++j) {
panel[j] = new JPanel();
panel[j].setBackground(Color.WHITE);
panel[j].setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
add(panel[j]);
}
}
}
public void actionPerformed(ActionEvent e) {
moveSquareVertical();
}
private Color randomColor() {
return new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
}
public void clearPanel() {
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < columns; ++j) {
panel[j].setBackground(Color.WHITE);
}
}
}
private void moveSquareVertical() {
if (currentRow < rows - 1) {
clearPanel();
panel[currentRow][currentColumn].setBackground(randomColor);
panel[currentRow][currentColumn + 1].setBackground(randomColor);
panel[currentRow + 1][currentColumn].setBackground(randomColor);
panel[currentRow + 1][currentColumn + 1].setBackground(randomColor);
} else {
timer.stop();
}
currentRow++;
}
private void moveSquareHorizontal() {
if (currentColumn > columns - 1) {
clearPanel();
panel[currentRow][currentColumn + 2].setBackground(randomColor);
panel[currentRow][currentColumn + 3].setBackground(randomColor);
panel[currentRow + 1][currentColumn + 2].setBackground(randomColor);
panel[currentRow + 1][currentColumn + 3].setBackground(randomColor);
currentColumn++;
} else {
clearPanel();
panel[currentRow][currentColumn - 1].setBackground(randomColor);
panel[currentRow][currentColumn - 2].setBackground(randomColor);
panel[currentRow + 1][currentColumn - 1].setBackground(randomColor);
panel[currentRow + 1][currentColumn - 2].setBackground(randomColor);
currentColumn--;
}
}
private class MyKeyListener extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
super.keyPressed(e);
int location = e.getKeyLocation();
if (location == KeyEvent.KEY_LOCATION_RIGHT || location == KeyEvent.KEY_LOCATION_LEFT) {
moveSquareHorizontal();
}
}
// @Override
// public void keyReleased(KeyEvent e) {
// super.keyReleased(e);
// }
}
}ال code مرفق أيضاً
----------------------------------------
شكراً ،،،،


