import javax.microedition.lcdui.*; import javax.microedition.lcdui.game.*; import java.io.*; import java.util.*; public class Game extends Canvas implements Runnable { boolean keyFire, keyLeft, keyRight, keyUp, keyDown; boolean rn, next, isAttack, isLadder, isJump, isSteps; int w, h; int x, y, y2; int k; int attacktimer, attackx, attacky, steptimer; Image ground, hero, pl, lad, att1, att2, att3, step1, step2; Sprite Hero, Pl, Pl2, Lad, Att1, Att2, Att3, Step1, Step2; Random r = new Random(); private volatile Thread T; private final long OFT = 500 / 25; public Game() { setFullScreenMode(true); w = getWidth(); h = getHeight(); try { hero = Image.createImage("/hr1.png"); ground = Image.createImage("/ground.png"); att1 = Image.createImage("/att1.png"); att2 = Image.createImage("/att2.png"); att3 = Image.createImage("/att3.png"); pl = Image.createImage("/pl.png"); lad = Image.createImage("/ladder.png"); step1=Image.createImage("/step1.png"); step2=Image.createImage("/step2.png"); } catch (IOException ex) { ex.printStackTrace(); } Hero = new Sprite(hero, hero.getWidth(), hero.getHeight()); Att1 = new Sprite(att1, att1.getWidth(), att1.getHeight()); Att2 = new Sprite(att2, att2.getWidth(), att2.getHeight()); Att3 = new Sprite(att3, att3.getWidth(), att3.getHeight()); Pl = new Sprite(pl, pl.getWidth(), pl.getHeight()); Pl2 = new Sprite(pl, pl.getWidth(), pl.getHeight()); Lad = new Sprite(lad, lad.getWidth(), lad.getHeight()); Step1=new Sprite(step1, step1.getWidth(), step1.getHeight()); Step2=new Sprite(step2, step2.getWidth(), step2.getHeight()); x = 40; y = h - Hero.getHeight() - 10; y2 = y; attackx=x+2; attacky=y-2; isLadder = false; rn = true; T = new Thread(this); T.start(); } public void paint(Graphics g) { checkKeys(); if (!isAttack) Hero.setPosition(x, y); Pl.setPosition(-5, 200); Pl2.setPosition(w - Pl.getWidth(), 200); Lad.setPosition(Pl.getWidth() - 30, 195); Att1.setPosition(attackx, attacky); Att2.setPosition(attackx, attacky); Att3.setPosition(attackx, attacky); attackx=x+2; attacky=y-2; Step1.setPosition(x, y); Step2.setPosition(x, y); g.setColor(250, 250, 250); g.fillRect(0, 0, w, h); g.setColor(0, 100, 0); g.fillRect(0, 300, w, h); g.drawImage(ground, 0, h-ground.getHeight(), 0); Pl.paint(g); Pl2.paint(g); Lad.paint(g); Hero.paint(g); g.drawString("x: " + x, 5, 5, 0); ladder(); jump(g); attack(g); steps_annimation(g); } public void steps_annimation (Graphics g) { if (isSteps) { steptimer++; if (steptimer<=10) Step1.paint(g); if (steptimer>=19) { Step2.paint(g); steptimer=0; } if (!keyLeft) isSteps=false; if (!keyRight) isSteps=false; } } public void jump(Graphics g) { int j, jj = 0; if (isJump && (!isLadder)) { for (j = 0; j < 10; j++) { y -= 4; jj++; } } if (jj >= 10) { isJump = false; jj = 0; } } public void attack(Graphics g) { if (isAttack) { attacktimer++; Hero.setPosition(-50, y); if (attacktimer >= 1) { if (attacktimer < 3) { Att1.paint(g); } } if (attacktimer >= 3) { if (attacktimer < 5) { Att2.paint(g); } } if (attacktimer >= 5) { if (attacktimer < 8) { Att3.paint(g); } } if (attacktimer >= 7) { isAttack = false; attacktimer = 0; } } } public void ladder() { if (Hero.collidesWith(Lad, true)) { if (x > 56 && x < 64) { isLadder = true; } } if (x > 64 || x < 56 || y < 203 - Hero.getHeight() ) { isLadder = false; } if (keyUp && isLadder == true) { y -= 2; } if (keyDown && isLadder == true && y<=h - Hero.getHeight() - 10) { y += 2; } if (y < y2 && isLadder == false) { y += 3; } if (Hero.collidesWith(Pl, true)) { y -= 3; } if (Hero.collidesWith(Pl2, true) && y < 201 + Hero.getHeight()) { y -= 3; } } public void run() { long tS, tD; while (rn) { tS = System.currentTimeMillis(); repaint(); tD = System.currentTimeMillis() - tS; if (tD < OFT) { try { Thread.sleep(OFT - tD); } catch (InterruptedException iex) { System.out.println(iex.toString()); } } } } private void checkKeys() { if (keyRight) { x += 2; isSteps=true; } if (keyLeft) { x -= 2; isSteps=true; } } protected void keyPressed(int key) { k = key; if (k == -5 || k == 53) { isAttack = true; } if (k == 50 || k == -1 && (!isLadder) && y>285) { isJump = true; } keys(key, true); } protected void keyReleased(int key) { k = 0; keys(key, false); } protected void keys(int key, boolean press) { int ga = getGameAction(key); if (ga == FIRE) { keyFire = press; } if (ga == UP) { keyUp = press; } if (ga == DOWN) { keyDown = press; } if (ga == LEFT) { keyLeft = press; } else if (ga == RIGHT) { keyRight = press; } } }