Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
5.1 kB
3
Indexable
Never
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;

public class Player extends KeyAdapter implements ActionListener{

    public int pIndex,rychl = Panel.puvodRychl, delka = Panel.startDelka, body = 0,sJabl = 0;
    static final int[] x = new int[Panel.unitPocet];
    static final int[] y = new int[Panel.unitPocet];
    String smer = "N";
    Boolean jizStiskl = false, bylyDefs = false, dash = false;
    Timer timer;
    public void SetDefs()
    {
        JButton b = new JButton("clear");
        b.addKeyListener(new MyKeyAdapter());

        delka = Panel.startDelka;
        sJabl = 0;
        body = 0;
        jizStiskl = false;
        dash = false;

        int j;
        if(pIndex == 0)j =150;
        else j = 450;

        for (int i : x) {
            x[i] =j;
        }for (int i : y) {
            y[i] = 500;
        }
        System.out.println(x[0]);

        timer = new Timer(100, this);
        setTimer(1);

        bylyDefs = true;
    }
    public void pohyb()
    {
        System.out.println(x[0]);
        if(bylyDefs) {
            for (int i = delka; i > 0; i--) {
                x[i] = x[i - 1];
                y[i] =  y[i - 1];
            }

            if (x[0] == Panel.gameSirka) {
                x[0] = 0;
            } else if (x[0] == -Panel.unitVel) {
                x[0] += Panel.gameSirka;
            } else if (y[0] == Panel.gameVyska + 50) {
                y[0] = 50;
            } else if (y[0] == -Panel.unitVel + 50) {
                y[0] += Panel.gameVyska;
            } else {
                switch (smer) {
                    case "N":
                        y[0] -= Panel.unitVel;
                        break;
                    case "D":
                        y[0] += Panel.unitVel;
                        break;
                    case "L":
                        x[0] -= Panel.unitVel;
                        break;
                    case "P":
                        x[0] += Panel.unitVel;
                        break;
                }
            }
        }else {
            SetDefs();
        }
    }

    public void setTimer(int delitel)
    {
        timer.stop();
        timer = new Timer(rychl/delitel,this);
        timer.start();
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if(!dash)jizStiskl = false;

        if(Panel.hra)
        {
            pohyb();
        }
        Panel.repaintExport();
    }
    public class MyKeyAdapter extends KeyAdapter {

        @Override
        public void keyPressed(KeyEvent e) {

            if (!jizStiskl && !dash) {
                switch (e.getKeyCode()) {
                    case KeyEvent.VK_A:
                        if (!smer.equals("P")) {
                            if (smer.equals("L")) {
                                dash = true;
                                setTimer(2);
                                body--;
                            }
                            smer = "L";
                            jizStiskl = true;
                        }
                        break;
                    case KeyEvent.VK_D:
                        if (!smer.equals("L")) {
                            if (smer.equals("P")) {
                                dash = true;
                                setTimer(2);
                                body--;
                            }
                            smer = "P";
                            jizStiskl = true;
                        }
                        break;
                    case KeyEvent.VK_W:
                        if (!smer.equals("D")) {
                            if (smer.equals("N")) {
                                dash = true;
                                setTimer(2);
                                body--;
                            }
                            smer = "N";
                            jizStiskl = true;
                        }
                        break;
                    case KeyEvent.VK_S:
                        if (!smer.equals("N")) {
                            if (smer.equals("D")) {
                                dash = true;
                                setTimer(2);
                                body--;
                            }
                            smer = "D";
                            jizStiskl = true;
                        }
                        break;
                }
            }

        }

        @Override
        public void keyReleased(KeyEvent e) {
            if (dash) {
                if (e.getKeyCode() == KeyEvent.VK_A || e.getKeyCode() == KeyEvent.VK_D || e.getKeyCode() == KeyEvent.VK_S || e.getKeyCode() == KeyEvent.VK_W) {
                    setTimer(1);
                    dash = false;
                    jizStiskl = false;
                }
            }
        }
    }
}
Leave a Comment