~eduardo_quiros/snake-game

cd7f8f2817979fc8811155bb455ad2113028022b — Eduardo Quiros 3 months ago aa2cda8
add management to change dot color using a decorator
M src/com/zetcode/Board.java => src/com/zetcode/Board.java +15 -10
@@ 1,7 1,10 @@
package com.zetcode;

import com.zetcode.bl.apples.concrete.RedApple;
import com.zetcode.bl.apples.prototype.Apple;
import com.zetcode.bl.dots.component.Dot;
import com.zetcode.tl.AppleManager;
import com.zetcode.tl.DotManager;

import java.awt.Color;
import java.awt.Dimension;


@@ 15,6 18,7 @@ import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.*;
import javax.swing.text.html.ImageView;

public class Board extends JPanel implements ActionListener {



@@ 34,7 38,7 @@ public class Board extends JPanel implements ActionListener {
    private int dots;
    private int apple_x;
    private int apple_y;
    private int lives = 3;
    private final int lives = 3;
    private int score = 0;

    private boolean leftDirection = false;


@@ 44,11 48,12 @@ public class Board extends JPanel implements ActionListener {
    private boolean inGame = true;

    private Timer timer;
    private Image ball;
    private Dot ball;
    private Apple apple;
    private Image head;
    
    private AppleManager appleManager = new AppleManager();
    private final AppleManager appleManager = new AppleManager();
    private final DotManager dotManager = new DotManager();

    public Board() {
        


@@ 69,14 74,14 @@ public class Board extends JPanel implements ActionListener {
    }

    private void loadImages() {
        if (apple != null) {
            dotManager.decorateDot(apple.getPoints());
        }else dotManager.decorateDot(3);
        ball = dotManager.getDot();

        ImageIcon iid = new ImageIcon("src/resources/dots/dot.png");
        ball = iid.getImage();

        
        apple = appleManager.getRandomApple();

        ImageIcon iih = new ImageIcon("src/resources/head.png");
        ImageIcon iih = new ImageIcon("src/resources/dots/head.png");
        head = iih.getImage();
    }



@@ 114,7 119,7 @@ public class Board extends JPanel implements ActionListener {
                if (z == 0) {
                    g.drawImage(head, x[z], y[z], this);
                } else {
                    g.drawImage(ball, x[z], y[z], this);
                    g.drawImage(new ImageIcon(ball.getColor()).getImage(), x[z], y[z], this);
                }
            }



@@ 141,7 146,7 @@ public class Board extends JPanel implements ActionListener {

        if ((x[0] == apple_x) && (y[0] == apple_y)) {
            score += apple.getPoints();
            apple = appleManager.getRandomApple();
            loadImages();
            dots++;
            scoreLabel.setText("Puntaje: " + score);
            locateApple();

A src/com/zetcode/bl/dots/component/Dot.java => src/com/zetcode/bl/dots/component/Dot.java +9 -0
@@ 0,0 1,9 @@
package com.zetcode.bl.dots.component;

public class Dot {
	private final String color = "src/resources/dots/green.png";

	public String getColor() {
		return this.color;
	}
}

A src/com/zetcode/bl/dots/concreteDecorator/Orange.java => src/com/zetcode/bl/dots/concreteDecorator/Orange.java +16 -0
@@ 0,0 1,16 @@
package com.zetcode.bl.dots.concreteDecorator;

import com.zetcode.bl.dots.component.Dot;
import com.zetcode.bl.dots.decorator.DecoratedDot;

public class Orange extends DecoratedDot {
	private final String color ="src/resources/dots/orange.png";
	public Orange(Dot pDot){
		this.decDot = pDot;
	}

	@Override
	public String getColor(){
		return this.color;
	}
}

A src/com/zetcode/bl/dots/concreteDecorator/Purple.java => src/com/zetcode/bl/dots/concreteDecorator/Purple.java +16 -0
@@ 0,0 1,16 @@
package com.zetcode.bl.dots.concreteDecorator;

import com.zetcode.bl.dots.component.Dot;
import com.zetcode.bl.dots.decorator.DecoratedDot;

public class Purple extends DecoratedDot {
	private final String color ="src/resources/dots/purple.png";
	public Purple(Dot pDot){
		this.decDot = pDot;
	}

	@Override
	public String getColor(){
		return this.color;
	}
}

A src/com/zetcode/bl/dots/decorator/DecoratedDot.java => src/com/zetcode/bl/dots/decorator/DecoratedDot.java +11 -0
@@ 0,0 1,11 @@
package com.zetcode.bl.dots.decorator;

import com.zetcode.bl.dots.component.Dot;

public abstract class DecoratedDot extends Dot{
	protected Dot decDot;
	
	public Dot getDecDot(){
		return decDot;
	}
}

A src/com/zetcode/tl/DotManager.java => src/com/zetcode/tl/DotManager.java +35 -0
@@ 0,0 1,35 @@
package com.zetcode.tl;

import com.zetcode.bl.dots.component.Dot;
import com.zetcode.bl.dots.concreteDecorator.Orange;
import com.zetcode.bl.dots.concreteDecorator.Purple;
import com.zetcode.bl.dots.decorator.DecoratedDot;

public class DotManager {
	private Dot dot = new Dot();
	
	public Dot getDot(){
		return this.dot;
	}

	public void decorateDot(int decType){
		switch (decType){
			case 1:
				dot = new Purple(dot);
				break;
			case 2:
				dot = new Orange(dot);
				break;
			case 3:
				unDecDot();
				break;
		}
	}
	
	public void unDecDot(){
		if (!this.dot.getColor().equals("src/resources/dots/green.png")) {
			DecoratedDot decorated = (DecoratedDot) this.dot;
			this.dot = decorated.getDecDot();
		}
	}
}

R src/resources/dots/dot.png => src/resources/dots/green.png +0 -0

R src/resources/head.png => src/resources/dots/head.png +0 -0

A src/resources/dots/orange.png => src/resources/dots/orange.png +0 -0

A src/resources/dots/purple.png => src/resources/dots/purple.png +0 -0