Doodle jump with java code source

Doodle jump with java code source


In this totorial we will explain how you can develop a doodle jump game with java

In Doodle Jump, the aim is to guide a four-legged creature called « The Doodler » up a never-ending series of platforms without falling see the screen bellow:


Source code: yX Media - Monetize your website traffic with us

package com.javatmz.javaapp.doodleJump;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.Timer;

public class DoodleJump extends JFrame {

	private static final int D_W = 1000;
	private static final int D_H = 700;
	DrawPanel drawPanel = new DrawPanel();

	int with = 1600;
	int height = 768;

	// int previousY = 0;
	int unit = 10;

	List<Line> lines = new ArrayList<Line>();
	boolean down = false;
	boolean keyPress = false;
	
	int x=100,y=100,h=100;
	double dx=0,dy=0;

	public DoodleJump() {

		add(drawPanel);
		pack();
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setLocationRelativeTo(null);
		setVisible(true);

		// lines = new ArrayList<Line>();
		for (int i = 0; i < 10; i++) {
			Line line = new Line();
			Random rx = new Random();
			line.x = rx.nextInt(600) + 20;

			Random ry = new Random();
			line.y = i*50;

			Random rw = new Random();
			line.with = rw.nextInt(60) + 120;

			lines.add(line);
		}

	

	}

	private class DrawPanel extends JPanel {
		public DrawPanel() {
			InputMap inputMap = getInputMap(WHEN_IN_FOCUSED_WINDOW);
			ActionMap actionMap = getActionMap();

			String VK_LEFT = "VK_LEFT";
			KeyStroke W = KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0);
			inputMap.put(W, VK_LEFT);
			actionMap.put(VK_LEFT, new AbstractAction() {
				@Override
				public void actionPerformed(ActionEvent e) {
					x-=5;

				}
			});

			String VK_RIGHT = "VK_RIGHT";
			KeyStroke WVK_RIGHT = KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0);
			inputMap.put(WVK_RIGHT, VK_RIGHT);
			actionMap.put(VK_RIGHT, new AbstractAction() {
				@Override
				public void actionPerformed(ActionEvent e) {
					x+=5;

				}
			});

			ActionListener listener = new AbstractAction() {
				public void actionPerformed(ActionEvent e) {

					dy+=0.2;
					y+=dy;
					if(y>650) dy=-10;
					
					if(y<h){
						for (Line line : lines) {
						y=h;
						line.y=line.y-(int)dy;
						if(line.y>700){
							Random rw = new Random();
							line.y=80;
							line.x = rw.nextInt(600) + 20;
						}
						}
					}
					
					for (Line line : lines) {
						if((x>line.x && x<line.x+line.with)&& (y>=line.y && y<=line.y+10 && dy>0)){
							dy=-10;
						}
					}

					drawPanel.repaint();

				}
			};
			Timer timer = new Timer(10, listener);
			
