Java實(shí)現(xiàn)貪吃蛇_(dá)第1頁
Java實(shí)現(xiàn)貪吃蛇_(dá)第2頁
Java實(shí)現(xiàn)貪吃蛇_(dá)第3頁
Java實(shí)現(xiàn)貪吃蛇_(dá)第4頁
Java實(shí)現(xiàn)貪吃蛇_(dá)第5頁
已閱讀5頁,還剩1頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、項(xiàng)目效果圖:源代碼:import java.awt.Color;import ;import java.awt.Dimension;import java.awt.Graphics;import ;import java.awt.Point;import java.awt.event.ComponentAdapter;import java.awt.event.ComponentEvent;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import ;import ;import ;import java.awt

2、.event.WindowAdapter;import java.awt.event.WindowEvent;import ;import java.util.ArrayList;import java.util.HashMap;import ;import java.util.*;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;/* author: 蔣冰*/public class KeyBoard extends JFra

3、meprivate int direction = 0;private int judge = 0;private Panel panel = new Panel();private RectLabel label = new RectLabel1200;private HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();private ArrayList<RectLabel> list = new ArrayList<RectLabel>(); private Poin

4、t point = new Point1200;private int targetX = 0;private int targetY = 0;private static int goal = 3;private int speed;private RectLabel targetLabel = new RectLabel();private static KeyBoard frame = new KeyBoard();public static void main(String args)frame.setVisible(true);public KeyBoard() super(); a

5、ddWindowListener(new WindowAdapter() Overridepublic void windowActivated(WindowEvent arg0) map.put(KeyEvent.VK_RIGHT, KeyEvent.VK_LEFT); map.put(KeyEvent.VK_LEFT, KeyEvent.VK_RIGHT); map.put(KeyEvent.VK_UP, KeyEvent.VK_DOWN); map.put(KeyEvent.VK_DOWN, KeyEvent.VK_UP); map.put(0, 0); map.put(KeyEvent

6、.VK_G, 0); Random ra = new Random(); targetX = ra.nextInt(30)*10 + 50; targetY = ra.nextInt(20)*10 + 50; targetLabel.setLocation(targetX, targetY); ); setBackground(Color.gray); setLayout(null);setBounds(400,200,400,300);addKeyListener(new KeyAdapter()public void keyPressed(KeyEvent e)if(e.getKeyCod

7、e() = map.get(direction)return;elsedirection = e.getKeyCode(););for(int i=1; i<goal; i+)labeli = new RectLabel();labeli.setBounds(-10,-10, 10, 10);list.add(labeli);panel.setBounds(-10, -10, 10, 10);panel.addComponentListener(new ComponentAdapter() Overridepublic void componentMoved(ComponentEvent

8、 arg0) point0 = panel.getLocation();if(Math.abs(point0.x-targetX)<5&&Math.abs(point0.y-targetY)<5) Random ra = new Random(); targetX = ra.nextInt(30)*10 + 50; targetY = ra.nextInt(20)*10 + 50; while(true) for(int i=1; i<goal-1; i+) Point p = labeli.getLocation(); if(p.x=targetX)&

9、;&(p.y=targetY) break; else judge +; if(judge=goal-2) break; else judge=0; targetX = ra.nextInt(30)*10 + 50; targetY = ra.nextInt(20)*10 + 50; targetLabel.setLocation(targetX, targetY); labelgoal = new RectLabel(); labelgoal.setBounds(-10, -10, 10, 10); pointgoal-1 = new Point(); list.add(labelg

10、oal); goal+; for(int i=1; i<list.size(); i+)pointi = labeli.getLocation();labeli.setLocation(pointi-1);frame.add(labeli);labellist.size().setLocation(pointlist.size()-1);frame.add(labellist.size();for(int i=3; i<list.size(); i+)if(Math.abs(label1.getLocation().x-labeli.getLocation().x)<=1&a

11、mp;&Math.abs(label1.getLocation().y-labeli.getLocation().y)<=1)direction = KeyEvent.VK_G;);targetLabel.setBounds(200, 100,10,10);add(targetLabel);add(panel);class Panel extends JPanel implements Runnableprivate int flag = 0;private int x = 0;private int y = 0;public Panel()setPreferredSize(ne

12、w Dimension(10, 10);new Thread(this).start();Overridepublic void run() while(true)if(direction = KeyEvent.VK_RIGHT)if(x<=this.getParent().getWidth()-10)x+=10;setLocation(x,y);elsedirection = KeyEvent.VK_G;if(direction = KeyEvent.VK_G)JOptionPane.showMessageDialog(null, "您得了"+(goal-3)+&q

13、uot;分,"+"歡迎再次光臨!");System.exit(0);setLocation(-10,-10);if(direction = KeyEvent.VK_LEFT)if(x>=0)x-=10;setLocation(x,y);elsedirection = KeyEvent.VK_G;if(direction = KeyEvent.VK_DOWN)if(y<=this.getParent().getHeight()-10)y+=10;setLocation(x,y);elsedirection = KeyEvent.VK_G;if(direc

14、tion = KeyEvent.VK_UP)if(y>=0)y-=10;setLocation(x,y);elsedirection = KeyEvent.VK_G;tryif(goal<30) speed = goal;elsespeed = 30;Thread.sleep(400 - speed*10);catch(InterruptedException e)e.printStackTrace();repaint();class RectLabel extends JLabel private int r = 5;/ 小球半徑 private int width = r * 2;/ 球?qū)挾?private int height = r * 2;/ 球高度 private Color ballColor = Color.BLUE;/ 默認(rèn)顏

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論