谁有打砖块java代码,急需

Python024

谁有打砖块java代码,急需,第1张

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=0i<20i++)

for(int j=0j<20j++)

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=0i<20i++)

for(j=0j<20j++){

matrix[i][j]=true

brickint[i][j]=(int)(Math.random()*7)+1

switch (brickint[i][j]){

case 1: brickcolor[i][j]=Color.greenbreak

case 2: brickcolor[i][j]=Color.bluebreak

default: brickcolor[i][j]=Color.orangebreak

}

}

}

public void move()

{

for(int i=0i<20i++)

for(int j=0j<20j++)

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.vyball.vx=ball.vxmatrix[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.vxball.vy=ball.vymatrix[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=0i<20i++)

for(int j=0j<20j++)

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

}

}

试试这个吧

这个还好一点

你可以控制它

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=0i<20i++)

for(int j=0j<20j++)

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()

你有点误解了 JAVA用到的英文单词其实不多,你说的接口名、类名等都是可以自己起的只要自己看到这个名字能知道是什么意思就行,没必要非要用英语。 这年头什么东西都不会缺的 去网上随便搜搜就是一堆书,但真的没多大用。 f