			timer.start();

		}

		protected void paintComponent(Graphics g) {
			super.paintComponent(g);

			drawValuesSteps(g);
			drawValues(g);
		}

		private void drawValuesSteps(Graphics g) {

			for (Line line : lines) {

				Graphics stepBorder = (Graphics) g;
				stepBorder.setColor(Color.black);
				// stepBorder.s
				stepBorder.drawRect(line.x, line.y, line.with, 25);
				Graphics step = (Graphics) g;
				step.setColor(Color.RED);
				step.fillRect(line.x, line.y, line.with, 25);
			}
		}

		private void drawValues(Graphics g) {

			//try {

				//g.drawImage(ImageIO.read(new File("ressource/bg.png")), 0, 0, this);

				Graphics step = (Graphics) g;
				step.setColor(Color.black);

				int currentX=x;
				int currentY=y;
				// 1
				step.fillRect(currentX, currentY, unit * 2, unit);
				step.fillRect(currentX + unit * 3, currentY, unit * 2, unit);
				step.fillRect(currentX + unit * 6, currentY, unit * 2, unit);
				step.fillRect(currentX + unit * 9, currentY, unit * 2, unit);

				// 2
				step.fillRect(currentX + unit, currentY - unit, unit, unit);
				step.fillRect(currentX + unit * 4, currentY - unit, unit, unit);
				step.fillRect(currentX + unit * 6, currentY - unit, unit, unit);
				step.fillRect(currentX + unit * 9, currentY - unit, unit, unit);

				// 3
				step.fillRect(currentX + unit, currentY - 2 * unit, unit, unit);
				step.fillRect(currentX + unit * 4, currentY - 2 * unit, unit, unit);
				step.fillRect(currentX + unit * 6, currentY - 2 * unit, unit, unit);
				step.fillRect(currentX + unit * 9, currentY - 2 * unit, unit, unit);

				// 4
				step.fillRect(currentX + unit, currentY - 3 * unit, 9 * unit, unit);

				// 5
				step.fillRect(currentX, currentY - 4 * unit, unit, unit);
				step.fillRect(currentX + 10 * unit, currentY - 4 * unit, unit, unit);

				// 6
				step.fillRect(currentX + 10 * unit, currentY - 12 * unit, unit, 9 * unit);
				step.fillRect(currentX, currentY - 12 * unit, unit, 9 * unit);

				// yellow
				Graphics stepYellow = (Graphics) g;
				stepYellow.setColor(Color.yellow);
				stepYellow.fillRect(currentX + unit, currentY - 12 * unit, 9 * unit, 9 * unit);
				stepYellow.fillRect(currentX + 2 * unit, currentY - 13 * unit, 8 * unit, unit);
				stepYellow.fillRect(currentX + 3 * unit, currentY - 14 * unit, 4 * unit, unit);

				step.setColor(Color.black);
				step.fillRect(currentX + unit, currentY - 13 * unit, unit, unit);
				step.fillRect(currentX + 9 * unit, currentY - 13 * unit, unit, unit);
				step.fillRect(currentX + 2 * unit, currentY - 12 * unit, unit, 3 * unit);
				step.fillRect(currentX + 8 * unit, currentY - 12 * unit, unit, 3 * unit);
				step.fillRect(currentX + 2 * unit, currentY - 14 * unit, 2 * unit, unit);
				step.fillRect(currentX + 7 * unit, currentY - 14 * unit, 2 * unit, unit);
				step.fillRect(currentX + 4 * unit, currentY - 15 * unit, 3 * unit, unit);
				step.fillRect(currentX + unit * 5, currentY - 10 * unit, unit, unit);

				Graphics stepGreen = (Graphics) step;// Gren
				// 5
				stepGreen.setColor(Color.green);
				stepGreen.fillRect(currentX + unit, currentY - 4 * unit, 9 * unit, unit);
				// 7
				stepGreen.fillRect(currentX + unit, currentY - 6 * unit, 3 * unit, unit);
				stepGreen.fillRect(currentX + 7 * unit, currentY - 6 * unit, 3 * unit, unit);

				Graphics stepGray = (Graphics) step;// Gray
				// 6
				stepGray.setColor(Color.gray);
				stepGray.fillRect(currentX + unit, currentY - 5 * unit, 9 * unit, unit);
				//
				stepGray.fillRect(currentX + unit, currentY - 7 * unit, 2 * unit, unit);
				//
				stepGray.fillRect(currentX + 8 * unit, currentY - 7 * unit, 2 * unit, unit);

				Graphics stepGray2 = step;// Gray2
				stepGray2.setColor(Color.orange);
				stepGray.fillRect(currentX + 3 * unit, currentY - 7 * unit, unit, unit);
				stepGray2.fillRect(currentX + 4 * unit, currentY - 6 * unit, 3 * unit, unit);
				stepGray2.fillRect(currentX + 7 * unit, currentY - 7 * unit, unit, unit);

			
		}

		public Dimension getPreferredSize() {
			return new Dimension(D_W, D_H);
		}
	}


	private class Line {
		int x;
		int y;
		int with;
	}

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {

			@Override
			public void run() {
				new DoodleJump();

			}
		});
	}
}

Comments