Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Font;
- import java.awt.FontMetrics;
- import java.awt.Graphics;
- import java.awt.Image;
- import java.awt.Toolkit;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.KeyAdapter;
- import java.awt.event.KeyEvent;
- import javax.swing.Timer;
- import javax.swing.ImageIcon;
- import javax.swing.JPanel;
- public class Plansza extends JPanel implements ActionListener {
- private final int WIDTH = 800;
- private final int HEIGHT = 800;
- private final int PUNKT_ROZMIAR = 10;
- private final int P_PUNKTY = 900;
- private final int DELAY = 100;
- private final int x[] = new int[P_PUNKTY];
- private final int y[] = new int[P_PUNKTY];
- private int punkty;
- private boolean lewo = false;
- private boolean prawo = true;
- private boolean gora = false;
- private boolean dol = false;
- private boolean wGrze = true;
- private Timer timer;
- private Image ogon;
- private Image glowa;
- private Image logo;
- private Image logo1;
- private int jabłko_x;
- private int jabłko_y;
- private int RAND=29;
- public Plansza() {
- tworzPlansza();
- }
- private void tworzPlansza() {
- setBackground(Color.white);
- setFocusable(true);
- setPreferredSize(new Dimension(WIDTH, HEIGHT));
- loadImages();
- tworzGra();
- addKeyListener(new TAdapter());
- }
- private void loadImages() {
- ImageIcon io = new ImageIcon("src/icons/ogon.png");
- ogon = io.getImage();
- ImageIcon ig = new ImageIcon("src/icons/glowa.png");
- glowa = ig.getImage();
- ImageIcon il = new ImageIcon("src/icons/logo.png");
- logo = il.getImage();
- ImageIcon ill = new ImageIcon("src/icons/logo1.jpg");
- }
- private void tworzGra() {
- punkty = 3;
- for (int z = 0; z < punkty; z++) {
- x[z] = 50 - z * 10;
- y[z] = 50;
- }
- Apple();
- timer = new Timer(DELAY, this);
- timer.start();
- }
- @Override
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- doDrawing(g);
- }
- private void doDrawing(Graphics g) {
- if (wGrze) {
- g.drawImage(logo, jabłko_x, jabłko_y, this);
- for (int z = 0; z < punkty; z++) {
- if (z == 0) {
- g.drawImage(glowa, x[z], y[z], this);
- } else {
- g.drawImage(ogon, x[z], y[z], this);
- }
- }
- }
- else {
- gameOver(g);
- }
- }
- private void ruch() {
- for (int z = punkty; z > 0; z--) {
- x[z] = x[(z - 1)];
- y[z] = y[(z - 1)];
- }
- if (lewo) {
- x[0] -= PUNKT_ROZMIAR;
- }
- if (prawo) {
- x[0] += PUNKT_ROZMIAR;
- }
- if (gora) {
- y[0] -= PUNKT_ROZMIAR;
- }
- if (dol) {
- y[0] += PUNKT_ROZMIAR;
- }
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- if (wGrze) {
- checkApple();
- ruch();
- checkCollision();
- }
- repaint();
- }
- private class TAdapter extends KeyAdapter {
- @Override
- public void keyPressed(KeyEvent e) {
- int key = e.getKeyCode();
- if ((key == KeyEvent.VK_LEFT) && (!prawo)) {
- lewo = true;
- gora = false;
- dol = false;
- }
- if ((key == KeyEvent.VK_RIGHT) && (!lewo)) {
- prawo = true;
- gora = false;
- dol = false;
- }
- if ((key == KeyEvent.VK_UP) && (!dol)) {
- gora = true;
- prawo = false;
- lewo = false;
- }
- if ((key == KeyEvent.VK_DOWN) && (!gora)) {
- dol = true;
- prawo = false;
- lewo = false;
- }
- }
- }
- private void Apple() {
- int r = (int) (Math.random() * RAND);
- jabłko_x = ((r * PUNKT_ROZMIAR));
- r = (int) (Math.random() * RAND);
- jabłko_y = ((r * PUNKT_ROZMIAR));
- }
- private void checkApple() {
- if ((x[0] == jabłko_x) && (y[0] == jabłko_y)) {
- punkty++;
- Apple();
- }
- }
- private void checkCollision() {
- for (int z = punkty; z > 0; z--) {
- if ((z > 4) && (x[0] == x[z]) && (y[0] == y[z])) {
- wGrze = false;
- }
- }
- if (y[0] >= HEIGHT) {
- wGrze = false;
- }
- if (y[0] < 0) {
- wGrze = false;
- }
- if (x[0] >= WIDTH) {
- wGrze = false;
- }
- if (x[0] < 0) {
- wGrze = false;
- }
- if (!wGrze) {
- timer.stop();
- }
- }
- private void gameOver(Graphics g) {
- String msg = "Koniec gry";
- Font small = new Font("Arial", Font.BOLD, 25);
- FontMetrics metr = getFontMetrics(small);
- Image logo1 = Toolkit.getDefaultToolkit().getImage("src/icons/logo1.jpg");
- g.drawImage(logo1, 50, 100, this);
- g.setColor(Color.black);
- g.setFont(small);
- g.drawString(msg, (WIDTH - metr.stringWidth(msg)) / 2, HEIGHT / 2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement