當前位置:首頁 » 操作系統 » java坦克大戰源碼

java坦克大戰源碼

發布時間: 2022-09-24 08:29:09

『壹』 用java寫坦克大戰難嗎

不難,你基礎沒打好才有這種感覺。
坦克大戰,so easy啊。

『貳』 java 坦克大戰 一段代碼 關於圖像繪制 不懂 求解

這個是雙緩沖的繪制方法

if (offScreenImage == null)
{
offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT);
}
這里是先在內存中創建一個 offScreenImage 的緩沖圖像

gOffScreen.drawImage(imgs, 0, 0, GAME_WIDTH, GAME_HEIGHT,null);
這里是將背景圖片繪制到剛才創建的這塊緩沖區上

paint(gOffScreen);
g.drawImage(offScreenImage, 0, 0, null);
這里是將緩沖區再繪制到實際屏幕上

採用雙緩沖可以保證畫面不會閃爍 因為圖片的刷新都是發生在內存區上的 實際屏幕上用戶是感覺不出來的 所以人眼感覺不到閃爍

『叄』 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;
}
}
}



『肆』 用java做《坦克大戰》需要積累那些java知識

java AWT Swing 還有一些工具類 比如說Random(獲取隨機數的)一些簡單數學計算在程序中的應用 另外還需要線程來控制畫面刷新 也需要一點線程知識 另外就是一些游戲用到的基礎知識了 比如說碰撞檢測 雙緩沖 一些paint方法的應用 如根據坦克方向將坦克畫出來等等 都比較簡單

『伍』 (100分)Java寫「坦克大戰」

package com.bjsxt.tank;

import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
* 這個類的作用是坦克游戲的主窗口
* @author mashibing
*
*/

