日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

坦克大战第一节——画出自己的坦克(新手篇)

發布時間:2023/12/19 综合教程 21 生活家
生活随笔 收集整理的這篇文章主要介紹了 坦克大战第一节——画出自己的坦克(新手篇) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

剛剛開始學習Java,對Java不是很熟悉,但是自己的興趣挺喜歡Java。現在自己在自學Java做一個小游戲,坦克大戰。

自己現在完成了畫出自己的坦克和坦克的移動方向。希望各位大神指導一下我這個剛剛學Java的新手小白。

我會每完成一個階段,就更新一下博客,跟進一下自己的學習進度。

在做這個坦克大戰的時候,不知道從哪里開始做起,再來自己就覺得先畫出自己的坦克吧。

下面是創建坦克類(方便以后能創建更加多的坦克),再創建自己的坦克繼承坦克類。

package tankgames;

import java.awt.*;

//創建坦克類
public class Tank {
    int x,y,w,h;
    int speed;
    int direct;
    Color c1,c2,c3;
    
    public Tank(){
        x=230;
        y=300;
        w=5;
        h=60;
        speed=10;
        direct=0;
    
    }

    public Tank(int x, int y, int w, int h, int speed, int direct) {
        super();
        this.x = x;
        this.y = y;
        this.w = w;
        this.h = h;
        this.speed = speed;
        this.direct = direct;
    }
}


//創建我的坦克子類
class MyTank extends Tank{
    //創建顏色對象,并設置顏色
    public MyTank(){
        this.c1 = new Color(128,128,128);
        this.c2 = new Color(0,255,0);
        this.c3 = new Color(0,0,255);
    }
    
    public MyTank(int x, int y, int w, int h, int speed, int direct) {
        super(x, y, w, h, speed, direct);
        this.c1 = new Color(128,128,128);
        this.c2 = new Color(0,255,0);
        this.c3 = new Color(0,0,255);
    }
}

創建完自己的坦克,要寫個主函數來畫出自己的坦克,以下是畫出自己的坦克:

package tankgames;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;

public class TankGame extends JFrame{
    private static final long serialVersionUID = 1L;
    
    MyPanel mp;

    public static void main(String[] args) {
        new TankGame().setVisible(true);

    }
    
    public TankGame(){
        this.setTitle("坦克大戰");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setResizable(false);
        this.setBounds(100, 100, 600, 500);
        
        //創建我的面板對象,并添加到我的窗體里面
        mp = new MyPanel();
        this.getContentPane().add(mp);
        
        //注冊鍵盤事件監聽
        this.addKeyListener(mp);
    }

}

//創建我的面板子類
class MyPanel extends JPanel implements KeyListener{
    MyTank myTank;
    private static final long serialVersionUID = 1L;
    
    public MyPanel(){
        this.setLayout(null);
        myTank = new MyTank();
    }
    
    //面板容器的設置和組件在面板容器里顯示
    public void paint(Graphics g){
        super.paint(g);
        g.setColor(new Color(255,255,221));
        g.fill3DRect(0, 0, 500, 400, false);
        drawTank(myTank,g);
    }
    
    //畫坦克
    public void drawTank(Tank t,Graphics g){
        int x = t.x, y = t.y, w = t.w, h = t.h;
        //畫出向上的坦克
        if(t.direct == 0){
            Graphics2D g2d = (Graphics2D)g;
            g.setColor(t.c1);
            g.fill3DRect(x, y, w, h, false);
            g.fill3DRect(x+7*w, y, w, h, false);
            g.setColor(t.c2);
            g.fill3DRect(x+w, y+2*w, 6*w, 8*w, false);
            g.fillOval(x+2*w, y+4*w, 4*w, 4*w);
            g2d.setColor(t.c3);
            g2d.setStroke(new BasicStroke(5.0f));
            g2d.drawLine(x+4*w, y, x+4*w, y+6*w);
        }
        //畫出向下的坦克
        else if(t.direct == 2){
            Graphics2D g2d = (Graphics2D)g;
            g.setColor(t.c1);
            g.fill3DRect(x, y, w, h, false);
            g.fill3DRect(x+7*w, y, w, h, false);
            g.setColor(t.c2);
            g.fill3DRect(x+w, y+2*w, 6*w, 8*w, false);
            g.fillOval(x+2*w, y+4*w, 4*w, 4*w);
            g2d.setColor(t.c3);
            g2d.setStroke(new BasicStroke(5.0f));
            g2d.drawLine(x+4*w, y+6*w, x+4*w, y+12*w);
        }
        //畫出向左的坦克
        else if(t.direct == 3){
            Graphics2D g2d = (Graphics2D)g;
            g.setColor(t.c1);
            g.fill3DRect(x, y, h, w, false);
            g.fill3DRect(x, y+7*w, h, w, false);
            g.setColor(t.c2);
            g.fill3DRect(x+2*w, y+w, 8*w, 6*w, false);
            g.fillOval(x+4*w, y+2*w, 4*w, 4*w);
            g2d.setColor(t.c3);
            g2d.setStroke(new BasicStroke(5.0f));
            g2d.drawLine(x, y+4*w, x+6*w, y+4*w);
        }
        //畫出向右的坦克
        else if(t.direct ==1){
            Graphics2D g2d = (Graphics2D)g;
            g.setColor(t.c1);
            g.fill3DRect(x, y, h, w, false);
            g.fill3DRect(x, y+7*w, h, w, false);
            g.setColor(t.c2);
            g.fill3DRect(x+2*w, y+w, 8*w, 6*w, false);
            g.fillOval(x+4*w, y+2*w, 4*w, 4*w);
            g2d.setColor(t.c3);
            g2d.setStroke(new BasicStroke(5.0f));
            g2d.drawLine(x+6*w, y+4*w, x+12*w, y+4*w);
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {
        
    }
    //我的坦克移動
    @Override
    public void keyPressed(KeyEvent e) {
        //向左移動
        if(e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyCode() == KeyEvent.VK_A){
            myTank.x -= myTank.speed;
            myTank.direct = 3;
        }
        //向右移動
        else if(e.getKeyCode() == KeyEvent.VK_RIGHT || e.getKeyCode() == KeyEvent.VK_D){
            myTank.x += myTank.speed;
            myTank.direct = 1;
        }
        //向上移動
        else if(e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_W){
            myTank.y -= myTank.speed;
            myTank.direct =0;
        }
        //向下移動
        else if(e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyCode() == KeyEvent.VK_S){
            myTank.y += myTank.speed;
            myTank.direct = 2;
        }
        //關閉游戲
        else if(e.getKeyCode() == KeyEvent.VK_ESCAPE){
            System.exit(0);
        }
        //刷新圖形
        this.repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {
        
    }

}

我畫自己的坦克只是計算好位置,畫出圖形。

再來自己到最后,有一個疑惑,自己不懂。

坦克每次改變方向移動時,能不能有更加方便的方法來封裝上面畫圖形時自己手動改變位置?若畫其他坦克,是不是還要自己手動改變坦克的位置?這里自己覺得處理不是很好,現在還在想怎么能更加完美,更加方便。

總結

以上是生活随笔為你收集整理的坦克大战第一节——画出自己的坦克(新手篇)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。