package com.oracle.tcs;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TanChiShenGame
{
// TODO:变量“声明”在此
/**
* 文档注释 豆所在的行
*/
int douHang = (int) (Math.random() * 20);
/**
* 豆所在的列
*/
int douLie = (int) (Math.random() * 20);
/**
* 蛇所有节的行
*/
int[] sheHangs = new int[100];
/**
* 蛇所有节的列
*/
int[] sheLies = new int[100];
/**
* 当前显示的节数
*/
int sheChangDu = 5;
/**
* 方向<br>
* 1 上<br>
* 2 左<br>
* 3 下<br>
* 0 右<br>
*/
int fangXiang = 1;
窗口 ck = null;
定时器 ds1 = null;
TanChiShenGame()
{
// TODO:程序初始化在此
for (int i = 0; i <sheChangDu; i++)
{
sheHangs[i] = 10 + i;
sheLies[i] = 10;
}
ck = new 窗口();
// 窗口 宽+2*立体边, 高+2*立体边+标题栏
ck.setSize(5 * 2 + 625, 645 + 2 * 5 + 25);
// 设定窗口可见性setVisible true/false
ck.setVisible(true);
// 延时的毫秒
ds1 = new 定时器(200);
}
class 窗口 extends JFrame
{
菜单 cd = null;
面板 mb = null;
窗口监听器 exit = null;
键盘监听器 jp = null;
窗口()
{
jp = new 键盘监听器();
this.addKeyListener(jp);
cd = new 菜单();
this.setJMenuBar(cd);
mb = new 面板();
this.add(mb);
exit = new 窗口监听器();
this.addWindowListener(exit);
this.repaint();
}
class 菜单 extends JMenuBar
{
JMenu dan; // 菜单
JMenuItem xiang1; // 菜单项
菜单监听器 cdjtq;
菜单()
{
dan = new JMenu("游戏"); // 菜单
xiang1 = new JMenuItem("开局"); // 菜单项
this.add(dan);
dan.add(xiang1);
cdjtq = new 菜单监听器();
xiang1.addActionListener(cdjtq);
}
class 菜单监听器 implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
// TODO:菜单事件处理
if (e.getSource() == xiang1)
{
}
}
}
}
class 窗口监听器 extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
ds1.xc.stop();
System.exit(0);
}
}
class 键盘监听器 implements KeyListener
{
// TODO:键盘处理,可加入KeyEvent.VK_XXX
public void keyPressed(KeyEvent e)
{
switch (e.getKeyCode())
{
case KeyEvent.VK_LEFT:// 左
fangXiang = 2;
break;
case KeyEvent.VK_RIGHT:// 右
fangXiang = 0;
break;
case KeyEvent.VK_UP:// 上
fangXiang = 1;
break;
case KeyEvent.VK_DOWN:// 下
fangXiang = 3;
break;
}
repaint();
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
}
}
class 面板 extends JPanel
{
鼠标监听器 sb = null;
面板()
{
sb = new 鼠标监听器();
this.addMouseListener(sb);
this.addMouseMotionListener(sb);
}
class 鼠标监听器 extends MouseAdapter implements MouseMotionListener
{
public void mousePressed(MouseEvent e)
{
}
public void mouseDragged(MouseEvent e)
{
}
public void mouseMoved(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public void mouseClicked(MouseEvent e)// 鼠标单击
{
// mx my鼠标的位置
int mx = e.getX();
int my = e.getY();
// 鼠标左键 BUTTON1、右键BUTTON3
if (e.getButton() == MouseEvent.BUTTON1)
{
// TODO:鼠标左键单击
}
repaint();
}
}
public void paint(Graphics g)
{
// g.setColor(Color.BLUE); //设定颜色:RED GREEN BLUE ORANGE WHITE
// GRAY BLACK PINK
// g.drawRect(左,上,宽,高); //矩形
// g.fillRect(左,上,宽,高); //实心矩形
// g.drawOval(左,上,宽,高); //椭圆
// g.fillOval(左,上,宽,高); //实心椭圆
// Image tu=(new ImageIcon( 路径 )).getImage();
// //路径:图片-右键-属性-路径src/
// g.drawImage(tu,x,y,null);
// TODO:绘图在此
Image beiJingTu = (new ImageIcon("E:\\ts\\src\\贪吃蛇\\背景.PNG")).getImage(); // 路径:图片-右键-属性-路径src/
g.drawImage(beiJingTu, 0, 0, null);
Image douTu = (new ImageIcon("E:\\ts\\src\\贪吃蛇\\豆.png")).getImage(); // 路径:图片-右键-属性-路径src/
g.drawImage(douTu, 10 + douLie * 30, 10 + douHang * 30, null);
for (int jie = 1; jie < sheChangDu; jie++)
{
Image sheShenTu = (new ImageIcon("E:\\ts\\src\\贪吃蛇\\身.png")).getImage(); // 路径:图片-右键-属性-路径src/
g.drawImage(sheShenTu, 10 + sheLies[jie] * 30, 10 + sheHangs[jie] * 30, null);
}
Image sheTouTu = (new ImageIcon("E:\\ts\\src\\贪吃蛇\\头" +fangXiang+".png")).getImage(); // 路径:图片-右键-属性-路径src/
g.drawImage(sheTouTu, 10 + sheLies[0] * 30, 10 + sheHangs[0] * 30, null);
}
}
}
class 定时器 implements Runnable// 实现Runnable接口
{
Thread xc = null;
long jianGe;
定时器(long jianGe)
{
this.jianGe = jianGe;
xc = new Thread(this);
xc.start();
}
public void run()
{
while (true)
{
try
{
xc.sleep(jianGe);
if (this == ds1)
{
// TODO:定时处理
// TODO 2 能吃豆 && 并且 || 或者
if (douHang== sheHangs[0] && douLie==sheLies[0] )
{
// TODO 长个
sheChangDu++;
// TODO 出新豆
douHang = (int) (Math.random() *20) ;//0-19
douLie = (int) (Math.random()*20 ) ;
}
// TODO 爬 流程结构表达 怎么爬
//身体爬
// sheHangs[4]=sheHangs[3];
// sheLies[4]=sheLies[3];
//
// sheHangs[3]=sheHangs[2];
// sheLies[3]=sheLies[2];
//
// sheHangs[2]=sheHangs[1];
// sheLies[2]=sheLies[1];
//
// sheHangs[1]=sheHangs[0];
// sheLies[1]=sheLies[0];
for (int jie =sheChangDu-1; jie >= 1; jie--)
{
sheHangs[jie]=sheHangs[jie-1];
sheLies[jie]=sheLies[jie-1];
}
//头 爬
switch ( fangXiang)
{
case 0:
sheLies[0]++;
break;
case 1:
sheHangs[0]--;
break;
case 2:
sheLies[0]--;
break;
case 3:
sheHangs[0]++;
break;
default:
break;
}
// TODO 能出界
if (sheHangs[0]<0|| sheHangs[0]>19|| sheLies[0]<0|| sheLies[0]>19)
{
System.exit(0);// 关闭程序
}
// if (sheHangs[0]<0)
// {
// sheHangs[0]=19;
// }
// TODO 能自咬
for(int i=1;i<sheHangs.length;i++){
if(sheLies[0]==sheLies[i]&&sheHangs[0]==sheHangs[i])
{
System.exit(0);// 关闭程序
}
}
ck.repaint();
}
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
// main主方法 ,主类的“入口方法”
public static void main(String[] args)
{
new TanChiShenGame();
}
}