溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么用Java實現(xiàn)貪吃蛇游戲

發(fā)布時間:2021-12-22 15:59:24 來源:億速云 閱讀:129 作者:iii 欄目:編程語言

本篇內(nèi)容介紹了“怎么用Java實現(xiàn)貪吃蛇游戲”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

1、執(zhí)行程序:程序啟動的入口

package cn.hncu;

public?class?GreedySnake {

public static void main(String[] args) {

Model model=new Model(80,?50);

Control control=new Control(model);

View view=new View(model,control);
 

model.addObserver(view);

(new Thread(model)).start();

}

}

控制類:主要進(jìn)行鍵盤的按鍵收集和傳遞

package cn.hncu;  

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;

public class Control implements KeyListener{

Model model;

public Control(Model?model){

this.model=model;

}

public void keyPressed(KeyEvent?e){

//此方法收集按鍵

int key=e.getKeyCode();

if (model.running) {

switch (key) {

case KeyEvent.VK_UP:

model.changeDirection(model.up);

break;

case KeyEvent.VK_DOWN:

model.changeDirection(model.down);

break;

case KeyEvent.VK_LEFT:

model.changeDirection(model.left);

break;

case KeyEvent.VK_RIGHT:

model.changeDirection(model.right);

break;

default:

break;

}

}

if (key==KeyEvent.VK_ENTER) {

model.reset();

}

}

public void keyReleased(KeyEvent?e) {

}

public void keyTyped(KeyEvent e) {

}

}

模型類:創(chuàng)建蛇身和蛇的運動方式的實現(xiàn),用到了線程。

import java.util.Arrays;

import java.util.LinkedList;

import java.util.Observable;

import java.util.Random;

import javax.swing.JOptionPane;

public class Model extends Observable implements Runnable{

public static final int left=1;

public static final int up=2;

public static final int right=3;

public?static?final?int?down=4;

public?boolean?coordinate[][];//用這個來當(dāng)做界面的坐標(biāo)

public?LinkedList?node=new?LinkedList();

public?int?direction=2;

boolean?running=false;

public?int?maxX,maxY;

Node?food;

public?int?sleeptime=200;

public?Model(int?maxX,int?maxY){

this.maxX=maxX;

this.maxY=maxY;

reset();

}

public?void?reset()?{

direction=this.up;

sleeptime=200;

coordinate=new?boolean[maxX][];

for?(int?i?=?0;?i?<?maxX;?i++)?{

coordinate[i]=new?boolean[maxY];

Arrays.fill(coordinate[i],?false);

}

//initialize?the?Snake'body

int?initlenght=10;

node.clear();

for?(int?j?=?0;?j?<?initlenght;?j++)?{

int?x=maxX/2+j;

int?y=maxY/2;

node.addLast(new?Node(x,y));

coordinate[x][y]=true;

}

food=createFood();

coordinate[food.x][food.y]=true;

}

public?boolean?move(){

Node?n=(Node)node.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;

default:

break;

}

if?((x>=0&&x<maxX)&&(y>=0&&y<maxY))?{

if?(coordinate[x][y])?{

if?(x==food.x&&y==food.y)?{

node.addFirst(food);

if?(sleeptime>35)?{

sleeptime-=20;

}

food=createFood();

coordinate[food.x][food.y]=true;

return?true;

}else?{

return?false;

}

}else?{

node.addFirst(new?Node(x,y));

coordinate[x][y]=true;

n=(Node)node.getLast();

node.removeLast();

coordinate[n.x][n.y]=false;

return?true;

}

}

return?false;

}

public?void?changeDirection(int?newdir){

if?(direction!=newdir)?{

direction=newdir;

}

}

public?Node?createFood()?{

int?x=0,y=0;

do?{

Random?r?=?new?Random();

x?=?r.nextInt(maxX);

y?=?r.nextInt(maxY);

}?while?(coordinate[x][y]);

return?new?Node(x,?y);

}

public?void?run()?{

running=true;

while(running){

try?{

Thread.sleep(sleeptime);

}?catch?(Exception?e)?{

break;

}

if?(move())?{

setChanged();

notifyObservers();

}else?{

JOptionPane.showMessageDialog(null,?"Game?Over");

break;

}

}

}

}

class?Node{//創(chuàng)建蛇身

public?int?x,y;

public?Node(int?x,int?y){

this.x=x;

this.y=y;

}

}

界面層:展現(xiàn)給用戶看的,用圖形界面展現(xiàn)蛇的運動

[java]?view plain?copy

import?java.awt.BorderLayout;

import?java.awt.Canvas;

import?java.awt.Color;

import?java.awt.Container;

import?java.awt.Graphics;

import?java.util.Iterator;

import?java.util.LinkedList;

import?java.util.Observable;

import?java.util.Observer;

import?javax.swing.JFrame;

import?javax.swing.JLabel;

import?javax.swing.JPanel;

public?class?View?extends?JFrame?implements?Observer{

Control?control;

Model?model;

Canvas?canvas;

public?static?final?int?canvasWidth=800,canvasHeight=500;

public?static?final?int?nodeWidth=10,nodeHeight=10;

public?View(Model?model,Control?control){

super("GreedySnake");

this.control=control;

this.model=model;

this.setLocation(400,?300);

this.setResizable(false);

this.setDefaultCloseOperation(EXIT_ON_CLOSE);

canvas=new?Canvas();

canvas.setSize(canvasWidth+1,?canvasHeight+1);

canvas.addKeyListener(control);

this.add(canvas,BorderLayout.NORTH);

JPanel?panel=new?JPanel();

this.add(panel,BorderLayout.SOUTH);

JLabel?label=new?JLabel("Enter?for?restart");

panel.add(label);

this.pack();

this.addKeyListener(control);??

this.setVisible(true);

}

public?void?repaint()?{

Graphics?g=canvas.getGraphics();

//    draw?background

g.setColor(Color.white);

g.fillRect(0,?0,?canvasWidth,?canvasHeight);

//      draw snake

g.setColor(Color.red);

LinkedList?node=model.node;

Iterator?it=node.iterator();

while(it.hasNext()){

Node?n=(Node)it.next();

drawNode(g,n);

}

//        draw food

g.setColor(Color.black);

Node?n=model.food;

drawNode(g,n);

}

private?void?drawNode(Graphics?g,?Node?n)?{

g.fillOval(n.x*nodeWidth,?n.y*nodeHeight,?nodeWidth,?nodeHeight);

}

public?void?update(Observable?o,?Object?arg){

repaint();

}

“怎么用Java實現(xiàn)貪吃蛇游戲”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI