Skip to content

Instantly share code, notes, and snippets.

@exavier
Created Sep 28, 2015
Embed
What would you like to do?
package ru.java2e;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
public class Road extends JPanel implements ActionListener, Runnable {
Timer mainTimer = new Timer(20, this);
Image img = new ImageIcon("res/fon.jpg").getImage();
Player p = new Player();
Thread enemiesFactory = new Thread(this);
Thread enemiesEjFactory = new Thread(this);
Thread bonusFactory = new Thread(this);
Thread audioThread = new Thread(new AudioTread());
List<Enemy> enemies = new ArrayList<Enemy>();
List<EnemyEj> enemiesEj = new ArrayList<EnemyEj>();
List<Bonus> bonus = new ArrayList<Bonus>();
public Road() {
audioThread.start();
mainTimer.start();
enemiesFactory.start();
enemiesEjFactory.start();
bonusFactory.start();
addKeyListener(new MyKeyAdapter());
setFocusable(true);
}
@Override
public void run() {
Random rand = new Random();
while (true) {
try {
Thread.sleep(2000);
enemies.add(new Enemy(900, 80, 4, this));
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
Thread.sleep(rand.nextInt(3000));
enemiesEj.add(new EnemyEj(900, 230, 2, this));
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
Thread.sleep(rand.nextInt(5000));
bonus.add(new Bonus(900, 230, this));
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
Thread.sleep(rand.nextInt(5000));
bonus.add(new Bonus(900, 80, this));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private class MyKeyAdapter extends KeyAdapter {
public void keyPressed(KeyEvent e) {
p.keyPressed(e);
}
public void keyReleased(KeyEvent e) {
p.keyReleased(e);
}
}
public void paint(Graphics g) {
g = (Graphics2D) g;
g.drawImage(img, p.layer1, 0, null);
g.drawImage(img, p.layer2, 0, null);
g.drawImage(p.img, p.x, p.y, null);
g.drawImage(p.img10, p.xHealth, p.yHealth, null);
g.drawString("Собрано трофеев: ", 250, 20);
Iterator<Enemy> i = enemies.iterator();
while (i.hasNext()) {
Enemy e = i.next();
if (e.x >= 1600 || e.x <= -1600) {
i.remove();
} else {
e.move(); // поправить позже
g.drawImage(e.img, e.x, e.y, null);
}
}
Iterator<EnemyEj> j = enemiesEj.iterator();
while (j.hasNext()) {
EnemyEj e = j.next();
if (e.x >= 1600 || e.x <= -1600) {
j.remove();
} else {
e.move(); // поправить позже
g.drawImage(e.img, e.x, e.y, null);
}
}
Iterator<Bonus> d = bonus.iterator();
while (d.hasNext()) {
Bonus e = d.next();
if (e.x >= 1600 || e.x <= -1600) {
d.remove();
} else {
e.move(); // поправить позже
g.drawImage(e.img, e.x, e.y, null);
}
}
}
public void actionPerformed(ActionEvent e) {
p.move();
repaint();
testCollisionWithEnemies();
testWin();
}
private void testWin() {
if (p.s >= 30000) {
JOptionPane.showMessageDialog(null, "You win!");
System.exit(0);
}
}
private void testCollisionWithEnemies() {
Iterator<Enemy> i = enemies.iterator();
while (i.hasNext()) {
Enemy e = i.next();
if (p.getRect().intersects(e.getRect())) {
p.health = p.health - p.DAMAGE;
p.x = p.x + 2; // при столкновении игрока откидывает на +2 по оси x
if (p.health <= 0) {
JOptionPane.showMessageDialog(null, "Game over!");
System.exit(1);
}
}
}
Iterator<EnemyEj> j = enemiesEj.iterator();
while (j.hasNext()) {
EnemyEj e = j.next();
if (p.getRect().intersects(e.getRect())) {
p.health = p.health - p.DAMAGE;
p.x = p.x + 2; // при столкновении игрока откидывает на +2 по оси x
if (p.health <= 0) {
JOptionPane.showMessageDialog(null, "Game over!");
System.exit(1);
}
}
}
Iterator<Bonus> d = bonus.iterator();
while (d.hasNext()) {
Bonus e = d.next();
if (p.getRect().intersects(e.getRect())) {
d.remove();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment