A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 姚伟涛 中级黑马   /  2012-4-21 20:27  /  2223 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. package com.MyTankGame;
  2. import java.awt.*;
  3. import javax.swing.*;
  4. import java.awt.event.*;
  5. public class MyTankGame extends JFrame{
  6. Mypanel mp=null;
  7. public static void main(String[] args) {
  8.   // TODO Auto-generated method stub
  9.   MyTankGame mtg=new MyTankGame();
  10. }
  11.     public MyTankGame()
  12.     {
  13.      mp=new Mypanel();
  14.      this.add(mp);
  15.      this.addKeyListener(mp);
  16.      this.setSize(400, 300);
  17.      this.setVisible(true);
  18.      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  19.      this.setLocation(350, 200);
  20.     }
  21. }
  22. class Mypanel extends JPanel implements KeyListener
  23. {
  24. Hero hero=null;
  25. int x=150;
  26. int y=200;
  27. public void Hero()
  28. {
  29.   hero=new Hero(10,10);
  30. }
  31. public void paint(Graphics g)
  32. {
  33.   super.paint(g);
  34.   g.fillRect(0, 0, 400, 300);
  35.   g.setColor(Color.blue);
  36.   this.drawTank(hero.getX(),hero.getY(), 1, g, 1);
  37. }
  38. //构造坦克函数
  39. public void drawTank(int x,int y,int direct,Graphics g,int Type)
  40. {
  41.   switch (Type)
  42.   {
  43.   case 1:
  44.    g.setColor(new Color(154,165,127));
  45.    g.fill3DRect(x, y, 10, 30, true);
  46.    g.fill3DRect(x+29, y, 10, 30, true);
  47.    g.fill3DRect(x+10, y+5, 20, 20, false);
  48.    g.drawOval(x+11, y+6, 15, 15);
  49.    g.drawLine(x+18, y-5, x+18, y+15);
  50.    break;
  51.   }
  52. }
  53. //按下键
  54. public void keyPressed(KeyEvent e) {
  55.   if(e.getKeyCode()==KeyEvent.VK_DOWN)
  56.   {
  57.    //设置Hero坦克fangxian
  58.    
  59.    y=y+5;
  60.   }else if(e.getKeyCode()==KeyEvent.VK_UP)
  61.   {
  62.   
  63.    y=y-5;
  64.   }else if(e.getKeyCode()==KeyEvent.VK_RIGHT)
  65.   {
  66.   
  67.    x=x+5;
  68.   }else if(e.getKeyCode()==KeyEvent.VK_LEFT)
  69.   {
  70.   
  71.    x=x-5;
  72.   }
  73.   this.repaint();
  74. }

  75. //释放键
  76. public void keyReleased(KeyEvent e) {
  77.   // TODO Auto-generated method stub
  78.   
  79. }
  80. //键上的值被输出
  81. public void keyTyped(KeyEvent e) {
  82.   // TODO Auto-generated method stub
  83.   
  84. }

  85. }
  86. class Tank
  87. {
  88. int x;
  89. int y;
  90. //0↑ 1↓ 2← 3→
  91. int direct;
  92. int speed=1;
  93. public int getSpeed() {
  94.   return speed;
  95. }
  96. public void setSpeed(int speed) {
  97.   this.speed = speed;
  98. }
  99. public int getDirect() {
  100.   return direct;
  101. }
  102. public void setDirect(int direct) {
  103.   this.direct = direct;
  104. }
  105. public int getX() {
  106.   return x;
  107. }
  108. public void setX(int x) {
  109.   this.x = x;
  110. }
  111. public int getY() {
  112.   return y;
  113. }
  114. public void setY(int y) {
  115.   this.y = y;
  116. }
  117. public Tank(int x,int y)
  118. {
  119.   this.x=x;
  120.   this.y=y;
  121. }

  122. }
  123. class Hero extends Tank
  124. {
  125. public Hero(int x,int y)
  126. {
  127.   super(x, y);
  128. }
  129. //创建坦克移动方法
  130. public void moveUp()
  131. {
  132.   y-=speed;
  133. }
  134. public void moveDown()
  135. {
  136.   y+=speed;
  137. }
  138. public void moveRight()
  139. {
  140.   x+=speed;
  141. }
  142. public void moveLeft()
  143. {
  144.   x-=speed;
  145. }

  146. }
复制代码

1 个回复

倒序浏览
您需要登录后才可以回帖 登录 | 加入黑马