Java贪吃蛇游戏
package snake.game;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Timer;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class SnakeGame implements ActionListener,KeyListener,MouseListener{
private long initTime ;
SnakeGame.InterClassSnake innerSnake;
boolean signal=false;
private Timer tim;
int douX=120,douY=120,direct;
private ArrayList<Point> snake = new ArrayList<Point>();
JFrame jf = new JFrame("贪吃蛇游戏");
JPanel mainPane = new JPanel();
//欢迎界面、开始界面、游戏界面
WelcomPanel wp = new WelcomPanel();
StartPanel sp = new StartPanel();
JPanel gamePane= new JPanel();
BallDemo bd ;
JButton [] btns = {new JButton("下一步"),new JButton("上一步"),new JButton("开始")};
Color[] yanse = {Color.black,Color.blue,Color.cyan,Color.green};
CardLayout gld= new CardLayout();
{
snake.add(new Point(90,150));
snake.add(new Point(90,120));
snake.add(new Point(90,90));
snake.add(new Point(90,60));
snake.add(new Point(90,30));
wp.setLayout(null);
wp.setBounds(0, 0, 500, 450);
btns[0].setMargin(new Insets(0,0,0,0));
btns[0].setBounds(350, 350, 50, 30);
wp.add(btns[0]);
sp.setLayout(null);
sp.setBounds(0, 0, 500, 450);
btns[1].setMargin(new Insets(0,0,0,0));
btns[1].setBounds(300, 350, 50, 30);
btns[2].setMargin(new Insets(0,0,0,0));
btns[2].setBounds(350, 350, 50, 30);
sp.add(btns[1]);
sp.add(btns[2]);
btns[0].addActionListener(this);
btns[1].addActionListener(this);
btns[2].addActionListener(this);
gamePane.addMouseListener(this);
jf.addKeyListener(this);
mainPane.setLayout(gld);
mainPane.add(wp,"1");
mainPane.add(sp, "2");
mainPane.add(gamePane, "3");
bd=new BallDemo(500,450,gamePane);
mainPane.setSize(500,450);
mainPane.setVisible(true);
}
public SnakeGame(){
innerSnake =this. new InterClassSnake();
jf.add(mainPane);
jf.setSize(500, 450);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args) {
SnakeGame sg=new SnakeGame();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==btns[0]){
gld.show(mainPane, "2");
}
if(e.getSource()==btns[1]){
gld.show(mainPane, "1");
}
if(e.getSource()== btns[2]){
gld.show(mainPane, "3");
initTime=System.currentTimeMillis();
tim = new Timer();
tim.scheduleAtFixedRate(new TimerCounts(initTime), 0,50000); //间隔大概10分钟提醒一次
innerSnake.setDaemon(true);
innerSnake.start();
}
}
class InterClassSnake extends Thread {
int tempDouX,tempDouY;
public void run(){
while(true){
this.displaySnake(gamePane.getGraphics());
this.displayDou(gamePane.getGraphics());
if(signal){moveSnake();signal=false;gamePane.repaint(); }
if(this.isEatDou()){
snake.add(0, new Point(douX,douY));System.out.println("hello world 1");
makeDou();
}
if(this.isEatSelf() || this.isOverBounds()){System.out.println("hello world 2");
JOptionPane.showMessageDialog(null, "你玩完了");
System.exit(0);
}
}
}
/*造豆和造蛇、显示蛇、显示豆子函数*/
public void makeDou(){
douX=(int)((int)(Math.random()*10));
douY=(int)((int)(Math.random()*8));
if(douX>15)douX=douX-4;
if(douY>14)douY=douY-4;
douX=douX*30;douY=douY*30;
for(Point x:snake){
int tempX=x.x;
int tempY=x.y;
if(tempX==douX&&tempY==douY){
makeDou();
}
}
}
public void displaySnake(Graphics g){
for(Point pos:snake){
if(pos==snake.get(0)){
g.setColor(Color.green);
g.fillRect(pos.x, pos.y, 30, 30);
}
else
g.setColor(Color.red);
g.fillRect(pos.x, pos.y, 30, 30);
}
}
public void displayDou(Graphics g){
g.setColor(gamePane.getBackground());
g.drawRect(tempDouX, tempDouY, 30, 30);
g.setColor(Color.blue);
g.fillRect(douX, douY, 30, 30);
tempDouX=douX;tempDouY=douY;
}
//eat the bean ,return true if eat successfully,otherwise return false;
public boolean isEatDou(){
if(douX==snake.get(0).x && douY==snake.get(0).y){
return true;
}
return false;
}
public boolean isEatSelf(){
int x=snake.get(0).x , y =snake.get(0).y;
for(int i=snake.size()-1;i>1;i--){
if(x==snake.get(i).x && y==snake.get(i).y)
return true;
}
return false;
}
public boolean isOverBounds(){
int x=snake.get(0).x,y=snake.get(0).y;
if(x<0 || x>480 || y<0 ||y>450)
return true;
return false;
}
public void moveSnake(){
for(int i =snake.size()-1;i>0;i--){
snake.get(i).x=snake.get(i-1).x;
snake.get(i).y=snake.get(i-1).y;
}
int tx=snake.get(0).x , ty=snake.get(0).y;
switch(direct){
case 1: if(snake.get(1).y<snake.get(0).y){System.out.println("不能往上");}
else ty -=30;
break;
case 2: if(tx-snake.get(1).x==0){
tx=tx-30;
}
else{
ty=ty+30;
}
break;
case 3: if(tx-snake.get(1).x==0){
tx=tx+30;
}
else{
ty=ty-30;
}
break;
case 4: if(snake.get(1).y>snake.get(0).y){System.out.println("不能往下");}
else ty += 30;
case 5:
break;
}
snake.remove(snake.size()-1);
snake.add(0, new Point(tx,ty));
}
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
signal = true;
switch(e.getKeyCode()){
case KeyEvent.VK_UP: direct=1;
break;
case KeyEvent.VK_LEFT: direct =2;
break;
case KeyEvent.VK_RIGHT: direct =3;
break;
case KeyEvent.VK_DOWN: direct =4;
break;
default :direct=5;
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
class BallDemo { //小球运动,
private int x=50; //初始位置
private int y=50;
private final int XSIZE=30;
private final int YSIZE=30;
private int dx=2;
private int dy=2;
private int tempX=50;
private int tempY=50;
private final int retangleX;
private final int retangleY;
private JPanel pane; //用于得到背景面板
private Color c=new Color(5);
public BallDemo(int x,int y,JPanel jtp){
retangleX=x;
retangleY=y;
this.pane=jtp;
}
public void left(){
x -= dx;
}
public void right(){
x += dx;
}
public void up(){
y -= dy;
}
public void down(){
y += dy;
}
public void move(){
x += dx;
y += dy;
if(x<2){
dx = -dx;
}
if(x+XSIZE>retangleX){
dx = -dx;
}
if(y<YSIZE){
dy = -dy;
}
if(y+YSIZE>=retangleY){
dy = -dy;
}
}
protected void setColor(Color co){
this.c=co;
}
public void paint(Graphics g){
g.setColor(pane.getBackground()); //这条语句用于擦除小球轨迹,让这条语句不起作用你就知道了
g.fillOval(tempX, tempY, XSIZE, YSIZE);
g.setColor(c);
g.fillOval(x, y, XSIZE, YSIZE);
tempX=x;tempY=y;
}
}
}
//定时器类,工作任务类
class TimerCounts extends java.util.TimerTask{
private long init;
private JFrame jf=new JFrame("游戏时间提示框");
private JLabel label = new JLabel ();
public TimerCounts(long i){
this.init=i;
jf.setSize(200, 100);
jf.add(label);
jf.setVisible(true);
}
@Override
public void run(){
long j=System.currentTimeMillis();
byte tim =(byte)( (j-init)/6000); //minutes
if(tim>=20){
JOptionPane.showMessageDialog(null,"你已经玩了很久了,要退出游戏休息了");
System.exit(0);
}
else{
label.setText("游戏时间已经过了"+tim+"分钟");
}
}
}
//欢迎界面
class WelcomPanel extends JPanel{
private static final long serialVersionUID = 1L;
Image img;
WelcomPanel(){
try{
img = ImageIO.read(new File("e:\\ok1.jpg"));
}catch(IOException e){
System.out.println("welcompanel");
}
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(img, getX(), getY(), null);
}
}
//开始界面
class StartPanel extends JPanel{
private static final long serialVersionUID = 1L;
Image img;
StartPanel(){
try{
img = ImageIO.read(new File("e:\\ok2.jpg"));
}catch(IOException e){
System.out.println("startpanel");
}
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(img, getX(), getY(), null);
}
}
Java贪吃蛇游戏的更多相关文章
- 用Java开发贪吃蛇游戏
贪吃蛇游戏的设计步骤: Part 1: 设计游戏图纸 画出900*700的白色窗口 在窗口上添加画布 在画布上添加标题 在画布上添加黑色游戏区 Part 2: 放置静态的蛇:一个头.两个身体 加上开始 ...
- 使用Java的GUI技术实现 “ 贪吃蛇 ” 游戏
详细教程: 使用Java的GUI技术实现 " 贪吃蛇 " 游戏_IT打工酱的博客-CSDN博客
- Android快乐贪吃蛇游戏实战项目开发教程-01项目概述与目录
一.项目简介 贪吃蛇是一个很经典的游戏,也很适合用来学习.本教程将和大家一起做一个Android版的贪吃蛇游戏. 我已经将做好的案例上传到了应用宝,无病毒.无广告,大家可以放心下载下来把玩一下.应用宝 ...
- Linux平台下贪吃蛇游戏的运行
1.参考资料说明: 这是一个在Linux系统下实现的简单的贪吃蛇游戏,同学找帮忙,我就直接在Red Hat中调试了一下,参考的是百度文库中"maosuhan"仁兄的文章,结合自己的 ...
- 用C++实现的贪吃蛇游戏
我是一个C++初学者,控制台实现了一个贪吃蛇游戏. 代码如下: //"贪吃蛇游戏"V1.0 //李国良于2016年12月29日编写完成 #include <iostream& ...
- WebGL实现HTML5的3D贪吃蛇游戏
js1k.com收集了小于1k的javascript小例子,里面有很多很炫很酷的游戏和特效,今年规则又增加了新花样,传统的classic类型基础上又增加了WebGL类型,以及允许增加到2K的++类型, ...
- 100行JS实现HTML5的3D贪吃蛇游戏
js1k.com收集了小于1k的javascript小例子,里面有很多很炫很酷的游戏和特效,今年规则又增加了新花样,传统的classic类型基础上又增加了WebGL类型,以及允许增加到2K的++类型, ...
- H5实现的可自定义贪吃蛇游戏
原创游戏,使用lufylegend.js开发 用canvas实现的贪吃蛇游戏,与一般的贪吃蛇游戏不同,图片经过美工设计,代码设计支持扩展和自定义. 游戏元素丰富,包括障碍物(仙人掌),金币(奖励),苹 ...
- JS贪吃蛇游戏
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
随机推荐
- oracle查询语句中case when的使用
case when语句语法如下: case when 表达式 then valueA else valueB end; 具体使用如下: select (case when a.colum ...
- HDU 4921 Map
题意: 给n个节点 他们形成了最多10条链 每条最多1000的长度 每一个节点有个val 你能够选择任何位置截断链 断点前的全部节点被你获得 通过题中计算公式得出你的val 问 通过随 ...
- XML序列化反序列化—常用类
public class XMLSerializer { #region (public) xml序列化 /// <summary> /// ...
- 小心LinkedHashMap的get()方法(转)
这是一个来自实际项目的例子,在这个案例中,有同事基于jdk中的LinkedHashMap设计了一个LRUCache,为了提高性能,使用了 ReentrantReadWriteLock 读写锁:写锁对应 ...
- AWS(0) - Amazon Web Services
Computer EC2 – Virtual Servers in the Cloud EC2 Container Service – Run and Manage Docker Containers ...
- 欧拉计划·第四题
题目4:找出由两个三位数乘积构成的回文. 一个回文数指的是从左向右和从右向左读都一样的数字.最大的由两个两位数乘积构成的回文数是9009 = 91 * 99. 找出最大的有由个三位数乘积构成的回文数. ...
- Cocos2d-x 3.1.1 lua-tests 开篇
Cocos2d-x 3.1.1 lua-tests开篇 本篇博客打算从研究Cocos2d-x引擎提供的測试样例来写起,笔者针对Cocos2d-x 3.1.1这个版本号来介绍怎样来学习它给我们提供的 ...
- DJ_Java_Decompiler新手入门教程
首先声明:这篇文章并不是我原创,只是感觉挺有用处,想跟大家分享一下,所以标注为原创,希望能有更多的朋友可以看到,还请原作者谅解. 昨天大D说让我写下DJ入门的基础,今天写了一大半了,结果不小心把浏览器 ...
- memwatch的使用
博主的新Blog地址:http://www.brantchen.com 欢迎訪问:) linux下的測试工具真是少之又少,还不好用,近期试用了memwatch,感觉网上的介绍不太好,所以放在这里跟大家 ...
- sql server2012附加的数据库问题
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjM2NzUxMw==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...