📄 demogamecanvas.java
字号:
long start = System.currentTimeMillis(); tick(g); input(g); drawBullet(g); long end = System.currentTimeMillis(); int duration = (int) (end - start); if (duration < timeStep) { try { Thread.sleep(timeStep - duration); } catch (InterruptedException e) { } } } catch (IOException ex) { ex.printStackTrace(); } } } public void input(Graphics g) { int keyState = getKeyStates(); if ((keyState & GameCanvas.LEFT_PRESSED) != 0) { if (lastState != LEFT_PRESSED) { lastState = LEFT_PRESSED; tunk.setTransform(Sprite.TRANS_MIRROR_ROT90); bullet_x = tunk.getX(); bullet_y = tunk.getY() + tunk.getHeight() / 2; i = 1; } if (tunk.getX() - step >= 0) { tunk.move(-step, 0); } else { tunk.setPosition(0, tunk.getY()); } if (this.spriteCollides(tunk)) { tunk.move(step, 0); } conn.sentMessage("" + tunk.getX(), "" + tunk.getY(), "" + i, "" + 0); } if ((keyState & RIGHT_PRESSED) != 0) { if (lastState != RIGHT_PRESSED) { lastState = RIGHT_PRESSED; tunk.setTransform(Sprite.TRANS_ROT270); bullet_x = tunk.getX() + tunk.getWidth(); bullet_y = tunk.getY() + tunk.getHeight() / 2; i = 2; } if (tunk.getX() + tunk.getWidth() <= getWidth()) { tunk.move(step, 0); } else { tunk.setPosition(getWidth() - tunk.getWidth(), tunk.getY()); } if (this.spriteCollides(tunk)) { tunk.move(-step, 0); } conn.sentMessage("" + tunk.getX(), "" + tunk.getY(), "" + i, "" + 0); } if ((keyState & UP_PRESSED) != 0) { if (lastState != UP_PRESSED) { lastState = UP_PRESSED; tunk.setTransform(Sprite.TRANS_MIRROR_ROT180); bullet_x = tunk.getX() + tunk.getWidth() / 2; bullet_y = tunk.getY(); i = 3; } if (tunk.getY() - step >= 0) { tunk.move(0, -step); } else { tunk.setPosition(tunk.getX(), 0); } if (this.spriteCollides(tunk)) { tunk.move(0, step); } conn.sentMessage("" + tunk.getX(), "" + tunk.getY(), "" + i, "" + 0); } if ((keyState & DOWN_PRESSED) != 0) { if (lastState != DOWN_PRESSED) { lastState = DOWN_PRESSED; tunk.setTransform(Sprite.TRANS_MIRROR); bullet_x = tunk.getX() + tunk.getWidth() / 2; bullet_y = tunk.getY() + tunk.getHeight(); i = 4; } if (tunk.getY() + tunk.getHeight() <= getHeight()) { tunk.move(0, step); } else { tunk.setPosition(tunk.getX(), getHeight() - tunk.getHeight()); } if (this.spriteCollides(tunk)) { tunk.move(0, -step); } conn.sentMessage("" + tunk.getX(), "" + tunk.getY(), "" + i, "" + 0); } if ((keyState & FIRE_PRESSED) != 0) { bullet = new Bullet(img); switch (lastState) { case LEFT_PRESSED: // tunk1.setTransform(Sprite.TRANS_MIRROR_ROT90); bullet_x = tunk.getX(); bullet_y = tunk.getY() + tunk.getHeight() / 2; // laststate = LEFT_PRESSED; break; case RIGHT_PRESSED: // tunk1.setTransform(Sprite.TRANS_MIRROR_ROT270); bullet_x = tunk.getX() + tunk.getWidth(); bullet_y = tunk.getY() + tunk.getHeight() / 2; // laststate = RIGHT_PRESSED; break; case UP_PRESSED: // tunk1.setTransform(Sprite.TRANS_MIRROR_ROT180); bullet_x = tunk.getX() + tunk.getWidth() / 2; bullet_y = tunk.getY(); // laststate = UP_PRESSED; break; case DOWN_PRESSED: //tunk1.setTransform(Sprite.TRANS_MIRROR); bullet_x = tunk.getX() + tunk.getWidth() / 2; bullet_y = tunk.getY() + tunk.getHeight(); // laststate = DOWN_PRESSED; break; } if (is) { if (bullet != null) { bullet.setPosition(bullet_x, bullet_y); bullet.setDirection(lastState); bullet.setCanvasSize(getWidth(), getHeight()); bullets.addElement(bullet); conn.sentMessage("" + tunk.getX(), "" + tunk.getY(), "" + i, "" + 1); } is = false; } } this.lm.paint(g, 0, 0); flushGraphics(0, 0, getWidth(), getHeight()); } private void drawBullet(Graphics g) { for (int i = 0; i < bullets.size(); i++) { bullet = (Bullet) bullets.elementAt(i); bullet.paint(g); } for (int i = 0; i < tunkBullet.size(); i++) { bullet = (Bullet) tunkBullet.elementAt(i); bullet.paint(g); } flushGraphics(); } private void tick(Graphics g) throws IOException { int N = 100000; for (int i = 0; i < bullets.size(); i++) { bullet = (Bullet) bullets.elementAt(i); bullet.tick(); if (bullet.isLive == false) { bullets.removeElementAt(i); is = true; Sprite exploit = new Sprite(explode); // tunk.setVisible(false); exploit.setPosition(bullet.getX(), bullet.getY()); exploit.paint(g); for (int t = 0; t < N; t++) { ; } exploit.setVisible(false); exploit = null; // tunk = null; } if (bullet.collidesWith(tunk1, true)) { bullets.removeElementAt(i); is = true; bullet.setVisible(false); exploit = new Sprite(explode); tunk1.setVisible(false); exploit.setPosition(tunk1.getRefPixelX(), tunk1.getRefPixelY()); exploit.paint(g); for (int t = 0; t < N; t++) { ; } exploit.setVisible(false); exploit = null; // conn.sentMessage("" + 0, "" + 0, "" + 0, "" + 2); --lifeB; if (lifeB > 0) { for (int t = 0; t < N; t++) { ; } tunk1.setVisible(true); tunk1.setPosition(firstPositionX1, firstPositionY1); } else { isPlay = false; Image l = Image.createImage("/win.png"); Sprite lose = new Sprite(l); lose.defineReferencePixel(lose.getWidth(), lose.getHeight()); lose.setRefPixelPosition(getWidth() / 2, getHeight() / 2); lose.paint(g); flushGraphics(); lm.setViewWindow(0, 0, 0, 0); } } if (bullet.collidesWith(tree, true) || bullet.collidesWith(brutal, true)) { bullets.removeElementAt(i); is = true; bullet.setVisible(false); exploit = new Sprite(explode); exploit.setPosition(bullet.getX(), bullet.getY()); exploit.paint(g); for (int t = 0; t < N; t++) { ; } exploit.setVisible(false); exploit = null; // conn.sentMessage("" + 0, "" + 0, "" + 0, "" + 2); } // System.out.println(bullets.size()); } for (int i = 0; i < tunkBullet.size(); i++) { bullet = (Bullet) tunkBullet.elementAt(i); bullet.tick(); if (bullet.isLive == false) { tunkBullet.removeElementAt(i); colleis = true; exploit = new Sprite(explode); // tunk.setVisible(false); exploit.setPosition(bullet.getX(), bullet.getY()); exploit.paint(g); for (int t = 0; t < N; t++) { ; } exploit.setVisible(false); exploit = null; // tunk = null; } if (bullet.collidesWith(tunk, true)) { bullet.setVisible(false); tunkBullet.removeElementAt(i); colleis = true; exploit = new Sprite(explode); tunk.setVisible(false); exploit.setPosition(tunk.getRefPixelX(), tunk.getRefPixelY()); exploit.paint(g); for (int t = 0; t < N; t++) { ; } exploit.setVisible(false); exploit = null; --lifeA; if (lifeA > 0) { for (int t = 0; t < N; t++) { ; } tunk.setVisible(true); tunk.setPosition(firstPositionX, firstPositionY); } else { isPlay = false; Image l = Image.createImage("/lose.png"); Sprite lose = new Sprite(l); lose.defineReferencePixel(lose.getWidth(), lose.getHeight()); lose.setRefPixelPosition(getWidth() / 2, getHeight() / 2); lose.paint(g); flushGraphics(); lm.setViewWindow(0, 0, 0, 0); } // conn.sentMessage("" + 0, "" + 0, "" + 0, "" + 3); } if (bullet.collidesWith(tree, true) || bullet.collidesWith(brutal, true)) { tunkBullet.removeElementAt(i); colleis = true; bullet.setVisible(false); exploit = new Sprite(explode); exploit.setPosition(bullet.getX(), bullet.getY()); exploit.paint(g); for (int t = 0; t < N; t++) { ; } exploit.setVisible(false); exploit = null; // conn.sentMessage("" + 0, "" + 0, "" + 0, "" + 2); } } flushGraphics(); } public void commandAction(Command arg0, Displayable arg1) {// throw new UnsupportedOperationException("Not supported yet."); if (arg0 == cmdExit) { gameMidlet.gameMenuCanvasBack(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -