求大神给一份JAVA小游戏,贪吃蛇或者俄罗斯方块源代码。my eclipse能直接打开运行的!
- 你的回答被采纳后将获得:
- 系统奖励15(财富值+成长值)+难题奖励30(财富值+成长值)
1个回答
展开全部
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Game extends JFrame implements KeyListener,Runnable
{
Thread Down10 = new Thread(this);
GameCanvas test = new GameCanvas();
JLabel LabelTetris = new JLabel("Tetris Game");
JLabel ScorePrint1 = new JLabel("Score");
JLabel ScorePrint2 = new JLabel("0"+test.Score);
Game()
{
setSize(400,800);
setVisible(true);
setLayout(new FlowLayout());
test.addKeyListener(this);
LabelTetris.setBackground(Color.white);
LabelTetris.setFont((new Font("Tahoma",1,50)));
ScorePrint1.setFont((new Font("Tahoma",1,40)));
ScorePrint2.setBackground(Color.white);
JLabel LM = new JLabel("L.M");
ScorePrint2.setFont((new Font("Tahoma",1,40)));
add(LabelTetris);
add(test);
add(ScorePrint1);
add(ScorePrint2);
LM.setForeground(Color.white);
add(LM);
validate();
Down10.start();
}
public void run()
{
while(true)
{
try
{
test.Down1();
Down10.sleep(700);
}
catch(InterruptedException I){I.printStackTrace();}
}
}
public void keyPressed(KeyEvent K)
{
if(K.getKeyCode()==KeyEvent.VK_LEFT)
test.Left1();
else if(K.getKeyCode()==KeyEvent.VK_UP)
test.Turn();
else if(K.getKeyCode()==KeyEvent.VK_RIGHT)
test.Right1();
else if(K.getKeyCode()==KeyEvent.VK_DOWN)
{
test.Down1();
if(test.ScoreBool==1)
{ScorePrint2.setText(""+test.Score);}
}
}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
}
class GameCanvas extends Canvas
{
int x=5,y=0;
int GameMap[][]=new int [50][100];
int BlockShape;
int BlockWay;
int Score=0;
int ScoreBool=0;
int OverBool=0;
public final static int[][][] Blocks={{{0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0},
{0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0},
{0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0},
{0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0}},
{{0,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0},
{1,0,0,0,1,1,0,0,0,1,0,0,0,0,0,0},
{0,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0},
{1,0,0,0,1,1,0,0,0,1,0,0,0,0,0,0}},
{{1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0},
{0,1,0,0,1,1,0,0,1,0,0,0,0,0,0,0},
{1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0},
{0,1,0,0,1,1,0,0,1,0,0,0,0,0,0,0}},
{{0,1,0,0,0,1,0,0,1,1,0,0,0,0,0,0},
{1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0},
{1,1,0,0,1,0,0,0,1,0,0,0,0,0,0,0},
{1,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0}},
{{1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0},
{1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0},
{1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0},
{1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0}},
{{1,0,0,0,1,0,0,0,1,1,0,0,0,0,0,0},
{1,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0},
{1,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0},
{0,0,1,0,1,1,1,0,0,0,0,0,0,0,0,0}},
{{0,1,0,0,1,1,1,0,0,0,0,0,0,0,0,0},
{0,1,0,0,1,1,0,0,0,1,0,0,0,0,0,0},
{1,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0},
{0,1,0,0,0,1,1,0,0,1,0,0,0,0,0,0}}};
GameCanvas()
{
setSize(340,500);
setBackground(Color.BLACK);
for(int i=0;i<25;i++)
{
GameMap[0][i]=2;
GameMap[16][i]=2;
}
for(int i=0;i<16;i++)
{
GameMap[i][24]=2;
}
}
public void NewBlocks()
{
x=5;y=0;
BlockShape=(int)(Math.random()*6);
BlockWay=(int)(Math.random()*3);
if(IsOver(x,y)==true)
{OverBool=1;}
}
public void Keep(int x,int y,int BlockShape,int BlockWay)
{
int n=0;
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
{
if(GameMap[x+j+1][y+i]==0)
{
GameMap[x+j+1][y+i]=Blocks[BlockShape][BlockWay][n];
}
n++;
}
}
public void paint(Graphics G)
{
G.setColor(Color.white);
for(int i=0;i<16;i++)
{
if(Blocks[BlockShape][BlockWay][i]==1)
G.drawRect((i%4+x+1)*20,(i/4+y)*20,20,20);
}
for(int i=0;i<30;i++)
for(int j=0;j<50;j++)
if(GameMap[i][j]==1||GameMap[i][j]==2)
G.fillRect(i*20,j*20,20,20);
}
public void Turn()
{
if(IsFeasible(x,y,BlockShape,BlockWay)==1)
{
BlockWay=(BlockWay+1)%4;
repaint();
}
}
public void Left1()
{
if(IsFeasible(x-1,y,BlockShape,BlockWay)==1)
{
x-=1;
repaint();
}
}
public void Right1()
{
if(IsFeasible(x+1,y,BlockShape,BlockWay)==1)
{
x+=1;
repaint();
}
}
public void Down1()
{
if(IsFeasible(x,y+1,BlockShape,BlockWay)==1)
{
y+=1;
repaint();
}
else if(IsFeasible(x,y+1,BlockShape,BlockWay)==0)
{
Keep(x,y,BlockShape,BlockWay);
Deline();
if(OverBool==0)
NewBlocks();
}
}
public int IsFeasible(int x,int y,int BlockShape,int BlockWay)
{
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
if(Blocks[BlockShape][BlockWay][i*4+j]==1&&GameMap[x+j+1][y+i]==1)
return 0;
else if(Blocks[BlockShape][BlockWay][i*4+j]==1&&GameMap[x+j+1][y+i]==2)
return 0;
}
}
return 1;
}
public void Deline()
{
int BlocksSum = 0;
for(int i=0;i<25;i++)
{
for(int j=0;j<16;j++)
{
if (GameMap[j][i]==1)
{
BlocksSum++;
if (BlocksSum==15)
{
for(int p=i;p>0;p--)
{
for(int q=0;q<16;q++)
{
GameMap[q][p]=GameMap[q][p-1];
}
}
Score+=10;
ScoreBool=1;
}
}
}
BlocksSum = 0;
}
}
public boolean IsOver(int x,int y)
{
if(IsFeasible(x,y,BlockShape,BlockWay)==0)
return true;
else
return false;
}
}
public class Tetris
{
public static void main(String[] args)
{
Game test2 = new Game();
}
}
拿去玩 JAVA新建文件命名为 Tetris 以前写的 懒得布置界面
import java.awt.*;
import java.awt.event.*;
class Game extends JFrame implements KeyListener,Runnable
{
Thread Down10 = new Thread(this);
GameCanvas test = new GameCanvas();
JLabel LabelTetris = new JLabel("Tetris Game");
JLabel ScorePrint1 = new JLabel("Score");
JLabel ScorePrint2 = new JLabel("0"+test.Score);
Game()
{
setSize(400,800);
setVisible(true);
setLayout(new FlowLayout());
test.addKeyListener(this);
LabelTetris.setBackground(Color.white);
LabelTetris.setFont((new Font("Tahoma",1,50)));
ScorePrint1.setFont((new Font("Tahoma",1,40)));
ScorePrint2.setBackground(Color.white);
JLabel LM = new JLabel("L.M");
ScorePrint2.setFont((new Font("Tahoma",1,40)));
add(LabelTetris);
add(test);
add(ScorePrint1);
add(ScorePrint2);
LM.setForeground(Color.white);
add(LM);
validate();
Down10.start();
}
public void run()
{
while(true)
{
try
{
test.Down1();
Down10.sleep(700);
}
catch(InterruptedException I){I.printStackTrace();}
}
}
public void keyPressed(KeyEvent K)
{
if(K.getKeyCode()==KeyEvent.VK_LEFT)
test.Left1();
else if(K.getKeyCode()==KeyEvent.VK_UP)
test.Turn();
else if(K.getKeyCode()==KeyEvent.VK_RIGHT)
test.Right1();
else if(K.getKeyCode()==KeyEvent.VK_DOWN)
{
test.Down1();
if(test.ScoreBool==1)
{ScorePrint2.setText(""+test.Score);}
}
}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
}
class GameCanvas extends Canvas
{
int x=5,y=0;
int GameMap[][]=new int [50][100];
int BlockShape;
int BlockWay;
int Score=0;
int ScoreBool=0;
int OverBool=0;
public final static int[][][] Blocks={{{0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0},
{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{
GameMap[i][24]=2;
}
}
public void NewBlocks()
{
x=5;y=0;
BlockShape=(int)(Math.random()*6);
BlockWay=(int)(Math.random()*3);
if(IsOver(x,y)==true)
{OverBool=1;}
}
public void Keep(int x,int y,int BlockShape,int BlockWay)
{
int n=0;
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
{
if(GameMap[x+j+1][y+i]==0)
{
GameMap[x+j+1][y+i]=Blocks[BlockShape][BlockWay][n];
}
n++;
}
}
public void paint(Graphics G)
{
G.setColor(Color.white);
for(int i=0;i<16;i++)
{
if(Blocks[BlockShape][BlockWay][i]==1)
G.drawRect((i%4+x+1)*20,(i/4+y)*20,20,20);
}
for(int i=0;i<30;i++)
for(int j=0;j<50;j++)
if(GameMap[i][j]==1||GameMap[i][j]==2)
G.fillRect(i*20,j*20,20,20);
}
public void Turn()
{
if(IsFeasible(x,y,BlockShape,BlockWay)==1)
{
BlockWay=(BlockWay+1)%4;
repaint();
}
}
public void Left1()
{
if(IsFeasible(x-1,y,BlockShape,BlockWay)==1)
{
x-=1;
repaint();
}
}
public void Right1()
{
if(IsFeasible(x+1,y,BlockShape,BlockWay)==1)
{
x+=1;
repaint();
}
}
public void Down1()
{
if(IsFeasible(x,y+1,BlockShape,BlockWay)==1)
{
y+=1;
repaint();
}
else if(IsFeasible(x,y+1,BlockShape,BlockWay)==0)
{
Keep(x,y,BlockShape,BlockWay);
Deline();
if(OverBool==0)
NewBlocks();
}
}
public int IsFeasible(int x,int y,int BlockShape,int BlockWay)
{
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
if(Blocks[BlockShape][BlockWay][i*4+j]==1&&GameMap[x+j+1][y+i]==1)
return 0;
else if(Blocks[BlockShape][BlockWay][i*4+j]==1&&GameMap[x+j+1][y+i]==2)
return 0;
}
}
return 1;
}
public void Deline()
{
int BlocksSum = 0;
for(int i=0;i<25;i++)
{
for(int j=0;j<16;j++)
{
if (GameMap[j][i]==1)
{
BlocksSum++;
if (BlocksSum==15)
{
for(int p=i;p>0;p--)
{
for(int q=0;q<16;q++)
{
GameMap[q][p]=GameMap[q][p-1];
}
}
Score+=10;
ScoreBool=1;
}
}
}
BlocksSum = 0;
}
}
public boolean IsOver(int x,int y)
{
if(IsFeasible(x,y,BlockShape,BlockWay)==0)
return true;
else
return false;
}
}
public class Tetris
{
public static void main(String[] args)
{
Game test2 = new Game();
}
}
拿去玩 JAVA新建文件命名为 Tetris 以前写的 懒得布置界面
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询