- 论坛徽章:
- 0
|
最近想学习学习JAVA,把一些练习内容贴出来,大家分享一下。
下面这个代码,比较粗糙。有兴趣的可以copy & paste 测试下。Thanks for any comment.
- package com.ftinternet;
- import java.awt.BorderLayout;
- import java.awt.Color;
- import java.awt.FlowLayout;
- import java.awt.GridLayout;
- import java.awt.event.KeyAdapter;
- import java.awt.event.KeyEvent;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.util.HashMap;
- import java.util.LinkedList;
- import java.util.Random;
- import java.util.Timer;
- import java.util.TimerTask;
- import javax.swing.JButton;
- import javax.swing.JComboBox;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- class SnakePoint extends JButton {
- private static final long serialVersionUID = 1L;
- public final static int SOBJECT_NORMAL = 0;
- public final static int SOBJECT_GIFT = 1;
-
- private int XPoint = -1;
- private int YPoint = -1;
- private int ObjectType = SOBJECT_NORMAL;
-
- public SnakePoint(int XPoint, int YPoint) {
- this.XPoint = XPoint;
- this.YPoint = YPoint;
- setText(" ");
- reset();
- }
-
- public void reset() {
- setBackground(Color.lightGray);
- ObjectType = SOBJECT_NORMAL;
- }
-
- public void SetPointType(int aPointType) {
- ObjectType = aPointType;
-
- switch(ObjectType) {
- case SOBJECT_GIFT:
- setBackground(Color.green);
- }
- }
-
- public int GetPointType() {
- return ObjectType;
- }
-
- public void SetIsSnakeBody(boolean IsBody) {
- if (IsBody)
- setBackground(Color.red);
- else
- setBackground(Color.lightGray);
- }
-
- public int GetXPoint() { return XPoint; }
- public int GetYPoint() { return YPoint; }
- }
- public class Snake extends JFrame {
- private static final long serialVersionUID = -7042297423926026518L;
- private int SnakeMaxLength = 10;
- private int SnakeMapWidth = 30;
- private int SnakeMapHeight = 30;
-
- JPanel pnlControl = new JPanel();
- JPanel pnlView = new JPanel();
- JLabel lbScore = new JLabel("0");
- JComboBox cbxGameSpeedLvl = new JComboBox(new String[] {"Easy", "Normal", "Hard"});
-
- private HashMap<String, SnakePoint> SnakeMap = new HashMap<String, SnakePoint>();
- LinkedList<SnakePoint> SnakeBody = new LinkedList<SnakePoint>();
- private boolean GameActive = false;
- private final Timer timer = new Timer();
- private final static int MOVE_STAY = -1;
- private final static int MOVE_EAST = 0;
- private final static int MOVE_SOUTH = 1;
- private final static int MOVE_WEST = 2;
- private final static int MOVE_NORTH = 3;
-
- private int MoveDirection = MOVE_STAY;
- private int NextDirection = MOVE_STAY;
-
- private String GetSnakePointCode(int XPoint, int YPoint) {
- return "" + XPoint + "-" + YPoint;
- }
-
- private String GetSnakePointCode(int Index) {
- return GetSnakePointCode(Index % SnakeMapWidth, Index / SnakeMapWidth);
- }
- private SnakePoint GetSnakePoint(int XPoint, int YPoint) {
- return (SnakePoint) SnakeMap.get(GetSnakePointCode(XPoint, YPoint));
- }
- private SnakePoint GetSnakePoint(int Index) {
- return (SnakePoint) SnakeMap.get(GetSnakePointCode(Index));
- }
- public Snake(int aWidth, int aHeight, int aMaxLength) {
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- setTitle("Snake 1.0");
-
- SnakeMapWidth = aWidth > 10 ? aWidth : 10;
- SnakeMapHeight = aHeight > 10 ? aHeight : 10;
- SnakeMaxLength = aMaxLength;
-
- getContentPane().add(pnlControl, BorderLayout.NORTH);
- pnlControl.setLayout(new FlowLayout(FlowLayout.LEFT, 30, 2));
-
- pnlControl.add(lbScore);
- JButton bRestart = new JButton("Start");
- pnlControl.add(bRestart);
- bRestart.addMouseListener(new MouseAdapter() {
- public void mouseClicked(MouseEvent ae) {
- InitGame();
- }
- });
- JButton bPause = new JButton("Pause");
- pnlControl.add(bPause);
- bPause.addMouseListener(new MouseAdapter() {
- public void mouseClicked(MouseEvent ae) {
- if (SnakeBody.size() < 1) return;
- GameActive = !GameActive;
- GetSnakePoint(0).grabFocus();
-
- JButton mbSender = (JButton) ae.getSource();
- if (GameActive)
- mbSender.setText("Pause");
- else
- mbSender.setText("Continue");
- }
- });
-
- pnlControl.add(new JLabel("Move Speed:"));
- pnlControl.add(cbxGameSpeedLvl);
- cbxGameSpeedLvl.setSelectedIndex(1);
- getContentPane().add(pnlView, BorderLayout.CENTER);
- pnlView.setLayout(new GridLayout(SnakeMapHeight, SnakeMapWidth));
- for (int h = 0; h < SnakeMapHeight; h++)
- for (int w = 0; w < SnakeMapWidth; w++)
- {
- SnakePoint aPoint = new SnakePoint(w, h);
- pnlView.add(aPoint);
- SnakeMap.put(GetSnakePointCode(w, h), aPoint);
- aPoint.addKeyListener(new KeyAdapter() {
- public void keyPressed(KeyEvent e) {
- switch (e.getKeyCode())
- {
- case KeyEvent.VK_W:
- case KeyEvent.VK_UP:
- SetMoveDirection(MOVE_NORTH);
- break;
- case KeyEvent.VK_S:
- case KeyEvent.VK_DOWN:
- SetMoveDirection(MOVE_SOUTH);
- break;
- case KeyEvent.VK_A:
- case KeyEvent.VK_LEFT:
- SetMoveDirection(MOVE_WEST);
- break;
- case KeyEvent.VK_D:
- case KeyEvent.VK_RIGHT:
- SetMoveDirection(MOVE_EAST);
- break;
- }
- }
- });
- }
- timer.schedule(new TimerTask() {
- public void run() {
- if (!GameActive) return;
- UpdateSnake();
- }
- }, 0, 50);
- pack();
- }
- private void GameOver(boolean Active)
- {
- GameActive = false;
- }
-
- protected void InitGame() {
- SnakeBody.clear();
-
- for (int i = 0; i < SnakeMapWidth * SnakeMapHeight; i++)
- GetSnakePoint(i).reset();
- SnakeBody.add(GetSnakePoint(SnakeMapWidth / 2, SnakeMapHeight / 2));
- SnakeBody.add(GetSnakePoint(SnakeMapWidth / 2 - 1, SnakeMapHeight / 2));
- SnakeBody.add(GetSnakePoint(SnakeMapWidth / 2 - 2, SnakeMapHeight / 2));
-
- RefreshSnake();
- CreateGift();
- lbScore.setText("0");
- GetSnakePoint(0).grabFocus();
- GameActive = true;
- }
-
- protected void UpdateScore(int aIncrement) {
- lbScore.setText("" + (Integer.parseInt(lbScore.getText()) + aIncrement));
-
- }
- protected synchronized void RefreshSnake() {
- for (int i = 0; i < SnakeBody.size(); i++)
- ((SnakePoint) SnakeBody.get(i)).SetIsSnakeBody(true);
- }
-
- protected synchronized void SetMoveDirection(int aDirection)
- {
- if (Math.abs(aDirection - MoveDirection) == 2) return;
- NextDirection = aDirection;
- }
-
- protected void CreateGift() {
- SnakePoint aGift = null;
- Random rand = new Random();
- while (aGift == null) {
- aGift = GetSnakePoint(rand.nextInt(SnakeMapWidth * SnakeMapHeight));
- if (SnakeBody.indexOf(aGift) >= 0) aGift = null;
- }
- aGift.SetPointType(SnakePoint.SOBJECT_GIFT);
- }
-
- private int RunTimeClock = 0;
- protected boolean CheckSpeedLvl() {
- if (++RunTimeClock > cbxGameSpeedLvl.getItemCount() - cbxGameSpeedLvl.getSelectedIndex() - 1) {
- RunTimeClock = 0;
- return true;
- }
- return false;
- }
- protected synchronized void UpdateSnake() {
- if (!GameActive || !CheckSpeedLvl()) return;
-
- SnakePoint SnakeHead = (SnakePoint) SnakeBody.get(0);
- int nXPoint = SnakeHead.GetXPoint();
- int nYPoint = SnakeHead.GetYPoint();
-
- if (NextDirection != MoveDirection) MoveDirection = NextDirection;
- switch(MoveDirection)
- {
- case MOVE_EAST:
- nXPoint = nXPoint == SnakeMapWidth - 1 ? 0 : nXPoint + 1;
- break;
- case MOVE_SOUTH:
- nYPoint = nYPoint == SnakeMapHeight - 1 ? 0 : nYPoint + 1;
- break;
- case MOVE_WEST:
- nXPoint = nXPoint == 0 ? SnakeMapWidth - 1 : nXPoint - 1;
- break;
- case MOVE_NORTH:
- nYPoint = nYPoint == 0 ? SnakeMapHeight - 1 : nYPoint - 1;
- break;
- default:
- return;
- }
- SnakeHead = GetSnakePoint(nXPoint, nYPoint);
-
- if (SnakeBody.indexOf(SnakeHead) > 1) {
- GameOver(false);
- return;
- }
-
- SnakeBody.addFirst(SnakeHead);
- if (SnakeHead.GetPointType() == SnakePoint.SOBJECT_GIFT) {
- UpdateScore(10);
- SnakeHead.SetPointType(SnakePoint.SOBJECT_NORMAL);
- CreateGift();
- if (SnakeBody.size() >= SnakeMaxLength)
- ((SnakePoint) SnakeBody.removeLast()).SetIsSnakeBody(false);
- }
- else
- ((SnakePoint) SnakeBody.removeLast()).SetIsSnakeBody(false);
-
- RefreshSnake();
- }
- public static void main(String[] args) {
- Snake aGame = new Snake(20, 20, 50);
- aGame.setVisible(true);
- }
- }
复制代码 |
|