天天看點

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飛機遊戲中飛機向上飛的代碼 希望能給你寫思路
可以把撞球也看作一個飛機