public class TankClient extends Frame {
/**
* 整個坦克游戲的寬度
*/
public static final int GAME_WIDTH = 800;
public static final int GAME_HEIGHT = 600;

Tank myTank = new Tank(50, 50, true, Direction.STOP, this);

Wall w1 = new Wall(100, 200, 20, 150, this), w2 = new Wall(300, 100, 300, 20, this);

List<Explode> explodes = new ArrayList<Explode>();
List<Missile> missiles = new ArrayList<Missile>();
List<Tank> tanks = new ArrayList<Tank>();
Image offScreenImage = null;

Blood b = new Blood();

public void paint(Graphics g) {
/*
* 指明子彈-爆炸-坦克的數量
* 以及坦克的生命值
*/
g.drawString("missiles count:" + missiles.size(), 10, 50);
g.drawString("explodes count:" + explodes.size(), 10, 70);
g.drawString("tanks count:" + tanks.size(), 10, 90);
g.drawString("tanks life:" + myTank.getLife(), 10, 110);

if(tanks.size() <= 0) {
for(int i=0; i<Integer.parseInt(PropertyMgr.getProperty("reProceTankCount")); i++) {
tanks.add(new Tank(50 + 40*(i+1), 50, false, Direction.D, this));
}
}

for(int i=0; i<missiles.size(); i++) {
Missile m = missiles.get(i);
m.hitTanks(tanks);
m.hitTank(myTank);
m.hitWall(w1);
m.hitWall(w2);
m.draw(g);
//if(!m.isLive()) missiles.remove(m);
//else m.draw(g);
}

for(int i=0; i<explodes.size(); i++) {
Explode e = explodes.get(i);
e.draw(g);
}

for(int i=0; i<tanks.size(); i++) {
Tank t = tanks.get(i);
t.collidesWithWall(w1);
t.collidesWithWall(w2);
t.collidesWithTanks(tanks);
t.draw(g);
}

myTank.draw(g);
myTank.eat(b);
w1.draw(g);
w2.draw(g);
b.draw(g);
}

public void update(Graphics g) {
if(offScreenImage == null) {
offScreenImage = this.createImage(GAME_WIDTH, GAME_HEIGHT);
}
Graphics gOffScreen = offScreenImage.getGraphics();
Color c = gOffScreen.getColor();
gOffScreen.setColor(Color.BLACK);
gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
gOffScreen.setColor(c);
paint(gOffScreen);
g.drawImage(offScreenImage, 0, 0, null);
}

/**
* 本方法顯示坦克主窗口
*
*/
public void lauchFrame() {

int initTankCount = Integer.parseInt(PropertyMgr.getProperty("initTankCount"));
for(int i=0; i<initTankCount; i++) {
tanks.add(new Tank(50 + 40*(i+1), 50, false, Direction.D, this));
}

//this.setLocation(400, 300);
this.setSize(GAME_WIDTH, GAME_HEIGHT);
this.setTitle("TankWar");
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setResizable(false);
this.setBackground(Color.GREEN);

this.addKeyListener(new KeyMonitor());

setVisible(true);

new Thread(new PaintThread()).start();
}

public static void main(String[] args) {
TankClient tc = new TankClient();
tc.lauchFrame();
}

private class PaintThread implements Runnable {

public void run() {
while(true) {
repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

private class KeyMonitor extends KeyAdapter {

public void keyReleased(KeyEvent e) {
myTank.keyReleased(e);
}

public void keyPressed(KeyEvent e) {
myTank.keyPressed(e);
}

}
}

『陸』 關於JAVA坦克大戰中坦克移動的問題,總有問題 不知道哪兒錯了,求高手解答

源碼[email protected]" data_size="0.01M" data_filelogo="https://gss0.bdstatic.com//yun-file-logo/file-logo-6.png" data_number="1" data_sharelink="https://pan..com/s/1i3pqmDR" data_code="">

用多線程做的坦克大戰,這里提供src目錄,運行請自己建立工程,並將src目錄下的所有包文件導入工程src目錄,main包運行; 很多年前寫的希望能幫到你

『柒』 java韓順平的視頻寫的坦克大戰中的坦克的繪制和移動,不能左右移動,還有坦克移動就會解體

比較貴發生毒皮哦咖啡色的

『捌』 我用java做一個坦克大戰的小游戲,怎麼實現游戲暫停和繼續的功能,敵人的坦克還有子彈類是線程

線程設置sleep阻塞,或則使用Util.Timer類和TimerTask來實現暫停功能,繼續可以用interupt中斷阻塞,我是這樣想的,不知道正不正確,希望對你有所幫助。

『玖』 在java中編寫坦克大戰時這樣實現子彈發射擊中敵方但敵方和子彈不消失

你是否有對子彈和坦克,這2個類進行完好的設計,你是否應該加一個生命狀態的屬性來實現此功能,比如加一個boolean islive;在繪圖的方法中,繪制g.drawXXX之前,if(islive)一下,再然後在子彈命中的判斷中,命中後把子彈的生命值和坦克的生命值都改變,而繪圖的方法通過線程不斷刷新,自然就消失了!

------最後,希望採納!畢竟我們純手打!

『拾』 我在做韓順平老師的坦克大戰java項目,剛做到能使敵方坦克爆炸時,產生爆炸效果,可是每次運行時,擊中第

在你的paint函數中加上下面三句話就ok了。
g.drawImage(image1, 100, 100, 30, 30, this);
g.drawImage(image2, 100, 100, 30, 30, this);
g.drawImage(image3, 100, 100, 30, 30, this);

在super下面寫上。

熱點內容
linux下ntp伺服器搭建 發布:2024-09-08 08:26:46 瀏覽:742
db2新建資料庫 發布:2024-09-08 08:10:19 瀏覽:171
頻率計源碼 發布:2024-09-08 07:40:26 瀏覽:778
奧迪a6哪個配置帶後排加熱 發布:2024-09-08 07:06:32 瀏覽:101
linux修改apache埠 發布:2024-09-08 07:05:49 瀏覽:209
有多少個不同的密碼子 發布:2024-09-08 07:00:46 瀏覽:566
linux搭建mysql伺服器配置 發布:2024-09-08 06:50:02 瀏覽:995
加上www不能訪問 發布:2024-09-08 06:39:52 瀏覽:811
銀行支付密碼器怎麼用 發布:2024-09-08 06:39:52 瀏覽:513
蘋果手機清理瀏覽器緩存怎麼清理緩存 發布:2024-09-08 06:31:32 瀏覽:554