在android中启动游戏

前端之家收集整理的这篇文章主要介绍了在android中启动游戏前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有PAC MAN游戏代码
如何在 Android中将此游戏称为活动..
我想在Android应用程序中调用在线游戏,而不是如何在链接调用Android应用程序中的在线游戏,如果用户点击该特定游戏链接,则比游戏午餐形式网络在线…游戏如PAC MAN.
谢谢
  1. public class Board extends JPanel implements ActionListener {
  2.  
  3. Dimension d;
  4. Font smallfont = new Font("Helvetica",Font.BOLD,14);
  5.  
  6. FontMetrics fmsmall,fmlarge;
  7. Image ii;
  8. Color dotcolor = new Color(192,192,0);
  9. Color mazecolor;
  10.  
  11. boolean ingame = false;
  12. boolean dying = false;
  13.  
  14. final int blocksize = 24;
  15. final int nrofblocks = 15;
  16. final int scrsize = nrofblocks * blocksize;
  17. final int pacanimdelay = 2;
  18. final int pacmananimcount = 4;
  19. final int maxghosts = 12;
  20. final int pacmanspeed = 6;
  21.  
  22. int pacanimcount = pacanimdelay;
  23. int pacanimdir = 1;
  24. int pacmananimpos = 0;
  25. int nrofghosts = 6;
  26. int pacsleft,score;
  27. int deathcounter;
  28. int[] dx,dy;
  29. int[] ghostx,ghosty,ghostdx,ghostdy,ghostspeed;
  30.  
  31. Image ghost;
  32. Image pacman1,pacman2up,pacman2left,pacman2right,pacman2down;
  33. Image pacman3up,pacman3down,pacman3left,pacman3right;
  34. Image pacman4up,pacman4down,pacman4left,pacman4right;
  35.  
  36. int pacmanx,pacmany,pacmandx,pacmandy;
  37. int reqdx,reqdy,viewdx,viewdy;
  38.  
  39. final short leveldata[] =
  40. { 19,26,18,22,21,17,16,20,24,25,28,1,19,9,8,28 };
  41.  
  42. final int validspeeds[] = { 1,2,3,4,6,8 };
  43. final int maxspeed = 6;
  44.  
  45. int currentspeed = 3;
  46. short[] screendata;
  47. Timer timer;
  48.  
  49.  
  50. public Board() {
  51.  
  52. GetImages();
  53.  
  54. addKeyListener(new TAdapter());
  55.  
  56. screendata = new short[nrofblocks * nrofblocks];
  57. mazecolor = new Color(5,100,5);
  58. setFocusable(true);
  59.  
  60. d = new Dimension(400,400);
  61.  
  62. setBackground(Color.black);
  63. setDoubleBuffered(true);
  64.  
  65. ghostx = new int[maxghosts];
  66. ghostdx = new int[maxghosts];
  67. ghosty = new int[maxghosts];
  68. ghostdy = new int[maxghosts];
  69. ghostspeed = new int[maxghosts];
  70. dx = new int[4];
  71. dy = new int[4];
  72. timer = new Timer(40,this);
  73. timer.start();
  74. }
  75.  
  76. public void addNotify() {
  77. super.addNotify();
  78. GameInit();
  79. }
  80.  
  81.  
  82. public void DoAnim() {
  83. pacanimcount--;
  84. if (pacanimcount <= 0) {
  85. pacanimcount = pacanimdelay;
  86. pacmananimpos = pacmananimpos + pacanimdir;
  87. if (pacmananimpos == (pacmananimcount - 1) || pacmananimpos == 0)
  88. pacanimdir = -pacanimdir;
  89. }
  90. }
  91.  
  92.  
  93. public void PlayGame(Graphics2D g2d) {
  94. if (dying) {
  95. Death();
  96. } else {
  97. MovePacMan();
  98. DrawPacMan(g2d);
  99. moveGhosts(g2d);
  100. CheckMaze();
  101. }
  102. }
  103.  
  104.  
  105. public void ShowIntroScreen(Graphics2D g2d) {
  106.  
  107. g2d.setColor(new Color(0,32,48));
  108. g2d.fillRect(50,scrsize / 2 - 30,scrsize - 100,50);
  109. g2d.setColor(Color.white);
  110. g2d.drawRect(50,50);
  111.  
  112. String s = "Press s to start.";
  113. Font small = new Font("Helvetica",14);
  114. FontMetrics metr = this.getFontMetrics(small);
  115.  
  116. g2d.setColor(Color.white);
  117. g2d.setFont(small);
  118. g2d.drawString(s,(scrsize - metr.stringWidth(s)) / 2,scrsize / 2);
  119. }
  120.  
  121.  
  122. public void Drawscore(Graphics2D g) {
  123. int i;
  124. String s;
  125.  
  126. g.setFont(smallfont);
  127. g.setColor(new Color(96,128,255));
  128. s = "score: " + score;
  129. g.drawString(s,scrsize / 2 + 96,scrsize + 16);
  130. for (i = 0; i < pacsleft; i++) {
  131. g.drawImage(pacman3left,i * 28 + 8,scrsize + 1,this);
  132. }
  133. }
  134.  
  135.  
  136. public void CheckMaze() {
  137. short i = 0;
  138. boolean finished = true;
  139.  
  140. while (i < nrofblocks * nrofblocks && finished) {
  141. if ((screendata[i] & 48) != 0)
  142. finished = false;
  143. i++;
  144. }
  145.  
  146. if (finished) {
  147. score += 50;
  148.  
  149. if (nrofghosts < maxghosts)
  150. nrofghosts++;
  151. if (currentspeed < maxspeed)
  152. currentspeed++;
  153. LevelInit();
  154. }
  155. }
  156.  
  157. public void Death() {
  158.  
  159. pacsleft--;
  160. if (pacsleft == 0)
  161. ingame = false;
  162. LevelContinue();
  163. }
  164.  
  165.  
  166. public void moveGhosts(Graphics2D g2d) {
  167. short i;
  168. int pos;
  169. int count;
  170.  
  171. for (i = 0; i < nrofghosts; i++) {
  172. if (ghostx[i] % blocksize == 0 && ghosty[i] % blocksize == 0) {
  173. pos =
  174. ghostx[i] / blocksize + nrofblocks * (int)(ghosty[i] / blocksize);
  175.  
  176. count = 0;
  177. if ((screendata[pos] & 1) == 0 && ghostdx[i] != 1) {
  178. dx[count] = -1;
  179. dy[count] = 0;
  180. count++;
  181. }
  182. if ((screendata[pos] & 2) == 0 && ghostdy[i] != 1) {
  183. dx[count] = 0;
  184. dy[count] = -1;
  185. count++;
  186. }
  187. if ((screendata[pos] & 4) == 0 && ghostdx[i] != -1) {
  188. dx[count] = 1;
  189. dy[count] = 0;
  190. count++;
  191. }
  192. if ((screendata[pos] & 8) == 0 && ghostdy[i] != -1) {
  193. dx[count] = 0;
  194. dy[count] = 1;
  195. count++;
  196. }
  197.  
  198. if (count == 0) {
  199. if ((screendata[pos] & 15) == 15) {
  200. ghostdx[i] = 0;
  201. ghostdy[i] = 0;
  202. } else {
  203. ghostdx[i] = -ghostdx[i];
  204. ghostdy[i] = -ghostdy[i];
  205. }
  206. } else {
  207. count = (int)(Math.random() * count);
  208. if (count > 3)
  209. count = 3;
  210. ghostdx[i] = dx[count];
  211. ghostdy[i] = dy[count];
  212. }
  213.  
  214. }
  215. ghostx[i] = ghostx[i] + (ghostdx[i] * ghostspeed[i]);
  216. ghosty[i] = ghosty[i] + (ghostdy[i] * ghostspeed[i]);
  217. DrawGhost(g2d,ghostx[i] + 1,ghosty[i] + 1);
  218.  
  219. if (pacmanx > (ghostx[i] - 12) && pacmanx < (ghostx[i] + 12) &&
  220. pacmany > (ghosty[i] - 12) && pacmany < (ghosty[i] + 12) &&
  221. ingame) {
  222.  
  223. dying = true;
  224. deathcounter = 64;
  225.  
  226. }
  227. }
  228. }
  229.  
  230.  
  231. public void DrawGhost(Graphics2D g2d,int x,int y) {
  232. g2d.drawImage(ghost,x,y,this);
  233. }
  234.  
  235.  
  236. public void MovePacMan() {
  237. int pos;
  238. short ch;
  239.  
  240. if (reqdx == -pacmandx && reqdy == -pacmandy) {
  241. pacmandx = reqdx;
  242. pacmandy = reqdy;
  243. viewdx = pacmandx;
  244. viewdy = pacmandy;
  245. }
  246. if (pacmanx % blocksize == 0 && pacmany % blocksize == 0) {
  247. pos =
  248. pacmanx / blocksize + nrofblocks * (int)(pacmany / blocksize);
  249. ch = screendata[pos];
  250.  
  251. if ((ch & 16) != 0) {
  252. screendata[pos] = (short)(ch & 15);
  253. score++;
  254. }
  255.  
  256. if (reqdx != 0 || reqdy != 0) {
  257. if (!((reqdx == -1 && reqdy == 0 && (ch & 1) != 0) ||
  258. (reqdx == 1 && reqdy == 0 && (ch & 4) != 0) ||
  259. (reqdx == 0 && reqdy == -1 && (ch & 2) != 0) ||
  260. (reqdx == 0 && reqdy == 1 && (ch & 8) != 0))) {
  261. pacmandx = reqdx;
  262. pacmandy = reqdy;
  263. viewdx = pacmandx;
  264. viewdy = pacmandy;
  265. }
  266. }
  267.  
  268. // Check for standstill
  269. if ((pacmandx == -1 && pacmandy == 0 && (ch & 1) != 0) ||
  270. (pacmandx == 1 && pacmandy == 0 && (ch & 4) != 0) ||
  271. (pacmandx == 0 && pacmandy == -1 && (ch & 2) != 0) ||
  272. (pacmandx == 0 && pacmandy == 1 && (ch & 8) != 0)) {
  273. pacmandx = 0;
  274. pacmandy = 0;
  275. }
  276. }
  277. pacmanx = pacmanx + pacmanspeed * pacmandx;
  278. pacmany = pacmany + pacmanspeed * pacmandy;
  279. }
  280.  
  281.  
  282. public void DrawPacMan(Graphics2D g2d) {
  283. if (viewdx == -1)
  284. DrawPacManLeft(g2d);
  285. else if (viewdx == 1)
  286. DrawPacManRight(g2d);
  287. else if (viewdy == -1)
  288. DrawPacManUp(g2d);
  289. else
  290. DrawPacManDown(g2d);
  291. }
  292.  
  293. public void DrawPacManUp(Graphics2D g2d) {
  294. switch (pacmananimpos) {
  295. case 1:
  296. g2d.drawImage(pacman2up,pacmanx + 1,pacmany + 1,this);
  297. break;
  298. case 2:
  299. g2d.drawImage(pacman3up,this);
  300. break;
  301. case 3:
  302. g2d.drawImage(pacman4up,this);
  303. break;
  304. default:
  305. g2d.drawImage(pacman1,this);
  306. break;
  307. }
  308. }
  309.  
  310.  
  311. public void DrawPacManDown(Graphics2D g2d) {
  312. switch (pacmananimpos) {
  313. case 1:
  314. g2d.drawImage(pacman2down,this);
  315. break;
  316. case 2:
  317. g2d.drawImage(pacman3down,this);
  318. break;
  319. case 3:
  320. g2d.drawImage(pacman4down,this);
  321. break;
  322. }
  323. }
  324.  
  325.  
  326. public void DrawPacManLeft(Graphics2D g2d) {
  327. switch (pacmananimpos) {
  328. case 1:
  329. g2d.drawImage(pacman2left,this);
  330. break;
  331. case 2:
  332. g2d.drawImage(pacman3left,this);
  333. break;
  334. case 3:
  335. g2d.drawImage(pacman4left,this);
  336. break;
  337. }
  338. }
  339.  
  340.  
  341. public void DrawPacManRight(Graphics2D g2d) {
  342. switch (pacmananimpos) {
  343. case 1:
  344. g2d.drawImage(pacman2right,this);
  345. break;
  346. case 2:
  347. g2d.drawImage(pacman3right,this);
  348. break;
  349. case 3:
  350. g2d.drawImage(pacman4right,this);
  351. break;
  352. }
  353. }
  354.  
  355.  
  356. public void DrawMaze(Graphics2D g2d) {
  357. short i = 0;
  358. int x,y;
  359.  
  360. for (y = 0; y < scrsize; y += blocksize) {
  361. for (x = 0; x < scrsize; x += blocksize) {
  362. g2d.setColor(mazecolor);
  363. g2d.setStroke(new BasicStroke(2));
  364.  
  365. if ((screendata[i] & 1) != 0) // draws left
  366. {
  367. g2d.drawLine(x,y + blocksize - 1);
  368. }
  369. if ((screendata[i] & 2) != 0) // draws top
  370. {
  371. g2d.drawLine(x,x + blocksize - 1,y);
  372. }
  373. if ((screendata[i] & 4) != 0) // draws right
  374. {
  375. g2d.drawLine(x + blocksize - 1,y + blocksize - 1);
  376. }
  377. if ((screendata[i] & 8) != 0) // draws bottom
  378. {
  379. g2d.drawLine(x,y + blocksize - 1,y + blocksize - 1);
  380. }
  381. if ((screendata[i] & 16) != 0) // draws point
  382. {
  383. g2d.setColor(dotcolor);
  384. g2d.fillRect(x + 11,y + 11,2);
  385. }
  386. i++;
  387. }
  388. }
  389. }
  390.  
  391. public void GameInit() {
  392. pacsleft = 3;
  393. score = 0;
  394. LevelInit();
  395. nrofghosts = 6;
  396. currentspeed = 3;
  397. }
  398.  
  399.  
  400. public void LevelInit() {
  401. int i;
  402. for (i = 0; i < nrofblocks * nrofblocks; i++)
  403. screendata[i] = leveldata[i];
  404.  
  405. LevelContinue();
  406. }
  407.  
  408.  
  409. public void LevelContinue() {
  410. short i;
  411. int dx = 1;
  412. int random;
  413.  
  414. for (i = 0; i < nrofghosts; i++) {
  415. ghosty[i] = 4 * blocksize;
  416. ghostx[i] = 4 * blocksize;
  417. ghostdy[i] = 0;
  418. ghostdx[i] = dx;
  419. dx = -dx;
  420. random = (int)(Math.random() * (currentspeed + 1));
  421. if (random > currentspeed)
  422. random = currentspeed;
  423. ghostspeed[i] = validspeeds[random];
  424. }
  425.  
  426. pacmanx = 7 * blocksize;
  427. pacmany = 11 * blocksize;
  428. pacmandx = 0;
  429. pacmandy = 0;
  430. reqdx = 0;
  431. reqdy = 0;
  432. viewdx = -1;
  433. viewdy = 0;
  434. dying = false;
  435. }
  436.  
  437. public void GetImages()
  438. {
  439.  
  440. ghost = new ImageIcon(Board.class.getResource("../pacpix/ghost.png")).getImage();
  441. pacman1 = new ImageIcon(Board.class.getResource("../pacpix/pacman.png")).getImage();
  442. pacman2up = new ImageIcon(Board.class.getResource("../pacpix/up1.png")).getImage();
  443. pacman3up = new ImageIcon(Board.class.getResource("../pacpix/up2.png")).getImage();
  444. pacman4up = new ImageIcon(Board.class.getResource("../pacpix/up3.png")).getImage();
  445. pacman2down = new ImageIcon(Board.class.getResource("../pacpix/down1.png")).getImage();
  446. pacman3down = new ImageIcon(Board.class.getResource("../pacpix/down2.png")).getImage();
  447. pacman4down = new ImageIcon(Board.class.getResource("../pacpix/down3.png")).getImage();
  448. pacman2left = new ImageIcon(Board.class.getResource("../pacpix/left1.png")).getImage();
  449. pacman3left = new ImageIcon(Board.class.getResource("../pacpix/left2.png")).getImage();
  450. pacman4left = new ImageIcon(Board.class.getResource("../pacpix/left3.png")).getImage();
  451. pacman2right = new ImageIcon(Board.class.getResource("../pacpix/right1.png")).getImage();
  452. pacman3right = new ImageIcon(Board.class.getResource("../pacpix/right2.png")).getImage();
  453. pacman4right = new ImageIcon(Board.class.getResource("../pacpix/right3.png")).getImage();
  454.  
  455. }
  456.  
  457. public void paint(Graphics g)
  458. {
  459. super.paint(g);
  460.  
  461. Graphics2D g2d = (Graphics2D) g;
  462.  
  463. g2d.setColor(Color.black);
  464. g2d.fillRect(0,d.width,d.height);
  465.  
  466. DrawMaze(g2d);
  467. Drawscore(g2d);
  468. DoAnim();
  469. if (ingame)
  470. PlayGame(g2d);
  471. else
  472. ShowIntroScreen(g2d);
  473.  
  474. g.drawImage(ii,5,this);
  475. Toolkit.getDefaultToolkit().sync();
  476. g.dispose();
  477. }
  478.  
  479. class TAdapter extends KeyAdapter {
  480. public void keyPressed(KeyEvent e) {
  481.  
  482. int key = e.getKeyCode();
  483.  
  484. if (ingame)
  485. {
  486. if (key == KeyEvent.VK_LEFT)
  487. {
  488. reqdx=-1;
  489. reqdy=0;
  490. }
  491. else if (key == KeyEvent.VK_RIGHT)
  492. {
  493. reqdx=1;
  494. reqdy=0;
  495. }
  496. else if (key == KeyEvent.VK_UP)
  497. {
  498. reqdx=0;
  499. reqdy=-1;
  500. }
  501. else if (key == KeyEvent.VK_DOWN)
  502. {
  503. reqdx=0;
  504. reqdy=1;
  505. }
  506. else if (key == KeyEvent.VK_ESCAPE && timer.isRunning())
  507. {
  508. ingame=false;
  509. }
  510. else if (key == KeyEvent.VK_PAUSE) {
  511. if (timer.isRunning())
  512. timer.stop();
  513. else timer.start();
  514. }
  515. }
  516. else
  517. {
  518. if (key == 's' || key == 'S')
  519. {
  520. ingame=true;
  521. GameInit();
  522. }
  523. }
  524. }
  525.  
  526. public void keyReleased(KeyEvent e) {
  527. int key = e.getKeyCode();
  528.  
  529. if (key == Event.LEFT || key == Event.RIGHT ||
  530. key == Event.UP || key == Event.DOWN)
  531. {
  532. reqdx=0;
  533. reqdy=0;
  534. }
  535. }
  536. }
  537.  
  538. public void actionPerformed(ActionEvent e) {
  539. repaint();
  540. }
  541. }

解决方法

你不能简单地“调用游戏代码”.您可以学习,理解和重用逻辑,并在Android中构建新游戏.即使重用此代码也需要大量重构.

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

这是一个很好的教程:

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

猜你在找的Android相关文章