package snakedemo;
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import graph.windowstemplate;
public class showgame extends windowstemplate {
public showgame(){
//final JDesktopPane desktopPane=new JDesktopPane();
//desktopPane.setOpaque(false);
setTitle("贪吃蛇");
final snakegame js=new snakegame();
js.setFocusable(true);
Runnable runnable=js;
Thread thread=new Thread(runnable);
thread.start();
js.setLayout(new BorderLayout());
JToolBar toolBar=new JToolBar();
toolBar.setFloatable(false);
final JButton start=new JButton("开始");
final JButton stop=new JButton("暂停");
JButton reset=new JButton("重置");
//JButton option=new JButton("设置");
stop.setEnabled(false);
JPanel ePanel=new JPanel();
ePanel.setLayout(null);
ePanel.setPreferredSize(new Dimension(150,440));
Color bgColor=new Color(100, 149, 254);
Color fsColor=new Color(200, 200, 200);
ePanel.setBackground(bgColor);
JLabel goal=new JLabel("得分");
JLabel level=new JLabel("等级");
final JLabel fsjl=new JLabel("排行:");
final JLabel fs1=new JLabel("");
final JLabel fs2=new JLabel("");
final JLabel fs3=new JLabel("");
final JTextField goaltext=new JTextField();
final JTextField leveltext=new JTextField();
goal.setBounds(20, 50, 28, 30);
level.setBounds(20, 100,28, 30);
fsjl.setOpaque(true);
fsjl.setBackground(fsColor);
fsjl.setBounds(20,160, 100,20);
fs1.setOpaque(true);
fs1.setBackground(fsColor);
fs1.setBounds(20,180, 100,30);
fs2.setOpaque(true);
fs2.setBackground(fsColor);
fs2.setBounds(20,210, 100,30);
fs3.setOpaque(true);
fs3.setBackground(fsColor);
fs3.setBounds(20,240, 100,30);
goaltext.setEditable(false);
goaltext.setBounds(50, 50, 70, 30);
leveltext.setEditable(false);
leveltext.setBounds(50, 100, 70, 30);
leveltext.setText(" "+js.lv);
ePanel.add(goal);
ePanel.add(level);
ePanel.add(goaltext);
ePanel.add(leveltext);
ePanel.add(fsjl);
ePanel.add(fs1);
ePanel.add(fs2);
ePanel.add(fs3);
toolBar.add(start);
toolBar.add(stop);
toolBar.add(reset);
//toolBar.add(option);
js.add(toolBar,BorderLayout.NORTH);
js.add(ePanel,BorderLayout.EAST);
//js.add(desktopPane,BorderLayout.CENTER);
add(js);
js.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method
int key=e.getKeyCode();
final int up=KeyEvent.VK_UP,down=KeyEvent.VK_DOWN,left=KeyEvent.VK_LEFT,right=KeyEvent.VK_RIGHT,space=KeyEvent.VK_SPACE;
switch (key) {
case up:
if(js.direction==1){
}else{
if(js.direction==3){
}else{
js.direction=1;
}
}
break;
case down:
if(js.direction==3){
}else{
if(js.direction==1){
}else{
js.direction=3;
}
}
break;
case left:
if(js.direction==4){
}else{
if(js.direction==2){
}else{
js.direction=4;
}
}
break;
case right:
if(js.direction==2){
}else{
if(js.direction==4){
}else{
js.direction=2;
}
}
break;
case space:
if(js.start){
start.setEnabled(true);
stop.setEnabled(false);
js.start=false;
}else {
js.requestFocus();
start.setEnabled(false);
stop.setEnabled(true);
js.start=true;
}
default:
break;
}
}
});
js.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e){
js.requestFocus();
}
});
start.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
js.requestFocus();
start.setEnabled(false);
stop.setEnabled(true);
js.start=true;
}
});
stop.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
start.setEnabled(true);
stop.setEnabled(false);
js.start=false;
}
});
reset.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
stop.setEnabled(false);
start.setEnabled(true);
js.resnake();
js.start=false;
js.enint=1;
js.life=true;
repaint();
}
});
/*//final JInternalFrame inter=new JInternalFrame("设置",false,true,false,false);
//inter.setBounds(210, 120, 200, 100);
option.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
desktopPane.add(inter);
inter.setVisible(true);
}
});
*/
File file=new File("text/fs.text");
if(!file.exists())
{
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//定时器
Timer asTimer1=new Timer();
asTimer1.schedule(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
File file1 =new File("music/bgmusic.wav");
URL url;
try {
url = file1.toURL();
AudioClip sound1 = Applet.newAudioClip(url);
sound1.play();
} catch (MalformedURLException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
//sound1.loop();hhnhh
//sound1.stop();
}
},300,34800);
Timer asTimer=new Timer();
asTimer.schedule(new TimerTask(){
public void run(){
int fs=js.length-2;
goaltext.setText(" "+fs);
leveltext.setText(" "+js.lv);
String oldtexts[]={"","",""};
File file=new File("text/fs.text");
FileInputStream out;
try {
out = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(out);
int tempchar,i=0,m=0,fss[]=new int[3];
while ((tempchar = isr.read()) != -1) {
if(tempchar>=48&&tempchar<=57){
oldtexts[i]=oldtexts[i]+(char)tempchar;
m++;
if(m%4==0){
i++;
}
}
}
if(m==0){
String fstext="first"+1000+"second"+1000+"third"+1000;
byte bt[] = new byte[1024];
bt = fstext.getBytes();
FileOutputStream in = new FileOutputStream(file);
in.write(bt, 0, bt.length);
in.close();
}else{
i=0;
while (i<3) {
fss[i]=Integer.parseInt(oldtexts[i]);
i++;
}
fs1.setText("first: "+(fss[0]-1000));
fs2.setText("secod: "+(fss[1]-1000));
fs3.setText("third: "+(fss[2]-1000
没有合适的资源?快使用搜索试试~ 我知道了~
java图形界面,贪吃蛇游戏练习.zip
共57个文件
class:29个
java:15个
png:6个
需积分: 2 0 下载量 180 浏览量
2024-12-02
20:06:26
上传
评论
收藏 4.71MB ZIP 举报
温馨提示
java图形界面,贪吃蛇游戏练习.zip
资源推荐
资源详情
资源评论
收起资源包目录
java图形界面,贪吃蛇游戏练习.zip (57个子文件)
projectcode1124
music
bgmusic.wav 5.89MB
image
food2.png 1KB
zai2.jpg 1KB
gameover.png 12KB
cjstart.jpg 86KB
zai.jpg 1KB
cj2.jpg 63KB
sbake.png 3KB
food3.png 1KB
sbake2.png 3KB
food.png 1KB
cj1.jpg 105KB
src
snakedemo
environment.java 1KB
actiongeme.java 8KB
roesfood.java 401B
hinder.java 291B
snakegame.java 4KB
rolesnake.java 913B
showgame.java 9KB
gamedemo
environment.java 654B
menu.java 136B
actiongeme.java 53B
role.java 165B
graph
graphtest.java 3KB
windowstemplate.java 946B
graphdemo.java 751B
graphslip.java 3KB
text
fs.text 28B
bin
snakedemo
roesfood.class 735B
showgame$4.class 983B
showgame$6.class 1KB
snakegame.class 4KB
showgame$3.class 1019B
hinder.class 665B
showgame$7.class 4KB
showgame.class 5KB
environment.class 2KB
showgame$5.class 1KB
rolesnake.class 2KB
actiongeme.class 6KB
showgame$2.class 739B
showgame$1.class 2KB
gamedemo
environment.class 1KB
actiongeme.class 273B
role.class 336B
menu.class 255B
graph
graphtest.class 447B
graphslip$3.class 1KB
windowstemplate.class 675B
graphdemo.class 1KB
windowstemplate$1.class 912B
graphslip.class 3KB
graphslip$1.class 1KB
windowsdemo.class 2KB
graphslip$2.class 1KB
windowsdemo$1.class 2KB
hei.class 305B
共 57 条
- 1
资源评论
龙年行大运
- 粉丝: 1315
- 资源: 3949
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功