lundi 25 avril 2016

Spawn Cookie 5 times, java, netbeans

So currently I am randomly spawning the 1st cookie.

My aim is to spawn the same cookie 5 times in random locations and when the player collects the currently displayed cookie a time is recorded how long it took him to collect the cookie.

package com.dawid.Levels;

import com.dawid.Game.Game;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.Timer;
import com.dawid.Characters.Player;
import java.awt.Font;
import com.dawid.Characters.Cookie;
import java.awt.Color;
import java.awt.Rectangle;
import java.util.Random;

public class Level1 extends JPanel implements ActionListener {

private int score = 0;
private Game game;
private Timer timer;
private BufferedImage background;
private Player thePlayer;
private Cookie theCookie;
private int timeCounter;
private int secondCounter;
private int[] time = new int[5];
private int currentCookie;

Random rand = new Random();

public Level1(Game theGame) {
    game = theGame;
    thePlayer = new Player();
    theCookie = new Cookie();

    init();
 }

 private void init() {
     score = 0;
    addKeyListener(new TAdapter());
    setFocusable(true);
    setDoubleBuffered(true);

    try {
        background =     ImageIO.read(getClass().getResource("/Images/wallpaper.jpg"));
    } catch (Exception ex) {
        System.err.println("Error loading background image");
    }
    timer = new Timer(10, this);
    timer.start();

}

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);

    g.drawImage(background, 0, 0, null);

    g.drawImage(thePlayer.getSprite(), thePlayer.getX(), thePlayer.getY(),      null);

    if (theCookie.getVisible() == true) {

        g.drawImage(theCookie.getSprite(), theCookie.getX(), theCookie.getY(), null);
    }

    Font uiFont = new Font("Arial", Font.PLAIN, 14);
    g.setColor(Color.yellow);
    g.setFont(uiFont);
    g.drawString("Score: " + score, 10, 15);
    g.drawString("Time: " + secondCounter, 10, 30);
    g.dispose();
}

public void checkCollisions() {
    Rectangle playerBounds = thePlayer.getBounds();
    Rectangle treasureBounds = theCookie.getBounds();

    if (theCookie.getVisible() == true) {
        if (playerBounds.intersects(treasureBounds)) {
            score = score + theCookie.getScore();

        }

    }

}

public void doMovement() {
    thePlayer.updateMove();
}

@Override
public void actionPerformed(ActionEvent ae) {
    checkCollisions();
    doMovement();
    repaint();
    timeCounter++;
    if (timeCounter == 100) {
        secondCounter++;
        timeCounter = 0;
    }
}

private class TAdapter extends KeyAdapter {

    @Override
    public void keyPressed(KeyEvent e) {
        int move = 0;

        switch (e.getKeyCode()) {
            case KeyEvent.VK_UP:
                move = 1;
                break;
            case KeyEvent.VK_DOWN:
                move = 2;
                break;
            case KeyEvent.VK_LEFT:
                move = 3;
                break;
            case KeyEvent.VK_RIGHT:
                move = 4;
                break;
            default:
                break;
        }
        thePlayer.move(move);

        if (e.getKeyCode() == KeyEvent.VK_Q) {
            System.exit(0);
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        thePlayer.stop();
    }
}

}




Aucun commentaire:

Enregistrer un commentaire