导航:首页 > 编程语言 > 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坦克大战代码相关的资料

热点内容
如何将iphone手机照片转到安卓手机 浏览:30
从本地邮件到服务器是什么协议 浏览:225
外国片公交车男女主 浏览:234
瓦房店新玛特有电影院吗 浏览:933
免费观看网页版 浏览:622
叶子楣徐锦江演的电影 浏览:40
一级建造师法规pdf 浏览:496
phpshiro 浏览:161
无忧云服务器放行 浏览:634
免费最新大片观看网址 浏览:250
微信手势图标加密 浏览:746
存储服务器能干什么 浏览:785
c编辑软件怎么编译 浏览:597
hpc编译器论文 浏览:814
javac编译子目录下所有文件 浏览:430
卡友帮帮团是什么app 浏览:242
vnc启动命令 浏览:374
哺乳十电影 浏览:629
贵州java编译器 浏览:645
欧美电影免费看平台 浏览:286