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 is_attack; boolean rn; int w, h, k; int x, y; int flame_timer, stars_x, stars_y; int shot_x, shot_x2, shot_y, flame_x, flame_y; int flame_enemy1_x, flame_enemy1_y; int enemy1_x, enemy1_y; Image ship1, shot1, flame1, flame2; Image enemy1; Sprite Ship1, Shot1, Flame1, Flame2; Sprite Enemy1, Flame1_enemy1, Flame2_enemy1; Random r = new Random(); private volatile Thread T; private final long OFT = 500 / 25; public Game() { setFullScreenMode(true); w = getWidth(); h = getHeight(); try { ship1 = Image.createImage("/ship1.png"); shot1 = Image.createImage("/shot1.png"); flame1 = Image.createImage("/flame1.png"); flame2 = Image.createImage("/flame2.png"); enemy1 = Image.createImage("/enemy1.png"); } catch (IOException ex) { ex.printStackTrace(); } Ship1 = new Sprite(ship1, ship1.getWidth(), ship1.getHeight()); Enemy1 = new Sprite(enemy1, enemy1.getWidth(), enemy1.getHeight()); Shot1 = new Sprite(shot1, shot1.getWidth(), shot1.getHeight()); Flame1 = new Sprite(flame1, flame1.getWidth(), flame1.getHeight()); Flame2 = new Sprite(flame2, flame2.getWidth(), flame2.getHeight()); Flame1_enemy1 = new Sprite(flame1, flame1.getWidth(), flame1.getHeight()); Flame2_enemy1 = new Sprite(flame2, flame2.getWidth(), flame2.getHeight()); x = w/2; y = 290; enemy1_x = 70; enemy1_y = 20; shot_x = -11; shot_x2 = x+11; shot_y = y-6; flame_x = x+12; flame_y = y+19; flame_enemy1_x = enemy1_x+12; flame_enemy1_y = enemy1_y-1; stars_x = 5; stars_y = 10; flame_timer = 0; rn = true; T = new Thread(this); T.start(); } public void paint(Graphics g) { checkKeys(); screen(); g.setColor(0, 0, 0); g.fillRect(0, 0, w, h); Ship1.setPosition(x, y); Enemy1.setPosition(enemy1_x, enemy1_y); Flame1.setPosition(flame_x, flame_y); Flame2.setPosition(flame_x, flame_y); Flame1_enemy1.setPosition(flame_enemy1_x, flame_enemy1_y); Flame2_enemy1.setPosition(flame_enemy1_x, flame_enemy1_y); Shot1.setPosition(shot_x, shot_y); Ship1.paint(g); Enemy1.paint(g); Shot1.paint(g); g.setColor(0, 200, 0); g.drawString("stars_y: " + stars_y, 5, 5, 0); cod_puli(); flame_anim(g); enemy1(); stars(g); } public void flame_anim(Graphics g) { flame_x = x + 12; // пламя корабля игрока flame_y = y + 19; flame_timer++; if (flame_timer <= 5) { Flame1.paint(g); Flame1_enemy1.paint(g); } if (flame_timer > 5) { Flame2.paint(g); Flame2_enemy1.paint(g); } if (flame_timer >= 10) { flame_timer = 0; } flame_enemy1_x = enemy1_x+12; // пламя корабля врага 1 flame_enemy1_y = enemy1_y-1; } public void cod_puli() { // великий Код Пули if (is_attack) { // если активно условие атаки shot_x = shot_x2; // пуля переносится к кораблю shot_y -= 17; // и перемещается вверх экрана } if (shot_y <= -5) { // когда пуля переместилась за экран is_attack = false; // условие атаки устанавливается как false shot_y = y-6; // пуля перемещается под корабль по оси y shot_x = -11; // и за экран по оси x shot_x2 = x+11; // координаты для появления пули (х - корабль) } } public void screen() { if (x >= w-ship1.getWidth()) { x = w-ship1.getWidth(); } if (x <= 0) { x = 0; } } public void stars(Graphics g) { g.setColor(100, 100, 100); stars_y ++; if (stars_y >= 320) { stars_y = 0; } g.drawString(".", stars_x+10, stars_y, 0); g.drawString(".", stars_x+100, stars_y+20, 0); g.drawString(".", stars_x+200, stars_y+30, 0); g.drawString(".", stars_x+50, stars_y+60, 0); g.drawString(".", stars_x+140, stars_y+80, 0); g.drawString(".", stars_x+30, stars_y+120, 0); g.drawString(".", stars_x+170, stars_y+130, 0); g.drawString(".", stars_x+110, stars_y+140, 0); g.drawString(".", stars_x+190, stars_y+180, 0); g.drawString(".", stars_x+40, stars_y+200, 0); g.drawString(".", stars_x+140, stars_y+230, 0); g.drawString(".", stars_x+90, stars_y+260, 0); g.drawString(".", stars_x+20, stars_y+270, 0); g.drawString(".", stars_x+200, stars_y+280, 0); g.drawString(".", stars_x+160, stars_y+320, 0); g.drawString(".", stars_x+10, stars_y-320, 0); g.drawString(".", stars_x+100, stars_y-300, 0); g.drawString(".", stars_x+200, stars_y-290, 0); g.drawString(".", stars_x+50, stars_y-260, 0); g.drawString(".", stars_x+140, stars_y-240, 0); g.drawString(".", stars_x+30, stars_y-200, 0); g.drawString(".", stars_x+170, stars_y-190, 0); g.drawString(".", stars_x+110, stars_y-180, 0); g.drawString(".", stars_x+190, stars_y-140, 0); g.drawString(".", stars_x+40, stars_y-120, 0); g.drawString(".", stars_x+140, stars_y-90, 0); g.drawString(".", stars_x+90, stars_y-60, 0); g.drawString(".", stars_x+20, stars_y-50, 0); g.drawString(".", stars_x+200, stars_y-40, 0); g.drawString(".", stars_x+160, stars_y, 0); } public void enemy1() { enemy1_y +=2; if (enemy1_y >= 320) { enemy1_y = -100; enemy1_x = r.nextInt(200); } } 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 += 3; if (is_attack == false & x<=w-ship1.getWidth()) { shot_x2 +=3; } } if (keyLeft) { x -= 3; if (is_attack == false & x>=0) { shot_x2 -=3; } } } protected void keyPressed(int key) { k = key; if (k == -5 || k == 53) { is_attack=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; } } }