All pastes #564151 Raw Edit

SI code.. jons noob

public text v1 · immutable
·published 2007-06-13 14:01 UTC
rendered paste body
/**
 * @(#)SI.java
 *
 * SI Applet application
 *
 * @author
 * @version 1.00 2007/5/29
 */

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class SI extends Applet implements Runnable, KeyListener
{

    int width, height;
    int shooter_x = 375; // initial x position of the shooter
    int shooter_y = 725; // initial y position of the shooter
    int bullet_x;
    int bullet_y;
    int barrier_y = 600; // y value of all barriers
    boolean[] segmentLives1 = {true, true, true, true, true, true, true}; // sets all segments of the barrier to alive
    boolean[] segmentLives2 = {true, true, true, true, true, true, true}; // sets all segments of the barrier to alive
    boolean[] segmentLives3 = {true, true, true, true, true, true, true}; // sets all segments of the barrier to alive
    boolean[] segmentLives4 = {true, true, true, true, true, true, true}; // sets all segments of the barrier to alive
    int barrier1_x = 20; // x position of barrier 1
    int barrier2_x = 245; // x position of barrier 2 
    int barrier3_x = 470; // x position of barrier 3
    int barrier4_x = 695; // x position of barrier 4
    Barrier barrier1 = new Barrier (barrier1_x, barrier_y, segmentLives1); // sets barrier 1 as a part of Barrier class
    Barrier barrier2 = new Barrier (barrier2_x, barrier_y, segmentLives2); // sets barrier 2 as a part of Barrier class
    Barrier barrier3 = new Barrier (barrier3_x, barrier_y, segmentLives3); // sets barrier 3 as a part of Barrier class
    Barrier barrier4 = new Barrier (barrier4_x, barrier_y, segmentLives4); // sets barrier 4 as a part of Barrier class
    boolean move_right = false; // boolean used to track shooter movement right
    boolean move_left = false; // boolean used to track shooter movement left
    boolean shooting = false; // boolean used to track if shoot button is pressed
    boolean bullet_go = false; // boolean used to keep the bullet independent from the shoot button
    private Image dbImage; // from site
    private Graphics dbg; // from site
    Monster[] monster = new Monster[40];
    public void init ()
    {
        width = getSize ().width;
        height = getSize ().height;
        setBackground (Color.white);
    }


    public void start ()
    {
        Thread begin = new Thread (this);
        begin.start ();
    }

    public void keyPressed (KeyEvent e)
    {
        int key = e.getKeyCode ();
        if (key == 37)
        {
            move_left = true; // indicates that the shooter is moving to the left if the left arrow pad is pressed
        }
        if (key == 39)
        {
            move_right = true; // indicates that the shooter is moving to the right if the right arrow pad is pressed
        }

        if (key == 32)
        {
            shooting = true; // indicates that the shoot button has been pressed when the space button has been pressed
        }


    }


    public void keyReleased (KeyEvent e)
    {

        int key = e.getKeyCode ();
        if (key == 37)
        {
            move_left = false; // indicates that the shooter should stop moving left because the left arrow pad has been released
            }
        if (key == 39)
        {
            move_right = false; // indicates that the shooter should stop moving right because the right arrow pad has been released
        }

        if (key == 32)
        {
            shooting = false; // indicates that the shoot button has been released
        }

    }


    public void keyTyped (KeyEvent e)
    {
    }


    public void movement ()
    {
        if (move_left == true)
        {
            shooter_x -= 3; // moves the shooter a certain speed left when the left arrow pad has been pressed 
           }
        if (move_left == true && shooter_x <= 0)
        {
            shooter_x += 3; // counterbalances the movement speed left when it reaches the far left wall by moving the opposite way at the same speed
        }

        if (move_right == true)
        {
            shooter_x += 3; // moves the shooter a certain speed right when the right arrow pad has been pressed 
        }
        if (move_right == true && shooter_x >= 750)
        {
            shooter_x -= 3; // counterbalances the movement speed right when it reaches the far right wall by moving the opposite way at the same speed
        }
    }


    public void shoot ()
    {
        if (shooting == true && bullet_go == false)
        {
            bullet_go = true; // sets bullet go to false when the spacebar has been pressed. This keeps the bullet independent from the shoot button
            bullet_x = shooter_x + 20; // sets the initial x position of the bullet
            bullet_y = shooter_y; // sets the initial y position of the bullet
        }
        if (bullet_go == true)
        {
            bullet_y -= 5; // sets the speed at which the bullet moves
        }
        if (bullet_y + 25 <= 0)
        {
            bullet_go = false; // resets bullet_go when it has reached the top of the screen
        }
    }


    public void hitBarrier (Barrier b)
    {
        if (b.checkHit (bullet_go) == true) // indicates whether we have hit a barrier part
        {
            bullet_go = false; // resets bullet go when a barrier part has been hit
            shooting = false; // resets shooting when a barrier part has been hit
        }
    }


public void drawMonsters(Graphics g, Monster m){
}
    
    public void drawBarrier (Graphics g, Barrier b)
    {
        if (b.checkAlive () [0] == true)
        {
            g.fillRect (b.getx (), b.gety (), 30, 30); // draws the top left box of the barrier if it is still alive
        }
        if (b.checkAlive () [1] == true)
        {
            g.fillRect (b.getx () + 30, b.gety (), 30, 30); // draws the top middle box of the barrier if it is still alive
        }
        if (b.checkAlive () [2] == true)
        {
            g.fillRect (b.getx () + 60, b.gety (), 30, 30); // draws the top right box of the barrier if it is still alive
        }
        if (b.checkAlive () [3] == true)
        {
            g.fillRect (b.getx (), b.gety () + 30, 30, 30); // draws the middle left box of the barrier if it is still alive
        }
        if (b.checkAlive () [4] == true)
        {
            g.fillRect (b.getx () + 60, b.gety () + 30, 30, 30); // draws the middle right box of the barrier if it is still alive
        }
        if (b.checkAlive () [5] == true)
        {
            g.fillRect (b.getx (), b.gety () + 60, 30, 30); // draws the bottom left box of the barrier if it is still alive
        }
        if (b.checkAlive () [6] == true)
        {
            g.fillRect (b.getx () + 60, b.gety () + 60, 30, 30); // draws the bottom right box of the barrier if it is still alive            
        }
    }




    public void run ()
    {
        // lower ThreadPriority
        Thread.currentThread ().setPriority (Thread.MIN_PRIORITY);

        // run a long while (true) this means in our case "always"
        addKeyListener (this);
        while (true)
        {
            movement ();
            repaint ();
            hitBarrier(barrier1);
            hitBarrier(barrier2);
            hitBarrier(barrier3);
            hitBarrier(barrier4);
            barrier1.breakBarrier (barrier1_x, barrier_y, bullet_x, bullet_y, segmentLives1,bullet_go);
            barrier2.breakBarrier (barrier2_x, barrier_y, bullet_x, bullet_y, segmentLives2,bullet_go);
            barrier3.breakBarrier (barrier3_x, barrier_y, bullet_x, bullet_y, segmentLives3,bullet_go);
            barrier4.breakBarrier (barrier4_x, barrier_y, bullet_x, bullet_y, segmentLives4,bullet_go);
            shoot ();
            try
            {
                // Stop thread for 20 milliseconds
                Thread.sleep (20);

            }
            catch (InterruptedException ex)
            {
                // do nothing

            }

            // set ThreadPriority to maximum value
            Thread.currentThread ().setPriority (Thread.MAX_PRIORITY);

        }



    }


    public void paint (Graphics g)
    {
        g.setColor (Color.black);

        drawBarrier (g, barrier1); // draws barrier 1
        drawBarrier (g, barrier2); // draws barrier 2
        drawBarrier (g, barrier3); // draws barrier 3
        drawBarrier (g, barrier4); // draws barrier 4


        g.fillRect (shooter_x, shooter_y, 50, 50); // draws the shooter
        if (bullet_go == true)
        {

            g.fillRect (bullet_x, bullet_y, 10, 25); // draws the bullet only if the shoot button has been pressed
        }

    }


    // from javaboutique
    public void update (Graphics g)
    {
        // initialize buffer
        if (dbImage == null)
        {
            dbImage = createImage (this.getSize ().width, this.getSize ().height);
            dbg = dbImage.getGraphics ();

        }


        // clear screen in background
        dbg.setColor (getBackground ());
        dbg.fillRect (0, 0, this.getSize ().width, this.getSize ().height);

        // draw elements in background
        dbg.setColor (getForeground ());
        paint (dbg);

        // draw image on the screen
        g.drawImage (dbImage, 0, 0, this);

    }
}