https://pastein.ru/t/BJ

  скопируйте уникальную ссылку для отправки


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.imageio.*;
import java.io.*;

public class Panel extends JPanel{
	//Переменная для логики игры
	private Logic gameEngine;
	private Timer timerDraw;
	private JLabel score;
	private JButton btn_new_game, btn_exit_game;
	private Image head, body, apple;
	private Timer timerUpdate;
	
	private class MoveByKey implements KeyListener {

		@Override
		public void keyTyped(KeyEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void keyPressed(KeyEvent e) {
			// TODO Auto-generated method stub
			int key = e.getKeyCode();
			
			//Если нажатие одной из четырех стрелочек
			if(key==KeyEvent.VK_LEFT) gameEngine.direction = 0;
			if(key==KeyEvent.VK_UP) gameEngine.direction = 1;
			if(key==KeyEvent.VK_RIGHT) gameEngine.direction = 2;
			if(key==KeyEvent.VK_DOWN) gameEngine.direction = 3;
		}

		@Override
		public void keyReleased(KeyEvent e) {
			// TODO Auto-generated method stub
			
		}
		
	}
	
	public Panel() {
		this.addKeyListener(new MoveByKey());
		this.setFocusable(true);
		setBackground(Color.darkGray);
		gameEngine = new Logic();
		//Запускаем игру 
		gameEngine.start();
		
		try {
			head = ImageIO.read(new File("c:\\head.png"));
			body = ImageIO.read(new File("c:\\body.png"));
			apple = ImageIO.read(new File("c:\\apple.png"));
		}
		catch(Exception ex) {
			
		}
		
		timerDraw = new Timer(16, new ActionListener() {		
			public void actionPerformed(ActionEvent e) {
				//вызываем перерисовку 
				repaint();
			}
		});
		
		timerDraw.start();
		
		timerUpdate = new Timer(100, new ActionListener() {		
			public void actionPerformed(ActionEvent e) {
				//Перемещаем голову змеи
				gameEngine.moveHead();
				//выводим информацию о количество баллов
				score.setText("Score: " + gameEngine.scored);
			}
		});
		
		timerUpdate.start();
		
		setLayout(null);
		
		score = new JLabel("Счет: 0");
		score.setForeground(Color.PINK);
		score.setFont(new Font("serif",0,20));
		score.setBounds(630,200,150,50);
		add(score);
		
		btn_new_game = new JButton();
		btn_new_game.setText("New game");
		btn_new_game.setForeground(Color.PINK);
		btn_new_game.setFont(new Font("serif",0,20));
		btn_new_game.setBounds(630,30,150,50);
		btn_new_game.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				gameEngine.start();
			}
			
		});
		add(btn_new_game);
		
		btn_exit_game = new JButton();
		btn_exit_game.setText("EXIT");
		btn_exit_game.setForeground(Color.PINK);
		btn_exit_game.setFont(new Font("serif",0,20));
		btn_exit_game.setBounds(630,100,150,50);
		btn_exit_game.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				System.exit(0);
			}
			
		});
		add(btn_exit_game);
	}
	
	public void paintComponent(Graphics gr) {
		super.paintComponent(gr);
		
		for(int i = 0; i<30; i++) {
			for(int j = 0; j < 30; j++) {
				
				if (gameEngine.array[i][j]!=0) {
					
					if(gameEngine.array[i][j]==1) {
						
						gr.drawImage(head, 10+j*20, 10+i*20 , 20, 20, null);
					}
					
					else if (gameEngine.array[i][j]==-1) {
						gr.drawImage(apple, 10+j*20, 10+i*20 , 20, 20, null);
					}
				}
			}
		}
		
		gr.setColor(Color.pink);
		for(int i = 0; i<=30; i++) {
			gr.drawLine(10+i*20, 10, 10 + i*20, 610);
			gr.drawLine(10, 10+i*20, 610, 10+i*20);		
		}
	}
}