有一個我們老師編寫的貪吃蛇程序 供你參考一下:
我們注重客戶提出的每個要求,我們充分考慮每一個細節(jié),我們積極的做好成都網(wǎng)站制作、成都做網(wǎng)站服務(wù),我們努力開拓更好的視野,通過不懈的努力,成都創(chuàng)新互聯(lián)贏得了業(yè)內(nèi)的良好聲譽,這一切,也不斷的激勵著我們更好的服務(wù)客戶。 主要業(yè)務(wù):網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)站設(shè)計,小程序制作,網(wǎng)站開發(fā),技術(shù)開發(fā)實力,DIV+CSS,PHP及ASP,ASP.Net,SQL數(shù)據(jù)庫的技術(shù)開發(fā)工程師。
GreedSnake.java (也是程序入口):
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Iterator;
import java.util.LinkedList;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class GreedSnake implements KeyListener {
JFrame mainFrame;
Canvas paintCanvas;
JLabel labelScore;// 計分牌
SnakeModel snakeModel = null;// 蛇
public static final int canvasWidth = 200;
public static final int canvasHeight = 300;
public static final int nodeWidth = 10;
public static final int nodeHeight = 10;
// ----------------------------------------------------------------------
// GreedSnake():初始化游戲界面
// ----------------------------------------------------------------------
public GreedSnake() {
// 設(shè)置界面元素
mainFrame = new JFrame("GreedSnake");
Container cp = mainFrame.getContentPane();
labelScore = new JLabel("Score:");
cp.add(labelScore, BorderLayout.NORTH);
paintCanvas = new Canvas();
paintCanvas.setSize(canvasWidth + 1, canvasHeight + 1);
paintCanvas.addKeyListener(this);
cp.add(paintCanvas, BorderLayout.CENTER);
JPanel panelButtom = new JPanel();
panelButtom.setLayout(new BorderLayout());
JLabel labelHelp;// 幫助信息
labelHelp = new JLabel("PageUp, PageDown for speed;", JLabel.CENTER);
panelButtom.add(labelHelp, BorderLayout.NORTH);
labelHelp = new JLabel("ENTER or R or S for start;", JLabel.CENTER);
panelButtom.add(labelHelp, BorderLayout.CENTER);
labelHelp = new JLabel("SPACE or P for pause", JLabel.CENTER);
panelButtom.add(labelHelp, BorderLayout.SOUTH);
cp.add(panelButtom, BorderLayout.SOUTH);
mainFrame.addKeyListener(this);
mainFrame.pack();
mainFrame.setResizable(false);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setVisible(true);
begin();
}
// ----------------------------------------------------------------------
// keyPressed():按鍵檢測
// ----------------------------------------------------------------------
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (snakeModel.running)
switch (keyCode) {
case KeyEvent.VK_UP:
snakeModel.changeDirection(SnakeModel.UP);
break;
case KeyEvent.VK_DOWN:
snakeModel.changeDirection(SnakeModel.DOWN);
break;
case KeyEvent.VK_LEFT:
snakeModel.changeDirection(SnakeModel.LEFT);
break;
case KeyEvent.VK_RIGHT:
snakeModel.changeDirection(SnakeModel.RIGHT);
break;
case KeyEvent.VK_ADD:
case KeyEvent.VK_PAGE_UP:
snakeModel.speedUp();// 加速
break;
case KeyEvent.VK_SUBTRACT:
case KeyEvent.VK_PAGE_DOWN:
snakeModel.speedDown();// 減速
break;
case KeyEvent.VK_SPACE:
case KeyEvent.VK_P:
snakeModel.changePauseState();// 暫停或繼續(xù)
break;
default:
}
// 重新開始
if (keyCode == KeyEvent.VK_R || keyCode == KeyEvent.VK_S
|| keyCode == KeyEvent.VK_ENTER) {
snakeModel.running = false;
begin();
}
}
// ----------------------------------------------------------------------
// keyReleased():空函數(shù)
// ----------------------------------------------------------------------
public void keyReleased(KeyEvent e) {
}
// ----------------------------------------------------------------------
// keyTyped():空函數(shù)
// ----------------------------------------------------------------------
public void keyTyped(KeyEvent e) {
}
// ----------------------------------------------------------------------
// repaint():繪制游戲界面(包括蛇和食物)
// ----------------------------------------------------------------------
void repaint() {
Graphics g = paintCanvas.getGraphics();
// draw background
g.setColor(Color.WHITE);
g.fillRect(0, 0, canvasWidth, canvasHeight);
// draw the snake
g.setColor(Color.BLACK);
LinkedList na = snakeModel.nodeArray;
Iterator it = na.iterator();
while (it.hasNext()) {
Node n = (Node) it.next();
drawNode(g, n);
}
// draw the food
g.setColor(Color.RED);
Node n = snakeModel.food;
drawNode(g, n);
updateScore();
}
// ----------------------------------------------------------------------
// drawNode():繪畫某一結(jié)點(蛇身或食物)
// ----------------------------------------------------------------------
private void drawNode(Graphics g, Node n) {
g.fillRect(n.x * nodeWidth, n.y * nodeHeight, nodeWidth - 1,
nodeHeight - 1);
}
// ----------------------------------------------------------------------
// updateScore():改變計分牌
// ----------------------------------------------------------------------
public void updateScore() {
String s = "Score: " + snakeModel.score;
labelScore.setText(s);
}
// ----------------------------------------------------------------------
// begin():游戲開始,放置貪吃蛇
你可以參考一下 每次遇到不會的都來求助是很不方便的
如果你想了解和學(xué)習(xí)更多的JAVA編程,成為一名真正的JAVA高手,你可以來這個裙,前面三個數(shù)是四二六 中間是三九六 后面是二八四
把這串數(shù)字連起來就可以了,這是一個高手的學(xué)習(xí)裙,在這里你可以學(xué)習(xí)到書上學(xué)不到的知識,還有大牛相互討論并指導(dǎo)你解答哦!
// ----------------------------------------------------------------------
void begin() {
if (snakeModel == null || !snakeModel.running) {
snakeModel = new SnakeModel(this, canvasWidth / nodeWidth,
this.canvasHeight / nodeHeight);
(new Thread(snakeModel)).start();
}
}
// ----------------------------------------------------------------------
// main():主函數(shù)
// ----------------------------------------------------------------------
public static void main(String[] args) {
GreedSnake gs = new GreedSnake();
}
}
Node.java:
public class Node {
int x;
int y;
Node(int x, int y) {
this.x = x;
this.y = y;
}
}
SnakeModel.java:
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Random;
import javax.swing.JOptionPane;
public class SnakeModel implements Runnable {
GreedSnake gs;
boolean[][] matrix;// 界面數(shù)據(jù)保存在數(shù)組里
LinkedList nodeArray = new LinkedList();
Node food;
int maxX;// 最大寬度
int maxY;// 最大長度
int direction = 2;// 方向
boolean running = false;
int timeInterval = 200;// 間隔時間(速度)
double speedChangeRate = 0.75;// 速度改變程度
boolean paused = false;// 游戲狀態(tài)
int score = 0;
int countMove = 0;
// UP和DOWN是偶數(shù),RIGHT和LEFT是奇數(shù)
public static final int UP = 2;
public static final int DOWN = 4;
public static final int LEFT = 1;
public static final int RIGHT = 3;
// ----------------------------------------------------------------------
// GreedModel():初始化界面
// ----------------------------------------------------------------------
public SnakeModel(GreedSnake gs, int maxX, int maxY) {
this.gs = gs;
this.maxX = maxX;
this.maxY = maxY;
matrix = new boolean[maxX][];
for (int i = 0; i maxX; ++i) {
matrix[i] = new boolean[maxY];
Arrays.fill(matrix[i], false);// 沒有蛇和食物的地區(qū)置false
}
// 初始化貪吃蛇
int initArrayLength = maxX 20 ? 10 : maxX / 2;
for (int i = 0; i initArrayLength; ++i) {
int x = maxX / 2 + i;
int y = maxY / 2;
nodeArray.addLast(new Node(x, y));
matrix[x][y] = true;// 蛇身處置true
}
food = createFood();
matrix[food.x][food.y] = true;// 食物處置true
}
// ----------------------------------------------------------------------
// changeDirection():改變運動方向
// ----------------------------------------------------------------------
public void changeDirection(int newDirection) {
if (direction % 2 != newDirection % 2)// 避免沖突
{
direction = newDirection;
}
}
// ----------------------------------------------------------------------
// moveOn():貪吃蛇運動函數(shù)
// ----------------------------------------------------------------------
public boolean moveOn() {
Node n = (Node) nodeArray.getFirst();
int x = n.x;
int y = n.y;
switch (direction) {
case UP:
y--;
break;
case DOWN:
y++;
break;
case LEFT:
x--;
break;
case RIGHT:
x++;
break;
}
if ((0 = x x maxX) (0 = y y maxY)) {
if (matrix[x][y])// 吃到食物或者撞到身體
{
if (x == food.x y == food.y)// 吃到食物
{
nodeArray.addFirst(food);// 在頭部加上一結(jié)點
// 計分規(guī)則與移動長度和速度有關(guān)
int scoreGet = (10000 - 200 * countMove) / timeInterval;
score += scoreGet 0 ? scoreGet : 10;
countMove = 0;
food = createFood();
matrix[food.x][food.y] = true;
return true;
} else
return false;// 撞到身體
} else// 什么都沒有碰到
{
nodeArray.addFirst(new Node(x, y));// 加上頭部
matrix[x][y] = true;
n = (Node) nodeArray.removeLast();// 去掉尾部
matrix[n.x][n.y] = false;
countMove++;
return true;
}
}
return false;// 越界(撞到墻壁)
}
// ----------------------------------------------------------------------
// run():貪吃蛇運動線程
// ----------------------------------------------------------------------
public void run() {
running = true;
while (running) {
try {
Thread.sleep(timeInterval);
} catch (Exception e) {
break;
}
if (!paused) {
if (moveOn())// 未結(jié)束
{
gs.repaint();
} else// 游戲結(jié)束
{
JOptionPane.showMessageDialog(null, "GAME OVER",
"Game Over", JOptionPane.INFORMATION_MESSAGE);
break;
}
}
}
running = false;
}
// ----------------------------------------------------------------------
// createFood():生成食物及放置地點
// ----------------------------------------------------------------------
private Node createFood() {
int x = 0;
int y = 0;
do {
Random r = new Random();
x = r.nextInt(maxX);
y = r.nextInt(maxY);
} while (matrix[x][y]);
return new Node(x, y);
}
// ----------------------------------------------------------------------
// speedUp():加快蛇運動速度
// ----------------------------------------------------------------------
public void speedUp() {
timeInterval *= speedChangeRate;
}
// ----------------------------------------------------------------------
// speedDown():放慢蛇運動速度
// ----------------------------------------------------------------------
public void speedDown() {
timeInterval /= speedChangeRate;
}
// ----------------------------------------------------------------------
// changePauseState(): 改變游戲狀態(tài)(暫?;蚶^續(xù))
// ----------------------------------------------------------------------
public void changePauseState() {
paused = !paused;
}
}
滿意請采納
最少是要求2個方格可以進行消除,至多沒有上線,可以連續(xù)多少個相投色的方格即可一次性消除多少個方格。
方塊消消樂,是以同色方格相連之后點擊進行消除,屬于休閑單機小游戲,消除過程中需要獲得通關(guān)要求分值方可繼續(xù)下一關(guān),失敗則需要重新從第一關(guān)開始挑戰(zhàn)。
小小樂作為休閑類游戲,以其清新的配色,簡單的方格界面,帶給用戶舒適的游戲體驗,過程中想要獲得高分,則需要有策略的調(diào)整幾種顏色方格連接消除位置。
Java實現(xiàn)貪吃蛇小游戲(附完整源碼)
JavaFX打磚塊游戲開發(fā) 第二課
javaFX寫的推箱子小游戲
使用JavaFx實現(xiàn)拼圖游戲
javafx官方范例代碼
javafx示例代碼
javaFx寫的卡牌游戲
javaFX寫的推箱子小游戲
游戲標簽。《開心消消樂》是一款休閑類游戲,在游玩時關(guān)卡顯示deslabel是該游戲出現(xiàn)錯誤,彈出的一種標簽,只需要將游戲退出重新登錄即可。
這屬于移動平臺的游戲軟件開發(fā),涉及到的技術(shù)環(huán)節(jié)很多,也很高端。移動平臺的基本開發(fā)環(huán)境就是JAVA,游戲軟件構(gòu)架就是C/S(客戶端/服務(wù)器)模式,需要分別設(shè)計服務(wù)器后臺程序,和客戶端前臺程序,客戶端前臺程序就是在手機里下載并運行的游戲APP。服務(wù)器端需要實現(xiàn)游戲數(shù)據(jù)庫,以及游戲邏輯、游戲數(shù)據(jù)模型,客戶端數(shù)據(jù)接口等??蛻舳诵枰獙崿F(xiàn)前臺美工、人機交互、物理模擬、人工智能等。開發(fā)工具方面,需要基本的開發(fā)平臺,如Android Studio,游戲開發(fā)套件等。
估計你想要的是動畫效果,應(yīng)該這樣:
new?Thread(){
public?void?run(){
//?動畫代碼:移動、漸變、縮放等
}
}.start();//?另啟線程,防止阻塞當初進程