java弹球计分代码 java篮球弹跳
急求一个java的躲避弹球小游戏的代码
import java.awt.*;
创新互联建站主要业务有网站营销策划、成都做网站、成都网站设计、微信公众号开发、成都微信小程序、html5、程序开发等业务。一次合作终身朋友,是我们奉行的宗旨;我们不仅仅把客户当客户,还把客户视为我们的合作伙伴,在开展业务的过程中,公司还积累了丰富的行业经验、全网整合营销推广资源和合作伙伴关系资源,并逐渐建立起规范的客户服务和保障体系。
import java.awt.event.*;
import java.util.Random;
import javax.swing.Timer;
public class PinBall
{
private final int TABLE_WIDTH = 300;//桌面宽度
private final int TABLE_HEIGHT = 400;//桌面高度
private final int RACKET_Y = 340;//球拍的垂直位置
private final int RACKET_HEIGHT = 20;//球拍高度
private final int RACKET_WIDTH = 60;//球拍宽度
private final int BALL_SIZE = 16;//球的大小
private Frame f = new Frame("弹球游戏");//实例化一个窗口
Random rand = new Random();//实例化一个随机数生成器
private int ySpeed = 10;//小球的纵向运动数度、
private double xyRate = rand.nextDouble() - 0.5;//返回一个-0.5到0.5之间的比率用控制小球运动方向
private int xSpeed = (int)(ySpeed*xyRate*2);//这个横向速度在-10到10之间,产生左右摆动运动效果
private int ballX = rand.nextInt(200)+20;//小球开始的横坐标位置,200表示产生0到100之间的随机数
private int ballY = rand.nextInt(10)+20;//小球开始的纵坐标位置
private int racketX = rand.nextInt(200);//球拍开始时的横坐标位置
private MyCanvas tableArea = new MyCanvas();//实力化一个画布工具,集成Canvas类
private String shape = "";//保存需要绘制图形的字符串属性
Timer timer;//声明一个时间变量
private boolean isLose = false;//表示游戏是否结束
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;//按一左键次向左移动10个像素
}
if(ke.getKeyCode()==KeyEvent.VK_RIGHT)//按下键盘右键时
{
if(racketX TABLE_WIDTH - RACKET_WIDTH)//球拍右边框不能出画布的右边框
racketX +=10;//按一次右键移动向右移动10个像素
}
}
};
f.addKeyListener(keyProcessor);//给窗口添加键盘监听器
tableArea.addKeyListener(keyProcessor);//给画布添加键盘监听器
ActionListener taskPerformer = new ActionListener()//这里是实例化了一个监听接口,这个接口里面只有一个方法
{
public void actionPerformed(ActionEvent evt)//重写这个接口里面的方法,判断小球的位置
{
if(ballX=0 || ballX=TABLE_WIDTH-BALL_SIZE)//保证小球横向上在画布之内运动
{
xSpeed = -xSpeed;//触发反方向运动
}
if(ballY=RACKET_Y-BALL_SIZE(ballXracketX||ballXracketX+RACKET_WIDTH))//出了球拍的可击打范围
{
timer.stop();//停止对监听器的触发
isLose=true;//将标志isLose变量置为true
tableArea.repaint();//调用画布的重绘方法
}
else if(ballY=0||(ballY=RACKET_Y-BALL_SIZEballYracketXballX=racketX+RACKET_WIDTH))//小球在球拍之内,而其到达球拍的高度
{
ySpeed=-ySpeed;//上下方向改变,小球反弹
}
ballY+=ySpeed;//小球的坐标在纵向上增加
ballX+=xSpeed;//小球的坐标在横向上的增加
tableArea.repaint();//调用画布的重绘方法3
}
};
timer = new Timer(100,taskPerformer);//每隔0.1秒运行一次监听器
timer.start();//计时器开始运行
f.addWindowListener(new MyListener());//关闭窗口事件
f.pack();//设置窗口最佳大小
f.setVisible(true);//显示窗口
}
class MyListener extends WindowAdapter//关闭窗口的类
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
public static void main(String[] args)//程序入口
{
new PinBall().init();//调用PinBall类里面的init()方法
}
class MyCanvas extends Canvas//建一个集成Canvas类的类
{
public void paint(Graphics g)//重写父类的绘图方法
{
if(isLose)//如果isLose为真,则在画布里打印“游戏已结束”
{
g.setColor(new Color(255,0,0));//当前颜色
g.setFont(new Font("黑体",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,RACKET_Y,RACKET_WIDTH,RACKET_HEIGHT);//填充颜色,根据坐标和长宽填充矩形
}
}
}
}
[求助]急求弹球游戏代码
已使用TC2.0 IDE测试,正常运行:
引用:
#include "graphics.h"
#include "math.h"
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define ESC 0x011b
int x,y,x1,y1,key,t1,t2,keyx,keylong,keymove,over=0,i,tt=0,fast;
int ballx[9]={53,103,263,120,236,390,410,450,600};
int bally[9]={158,239,120,113,30,313,120,263,231},ballz[9]={0};
float ls;
win()
{float lx,ly,ll;
for(i=0;i9;i++)
{
if(ballz==1)/*如果球体已经消失就不判断*/
continue;
lx=(float)(ballx-x);
ly=(float)bally-y;
ll=lx*lx+ly*ly;
ls=sqrt(ll);
if((int)ls17)/*判断两球是否碰到*/
{tt++;/*记录消失几个球*/
fast-=50;/*速度加快*/
if(i%2==0)/*移动加快*/
keymove+=5;
else
{
setcolor(0);
rectangle(keyx,460,keyx+keylong,470);
keylong-=10;/*长条变短*/
setcolor(YELLOW);
rectangle(keyx,460,keyx+keylong,470);
}
ballz=1;
setcolor(0);
circle(ballx,bally,10);
if(ybally)
if(xballx)
{t1=-1;t2=-1;}
else
{t1=1;t2=-1;}
else
if(xballx)
{t1=-1;t2=1;}
else
{t1=1;t2=1;}
}
}
}
fungo()
{ x=100,y=50,keyx=200,keylong=100,keymove=15,fast=600;
for(i=0;i9;i++)
{
setcolor(i+1);
circle(ballx,bally,10);
}
sleep(1);
t1=t2=1;
while(1)
{
while(!kbhit())
{
setcolor(YELLOW);
rectangle(keyx,460,keyx+keylong,470);
x1=x;
y1=y;
x+=t1;
y+=t2;
setcolor(4);
circle(x,y,6);
delay(fast);
setcolor(0);
circle(x,y,6);
win();
if(tt==9)/*胜利*/
{
cleardevice();
over=1;
setcolor(RED);
settextstyle(0,0,4);
setusercharsize(3,1,3,1);
outtextxy(200,200," YOU WIN! ");
break;
}
if(y480)/*失败*/
{
cleardevice();
over=1;
setcolor(RED);
settextstyle(0,0,4);
setusercharsize(3,1,3,1);
outtextxy(200,200,"GAME OVER");
break;
}
else if(x==11y==11)/*判断碰到东西*/
{t1=1;t2=1;}
else if(x==629y==11)
{t1=-1;t2=1;}
else if(x==11)
{
if(yy1)
{t1=1;t2=-1;}
else
{t1=1;t2=1;}
}
else if(y==11)
{
if(xx1)
{t1=-1;t2=1;}
else
{t1=1;t2=1;}
}
else if(x==629)
{
if(y1y)
{t1=-1;t2=-1;}
else
{t1=-1;t2=1;}
}
else if(y==449)
{
if(x=keyxx=keyx+keylong)
{
if(x(keyx+keylong/4))
{t1=-1;t2=-1;}
else if(x=(keyx+keylong/4)x(keyx+keylong/2))
{t1=-1;t2=-2;}
else if(x=(keyx+keylong/2)x(keyx+keylong/4*3))
{t1=1;t2=-2;}
else
{t1=1;t2=-1;}
}
}
}
if(over)
break;
key=bioskey(0);
setcolor(0);
rectangle(keyx,460,keyx+keylong,470);
if(key==ESC)
{cleardevice();break;}
else if(key==LEFT)
keyx-=keymove;
else if(key==RIGHT)
keyx+=keymove;
}
}
main()
{int gd=DETECT,gr;
initgraph(gd,gr,"c:\\tc");
fungo();
getch();
sleep(1);
closegraph();
}
JAVA如何实现小球的弹性碰撞
我没用java写过代码,所以我只说算法,代码你自己翻译下
按C的语法来:
void xiaoqiu
{
int UB=10,DB=200,LB=10,RB=200; //定义弹球范围的边界
int sh=1; //定义横向步长
int sz=1; //定义纵向步长(两步长之比决定了反弹的角度)
int x=LB,y=UB; //定义坐标
int i=10000; //循环次数(自己选择跳出手段)
while(i0)
{
i--;
x=x+sh;
if(x=RB||x=LB) sh=-sh; //碰壁后步长变反
y=y+sz;
if(y=DB||y=UB) sz=-sz; //碰壁后步长变反
(显示代码)
}
return;
}
总得来说,就是相当于横向和纵向分别处理移动、反弹的问题,碰壁后步长变为相反数
不懂请追问
java弹球游戏
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class CopyOfBallCrash implements Runnable {
public static void main(final String[] args) {
new Thread(new CopyOfBallCrash()).start();
}
private final int width = 400;
private final int height = 700;
private int mouse_X, mouse_Y;
private final BufferedImage offscreen = new BufferedImage(width, height,
BufferedImage.TYPE_4BYTE_ABGR);
private final JPanel panel = new JPanel();
private final Shape ball = new Shape(100, 100, 1, 1, 20);
private final Shape rect = new Shape(0, 100, 20);
public CopyOfBallCrash() {
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.setPreferredSize(new Dimension(width, height));
ball.setBounds(width, height);
rect.setBounds(width, height);
frame.setContentPane(panel);
panel.addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseDragged(final MouseEvent e) {}
@Override
public void mouseMoved(final MouseEvent e) {
mouse_X = e.getX();
mouse_Y = e.getY();
}
});
frame.pack();
frame.setVisible(true);
}
public void paint(final Graphics g) {
final Graphics2D g2d = offscreen.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.fillRect(0, 0, width, height);
g2d.setColor(Color.blue);
rect.drawRect(g2d, mouse_X);
g2d.setColor(Color.red);
ball.drawOval(g2d, rect);
if (Shape.isLose) g2d.drawString("你输了!!!", 100, 300);
g2d.dispose();
g.drawImage(offscreen, 0, 0, null);
}
@Override
public void run() {
while (true)
paint(panel.getGraphics());
}
}
class Shape {
public int width, height;
public int x, y, vx, vy, r, w, h;
public static boolean isLose;
public Shape(final int x, final int y, final int vx, final int vy, final int r) {
super();
this.x = x;
this.y = y;
this.vx = vx;
this.vy = vy;
this.r = r;
}
public Shape(final int x, final int w, final int h) {
super();
this.x = x;
this.w = w;
this.h = h;
}
public final void setBounds(final int width, final int height) {
this.width = width;
this.height = height;
}
public final void drawOval(final Graphics2D g2d, final Shape shape) {
if (y + h = height) {
isLose = true;
return;
}
if (x + vx = 0 || x + vx + w = width) vx = -vx;
if (y + vy = 0) vy = -vy;
if (isCrashOutside(shape.x, shape.y, shape.w, shape.h) y + w = shape.y)
vy = -vy;
x += vx;
y += vy;
g2d.fillOval(x, y, r, r);
}
public final void drawRect(final Graphics2D g2d, final int mouseX) {
y = height - h;
if (x + w width x mouseX) x++;
if (x 0 x mouseX) x--;
g2d.fillRect(x, y, w, h);
}
public final boolean isCrashOutside(final int x, final int y, final int w,
final int h) {
return (this.x x ? this.x = w + x : x = r + this.x)
(this.y y ? this.y = h + y : y = r + this.y);
}
}
文章标题:java弹球计分代码 java篮球弹跳
文章地址:http://ybzwz.com/article/ddschci.html