harrypotter2's Profile User Rating: -----

Reputation: -1 Dishonored
Group:
Members
Active Posts:
24 (0.03 per day)
Joined:
19-February 12
Profile Views:
908
Last Active:
User is offline Jun 21 2012 07:26 AM
Currently:
Offline

Previous Fields

Country:
Who Cares
OS Preference:
Who Cares
Favorite Browser:
Chrome
Favorite Processor:
Who Cares
Favorite Gaming Platform:
Playstation
Your Car:
Who Cares
Dream Kudos:
0
Icon   harrypotter2 watts up

Posts I've Made

  1. In Topic: Incredibly Easy Way to play Sounds

    Posted 14 Jun 2012

    View PostRyano121, on 14 June 2012 - 11:15 AM, said:

    Have you even read the tutorial?



    Yes, I did. But I'm not doing an applet. I just wanna insert it in the same class thats all.
  2. In Topic: Incredibly Easy Way to play Sounds

    Posted 14 Jun 2012

    Hey do u guys know how to make sound in the same java application like i am trying to here:


    package snake;
    
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Point;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.image.BufferStrategy;
    import java.util.LinkedList;
    import java.util.Random;
    import javax.swing.JFrame;
    
    public class Snake extends JFrame implements KeyListener {
    	private int windowWidth = 1000;
    	private int windowHeight = 600;
    	private LinkedList<Point> snake;
    	private int dx;
    	private int dy;
    	private Random generator = new Random();
    	private Point food;
    	private int points;
    
    	public static void main(String[] args) {
    		new Snake();
    	}
    
    	public Snake() {
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		this.setSize(windowWidth, windowHeight);
    		this.setResizable(false);
    		this.setLocation(10, 100);
    		this.setVisible(true);
    
    		this.createBufferStrategy(2);
    
    		this.addKeyListener(this);
    
    		initGame();
    
    		while (true) {
    			long start = System.currentTimeMillis();
    			gameLoop();
    			while (System.currentTimeMillis() - start < 40) {
    			}
    		}
    	}
    
    	private void initGame() {
    		snake = new LinkedList<Point>();
    		snake.addFirst(new Point(20, 20));
    		growSnake(5);
    
    		dx = 0;
    		dy = 0;
    
    		food = new Point(10, 10);
    
    		points = 0;
    	}
    
    	private void gameLoop() {
    		moveSnake(dx, dy);
    		if (snake.getFirst().equals(food)) {
    			moveFood();
    			growSnake(3);
    			points++;
    		}
    		if (snake.getFirst().x <= 0 || snake.getFirst().x >= windowWidth / 10
    				|| snake.getFirst().y <= 2
    				|| snake.getFirst().y >= windowHeight / 10) {
    			initGame();
    		}
    		for (int n = 1; n < snake.size(); n++) {
    			if (snake.getFirst().equals(snake.get(n))) {
    				initGame();
    			}
    		}
    		drawFrame();
    	}
    
    	private void drawFrame() {
    		BufferStrategy bf = this.getBufferStrategy();
    		Graphics g = null;
    
    		try {
    			g = bf.getDrawGraphics();
    			g.setColor(Color.RED);
    			g.fillRect(0, 0, windowWidth, windowHeight);
    
    			drawSnake(g);
    			drawFood(g);
    			drawPoints(g);
    		} finally {
    			g.dispose();
    		}
    		bf.show();
    	}
    
    	private void drawSnake(Graphics g) {
    		for (int n = 0; n < snake.size(); n++) {
    			g.setColor(Color.GREEN);
    			Point p = snake.get(n);
    			g.fillOval(p.x * 10, p.y * 10, 10, 10);
    		}
    	}
    
    	private void moveSnake(int dx, int dy) {
    		for (int n = snake.size() - 1; n >= 1; n--) {
    			snake.get(n).setLocation(snake.get(n - 1));
    		}
    		snake.getFirst().x += dx;
    		snake.getFirst().y += dy;
    	}
    
    	private void growSnake(int n) {
    		while (n > 0) {
    			snake.add(new Point(snake.getLast()));
    			n--;
    		}
    	}
    
    	private void moveFood() {
    		food.x = generator.nextInt((windowWidth / 10) - 4) + 2;
    		food.y = generator.nextInt((windowHeight / 10) - 5) + 3;
    	}
    
    	private void drawFood(Graphics g) {
    		g.setColor(Color.ORANGE);
    		g.fillOval(food.x * 10, food.y * 10, 10, 10);
    	}
    
    	private void drawPoints(Graphics g) {
    		g.setColor(Color.GRAY);
    		g.drawString("points: " + points, 10, 40);
    	}
    
    	@Override
    	public void keyPressed(KeyEvent e) {
    		int key = e.getKeyCode();
    
    		if (key == 37) {
    			dx = -1;
    			dy = 0;
    		} else if (key == 38) {
    			dx = 0;
    			dy = -1;
    		} else if (key == 39) {
    			dx = 1;
    			dy = 0;
    		} else if (key == 40) {
    			dx = 0;
    			dy = 1;
    		}
    	}
    
    	public void keyReleased(KeyEvent e) {
    	}
    
    	public void keyTyped(KeyEvent e) {
    	}
    }
    
  3. In Topic: Snake Movement

    Posted 14 Jun 2012

    View Postharrypotter2, on 14 June 2012 - 09:26 AM, said:

    How do I get the snake to go faster and run into its self?

    Any Help will do



    Um never mind about this I finished now.:clap:
  4. In Topic: Snake Movement

    Posted 14 Jun 2012

    How do I get the snake to go faster and run into its self?

    Any Help will do
  5. In Topic: Snake Movement

    Posted 12 Jun 2012

    Do any of you think i will need two if statements instead of two. One for the collision and the other to add a segment

My Information

Member Title:
New D.I.C Head
Age:
Age Unknown
Birthday:
Birthday Unknown
Gender:

Contact Information

E-mail:
Click here to e-mail me

Friends

Comments

harrypotter2 has no profile comments yet. Why not say hello?