在android中启动游戏

在android中启动游戏,第1张

概述我有PAC MAN游戏代码 如何在 Android中将此游戏称为活动.. 我想在Android应用程序中调用在线游戏,而不是如何在链接上调用Android应用程序中的在线游戏,如果用户点击该特定游戏链接,则比游戏午餐形式网络在线…游戏如PAC MAN. 谢谢 public class Board extends JPanel implements ActionListener { Dim 我有PAC MAN游戏代码
如何在 Android中将此游戏称为活动..
我想在AndroID应用程序中调用在线游戏,而不是如何在链接上调用AndroID应用程序中的在线游戏,如果用户点击该特定游戏链接,则比游戏午餐形式网络在线…游戏如PAC MAN.
谢谢
public class Board extends JPanel implements ActionListener {    Dimension d;    Font smallFont = new Font("Helvetica",Font.BolD,14);    FontMetrics fmsmall,fmlarge;    Image ii;    color dotcolor = new color(192,192,0);    color mazecolor;    boolean ingame = false;    boolean dying = false;    final int blocksize = 24;    final int nrofblocks = 15;    final int scrsize = nrofblocks * blocksize;    final int pacanimdelay = 2;    final int pacmananimcount = 4;    final int maxghosts = 12;    final int pacmanspeed = 6;    int pacanimcount = pacanimdelay;    int pacanimdir = 1;    int pacmananimpos = 0;    int nrofghosts = 6;    int pacsleft,score;    int deathcounter;    int[] dx,dy;    int[] ghostx,ghosty,ghostdx,ghostdy,ghostspeed;    Image ghost;    Image pacman1,pacman2up,pacman2left,pacman2right,pacman2down;    Image pacman3up,pacman3down,pacman3left,pacman3right;    Image pacman4up,pacman4down,pacman4left,pacman4right;    int pacmanx,pacmany,pacmandx,pacmandy;    int reqdx,reqdy,vIEwdx,vIEwdy;    final short leveldata[] =    { 19,26,18,22,21,17,16,20,24,25,28,1,19,9,8,28 };    final int valIDspeeds[] = { 1,2,3,4,6,8 };    final int maxspeed = 6;    int currentspeed = 3;    short[] screendata;    Timer timer;    public Board() {        Getimages();        addKeyListener(new TAdapter());        screendata = new short[nrofblocks * nrofblocks];        mazecolor = new color(5,100,5);        setFocusable(true);        d = new Dimension(400,400);        setBackground(color.black);        setDoubleBuffered(true);        ghostx = new int[maxghosts];        ghostdx = new int[maxghosts];        ghosty = new int[maxghosts];        ghostdy = new int[maxghosts];        ghostspeed = new int[maxghosts];        dx = new int[4];        dy = new int[4];        timer = new Timer(40,this);        timer.start();    }    public voID addNotify() {        super.addNotify();        GameInit();    }    public voID DoAnim() {        pacanimcount--;        if (pacanimcount <= 0) {            pacanimcount = pacanimdelay;            pacmananimpos = pacmananimpos + pacanimdir;            if (pacmananimpos == (pacmananimcount - 1) || pacmananimpos == 0)                pacanimdir = -pacanimdir;        }    }    public voID PlayGame(Graphics2D g2d) {        if (dying) {            Death();        } else {            MovePacMan();            DrawPacMan(g2d);            moveGhosts(g2d);            CheckMaze();        }    }    public voID ShowIntroScreen(Graphics2D g2d) {        g2d.setcolor(new color(0,32,48));        g2d.fillRect(50,scrsize / 2 - 30,scrsize - 100,50);        g2d.setcolor(color.white);        g2d.drawRect(50,50);        String s = "Press s to start.";        Font small = new Font("Helvetica",14);        FontMetrics metr = this.getFontMetrics(small);        g2d.setcolor(color.white);        g2d.setFont(small);        g2d.drawString(s,(scrsize - metr.stringWIDth(s)) / 2,scrsize / 2);    }    public voID Drawscore(Graphics2D g) {        int i;        String s;        g.setFont(smallFont);        g.setcolor(new color(96,128,255));        s = "score: " + score;        g.drawString(s,scrsize / 2 + 96,scrsize + 16);        for (i = 0; i < pacsleft; i++) {            g.drawImage(pacman3left,i * 28 + 8,scrsize + 1,this);        }    }    public voID CheckMaze() {        short i = 0;        boolean finished = true;        while (i < nrofblocks * nrofblocks && finished) {            if ((screendata[i] & 48) != 0)                finished = false;            i++;        }        if (finished) {            score += 50;            if (nrofghosts < maxghosts)                nrofghosts++;            if (currentspeed < maxspeed)                currentspeed++;            Levelinit();        }    }    public voID Death() {        pacsleft--;        if (pacsleft == 0)            ingame = false;        LevelContinue();    }    public voID moveGhosts(Graphics2D g2d) {        short i;        int pos;        int count;        for (i = 0; i < nrofghosts; i++) {            if (ghostx[i] % blocksize == 0 && ghosty[i] % blocksize == 0) {                pos = ghostx[i] / blocksize + nrofblocks * (int)(ghosty[i] / blocksize);                count = 0;                if ((screendata[pos] & 1) == 0 && ghostdx[i] != 1) {                    dx[count] = -1;                    dy[count] = 0;                    count++;                }                if ((screendata[pos] & 2) == 0 && ghostdy[i] != 1) {                    dx[count] = 0;                    dy[count] = -1;                    count++;                }                if ((screendata[pos] & 4) == 0 && ghostdx[i] != -1) {                    dx[count] = 1;                    dy[count] = 0;                    count++;                }                if ((screendata[pos] & 8) == 0 && ghostdy[i] != -1) {                    dx[count] = 0;                    dy[count] = 1;                    count++;                }                if (count == 0) {                    if ((screendata[pos] & 15) == 15) {                        ghostdx[i] = 0;                        ghostdy[i] = 0;                    } else {                        ghostdx[i] = -ghostdx[i];                        ghostdy[i] = -ghostdy[i];                    }                } else {                    count = (int)(Math.random() * count);                    if (count > 3)                        count = 3;                    ghostdx[i] = dx[count];                    ghostdy[i] = dy[count];                }            }            ghostx[i] = ghostx[i] + (ghostdx[i] * ghostspeed[i]);            ghosty[i] = ghosty[i] + (ghostdy[i] * ghostspeed[i]);            DrawGhost(g2d,ghostx[i] + 1,ghosty[i] + 1);            if (pacmanx > (ghostx[i] - 12) && pacmanx < (ghostx[i] + 12) &&                pacmany > (ghosty[i] - 12) && pacmany < (ghosty[i] + 12) &&                ingame) {                dying = true;                deathcounter = 64;            }        }    }    public voID DrawGhost(Graphics2D g2d,int x,int y) {        g2d.drawImage(ghost,x,y,this);    }    public voID MovePacMan() {        int pos;        short ch;        if (reqdx == -pacmandx && reqdy == -pacmandy) {            pacmandx = reqdx;            pacmandy = reqdy;            vIEwdx = pacmandx;            vIEwdy = pacmandy;        }        if (pacmanx % blocksize == 0 && pacmany % blocksize == 0) {            pos = pacmanx / blocksize + nrofblocks * (int)(pacmany / blocksize);            ch = screendata[pos];            if ((ch & 16) != 0) {                screendata[pos] = (short)(ch & 15);                score++;            }            if (reqdx != 0 || reqdy != 0) {                if (!((reqdx == -1 && reqdy == 0 && (ch & 1) != 0) ||                      (reqdx == 1 && reqdy == 0 && (ch & 4) != 0) ||                      (reqdx == 0 && reqdy == -1 && (ch & 2) != 0) ||                      (reqdx == 0 && reqdy == 1 && (ch & 8) != 0))) {                    pacmandx = reqdx;                    pacmandy = reqdy;                    vIEwdx = pacmandx;                    vIEwdy = pacmandy;                }            }            // Check for standstill            if ((pacmandx == -1 && pacmandy == 0 && (ch & 1) != 0) ||                (pacmandx == 1 && pacmandy == 0 && (ch & 4) != 0) ||                (pacmandx == 0 && pacmandy == -1 && (ch & 2) != 0) ||                (pacmandx == 0 && pacmandy == 1 && (ch & 8) != 0)) {                pacmandx = 0;                pacmandy = 0;            }        }        pacmanx = pacmanx + pacmanspeed * pacmandx;        pacmany = pacmany + pacmanspeed * pacmandy;    }    public voID DrawPacMan(Graphics2D g2d) {        if (vIEwdx == -1)            DrawPacManleft(g2d);        else if (vIEwdx == 1)            DrawPacManRight(g2d);        else if (vIEwdy == -1)            DrawPacManUp(g2d);        else            DrawPacManDown(g2d);    }    public voID DrawPacManUp(Graphics2D g2d) {        switch (pacmananimpos) {        case 1:            g2d.drawImage(pacman2up,pacmanx + 1,pacmany + 1,this);            break;        case 2:            g2d.drawImage(pacman3up,this);            break;        case 3:            g2d.drawImage(pacman4up,this);            break;        default:            g2d.drawImage(pacman1,this);            break;        }    }    public voID DrawPacManDown(Graphics2D g2d) {        switch (pacmananimpos) {        case 1:            g2d.drawImage(pacman2down,this);            break;        case 2:            g2d.drawImage(pacman3down,this);            break;        case 3:            g2d.drawImage(pacman4down,this);            break;        }    }    public voID DrawPacManleft(Graphics2D g2d) {        switch (pacmananimpos) {        case 1:            g2d.drawImage(pacman2left,this);            break;        case 2:            g2d.drawImage(pacman3left,this);            break;        case 3:            g2d.drawImage(pacman4left,this);            break;        }    }    public voID DrawPacManRight(Graphics2D g2d) {        switch (pacmananimpos) {        case 1:            g2d.drawImage(pacman2right,this);            break;        case 2:            g2d.drawImage(pacman3right,this);            break;        case 3:            g2d.drawImage(pacman4right,this);            break;        }    }    public voID DrawMaze(Graphics2D g2d) {        short i = 0;        int x,y;        for (y = 0; y < scrsize; y += blocksize) {            for (x = 0; x < scrsize; x += blocksize) {                g2d.setcolor(mazecolor);                g2d.setstroke(new Basicstroke(2));                if ((screendata[i] & 1) != 0) // draws left                {                    g2d.drawline(x,y + blocksize - 1);                }                if ((screendata[i] & 2) != 0) // draws top                {                    g2d.drawline(x,x + blocksize - 1,y);                }                if ((screendata[i] & 4) != 0) // draws right                {                    g2d.drawline(x + blocksize - 1,y + blocksize - 1);                }                if ((screendata[i] & 8) != 0) // draws bottom                {                    g2d.drawline(x,y + blocksize - 1,y + blocksize - 1);                }                if ((screendata[i] & 16) != 0) // draws point                {                    g2d.setcolor(dotcolor);                    g2d.fillRect(x + 11,y + 11,2);                }                i++;            }        }    }    public voID GameInit() {        pacsleft = 3;        score = 0;        Levelinit();        nrofghosts = 6;        currentspeed = 3;    }    public voID Levelinit() {        int i;        for (i = 0; i < nrofblocks * nrofblocks; i++)            screendata[i] = leveldata[i];        LevelContinue();    }    public voID LevelContinue() {        short i;        int dx = 1;        int random;        for (i = 0; i < nrofghosts; i++) {            ghosty[i] = 4 * blocksize;            ghostx[i] = 4 * blocksize;            ghostdy[i] = 0;            ghostdx[i] = dx;            dx = -dx;            random = (int)(Math.random() * (currentspeed + 1));            if (random > currentspeed)                random = currentspeed;            ghostspeed[i] = valIDspeeds[random];        }        pacmanx = 7 * blocksize;        pacmany = 11 * blocksize;        pacmandx = 0;        pacmandy = 0;        reqdx = 0;        reqdy = 0;        vIEwdx = -1;        vIEwdy = 0;        dying = false;    }    public voID Getimages()    {      ghost = new ImageIcon(Board.class.getResource("../pacpix/ghost.png")).getimage();      pacman1 = new ImageIcon(Board.class.getResource("../pacpix/pacman.png")).getimage();      pacman2up = new ImageIcon(Board.class.getResource("../pacpix/up1.png")).getimage();      pacman3up = new ImageIcon(Board.class.getResource("../pacpix/up2.png")).getimage();      pacman4up = new ImageIcon(Board.class.getResource("../pacpix/up3.png")).getimage();      pacman2down = new ImageIcon(Board.class.getResource("../pacpix/down1.png")).getimage();      pacman3down = new ImageIcon(Board.class.getResource("../pacpix/down2.png")).getimage();       pacman4down = new ImageIcon(Board.class.getResource("../pacpix/down3.png")).getimage();      pacman2left = new ImageIcon(Board.class.getResource("../pacpix/left1.png")).getimage();      pacman3left = new ImageIcon(Board.class.getResource("../pacpix/left2.png")).getimage();      pacman4left = new ImageIcon(Board.class.getResource("../pacpix/left3.png")).getimage();      pacman2right = new ImageIcon(Board.class.getResource("../pacpix/right1.png")).getimage();      pacman3right = new ImageIcon(Board.class.getResource("../pacpix/right2.png")).getimage();      pacman4right = new ImageIcon(Board.class.getResource("../pacpix/right3.png")).getimage();    }    public voID paint(Graphics g)    {      super.paint(g);      Graphics2D g2d = (Graphics2D) g;      g2d.setcolor(color.black);      g2d.fillRect(0,d.wIDth,d.height);      DrawMaze(g2d);      Drawscore(g2d);      DoAnim();      if (ingame)        PlayGame(g2d);      else        ShowIntroScreen(g2d);      g.drawImage(ii,5,this);      Toolkit.getDefaultToolkit().sync();      g.dispose();    }    class TAdapter extends KeyAdapter {        public voID keypressed(KeyEvent e) {          int key = e.getKeyCode();          if (ingame)          {            if (key == KeyEvent.VK_left)            {              reqdx=-1;              reqdy=0;            }            else if (key == KeyEvent.VK_RIGHT)            {              reqdx=1;              reqdy=0;            }            else if (key == KeyEvent.VK_UP)            {              reqdx=0;              reqdy=-1;            }            else if (key == KeyEvent.VK_DOWN)            {              reqdx=0;              reqdy=1;            }            else if (key == KeyEvent.VK_ESCAPE && timer.isRunning())            {              ingame=false;            }            else if (key == KeyEvent.VK_PAUSE) {                if (timer.isRunning())                    timer.stop();                else timer.start();            }          }          else          {            if (key == 's' || key == 'S')          {              ingame=true;              GameInit();            }          }      }          public voID keyreleased(KeyEvent e) {              int key = e.getKeyCode();              if (key == Event.left || key == Event.RIGHT ||                  key == Event.UP ||  key == Event.DOWN)              {                reqdx=0;                reqdy=0;              }          }      }    public voID actionPerformed(ActionEvent e) {        repaint();      }}
解决方法 你不能简单地“调用游戏代码”.您可以学习,理解和重用逻辑,并在AndroID中构建新游戏.即使重用此代码也需要大量重构.

如果你想构建安卓游戏,那么你可以使用像Cocos2D这样的框架.

这是一个很好的教程:

http://dan.clarke.name/2011/04/how-to-make-a-simple-android-game-with-cocos2d/

总结

以上是内存溢出为你收集整理的在android中启动游戏全部内容,希望文章能够帮你解决在android中启动游戏所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1135456.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存