import java.awt.*;
import java.awt.event.*;
public class bowlingarray extends Frame{
private static final long serialVersionUID = 1L;
private int ballx = 250, bally = 380;//position of ball
private int ball1x = 150, ball1y = 30;
private int ball2x = 190, ball2y = 30;
private int ball3x = 230, ball3y = 30;
private int ball4x = 270, ball4y = 30;
private int ball5x = 170, ball5y = 70;
private int ball6x = 210, ball6y = 70;
private int ball7x = 250, ball7y = 70;
private int ball8x = 190, ball8y = 110;
private int ball9x = 230, ball9y = 110;
private int ball10x = 210, ball10y = 150;
private int x1=100, y1=1;
private int x2=350, y2=1;//position of paddle
//define ball's direction,then we can know where it will go
int right=5,left= -5,up=5,down= -5,score=0;
int width, height; // Width and height of the ball
boolean game, gameOver;
//画图。
Image offScreenImage = null;
private Thread th;
public boolean live = true;
public void paint(Graphics g) {
Color c=g.getColor();
g.setColor(Color.red);
g.fillOval(ballx, bally, 30, 30);//draw pong
g.fillOval(ball1x, ball1y, 30, 30);
g.fillOval(ball2x, ball2y, 30, 30);
g.fillOval(ball3x, ball3y, 30, 30);
g.fillOval(ball4x, ball4y, 30, 30);
g.fillOval(ball5x, ball5y, 30, 30);
g.fillOval(ball6x, ball6y, 30, 30);
g.fillOval(ball7x, ball7y, 30, 30);
g.fillOval(ball8x, ball8y, 30, 30);
g.fillOval(ball9x, ball9y, 30, 30);
g.fillOval(ball10x, ball10y, 30, 30);
if(live=false)
{
remove(this);
}
if (210 < ballx&&ballx < 240)
{
if (150<bally && bally<180)
{
live=false;
score++;
// g.setColor(Color.magenta);
// g.fillOval(ball10x, ball10y, 30, 30);
}
}
g.fillRect(x1,y1, 20, 400);
g.fillRect(x2,y2, 20,400);//draw paddles
g.drawString("score: "+score, 20, 40);
}
//not stable,so use update.
public void update(Graphics g) {
if(offScreenImage == null){
offScreenImage = this.createImage(500, 400);
}
Graphics gOffScreen = offScreenImage.getGraphics();
Color c = gOffScreen.getColor();
gOffScreen.setColor(Color.blue);
gOffScreen.fillRect(0, 0, 500, 400);
gOffScreen.setColor(c);
paint(gOffScreen);
g.drawImage(offScreenImage,0,0,null);
}
public void launchgame(){
this.setLocation(400, 300);//frame position
this.setSize(500, 400); //frame size
this.setBackground(Color.blue);//color
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
}); //close the window
this.setResizable(false);//不能改变大小。
this.setTitle("pong");//标题栏文字。
setVisible(true);
th = new Thread(new PaintThread());
th.start(); //method:start the thread.
}
public static void main (String[] args) {
bowlingarray w=new bowlingarray();
w.launchgame();
}
public void newposition (int nx, int ny)
{
ballx= nx;
bally= ny;
this.width=this.getWidth();
this.height=this.getHeight();
repaint();
}
private class PaintThread implements Runnable{
public void run() {
game = true;
boolean LR=false;
boolean UD=false;
while(true){//true,使他不断的循环
if(game){
// The ball moves from left to right
if (LR) {
ballx += right;
if (ballx == 330)//ball的位置不是中心的位置,是左边点的位置
LR= false;
}
else
{ ballx += left;
if ( ballx == 120)
LR = true; //越界后则lr是true返回执行if么,则位置就向右加了。所以又往右弹回呗。
}
// The ball moves from up to down
if (UD){
UD= false;
}
else
{ bally += down;
if ( bally <= 0)
UD = true;
}
newposition(ballx, bally);
try
{
Thread.sleep(50);//每隔50ms就会,调用改方法时,试图捕获异常。
}
catch(InterruptedException ex)
{
}
if (150 < ballx&&ballx < 180)
{
if (30<bally && bally<60)
{ score++;
}
}
if (190 < ballx&&ballx < 220)
{
if (30<bally && bally<60)
{
score++;
}
}
if (230 < ballx&&ballx < 260)
{
if (30<bally && bally<60)
{
score++;
}
}
if (270 < ballx&&ballx < 300)
{
if (30<bally && bally<60)
{
score++;
}
}
if (170 < ballx&&ballx < 200)
{
if (70<bally && bally<100)
{
score++;
}
}
if (210 < ballx&&ballx < 240)
{
if (70<bally && bally<100)
{
score++;
}
}
if (250 < ballx&&ballx < 280)
{if (70<bally && bally<100)
{
score++;
}
}
if (190 < ballx&&ballx < 220)
{if (110<bally && bally<140)
{
score++;
}
}
if (270 < ballx&&ballx < 300)
{if (110<bally && bally<140)
{
score++;
}
}
if (210 < ballx&&ballx < 240)
{
if (150<bally && bally<180)
{
score++;
}
}
if(score==10)
{
game=false;
gameOver=true;
}
// The ball stroke with the paddle1
if(ballx==x1+10 && bally>=y1 && bally<=(y1+50))
LR=true;
// The ball stroke with the paddle2
if(ballx==(x2-5) && bally>=y2 && bally<=(y2+50))
LR=false;
}
}
} }
public void remove() {
// TODO Auto-generated method stub
}}
解决方案 »
- 各位过来人给个意见,SE部分看完了,是巩固呢,还是继续EE部分,回头再巩固SE?
- 新手求助
- 排序
- 关于jcomboBox的两个问题
- 怎么样用java写程序将主从数据库(oracle)实现数据传输和下载?请各位大侠帮忙!!!
- 请问用什么函数能得到计算机的所有盘符???????
- 怎样在文本框中限定输入的数字或浮点数
- 请教菜菜的问题
- 斑竹,救救我!各位大虾,打搅一下,不好意思!专家门诊做得真不错,我很想用Offilne Explorer把它当下来,但下载下来浏览时,具体的帖子内容总是看不见,那页在出现“正在等待连接,请稍后”几个字后,总是变成一片空白,唉,怎么办呢?
- 请问这个怎么解决?
- java
- 用JDBC 批处理插入数据出错后不能回滚,数据库是mysql
能说的具体点吗?
其实保龄球一直在动的,所以一直在重画。但是当击中那10个小球时,要去掉他们好像很麻烦。dispose的话就把窗口都关掉了。
谢谢哈