0
//This is the human class
//I have added runnable method to make it work but the AI is not moving.

import java.awt.Graphics2D;

import java.awt.event.KeyEvent;

import java.util.Random;

import java.awt.Color;



public class Human implements Runnable{




    private double position=100000;
    private double xa=0;
    private int attack=0;
    private Game game;
    private long time;
    private long lastTime;
    private long MIN_DELAY;
    private int health;
    private int combo;
    private Random r;
    Thread thread;




    public void start(){
        thread = new Thread (this);
        thread.start();

        public void stop(){
            thread.stop();
        }

        public void run1(){

            Thread.currentThread().setPriority(Thread.MIN_PRIORITY);

            while (true)
            {

                game.move();

                if (Game.() < 0){
                }

                try
                {
                    Thread.sleep (10);
                }
                catch (InterruptedException ex)
                {
                    break;
                }

                Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
            }
        }




    public Human(Game game){
        this.game = game;

        MIN_DELAY = 150;
        lastTime=0;
        health=100;
        combo=0;
        r = new Random();
    }
    public Human getPlayer(){
        return this;
    }
    public double getPosition(){
        return position;
    }
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_LEFT){
            xa = -1;
        }
        if (e.getKeyCode() == KeyEvent.VK_RIGHT){
            xa = 1;
        }
        if(e.getKeyCode()== KeyEvent.VK_Z){
            attack = 1;
        }
        if(e.getKeyCode()== KeyEvent.VK_X){
            attack = 2;
        }
        if(e.getKeyCode()== KeyEvent.VK_C){
            attack = 3;
        }
    }

    public String printHealth(){
        String st = "Human Health: ";
        st = st + Integer.toString(health);
        return st;
    }
    public String printCombo(){
        String st = "Combo: ";
        st = st + Integer.toString(combo);
        return st;
    }

    public boolean isAlive(){
        if(health>0){
            return true;
        }else{
            return false;
        }
    }




    public int attack(){
        int dam = 0;
        time = System.currentTimeMillis();
        if(time-lastTime>MIN_DELAY){
            System.out.println("COMBO" + combo);
            if(combo<3){
            if(attack==1){
                System.out.println("Human Punch");
                dam = r.nextInt(6-1)+1;;
                combo++;
            }else if(attack==2){
                System.out.println("Human Kick");
                dam = r.nextInt(11-5)+5;
                combo++;
            }else if(attack==3){
                System.out.println("Human Uppercut");
                dam = r.nextInt(16-10)+10;
                combo++;
            }
            }else{
                if(attack == 1){
                System.out.println(" SUPER Human PUNCH");
                dam = r.nextInt(11-1)+1;
                combo = 0;
            }else if(attack ==2){
                System.out.println(" SUPER Human KICK");
                dam = r.nextInt(21-10)+10;
                combo = 0;
            }else if(attack == 3){
                System.out.println(" SUPER Human UPPERCUT");
                dam = r.nextInt(31-20)+20;
                combo = 0;
            }
            }
        }
        lastTime = time;
        return dam;

    }
    public void keyReleased(KeyEvent e) {
        xa = 0;
        attack = 0;
    }
    public int getAttack(){
        return attack;
    }
    public void paint(Graphics2D g) {
        g.fillRect(position-30, 200, 30, 30);
    }



    public void move(double opp) {

        //if(time-lastTime>mDelay){
            if (position + xa > 30 && position + xa < opp-17000)
                position = position + xa/500;
        //}

    }

    public int getHealth(){
        return health;
    }

    public void takeHealth(int dam){
        health = health - dam;
        combo = 0;
    }



      // private Thread t;

       public void run() {

          try {
                move(game.getAIPosition());


                // Let the thread sleep for a while.
                Thread.sleep(1000);

         } catch (InterruptedException e) {
             System.out.println("Human Thread interrupted.");
         }
         System.out.println("Human Thread exiting.");
       }




       /*public void start ()
       {
          System.out.println("Starting Human" );
          if (t == null)
          {
             t = new Thread (this);
             t.start ();
          }
       }*/``



// This is the game class




import java.awt.Color;

import java.awt.Component;

import java.awt.Container;

import java.awt.Dimension;

import java.awt.Font;

import java.awt.Graphics;

import java.awt.Graphics2D;

import java.awt.RenderingHints;

import java.awt.event.KeyEvent;

import java.awt.event.KeyListener;

import java.awt.geom.Ellipse2D;

import java.awt.image.BufferedImage;

import java.io.IOException;

import java.util.Random;

import javax.imageio.ImageIO;

import javax.swing.JFrame;

import javax.swing.JPanel;

@SuppressWarnings("serial")
public class Game extends JPanel{

    public Human human = new Human(this);
    public  AIPlayer ai = new AIPlayer(this);




