java实现简单的弹球游戏

网友投稿 312 2023-04-22


java实现简单的弹球游戏

弹球游戏实现原理:

隔一定时间(小于1秒)重新绘制图像,因为Graphics类是一个抽象类,创建子类的时候需要把所有涉及的方法都得重写,所以这里使用的是创建Canvas的子类,只需要重写它的paint()方法来实现。这里我们用了键盘监听事件、Timer类等。

游戏说明:

该弹球游戏中的小球会随着时间增加速度且速度最多是横向速度和垂直速度为10。当小球的y坐标(垂直坐标)大于球拍的y坐标(垂直坐标)即判断游戏结束。控制台显示的是小球的x方向的速度和y方向的速度。

import java.awt.*;

import java.util.Random;

import javax.swing.Timer;

import java.awt.event.*;

public class PinBall

{

private Frame f=new Frame("弹球游戏");

Random rand=new Random();

//桌面的宽度以及高度

private final int TABhttp://LE_WIDTH=300;

private final int TABLE_HEIGHT=400;

//球拍的宽度以及高度以及水平位置、垂直位置

private final int RACKET_WIDTH=60;

private final int RACKET_HEIGHT=20;

private int racketX=rand.nextInt(24)*10;

private int racketY=300;

//小球的大小、运行速度、坐标

private final int BALL_SIZE=16;

private int ySpeed=1;

private double xyRate=1;

private int xSpeed=(int)(xyRate*ySpeed);

private int ballX=rand.nextInt(284);

private int ballY=1;

//创建画布

private MyCanvas tableArea=new MyCanvas();

//定义时间类

Timer timer;

//游戏是否结束的旗标

private boolean isLose=false;

//设置游戏等级

private int time_times=1;

public void init(){

tableArea.setPreferredSize(new Dimension(TABLE_WIDTH,TABLE_HEIGHT));

f.add(tableArea);

//定义键盘监听器

KeyAdapter keyProcessor=new KeyAdapter()

{

public void keyPressed(KeyEvent ke){

if(ke.getKeyCode()==KeyEvent.VK_LEFT){

if(racketX>0)

racketX-=10;

}

if(ke.getKeyCode()==KeyEvent.VK_RIGHT){

if(racketX

racketX+=10;

}

}

};

f.addKeyListener(keyProcessor);

//tableArea.addKeyListener(keyProcessor);

ActionListener taskPerformer=evt->

{

//小球碰到左边框或右边框

if(ballX<=0||ballX>=TABLE_WIDTH-BALL_SIZE){

xSpeed=-xSpeed;

}

if(ballY>racketY-BALL_SIZE&&(ballXracketX+RACKET_WIDTH-BALL_SIZRdeLjsE)){

timer.stop();

isLose=true;

tableArea.repaint();

}else if(ballY<=0||(ballY>=racketY-BALL_SIZE&&ballX>racketX&&ballX<=racketX+RACKET_WIDTH)){

ySpeed=-ySpeed;

}

ballY+=ySpeed;

ballX+=xSpeed;

tableArea.repaint();

if((xSpeed<10&&xSpeed>-10)&&(ySpeed<10&&ySpeed>-10)){

time_times++;

}

if(time_times==10){

if(xSpeed>0){

xSpeed++;

}else{

xSpeed--;

}

if(ySpeed>0){

ySpeed++;

}else{

ySpeed--;

}

time_times-=10;

System.out.println(xSpeed+" "+ySpeed);

}

};

timer=new Timer(100,taskPerformer);

timer.start();

f.pack();

f.setVisible(true);

}

class MyCanvas extends Canvas

{

public void paint(Graphics g){

if(isLose){

g.setColor(new Color(255,0,0));

g.setFont(new Font("Times",Font.BOLD,30));

g.drawString("游戏结束",50,200);

}else{

g.setColor(new Color(240,240,80));

g.fillOval(ballX,ballY,BALL_SIZE,BALL_SIZE);

g.setColor(new Color(80,80,200));

g.fillRect(racketX,racketY,RACKET_WIDTH,RACKET_HEIGHT);

}

}

}

public static void main(String[] args)

{

new PinBall().init();

}

}

racketX+=10;

}

}

};

f.addKeyListener(keyProcessor);

//tableArea.addKeyListener(keyProcessor);

ActionListener taskPerformer=evt->

{

//小球碰到左边框或右边框

if(ballX<=0||ballX>=TABLE_WIDTH-BALL_SIZE){

xSpeed=-xSpeed;

}

if(ballY>racketY-BALL_SIZE&&(ballXracketX+RACKET_WIDTH-BALL_SIZRdeLjsE)){

timer.stop();

isLose=true;

tableArea.repaint();

}else if(ballY<=0||(ballY>=racketY-BALL_SIZE&&ballX>racketX&&ballX<=racketX+RACKET_WIDTH)){

ySpeed=-ySpeed;

}

ballY+=ySpeed;

ballX+=xSpeed;

tableArea.repaint();

if((xSpeed<10&&xSpeed>-10)&&(ySpeed<10&&ySpeed>-10)){

time_times++;

}

if(time_times==10){

if(xSpeed>0){

xSpeed++;

}else{

xSpeed--;

}

if(ySpeed>0){

ySpeed++;

}else{

ySpeed--;

}

time_times-=10;

System.out.println(xSpeed+" "+ySpeed);

}

};

timer=new Timer(100,taskPerformer);

timer.start();

f.pack();

f.setVisible(true);

}

class MyCanvas extends Canvas

{

public void paint(Graphics g){

if(isLose){

g.setColor(new Color(255,0,0));

g.setFont(new Font("Times",Font.BOLD,30));

g.drawString("游戏结束",50,200);

}else{

g.setColor(new Color(240,240,80));

g.fillOval(ballX,ballY,BALL_SIZE,BALL_SIZE);

g.setColor(new Color(80,80,200));

g.fillRect(racketX,racketY,RACKET_WIDTH,RACKET_HEIGHT);

}

}

}

public static void main(String[] args)

{

new PinBall().init();

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:开源api网关排名(api网关架构)
下一篇:spring hibernate实现动态替换表名(分表)的方法
相关文章

 发表评论

暂时没有评论,来抢沙发吧~