Displayable1

package map;

import java.io.*;
import javax.microedition.lcdui.*;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2010</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class Displayable1
    extends Canvas
    implements Runnable {
  Image role[] = new Image[4];
  Graphics g;
  int role_x = 0;
  int role_y = 0;
  int role_x_pos = 0;
  int role_y_pos = 0;
  Role m_role = new Role();
  public static int currentX = 0;
  public static int   currentY = 0;
  Draw draw = new Draw();
  public Displayable1() {
    init();
    new Thread(this).start();
  }

  public void init() {
    try {
      for (int i = 0; i < 3; i++) {
        role[i] = Image.createImage(Image.createImage("/role.png"), 0, 31 * i,
                                    45, 31, 0);
      }
      role[3] = Image.createImage(Image.createImage("/role.png"), 0, 61, 45, 31,
                                  2);
    }
    catch (IOException ex) {
      System.out.println("+++");
    }
  }

  public void paint(Graphics g) {
    g.setColor(0);
    g.setClip(0, 0, Draw.Screen_Width, Draw.Screen_Height);
    g.fillRect(0, 0, Draw.Screen_Width, Draw.Screen_Height);
    draw.paintMap(g, currentX, currentY);
    draw.paintObject(g, currentX, currentY);
    m_role.update();
    m_role.paint(g);
  }

  public void keyPressed(int n) {

    if (n == 54) { //right
      m_role.dir = 3;
      if (m_role.role_x >= 124) {
        if (currentX < 33 - (Draw.Screen_Width / 16 + 1)) {
          currentX += 1;
          if (!m_role.checkPhy()) {
            currentX -= 1;
          }
        }
        else {
          m_role.role_x += 8;
          if (!m_role.checkPhy()) {
            m_role.role_x -= 8;
          }
          if (m_role.role_x > 232) {
            m_role.role_x = 232;
          }
        }
      }
      else {
        m_role.role_x += 8;
        if (!m_role.checkPhy()) {
          m_role.role_x -= 8;
        }
      }

    }
    if (n == 52) { //left
      m_role.dir = 2;
      if (m_role.role_x < 124) {
        if (currentX <= 33 - (Draw.Screen_Width / 16 + 1)) {
          if (currentX > 0) {
            currentX -= 1;
            if (!m_role.checkPhy()) {
              currentX += 1;
            }
          }
          else {
            m_role.role_x -= 8;
            if (!m_role.checkPhy()) {
              m_role.role_x += 8;
            }
            if (m_role.role_x < 8) {
              m_role.role_x = 8;
            }
          }
        }
      }
      else {
        m_role.role_x -= 8;
        if (!m_role.checkPhy()) {
          m_role.role_x += 8;
        }
      }

    }
    if (n == 50) { //up
      m_role.dir = 1;
      if (m_role.role_y < 150) {
        if (currentY <= 26 - (Draw.Screen_Height / 16 + 1)) {
          if (currentY > 0) {
            currentY -= 1;
            if (!m_role.checkPhy()) {
              currentY += 1;
            }
          }
          else {
            m_role.role_y -= 8;
            if (!m_role.checkPhy()) {
              m_role.role_y += 8;
            }
            if (m_role.role_y < 15) {
              m_role.role_y = 15;
            }
          }
        }
      }
      else {
        m_role.role_y -= 8;
        if (!m_role.checkPhy()) {
          m_role.role_y += 8;
        }
      }

    }
    if (n == 56) { //down
      m_role.dir = 0;
      if (m_role.role_y >= 150) {
        if (currentY < 26 - (Draw.Screen_Height / 16 + 1)) {
          currentY += 1;
          if (!m_role.checkPhy()) {
            currentY -= 1;
          }
        }
        else {
          m_role.role_y += 8;
          if (!m_role.checkPhy()) {
             m_role.role_y -= 8;
          }
          if (m_role.role_y > 305) {
            m_role.role_y = 305;
          }
        }
      }
      else {
        m_role.role_y += 8;
        if (!m_role.checkPhy()) {
           m_role.role_y -= 8;
        }
      }

    }
  }

  /**
   * run
   */
  public void run() {

    while (true) {
      try {
        repaint();
      }
      catch (Exception e) {}
    }
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值