谁有打砖块java代码,急需

请问这个代码怎样玩,我运行时好像不能操作?它好像是自己移动的不能操作。能不能给运动规则说一下?... 请问这个代码怎样玩,我运行时好像不能操作?它好像是自己移动的不能操作。能不能给运动规则说一下? 展开
 我来答
lsz13258
2009-12-28 · 超过14用户采纳过TA的回答
知道答主
回答量:91
采纳率:0%
帮助的人:57.7万
展开全部
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Timer;

import javax.swing.JFrame;

public class dazhuankuai implements KeyListener
{
private JFrame jframe;
Canvas canvas;
board b;
int x;
int scoret;
int diffscore;
private int canvaswidth = 400;//canvas 的属性
private int canvasheight =600;

dazhuankuai(){
jframe = new JFrame("打砖块");
canvas = new Canvas();
canvas.setSize(canvaswidth, canvasheight);
canvas.addKeyListener(this);
jframe.add(canvas);
jframe.setBounds(320, 100,410, 500);
jframe.addKeyListener(this);
jframe.setVisible(true);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
begin();
}
public void begin()
{ b=new board(this);
b.run();
b.ball.vx=5;
b.ball.vy=10;
}
void paint()
{Graphics g = canvas.getGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, 400, 350);

for(int i=0;i<20;i++)
for(int j=0;j<20;j++)
if(b.matrix[i][j]){
g.setColor(b.brickcolor[i][j]);
g.fillRect(i*20, j*10, 20, 10);
g.setColor(Color.white);
g.drawRect(i*20, j*10, 20, 10);}
g.setColor(Color.RED);
if(b.ball.y<=350)
g.fillOval(b.ball.x, b.ball.y, 10, 10);
}
void paintrect()
{Graphics g = canvas.getGraphics();
g.setColor(Color.white);
g.fillRect(0, 350, 400, 10);
g.setColor(Color.black);
g.fillRect(b.leftpoint, 350, 50, 10);
}
void paintscore()
{Graphics g = canvas.getGraphics();
g.setColor(Color.white);
g.fillRect(0, 360, 400, 50);
b.getscore();
if(b.score>scoret)
diffscore=b.score-scoret;
g.setColor(Color.black);
g.drawString("你的分数为:"+b.score+"分", 0, 400);
if(b.ball.y>360)
g.drawString("提示:你输了,重新开始按ENTER。",200, 410);
else g.drawString("提示:新得分数"+diffscore+"分", 200, 410);
scoret=b.score;
b.score=0;
}
public void keyPressed(KeyEvent e) {//监听键盘按键
int keycode = e.getKeyCode();

switch (keycode) {
case KeyEvent.VK_LEFT:
if(b.leftpoint>=10)
b.leftpoint-=10;
paintrect();
break;

case KeyEvent.VK_RIGHT:
if(b.leftpoint<=340)
b.leftpoint+=10;
paintrect();
break;

case KeyEvent.VK_ENTER:
begin();
break;
}
}
public void keyReleased(KeyEvent e) {
}

public void keyTyped(KeyEvent e) {
}

public static void main(String[] args) {
dazhuankuai s = new dazhuankuai();
}
}
class ball
{
int x=200;
int y=340;
int vx;
int vy;
int r;
}

class board
{
Timer timer =new Timer(true);
ball ball;
dazhuankuai dzk;
int score=0;
boolean[][] matrix;
Color[][] brickcolor;
int[][] brickint;
int leftpoint=180;
int i,j;

board(dazhuankuai dzk)
{this.dzk=dzk;
ball=new ball();
matrix = new boolean[20][20];
brickcolor=new Color[20][20];
brickint=new int[20][20];

for(i=0;i<20;i++)
for(j=0;j<20;j++){
matrix[i][j]=true;
brickint[i][j]=(int)(Math.random()*7)+1;
switch (brickint[i][j]){
case 1: brickcolor[i][j]=Color.green;break;
case 2: brickcolor[i][j]=Color.blue;break;
default: brickcolor[i][j]=Color.orange;break;
}
}
}
public void move()
{
for(int i=0;i<20;i++)
for(int j=0;j<20;j++)
if(matrix[i][j])
{if(ball.x<(i+1)*20&&ball.x>=i*20)
if(ball.y<=(j+1)*10&&ball.y>=j*10)
{ball.vy=-ball.vy;ball.vx=ball.vx;matrix[i][j]=false;
}
if(ball.y<=(j+1)*10&&ball.y>=j*10)
if(ball.x<=(i+1)*20&&ball.x>i*20)
{ball.vx=-ball.vx;ball.vy=ball.vy;matrix[i][j]=false;
}
}

if(ball.x<=0)ball.vx=-ball.vx;//碰墙
if(ball.y<=0&&ball.vy>0)ball.vy=-ball.vy;
if(ball.x>=390)ball.vx=-ball.vx;

if(ball.y>=340&&ball.vy<=0)//碰木板
if(ball.x>=leftpoint&&ball.x<=leftpoint+50)
ball.vy=-ball.vy;
}

public void getscore()
{for(int i=0;i<20;i++)
for(int j=0;j<20;j++)
if(matrix[i][j]==false)
{if(brickint[i][j]==1)
score+=20;
if(brickint[i][j]==2)
score+=30;
else score+=10;}
}

public void run(){

timer.schedule(new java.util.TimerTask(){
public void run() {
change();
dzk.paintscore();
dzk.paint();
dzk.paintrect();
}
},0,(100*1));
}
public void change(){
move();
ball.x+=ball.vx;
ball.y-=ball.vy;
}
}

试试这个吧
这个还好一点
你可以控制它
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式