天天看点

java桌球小游戏的球袋怎么编程

java小游戏编程
*File:ControlPanel.java
*User:相思无偿
*Date:2004.12.3
*Describe:俄罗斯方块的Java实现
importjavax.swing.*;
importjavax.swing.border.Border;
importjavax.swing.border.EtchedBorder;
importjava.awt.*;
importjava.awt.event.*;
*控制面板类,继承自JPanel.
*上边安放预显窗口、等级、得分、控制按钮
*主要用来控制游戏进程。
classControlPanelextendsJPanel{
privateJTextField
tfLevel=newJTextField(""+ErsBlocksGame.DEFAULT_LEVEL),
tfScore=newJTextField("0");
privateJButton
btPlay=newJButton("Play"),
btPause=newJButton("Pause"),
btStop=newJButton("Stop"),
btTurnLevelUp=newJButton("Turnhard"),
btTurnLevelDown=newJButton("Turneasy");
privateJPanelplTip=newJPanel(newBorderLayout());
privateTipPanelplTipBlock=newTipPanel();
privateJPanelplInfo=newJPanel(newGridLayout(4,1));
privateJPanelplButton=newJPanel(newGridLayout(5,1));
privateTimertimer;
privateErsBlocksGamegame;
privateBorderborder=newEtchedBorder(
EtchedBorder.RAISED,Color.white,newColor(148,145,140));
*控制面板类的构造函数
*@paramgameErsBlocksGame,ErsBoxesGame类的一个实例引用,
*方便直接控制ErsBoxesGame类的行为。
publicControlPanel(finalErsBlocksGamegame){
setLayout(newGridLayout(3,1,0,4));
this.game=game;
plTip.add(newJLabel("Nextblock"),BorderLayout.NORTH);
plTip.add(plTipBlock);
plTip.setBorder(border);
plInfo.add(newJLabel("Level"));
plInfo.add(tfLevel);
plInfo.add(newJLabel("Score"));
plInfo.add(tfScore);
plInfo.setBorder(border);
tfLevel.setEditable(false);
tfScore.setEditable(false);
plButton.add(btPlay);
plButton.add(btPause);
plButton.add(btStop);
plButton.add(btTurnLevelUp);
plButton.add(btTurnLevelDown);
plButton.setBorder(border);
add(plTip);
add(plInfo);
add(plButton);
addKeyListener(newControlKeyListener());
btPlay.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventae){
game.playGame();
btPause.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventae){
if(btPause.getText().equals(newString("Pause"))){
game.pauseGame();
}else{
game.resumeGame();
btStop.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventae){
game.stopGame();
btTurnLevelUp.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventae){
intlevel=Integer.parseInt(tfLevel.getText());
if(level
     
    
tfLevel.setText(""+(level+1));
}catch(NumberFormatExceptione){
requestFocus();
btTurnLevelDown.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventae){
intlevel=Integer.parseInt(tfLevel.getText());
if(level>1)
tfLevel.setText(""+(level-1));
}catch(NumberFormatExceptione){
requestFocus();
addComponentListener(newComponentAdapter(){
publicvoidcomponentResized(ComponentEventce){
plTipBlock.fanning();
timer=newTimer(500,newActionListener(){
publicvoidactionPerformed(ActionEventae){
tfScore.setText(""+game.getScore());
intscoreForLevelUpdate=
game.getScoreForLevelUpdate();
if(scoreForLevelUpdate>=ErsBlocksGame.PER_LEVEL_SCORE
&&scoreForLevelUpdate>0)
game.levelUpdate();
timer.start();
*设置预显窗口的样式,
*@paramstyleint,对应ErsBlock类的STYLES中的28个值
publicvoidsetTipStyle(intstyle){
plTipBlock.setStyle(style);
*取得用户设置的游戏等级。
*@returnint,难度等级,1-ErsBlocksGame.MAX_LEVEL
publicintgetLevel(){
intlevel=0;
level=Integer.parseInt(tfLevel.getText());
}catch(NumberFormatExceptione){
returnlevel;
*让用户修改游戏难度等级。
*@paramlevel修改后的游戏难度等级
publicvoidsetLevel(intlevel){
if(level>0&&level<11)tfLevel.setText(""+level);
*设置"开始"按钮的状态。
publicvoidsetPlayButtonEnable(booleanenable){
btPlay.setEnabled(enable);
publicvoidsetPauseButtonLabel(booleanpause){
btPause.setText(pause?"Pause":"Continue");
*重置控制面板
publicvoidreset(){
tfScore.setText("0");
plTipBlock.setStyle(0);
*重新计算TipPanel里的boxes[][]里的小框的大小
publicvoidfanning(){
plTipBlock.fanning();
*预显窗口的实现细节类
privateclassTipPanelextendsJPanel{
privateColorbackColor=Color.darkGray,frontColor=Color.lightGray;
privateErsBox[][]boxes=
newErsBox[ErsBlock.BOXES_ROWS][ErsBlock.BOXES_COLS];
privateintstyle,boxWidth,boxHeight;
privatebooleanisTiled=false;
*预显窗口类构造函数
publicTipPanel(){
for(inti=0;i
     
    
for(intj=0;j
     
    
boxes[i][j]=newErsBox(false);
*预显窗口类构造函数
*@parambackColorColor,窗口的背景色
*@paramfrontColorColor,窗口的前景色
publicTipPanel(ColorbackColor,ColorfrontColor){
this();
this.backColor=backColor;
this.frontColor=frontColor;
*设置预显窗口的方块样式
*@paramstyleint,对应ErsBlock类的STYLES中的28个值
publicvoidsetStyle(intstyle){
this.style=style;
repaint();
*覆盖JComponent类的函数,画组件。
*@paramg图形设备环境
publicvoidpaintComponent(Graphicsg){
super.paintComponent(g);
if(!isTiled)fanning();
intkey=0x8000;
for(inti=0;i
     
    
for(intj=0;j
     
    
Colorcolor=(((key&style)!=0)?frontColor:backColor);
g.setColor(color);
g.fill3DRect(j*boxWidth,i*boxHeight,
boxWidth,boxHeight,true);
key>>=1;
*根据窗口的大小,自动调整方格的尺寸
publicvoidfanning(){
boxWidth=getSize().width/ErsBlock.BOXES_COLS;
boxHeight=getSize().height/ErsBlock.BOXES_ROWS;
isTiled=true;
privateclassControlKeyListenerextendsKeyAdapter{
publicvoidkeyPressed(KeyEventke){
if(!game.isPlaying())return;
ErsBlockblock=game.getCurBlock();
switch(ke.getKeyCode()){
caseKeyEvent.VK_DOWN:
block.moveDown();
break;
caseKeyEvent.VK_LEFT:
block.moveLeft();
break;
caseKeyEvent.VK_RIGHT:
block.moveRight();
break;
caseKeyEvent.VK_UP:
block.turnNext();
break;
default:
break;
最近在看高淇老师的java300集,第22集的时候是写一个桌球小游戏,但是里面的图片加载不出来。求大神解答
图片路径不对,你这里用的是想对路径,图片资源要放在项目加载资源部分才行,不然就改成绝对路径
急求一个java的躲避弹球小游戏的代码
import java.awt.*; 
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&&(ballX
    
     racketX+RACKET_WIDTH))//出了球拍的可击打范围 
    
{ 
timer.stop();//停止对监听器的触发 
isLose=true;//将标志isLose变量置为true 
tableArea.repaint();//调用画布的重绘方法 
} 
else if(ballY<=0||(ballY>=RACKET_Y-BALL_SIZE&&ballY>racketX&&ballX<=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);//填充颜色,根据坐标和长宽填充矩形 
} 
} 
}具体要求如下:用JAVA语言设计一个弹球游戏。要求在屏幕上画出一个密闭的围墙球在木棒以下就游戏结束 15分就想换一个弹球的代码? 乐成手机楼主有没有完成啊?我也需要这个代码求啊求真是不巧啊,居然做完了,或许我看到太晚了,呵呵,还是回答一下吧!这个简单,你看看Graphic类
用j2me编写的台球游戏中方向的控制和发射实现方法
public class FlyUp extends Thread {
 public void run() {
 this.flyUp();
 public synchronized void flyUp() {
 while (Plane.isUpMove && Plane.plane_y >= 0) {
 try {
 Thread.sleep(5);
 } catch (InterruptedException e) {
 e.printStackTrace();
 Plane.plane_y-=2;
 Fly.jl_plane.setLocation(Plane.plane_x, Plane.plane_y);
 this.stop();
这是我做的一个java飞机游戏中飞机向上飞的代码 希望能给你写思路
可以把台球也看作一个飞机