導航:首頁 > 編程語言 > php坦克大戰代碼

php坦克大戰代碼

發布時間:2023-06-06 18:41:19

① FC坦克大戰 簡單的金手指設置或者代碼設置

實現基本要求代碼: 100行 主要涉及: 基本控制項及演算法
一、設計題目:坦克大戰

二、目的與要求
1.目的
培養學生綜合利用VB語言進行程序設計的能力,主要是培養學生的時鍾控制和綜合編程能力。
2.基本要求
n 要求使用面向對象和結構化程序設計的編程思路
n 能通過鍵盤W(上)、S(下)、A(左)、D(右)移動坦克。
n 可以通過上述按鍵隨時改變坦克的方向。
n 炮身和炮筒分別為標簽控制項
u 當坦克移動到窗體邊框時停止前進,但能改變方向。
u 按鍵J時可以按炮筒方向發射炮彈(炮彈為標簽控制項)。
3.創新要求
在基本要求達到後,進行創新設計,創新方向提示如下:
★ 增加固定障礙物。
★ 增加不固定障礙物。
★ 可發射連續炮彈。
★ 用戶可自行設置按鍵。
★ 增加坦克移動速度等級。
★ 坦克使用其他控制項
★ 其它創新(有一定難度,實現代碼在30行以上)
三、設計方法和基本原理
1.問題描述
坦克由兩個控制項構成label1作為坦克的主體部分,label2作為炮管。在一個矩形區域內可以通過鍵盤控制坦克向上下左右移動,當移動到邊界後就不再移動但可以變換方向繼續移動。通過鍵盤控制可發炮。
2.問題的解決方案
①設計程序界面
因為用的全是基本控制項,界面設計沒有什麼難度,照樣子拼就可以了。
②編寫代碼
代碼包括事件處理過程和功能函數,還有必要的聲明。變數聲明部分聲明本程序所需要的變數和數組。
事件處理包括下面的主要事件:
Form_Load()
作一些初始化的工作。
Sub Form_KeyPress(KeyAscii As Integer)
坦克的移動通過窗體的keypress事件接受鍵盤按鍵,w鍵控制坦克向上,s鍵控制坦克向下,a鍵控制坦克向左,d鍵控制坦克向右。
炮彈的發射由鍵盤事件控制,單擊j鍵發射炮彈。利用timer控制項控制炮彈延直線移動(改變炮彈的top和left屬性),當炮彈擊中邊緣後消失。
Sub Timer1_Timer()
炮彈的運動是由timer控制項控制,發炮時啟動timer事件。
四、主要技術問題的描述
本題中的難點並不在於界面設計,只需要放幾個標簽和一個時鍾控制項就可以了。
1. 坦克的移動
每當觸發keypress事件後改變坦克的組成部分label1和label2的top和left屬性,注意當坦克改變方向時,label1控制項和label2控制項的相對位置會改變。
2.炮彈的移動
每當利用keypress事件觸發坦克的發炮,首先判斷是否炮彈在運行當中,如果運行當中則不進行響應。炮彈的運動是由timer控制項控制,發炮時啟動timer事件。
五、參考資料
★ Visual Basic 程序設計教程 及 Visual Basic 實驗教程
★ VB6.0程序員指南
★ VB6.0控制項參考手冊
★ 設計資料中的scientificcalculator.ZIP以及「計算器.ZIP」
★ MSDN中文版,課程設計網站上提供的其它資料。

java坦克大戰,這段代碼為什麼子彈的坐標在變,卻不能repaint,但是按下任意鍵盤的建卻重繪了呢

Mypanel的 run方法里要調用repaint方法 否則你的repaint方法只會在keyPressed發生的時候才調用

修改一下兩個地方

(1)

// 鍵盤獲取事件的函數
public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub
if (arg0.getKeyCode() == KeyEvent.VK_J) {
if (hero.shot.size() < 5) {
hero.shott();
}
}
if (arg0.getKeyCode() == KeyEvent.VK_W) {
hero.setSDC(hero.getSpeed(), 0, hero.getColor());
hero.moveUp();
} else if (arg0.getKeyCode() == KeyEvent.VK_S) {
hero.setSDC(hero.getSpeed(), 1, hero.getColor());
hero.moveDown();
} else if (arg0.getKeyCode() == KeyEvent.VK_A) {
hero.setSDC(hero.getSpeed(), 2, hero.getColor());
hero.moveLeft();
} else if (arg0.getKeyCode() == KeyEvent.VK_D) {
hero.setSDC(hero.getSpeed(), 3, hero.getColor());
hero.moveRight();
}
/**
* 這個repaint注釋掉
*/
//this.repaint();

}

(2)

