Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.Graphics;
- import java.awt.Point;
- import java.awt.Rectangle;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.awt.event.MouseMotionAdapter;
- import java.awt.event.MouseMotionListener;
- import java.awt.geom.Line2D;
- public class ad extends JPanel {
- public int spawnRate = 2000;
- public int maximumAmountOfMobs = 200;
- public int[] mobKind;
- public int mobMovementSpeed = 10;
- public int mobTimmy = 0;
- public int mobSize = 30;
- public int health = 100;
- public int money = 500;
- public int shopSize = 11;
- public int sizeBetween = 10;
- public int fade = shopSize+1;
- public int heldTower = 0;
- public int towerID[];
- public int air = 99;
- public Rectangle[] mobs;
- public Rectangle[] tower;
- public Rectangle[] shop = new Rectangle[shopSize];
- public Rectangle end;
- public Line2D shot;
- public Point mse;
- public Timer spawn;
- public Timer mobMovement;
- public boolean gameOver = false;
- public boolean inGame = true;
- public boolean holdingTower = false;
- public void defineObjects() {
- mobKind = new int[maximumAmountOfMobs];
- mobs = new Rectangle[maximumAmountOfMobs];
- for (int i=0;i<mobs.length;i++) {
- mobs[i] = new Rectangle(-5000, -5000, 0, 0);
- mobKind[i] = 0;
- }
- for(int i=0;i<shop.length;i++) {
- shop[i] = new Rectangle((100)+i*32 + (sizeBetween*i), 12, 32, 32);
- }
- end = new Rectangle(0, aa.height/2-100/2, 30, 100);
- tower = new Rectangle[200];
- towerID = new int[200];
- for(int i=0;i<tower.length;i++) {
- tower[i] = new Rectangle(-5000, -5000, 0, 0);
- towerID[i] = air;
- }
- }
- public ad(ab f, ac bp) {
- defineObjects();
- f.addMouseListener(new MouseAdapter() {
- public void mouseClicked(MouseEvent e) {
- if(e.getButton() == MouseEvent.BUTTON1) {
- if(holdingTower) {
- if(shop[0].contains(mse)) {
- heldTower = mobTimmy;
- holdingTower = true;
- }
- } else {
- for(int i=0;i<tower.length;i++) {
- if(towerID[i] == air) {
- tower[i] = new Rectangle(mse.x-16, mse.y-16, 32, 32);
- towerID[i] = heldTower;
- }
- }
- }
- }
- }
- });
- f.addMouseMotionListener(new MouseMotionAdapter() {
- public void mouseMoved(MouseEvent e) {
- mse = new Point(e.getX(), e.getY()-28);
- boolean nope = true;
- for(int i=0;i<shop.length;i++) {
- if(shop[i].contains(mse)) {
- fade = i;
- nope = false;
- }
- }
- if(nope){
- fade = shopSize+1;
- }
- repaint();
- }
- });
- ActionListener spawnListener = new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- if(inGame) {
- for(int i=0;i<maximumAmountOfMobs;i++) {
- if(mobs[i].x <= 0) {
- mobs[i] = new Rectangle(aa.width, aa.height/2-mobSize/2-15, mobSize, mobSize);
- i += maximumAmountOfMobs+1;
- }
- }
- }
- repaint();
- }
- };
- spawn = new Timer(spawnRate, spawnListener);
- spawn.start();
- ActionListener mobMovementListener = new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- if(inGame) {
- for(int i=0;i<maximumAmountOfMobs;i++) {
- if(end.contains(mobs[i])) {
- health -= 1;
- mobs[i] = new Rectangle(-5000, -5000, 0, 0);
- checkForGameOver ();
- } else {
- mobs[i].x -= 1;
- }
- }
- }
- repaint();
- }
- };
- mobMovement = new Timer(mobMovementSpeed, mobMovementListener);
- mobMovement.start();
- }
- public void checkForGameOver () {
- if(health == 0) {
- gameOver = true;
- inGame = false;
- }
- }
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- if(aa.f.bp.imagesLoaded) {
- for(int i=0;i<maximumAmountOfMobs;i++) {
- g.drawImage(aa.f.bp.ene[mobKind[i]], mobs[i].x, mobs[i].y, mobs[i].width, mobs[i].height, null);
- }
- }
- //Panel
- g.setColor(Color.DARK_GRAY);
- g.fill3DRect(-4, -4, aa.width+4,60, true);
- //Health
- g.setFont(new Font("Arial", Font.BOLD, 16));
- g.setColor(Color.WHITE);
- g.drawImage(aa.f.bp.style[0], 10, 10, 16, 16, null);
- g.drawString(health + "", 10+16+3, 23);
- //Money
- g.drawImage(aa.f.bp.style[1], 10, 10+16+5, 16, 16, null);
- g.drawString("$" + money, 10+16+3, 23+16+5);
- //Towers
- for(int i=0;i<tower.length;i++) {
- if(towerID[i] != air)
- g.drawImage(aa.f.bp.tower[towerID[i]], tower[0].x, tower[0].y, tower[0].width, tower[0].height, null);
- }
- //Hand held
- if(holdingTower) {
- g.drawImage(aa.f.bp.tower[heldTower], mse.x - 32/2, mse.y - 32/2, 32, 32, null);
- }
- //Panel Components
- for(int i=0;i<shop.length;i++) {
- g.setColor(Color.GRAY);
- g.fillRect(shop[i].x, shop[i].y, shop[i].width, shop[i].height);
- g.setColor(Color.WHITE);
- g.drawRect(shop[i].x-2, shop[i].y-2, shop[i].width+3, shop[i].height+3);
- if(i == fade) {
- g.drawImage(aa.f.bp.style[2], shop[i].x, shop[i].y, shop[i].width, shop[i].height, null);
- }
- }
- g.drawImage(aa.f.bp.tower[0], 100, 12, 32, 32, null);
- if(gameOver) {
- g.setColor(Color.RED);
- g.fillRect(0, 0, aa.width+0, aa.height+10);
- g.setColor(Color.WHITE);
- g.drawString("GAME OVER! :(", 10, 20);
- }
- }
- }
Add Comment
Please, Sign In to add comment