Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import java.util.Random;
- public class GamePanel extends JPanel implements ActionListener {
- static final int SCREEN_WIDTH = 1300;
- static final int SCREEN_HEIGHT = 750;
- static final int UNIT_SIZE = 40;
- static final int GAME_UNITS = (SCREEN_WIDTH * SCREEN_HEIGHT) / UNIT_SIZE;
- static final int DELAY = 150;
- int x[] = new int[GAME_UNITS];
- int y[] = new int[GAME_UNITS];
- int bodyParts = 4;
- int appleEaten;
- int appleX;
- int appleY;
- char direction = 'R';
- boolean running = false;
- Timer timer;
- Random random;
- GamePanel(){
- random = new Random();
- this.setPreferredSize(new Dimension(SCREEN_WIDTH,SCREEN_HEIGHT));
- this.setBackground(new Color(26,27,37));
- this.setFocusable(true);
- this.addKeyListener(new MyKeyAdapter());
- startGame();
- }
- public void startGame(){
- newApple();
- running = true;
- timer = new Timer(DELAY,this);
- timer.start();
- }
- public void paintComponent(Graphics g){
- super.paintComponent(g);
- draw(g);
- }
- public void draw(Graphics g) {
- if (running) {
- g.setColor(Color.red);
- g.fillOval(appleX, appleY, UNIT_SIZE-8, UNIT_SIZE-8);
- int intensity=255;
- for (int i = 0; i < bodyParts; i++) {
- if (i == 0) {
- g.setColor(new Color(27,200,139,255));
- g.fillRect(x[i], y[i], UNIT_SIZE-1, UNIT_SIZE-1);
- } else {
- g.setColor(new Color(27, 153, 139,intensity));
- intensity -= 2;
- g.fillRect(x[i], y[i], UNIT_SIZE-1, UNIT_SIZE-1);
- }
- }
- g.setColor(new Color(120,153,212));
- g.setFont(new Font("Fira Sans Regular",Font.PLAIN,35));
- FontMetrics metrics = getFontMetrics(g.getFont());
- g.drawString("Score: "+appleEaten,(SCREEN_WIDTH-metrics.stringWidth("Score: "+appleEaten))/2,g.getFont().getSize());
- }
- else{
- gameOver(g);
- }
- }
- public void newApple(){
- appleX = random.nextInt((int)(SCREEN_WIDTH/UNIT_SIZE))*UNIT_SIZE;
- appleY = random.nextInt((int)(SCREEN_HEIGHT/UNIT_SIZE))*UNIT_SIZE;
- }
- public void move(){
- for(int i=bodyParts;i>0;i--){
- x[i] = x[i-1];
- y[i] = y[i-1];
- }
- switch (direction){
- case 'U':
- y[0] = y[0]-UNIT_SIZE;
- break;
- case 'D':
- y[0] = y[0]+UNIT_SIZE;
- break;
- case 'L':
- x[0] = x[0]-UNIT_SIZE;
- break;
- case 'R':
- x[0] = x[0]+UNIT_SIZE;
- break;
- }
- }
- public void checkApple(){
- if((x[0]==appleX)&&(y[0]==appleY)){
- bodyParts++;
- appleEaten++;
- newApple();
- }
- }
- public void checkCollisions(){
- //check if head collides with the body
- for(int i=bodyParts;i>0;i--)
- if ((x[0] == x[i]) && (y[0] == y[i]))
- running = false;
- //check if head touches left borders
- if(x[0]<0)
- running = false;
- //if head touches right border
- if(x[0]>SCREEN_WIDTH)
- running = false;
- //if head touches top border
- if(y[0]<0)
- running = false;
- //if head touches bottom border;
- if(y[0]>SCREEN_HEIGHT)
- running = false;
- if(!running)
- timer.stop();
- }
- public void gameOver(Graphics g){
- //score
- g.setColor(new Color(120,153,212));
- g.setFont(new Font("Fira Sans Regular",Font.PLAIN,35));
- FontMetrics metrics1 = getFontMetrics(g.getFont());
- g.drawString("Score: "+appleEaten,(SCREEN_WIDTH-metrics1.stringWidth("Score: "+appleEaten))/2,g.getFont().getSize());
- //game over text
- g.setColor(new Color(255,0,0,200));
- g.setFont(new Font("Monospace Regular",Font.PLAIN,60));
- FontMetrics metrics = getFontMetrics(g.getFont());
- g.drawString("Game Over",(SCREEN_WIDTH-metrics.stringWidth("Game Over"))/2,(int)((double)SCREEN_HEIGHT/2.5));
- //restart button
- g.setColor(new Color(248,241,255));
- g.setFont(new Font("Monospace Regular",Font.ITALIC,50));
- FontMetrics metrics2 = getFontMetrics(g.getFont());
- g.drawString("Press SPACE to play again",(SCREEN_WIDTH-metrics2.stringWidth("Press SPACE to play again"))/2,(int) ((double)SCREEN_HEIGHT/1.8));
- }
- public void restart(){
- startGame();
- repaint();
- bodyParts = 4;
- direction = 'R';
- appleEaten = 0;
- for(int i=0;i<4;i++){
- x[i]=0;
- y[i]=0;
- }
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- if(running){
- move();
- checkApple();
- checkCollisions();
- }
- repaint();
- }
- public class MyKeyAdapter extends KeyAdapter{
- @Override
- public void keyPressed(KeyEvent e){
- switch (e.getKeyCode()){
- case KeyEvent.VK_LEFT:
- if(direction != 'R')
- direction = 'L';
- break;
- case KeyEvent.VK_RIGHT:
- if(direction != 'L')
- direction = 'R';
- break;
- case KeyEvent.VK_UP:
- if(direction != 'D')
- direction = 'U';
- break;
- case KeyEvent.VK_DOWN:
- if(direction != 'U')
- direction = 'D';
- break;
- case KeyEvent.VK_SPACE:
- if(running == false)
- restart();
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement