有一个java小程序,是贪吃蛇游戏,如何往里面添加声音代码呢,让声音在“Gameover”的时候播放 20
importjava.awt.*;importjava.awt.event.*;publicclassGreedSnake//主类{/***@paramargs*/pub...
import java.awt.*;
import java.awt.event.*;
public class GreedSnake //主类
{
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new MyWindow();
}
}
class MyPanel extends Panel implements KeyListener,Runnable
{
Button snake[];
int shu=0;
int food[];
boolean result=true;
Thread thread;
static int weix,weiy;
boolean t=true;
int fangxiang=0;
int x=0,y=0;
MyPanel()
{
setLayout(null);
snake=new Button[20];
food=new int [20];
thread=new Thread(this);
for(int j=0;j<20;j++)
{
food[j]=(int)(Math.random()*99);
}
weix=(int)(food[0]*0.1)*60;
weiy=(int)(food[0]%10)*40;
for(int i=0;i<20;i++)
{
snake[i]=new Button();
}
add(snake[0]);
snake[0].setBackground(Color.black);
snake[0].addKeyListener(this);
snake[0].setBounds(0,0,10,10);
setBackground(Color.cyan);
}
public void run()
{
while(t)
{
if(fangxiang==0)
{
try
{
x+=10;
snake[0].setLocation(x, y);
if(x==weix&&y==weiy)
{
shu++;
weix=(int)(food[shu]*0.1)*60;
weiy=(int)(food[shu]%10)*40;
repaint();
add(snake[shu]);
snake[shu].setBounds(snake[shu-1].getBounds());
}
thread.sleep(100);
}
catch(Exception e){}
}
else if(fangxiang==1)
{
try
{
x-=10;
snake[0].setLocation(x, y);
if(x==weix&&y==weiy)
{
shu++;
weix=(int)(food[shu]*0.1)*60;
weiy=(int)(food[shu]%10)*40;
repaint();
add(snake[shu]);
snake[shu].setBounds(snake[shu-1].getBounds());
}
thread.sleep(100);
}
catch(Exception e){}
}
else if(fangxiang==2)
{
try
{
y-=10;
snake[0].setLocation(x, y);
if(x==weix&&y==weiy)
{
shu++;
weix=(int)(food[shu]*0.1)*60;
weiy=(int)(food[shu]%10)*40;
repaint();
add(snake[shu]);
snake[shu].setBounds(snake[shu-1].getBounds());
}
thread.sleep(100);
}
catch(Exception e){}
}
else if(fangxiang==3)
{
try
{
y+=10;
snake[0].setLocation(x, y);
if(x==weix&&y==weiy)
{
shu++;
weix=(int)(food[shu]*0.1)*60;
weiy=(int)(food[shu]%10)*40;
repaint();
add(snake[shu]);
snake[shu].setBounds(snake[shu-1].getBounds());
}
thread.sleep(100);
}
catch(Exception e){}
}
int num1=shu;
while(num1>1)
{
if(snake[num1].getBounds().x==snake[0].getBounds().x&&snake[num1].getBounds().y==snake[0].getBounds().y)
{
t=false;
result=false;
repaint();
}
num1--;
}
if(x<0||x>=this.getWidth()||y<0||y>=this.getHeight())
{
t=false;
result=false;
repaint();
}
int num=shu;
while(num>0)
{
snake[num].setBounds(snake[num-1].getBounds());
num--;
}
请帮我做出来 展开
import java.awt.event.*;
public class GreedSnake //主类
{
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new MyWindow();
}
}
class MyPanel extends Panel implements KeyListener,Runnable
{
Button snake[];
int shu=0;
int food[];
boolean result=true;
Thread thread;
static int weix,weiy;
boolean t=true;
int fangxiang=0;
int x=0,y=0;
MyPanel()
{
setLayout(null);
snake=new Button[20];
food=new int [20];
thread=new Thread(this);
for(int j=0;j<20;j++)
{
food[j]=(int)(Math.random()*99);
}
weix=(int)(food[0]*0.1)*60;
weiy=(int)(food[0]%10)*40;
for(int i=0;i<20;i++)
{
snake[i]=new Button();
}
add(snake[0]);
snake[0].setBackground(Color.black);
snake[0].addKeyListener(this);
snake[0].setBounds(0,0,10,10);
setBackground(Color.cyan);
}
public void run()
{
while(t)
{
if(fangxiang==0)
{
try
{
x+=10;
snake[0].setLocation(x, y);
if(x==weix&&y==weiy)
{
shu++;
weix=(int)(food[shu]*0.1)*60;
weiy=(int)(food[shu]%10)*40;
repaint();
add(snake[shu]);
snake[shu].setBounds(snake[shu-1].getBounds());
}
thread.sleep(100);
}
catch(Exception e){}
}
else if(fangxiang==1)
{
try
{
x-=10;
snake[0].setLocation(x, y);
if(x==weix&&y==weiy)
{
shu++;
weix=(int)(food[shu]*0.1)*60;
weiy=(int)(food[shu]%10)*40;
repaint();
add(snake[shu]);
snake[shu].setBounds(snake[shu-1].getBounds());
}
thread.sleep(100);
}
catch(Exception e){}
}
else if(fangxiang==2)
{
try
{
y-=10;
snake[0].setLocation(x, y);
if(x==weix&&y==weiy)
{
shu++;
weix=(int)(food[shu]*0.1)*60;
weiy=(int)(food[shu]%10)*40;
repaint();
add(snake[shu]);
snake[shu].setBounds(snake[shu-1].getBounds());
}
thread.sleep(100);
}
catch(Exception e){}
}
else if(fangxiang==3)
{
try
{
y+=10;
snake[0].setLocation(x, y);
if(x==weix&&y==weiy)
{
shu++;
weix=(int)(food[shu]*0.1)*60;
weiy=(int)(food[shu]%10)*40;
repaint();
add(snake[shu]);
snake[shu].setBounds(snake[shu-1].getBounds());
}
thread.sleep(100);
}
catch(Exception e){}
}
int num1=shu;
while(num1>1)
{
if(snake[num1].getBounds().x==snake[0].getBounds().x&&snake[num1].getBounds().y==snake[0].getBounds().y)
{
t=false;
result=false;
repaint();
}
num1--;
}
if(x<0||x>=this.getWidth()||y<0||y>=this.getHeight())
{
t=false;
result=false;
repaint();
}
int num=shu;
while(num>0)
{
snake[num].setBounds(snake[num-1].getBounds());
num--;
}
请帮我做出来 展开
3个回答
2010-07-07
展开全部
这个设置好的没办法做;如果真的要做的话那么就相当于重做一个游戏了,你想想看真的有这样的善人吗?所以还是现实点吧朋友你就凑合着玩就好了如何?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在FLASH里的层加声音啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
game over时候应该有一个条件吧,到那条件的时候play不就完了么
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询