    public Game(){
        setPreferredSize(new Dimension(WIDTH, HEIGHT));
        setFocusable(true);
        requestFocus();
        init();

        addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {
            }

            @Override
            public void keyReleased(KeyEvent e) {
                human.keyReleased(e);
            }

            @Override
            public void keyPressed(KeyEvent e) {
                human.keyPressed(e);
            }
        });
        setFocusable(true);


    }



    double x=ai.getPosition();
    int z=75; //Enemy Position
    int y = 200;

    public void move(){
        human.move(ai.getPosition());
        ai.move((ai.getPosition()-human.getPosition()), human.getHealth(), human.getPosition()); //CAN'T GET THIS TO WORK WITH HUMAN MOVING
        System.out.println("Human position: " + human.getPosition());
        System.out.println("AI Position: " + ai.getPosition());
        System.out.println("Range: " + (ai.getPosition()-human.getPosition()));
    }


    public void makeSound(){
        Random r = new Random();
        int x = r.nextInt(10);
        if(x==5 || x==6){
        ai.makeSound(human.getHealth());
        }

    }



    public double getAIPosition(){
        return ai.getPosition();
    }
    public double getHumanPosition(){
        return human.getPosition();
    }
    public int getHumanHealth(){
        return human.getHealth();
    }
    public double getDistance(){
        return (ai.getPosition()-human.getPosition());
    }

    public void attack(){
        int hDec = human.getAttack(); 
        System.out.println("Human Decision: " + hDec);
        if(checkDistance(hDec)){
            ai.takeHealth(human.attack());


        }
        human.takeHealth(ai.attack(ai.getPosition()-human.getPosition()));

        ai.attack(ai.getPosition()-human.getPosition());
    }




    public boolean checkDistance(int mve){
        double dist = ai.getPosition() - human.getPosition();
        if(mve==1 && dist<=25364){
            return true;
        }else if(mve ==2 && dist<=29040){
            return true;
        }else if(mve==3 && dist<=20536){
            return true;
        }else{
            return false;
        }


    }
    public void decreaseAnger(){
        ai.decreaseAnger();
    }




    public boolean gameAlive(){
        if(ai.isAlive()&&human.isAlive()){
            return true;
        }else{
            return false;
        }
    }




    public void displayDetails(Graphics g){
        Font myFont = new Font("Courier New", 1, 17);
        g.setColor(Color.YELLOW);
        g.setFont(myFont);

        g.drawString(human.printHealth(), 10,15);
        g.drawString(human.printCombo(), 10, 30);
        g.drawString(ai.printHealth(), 550,15);
        g.drawString(ai.printCombo(), 550, 30);
        g.drawString(ai.printAnger(), 550, 45);
    }




    public static void main(String[] args) throws Exception {
         TODO Auto-generated method stub
    JFrame frame = new JFrame("Mini Tekken");
        Game game = new Game();
    frame.add(game);        
        frame.setSize(300, 300);
    frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        while (game.gameAlive()) {
            game.move();
        game.attack();
            game.decreaseAnger();
            game.repaint();
            game.makeSound();
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }




        public static void main(String[] args){
            Game game = new Game();
            JFrame window = new JFrame("Mini-Tekken");
            window.setContentPane(game);
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            window.setResizable(false);
            window.pack();
            window.setLocationRelativeTo(null);
            window.setVisible(true);

            while(game.gameAlive()){
            game.move();
            game.attack();
            game.decreaseAnger();
            game.makeSound();
            game.repaint();
            }




        }




        //Dimensions
        public static final int WIDTH = 736;
        public static final int HEIGHT = 309;
        BufferedImage background;
        BufferedImage playerOne;
        BufferedImage playerTwo;
        public void paintComponent(Graphics g){
            super.paintComponent(g);
            if (background != null) {
                g.drawImage(background, 0, 0, null);
                }
            if(playerOne != null){
                g.drawImage(playerOne, ((int)human.getPosition()/500), 200, null);

            }
            if(playerTwo != null){
                g.drawImage(playerTwo, ((int)ai.getPosition()/500), 210, null);
            }
            displayDetails(g);

        }

        private void init() {
            try {
                background =                    ImageIO.read(getClass().getResourceAsStream("/images/background.jpg"));
                playerOne = ImageIO.read(getClass().getResourceAsStream("/images/p1.fw.png"));
                playerTwo = ImageIO.read(getClass().getResourceAsStream("/images/p2.fw.png"));


            }catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }



}
techraf
  • 64,883
  • 27
  • 193
  • 198
  • 1
    Threads are complicated. You don't necessarily need threads in any programming language for gaming if you construct the game loop such that it schedules the movement of both entities for each cycle through the loop. If the intent is to learn about threading and concurrency then go ahead, but it can be done without to save you from the complexity. – nbering Apr 26 '16 at 14:27
  • You don't need threads for this. You simply loop through the players, update them, then code your collision detection, etc, accordingly. No need for threads to do that. – ManoDestra Apr 26 '16 at 14:34

0 Answers0