// 線程
/**
* 一秒鍾60幀
*/
public void run() {
// TODO Auto-generated method stub
while(true){
this.repaint();
try {

Thread.sleep(1000 / 60);
} catch (InterruptedException e) {
// TODO 自動生成的 catch 塊
e.printStackTrace();
}
}

}


完整代碼如下:

importjava.awt.*;

importjavax.swing.*;

importjava.util.*;
importjava.awt.event.*;

publicclassaaaextendsJFrame{
publicstaticvoidmain(String[]args){
aaaa1=newaaa();
Threadt1=newThread(a1.mp);
t1.start();
}

MyPanelmp=null;

publicaaa(){
mp=newMyPanel();
this.add(mp);
this.addKeyListener(mp);
this.setSize(500,500);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

,Runnable{
MyTankhero=null;
Vector<EmenyTank>emeny=newVector<EmenyTank>();
intemsize=5;

//鍵盤獲取事件的函數
publicvoidkeyPressed(KeyEventarg0){
//TODOAuto-generatedmethodstub
if(arg0.getKeyCode()==KeyEvent.VK_J){
if(hero.shot.size()<5){
hero.shott();
}
}
if(arg0.getKeyCode()==KeyEvent.VK_W){
hero.setSDC(hero.getSpeed(),0,hero.getColor());
hero.moveUp();
}elseif(arg0.getKeyCode()==KeyEvent.VK_S){
hero.setSDC(hero.getSpeed(),1,hero.getColor());
hero.moveDown();
}elseif(arg0.getKeyCode()==KeyEvent.VK_A){
hero.setSDC(hero.getSpeed(),2,hero.getColor());
hero.moveLeft();
}elseif(arg0.getKeyCode()==KeyEvent.VK_D){
hero.setSDC(hero.getSpeed(),3,hero.getColor());
hero.moveRight();
}
/**
*這個repaint注釋掉
*/
//this.repaint();

}

publicvoidkeyReleased(KeyEventarg0){
//TODOAuto-generatedmethodstub
}

publicvoidkeyTyped(KeyEventarg0){
//TODOAuto-generatedmethodstub
}

//完畢
publicMyPanel(){
hero=newMyTank(250,250);
hero.setSDC(5,2,2);
for(inti=0;i<emsize;++i){
EmenyTankem=newEmenyTank((i+1)*60,20);
em.setSDC(5,1,1);
emeny.add(em);
}
}

//線程
/**
*一秒鍾60幀
*/
publicvoidrun(){
//TODOAuto-generatedmethodstub
while(true){
this.repaint();
try{

Thread.sleep(1000/60);
}catch(InterruptedExceptione){
//TODO自動生成的catch塊
e.printStackTrace();
}
}

}

publicvoidpaint(Graphicsg){
super.paint(g);
//畫板,坦克得放在畫板後頭
g.fillRect(0,0,400,400);
//paint敵人坦克
for(inti=0;i<emeny.size();++i){
EmenyTankem=null;
em=emeny.get(i);
this.drawTank(em.getX(),em.getY(),g,em.getDirect(),
em.getColor());
}
//畫我自己的坦克
this.drawTank(hero.getX(),hero.getY(),g,hero.getDirect(),
hero.getColor());
//畫出我的子彈
for(inti=0;i<hero.shot.size();i++){
ShotmyShot=hero.shot.get(i);
if(myShot!=null&&myShot.live==true){
g.draw3DRect(myShot.x,myShot.y,2,2,false);
}
if(myShot.live==false){
hero.shot.remove(myShot);
}
}
}

publicvoiddrawTank(intx,inty,Graphicsg,intdirect,intcolor){
//判斷坦克的顏色(敵我)然後畫出坦克
switch(color){
case0:
g.setColor(Color.BLUE);
break;
case1:
g.setColor(Color.YELLOW);
break;
case2:
g.setColor(Color.GREEN);
break;
}
//判斷坦克的方向然後再畫出坦克
switch(direct){
case0:
g.fill3DRect(x,y,10,30,false);
g.fill3DRect(x+26,y,10,30,false);
g.fill3DRect(x+10,y+5,16,20,false);
g.drawLine(x+18,y+15,x+18,y);
break;
case1:
g.fill3DRect(x,y,10,30,false);
g.fill3DRect(x+26,y,10,30,false);
g.fill3DRect(x+10,y+5,16,20,false);
g.drawLine(x+18,y+15,x+18,y+30);
break;
case2:
g.fill3DRect(x+3,y-3,30,10,false);
g.fill3DRect(x+3,y+23,30,10,false);
g.fill3DRect(x+8,y+7,20,16,false);
g.drawLine(x+18,y+15,x+3,y+15);
break;
case3:
g.fill3DRect(x+3,y-3,30,10,false);
g.fill3DRect(x+3,y+23,30,10,false);
g.fill3DRect(x+8,y+7,20,16,false);
g.drawLine(x+18,y+15,x+33,y+15);
break;
}
}
}

{
publicEmenyTank(intx,inty){
//TODOAuto-generatedmethodstub
super(x,y);
}

publicvoidrun(){
}
}

classShotimplementsRunnable{
protectedintx;
protectedinty;
protectedintdirect;
protectedintspeed=4;
protectedbooleanlive=true;

publicvoidsetX(intx){
this.x=x;
this.y=y;
}

publicintgetX(){
returnx;
}

publicintgetY(){
returny;
}

publicvoidsetDirect(intdirect){
this.direct=direct;
}

publicintgetDirect(){
returndirect;
}

publicvoidsetSpeed(intspeed){
this.speed=speed;
}

publicintgetSpeed(){
returnspeed;
}

//子彈的上下左右以及走的速度
publicvoidrun(){
//TODOAuto-generatedmethodstub
while(true){
try{
Thread.sleep(100);
}catch(Exceptione){
}
switch(direct){
case0:
y-=speed;
break;
case1:
y+=speed;
break;
case2:
x-=speed;
break;
case3:
x+=speed;
break;
}
if(x>400||x<0||y>400||y<0){
this.live=false;
break;
}
}
}
}

classTank{
protectedintx;
protectedinty;
protectedintspeed=5;
protectedintdirect;
protectedintcolor;
booleanlive;

publicTank(intx,inty){
this.x=x;
this.y=y;
}

publicintgetX(){
returnx;
}

publicintgetY(){
returny;
}

publicvoidsetSDC(intspeed,intdirect,intcolor){
this.speed=speed;
this.direct=direct;
this.color=color;
}

publicintgetSpeed(){
returnspeed;
}

publicintgetDirect(){
returndirect;
}

publicintgetColor(){
returncolor;
}
}

classMyTankextendsTank{
publicMyTank(intx,inty){
//TODOAuto-generatedmethodstub
super(x,y);
}

Vector<Shot>shot=newVector<Shot>();
Shotshota=null;

publicvoidshott(){
switch(this.direct){
case0:
shota=newShot();
shota.x=x+18;
shota.y=y;
shota.direct=0;
shot.add(shota);
break;
case1:
shota=newShot();
shota.x=x+18;
shota.y=y+30;
shota.direct=1;
shot.add(shota);
break;
case2:
shota=newShot();
shota.x=x+3;
shota.y=y+15;
shota.direct=2;
shot.add(shota);
break;
case3:
shota=newShot();
shota.x=x+33;
shota.y=y+15;
shota.direct=3;
shot.add(shota);
break;
}
Threadt=newThread(shota);
t.start();
}

publicvoidmoveUp(){
if(y>0){
y-=speed;
}
}//我的坦克得在自己的類里定義怎麼移動

publicvoidmoveDown(){
if(y<367){
y+=speed;
}
}

publicvoidmoveLeft(){
if(x>0){
x-=speed;
}
}

publicvoidmoveRight(){
if(x<365){
x+=speed;
}
}
}



③ 新手求教,坦克大戰源碼如何用Eclipse運行

坦克大戰源代碼應該是個完整的項目吧。
對於完整的帶項目配置文件的java源碼,按步驟操作即可:
File -> Import -> General
選擇Existing Projects into Workspace,選擇要導入的文件,點擊「finish",OK。

閱讀全文

與php坦克大戰代碼相關的資料

熱點內容
日本電影校園男主最後死了 瀏覽:957
香港古裝艷情史 瀏覽:461
一個小男孩和一個小女孩離家出走的電影 瀏覽:942
小說主角陳凡 瀏覽:75
壓縮機連鎖條件 瀏覽:234
奢侈的pdf 瀏覽:834
拜誰都不如拜自己電影叫啥 瀏覽:27
啄木鳥電影網頁 瀏覽:418
戀聽網有聲小說 瀏覽:984
《與鳳行》txt下載 瀏覽:603
購自卸車下載什麼app 瀏覽:568
phpurlencode編碼 瀏覽:810
主角抗戰現代來回穿越被封的小說 瀏覽:440
ppt怎麼pdf 瀏覽:809
能看動漫成人的網址 瀏覽:769
親親寶貝by小鳥baby免費閱讀 瀏覽:25
《不看左上角》電影 瀏覽:867
傑西卡摩爾拍過的電影 瀏覽:12
主角叫江辰的小說 瀏覽:424
狼鷹電影女演員表 瀏覽:221