Mypanel的? run方法里要調(diào)用repaint方法??? 否則你的repaint方法只會在keyPressed發(fā)生的時候才調(diào)用
站在用戶的角度思考問題,與客戶深入溝通,找到略陽網(wǎng)站設計與略陽網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設計與互聯(lián)網(wǎng)技術結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:做網(wǎng)站、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、域名注冊、網(wǎng)站空間、企業(yè)郵箱。業(yè)務覆蓋略陽地區(qū)。
修改一下兩個地方
(1)
// 鍵盤獲取事件的函數(shù)
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();
?}
}
}
完整代碼如下:
import?java.awt.*;
import?javax.swing.*;
import?java.util.*;
import?java.awt.event.*;
public?class?aaa?extends?JFrame?{
public?static?void?main(String[]?args)?{
aaa?a1?=?new?aaa();
Thread?t1?=?new?Thread(a1.mp);
t1.start();
}
MyPanel?mp?=?null;
public?aaa()?{
mp?=?new?MyPanel();
this.add(mp);
this.addKeyListener(mp);
this.setSize(500,?500);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class?MyPanel?extends?JPanel?implements?KeyListener,?Runnable?{
MyTank?hero?=?null;
VectorEmenyTank?emeny?=?new?VectorEmenyTank();
int?emsize?=?5;
//?鍵盤獲取事件的函數(shù)
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();
}
public?void?keyReleased(KeyEvent?arg0)?{
//?TODO?Auto-generated?method?stub
}
public?void?keyTyped(KeyEvent?arg0)?{
//?TODO?Auto-generated?method?stub
}
//?完畢
public?MyPanel()?{
hero?=?new?MyTank(250,?250);
hero.setSDC(5,?2,?2);
for?(int?i?=?0;?i??emsize;?++i)?{
EmenyTank?em?=?new?EmenyTank((i?+?1)?*?60,?20);
em.setSDC(5,?1,?1);
emeny.add(em);
}
}
//?線程
/**
*?一秒鐘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();
}
}
}
public?void?paint(Graphics?g)?{
super.paint(g);
//?畫板,坦克得放在畫板后頭
g.fillRect(0,?0,?400,?400);
//?paint敵人坦克
for?(int?i?=?0;?i??emeny.size();?++i)?{
EmenyTank?em?=?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?(int?i?=?0;?i??hero.shot.size();?i++)?{
Shot?myShot?=?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);
}
}
}
public?void?drawTank(int?x,?int?y,?Graphics?g,?int?direct,?int?color)?{
//?判斷坦克的顏色(敵我)然后畫出坦克
switch?(color)?{
case?0:
g.setColor(Color.BLUE);
break;
case?1:
g.setColor(Color.YELLOW);
break;
case?2:
g.setColor(Color.GREEN);
break;
}
//?判斷坦克的方向然后再畫出坦克
switch?(direct)?{
case?0:
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;
case?1:
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;
case?2:
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;
case?3:
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;
}
}
}
class?EmenyTank?extends?Tank?implements?Runnable?{
public?EmenyTank(int?x,?int?y)?{
//?TODO?Auto-generated?method?stub
super(x,?y);
}
public?void?run()?{
}
}
class?Shot?implements?Runnable?{
protected?int?x;
protected?int?y;
protected?int?direct;
protected?int?speed?=?4;
protected?boolean?live?=?true;
public?void?setX(int?x)?{
this.x?=?x;
this.y?=?y;
}
public?int?getX()?{
return?x;
}
public?int?getY()?{
return?y;
}
public?void?setDirect(int?direct)?{
this.direct?=?direct;
}
public?int?getDirect()?{
return?direct;
}
public?void?setSpeed(int?speed)?{
this.speed?=?speed;
}
public?int?getSpeed()?{
return?speed;
}
//?子彈的上下左右以及走的速度
public?void?run()?{
//?TODO?Auto-generated?method?stub
while?(true)?{
try?{
Thread.sleep(100);
}?catch?(Exception?e)?{
}
switch?(direct)?{
case?0:
y?-=?speed;
break;
case?1:
y?+=?speed;
break;
case?2:
x?-=?speed;
break;
case?3:
x?+=?speed;
break;
}
if?(x??400?||?x??0?||?y??400?||?y??0)?{
this.live?=?false;
break;
}
}
}
}
class?Tank?{
protected?int?x;
protected?int?y;
protected?int?speed?=?5;
protected?int?direct;
protected?int?color;
boolean?live;
public?Tank(int?x,?int?y)?{
this.x?=?x;
this.y?=?y;
}
public?int?getX()?{
return?x;
}
public?int?getY()?{
return?y;
}
public?void?setSDC(int?speed,?int?direct,?int?color)?{
this.speed?=?speed;
this.direct?=?direct;
this.color?=?color;
}
public?int?getSpeed()?{
return?speed;
}
public?int?getDirect()?{
return?direct;
}
public?int?getColor()?{
return?color;
}
}
class?MyTank?extends?Tank?{
public?MyTank(int?x,?int?y)?{
//?TODO?Auto-generated?method?stub
super(x,?y);
}
VectorShot?shot?=?new?VectorShot();
Shot?shota?=?null;
public?void?shott()?{
switch?(this.direct)?{
case?0:
shota?=?new?Shot();
shota.x?=?x?+?18;
shota.y?=?y;
shota.direct?=?0;
shot.add(shota);
break;
case?1:
shota?=?new?Shot();
shota.x?=?x?+?18;
shota.y?=?y?+?30;
shota.direct?=?1;
shot.add(shota);
break;
case?2:
shota?=?new?Shot();
shota.x?=?x?+?3;
shota.y?=?y?+?15;
shota.direct?=?2;
shot.add(shota);
break;
case?3:
shota?=?new?Shot();
shota.x?=?x?+?33;
shota.y?=?y?+?15;
shota.direct?=?3;
shot.add(shota);
break;
}
Thread?t?=?new?Thread(shota);
t.start();
}
public?void?moveUp()?{
if?(y??0)?{
y?-=?speed;
}
}//?我的坦克得在自己的類里定義怎么移動
public?void?moveDown()?{
if?(y??367)?{
y?+=?speed;
}
}
public?void?moveLeft()?{
if?(x??0)?{
x?-=?speed;
}
}
public?void?moveRight()?{
if?(x??365)?{
x?+=?speed;
}
}
}
給你一個猜數(shù)字游戲代碼。辛辛苦苦打的,希望采納。謝謝。 package caishuzi.java; import javax.swing.JOptionPane; public class caishuzi { public static void main (String args[ ]) { JOptionPane.showMessageDialog(null,"給你一個1至100...
需要看你的tank的draw方法里的內(nèi)容代碼和碰撞檢測代碼。
一般二維碰撞檢測基本都是以單位格,也就是坦克的大小格作為單位,提前一格判斷兩個方格是否交叉,如果交叉則在當前,也就是交叉后退后一格這個位置讓它停止當前方向的移動。
坦克大戰(zhàn)源代碼應該是個完整的項目吧。
對于完整的帶項目配置文件的java源碼,按步驟操作即可:
File - Import - General
選擇Existing Projects into Workspace,選擇要導入的文件,點擊“finish",OK。
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);
ListExplode explodes = new ArrayListExplode();
ListMissile missiles = new ArrayListMissile();
ListTank tanks = new ArrayListTank();
Image offScreenImage = null;
Blood b = new Blood();
public void paint(Graphics g) {
/*
* 指明子彈-爆炸-坦克的數(shù)量
* 以及坦克的生命值
*/
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; iInteger.parseInt(PropertyMgr.getProperty("reProduceTankCount")); i++) {
tanks.add(new Tank(50 + 40*(i+1), 50, false, Direction.D, this));
}
}
for(int i=0; imissiles.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; iexplodes.size(); i++) {
Explode e = explodes.get(i);
e.draw(g);
}
for(int i=0; itanks.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; iinitTankCount; 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);
}
}
}
最近學習不喜歡在算法上磕太長時間了,所以我就開始寫小游戲了,也算是給自己一個目標,寫個游戲里面需要的東西很多,就第一項窗口的建造而言,JFreme類的使用中有很多的方法需要記憶,此外對于類與對象的使用也占了很大的部分。
我是觀看教程然后學習制作坦克大戰(zhàn),最開始是制作窗口,用到了一個類JFrame下面是一些使用方法。
jf.setTitle("我是標題鴨?。?");(制作標題)
jf.setSize(20,10)//設置了一個長為20,高為10的框圖。
jf.setBounds(1,2,20,10)//設置一個左上角頂點在(1,2),長為20,寬為10的窗體。
jf.setLocation(1,2)//設置一個左上角頂點在(1,2)的窗體。
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);(這樣設置會導致你在關閉窗體的同時,終止程序的運行,會帶來一定的便利性。)
jf.setVisible(true);(窗體在默認情況下是不可見的,只在后臺運行,只有像這樣設置可見后,在程序運行的時候,窗體才會出現(xiàn)。)
然后是背景窗口的上色與選項字體的添加, setColor方法:設置顏色, 該方法用于設置畫筆的顏色,例如Color.BLACK:黑色,還需要填充畫布使用 fillRect (int x,int y,int width,int height)繪制并填充矩形?? 4個參數(shù),x、y起點坐標,寬,長。
不得不說有一個明確的目標確實學習的比較快。