Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 4 player tag
Index -> Programming, Java -> Java Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
goroyoshi




PostPosted: Wed Oct 05, 2011 6:17 pm   Post subject: 4 player tag

here is a four player tag game i made, i play with my friends and it's always fun
Java:

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.geom.Point2D;
import java.awt.image.*;
import java.applet.Applet;
public class game extends Applet implements Runnable
{
    int x = 2, y = 2, x2 = 490, y2 = 490, x3 = 2, y3 = 490, x4 = 490, y4 = 2, tagged = 1;
    boolean[] keys = new boolean [32767];
    public void start ()
    {
        enableEvents (AWTEvent.KEY_EVENT_MASK);
        new Thread (this).start ();
        setSize (503, 503);
    }


    public void paint (Graphics g)
    {
        Dimension d = getSize ();
        Image i = createImage (d.width, d.height);
        Graphics bG = i.getGraphics ();
        Font font = new Font ("Arial", Font.PLAIN, 24);
        Font font2 = new Font ("Arial", Font.PLAIN, 10);
        bG.setColor (Color.blue);
        bG.drawRect (1, 1, 500, 500);
        if (tagged == 1)
            bG.setColor (Color.red);
        else
            bG.setColor (Color.yellow);
        bG.fillOval (x, y, 10, 10);
        bG.setFont (font2);
        bG.setColor (Color.black);
        bG.drawString ("1", x + 3, y + 8);
        if (tagged == 2)
            bG.setColor (Color.red);
        else
            bG.setColor (Color.yellow);
        bG.fillOval (x2, y2, 10, 10);
        bG.setFont (font2);
        bG.setColor (Color.black);
        bG.drawString ("2", x2 + 3, y2 + 8);
        if (tagged == 3)
            bG.setColor (Color.red);
        else
            bG.setColor (Color.yellow);
        bG.fillOval (x3, y3, 10, 10);
        bG.setFont (font2);
        bG.setColor (Color.black);
        bG.drawString ("3", x3 + 3, y3 + 8);
        if (tagged == 4)
            bG.setColor (Color.red);
        else
            bG.setColor (Color.yellow);
        bG.fillOval (x4, y4, 10, 10);
        bG.setFont (font2);
        bG.setColor (Color.black);
        bG.drawString ("4", x4 + 3, y4 + 8);
        bG.setFont (font);
        bG.setColor (Color.green);
        bG.drawString ("Player " + tagged + " is it!", d.width / 2 - 70, d.height / 2);
        g.drawImage (i, 0, 0, null);
    }


    public void update (Graphics g)
    {
        paint (g);
    }


    public void run ()
    {
        int tagCounter = 0, tag = 0;
        String vars;
        while (true)
        {
            if (tag != 1)
            {
                if (keys [KeyEvent.VK_W])
                    y -= 1;
                if (keys [KeyEvent.VK_S])
                    y += 1;
                if (keys [KeyEvent.VK_D])
                    x += 1;
                if (keys [KeyEvent.VK_A])
                    x -= 1;
            }
            if (tag != 2)
            {
                if (keys [KeyEvent.VK_UP])
                    y2 -= 1;
                if (keys [KeyEvent.VK_DOWN])
                    y2 += 1;
                if (keys [KeyEvent.VK_RIGHT])
                    x2 += 1;
                if (keys [KeyEvent.VK_LEFT])
                    x2 -= 1;
            }
            if (tag != 3)
            {
                if (keys [KeyEvent.VK_I])
                    y3 -= 1;
                if (keys [KeyEvent.VK_K])
                    y3 += 1;
                if (keys [KeyEvent.VK_J])
                    x3 -= 1;
                if (keys [KeyEvent.VK_L])
                    x3 += 1;
            }
            if (tag != 4)
            {
                if (keys [KeyEvent.VK_NUMPAD8])
                    y4 -= 1;
                if (keys [KeyEvent.VK_NUMPAD5])
                    y4 += 1;
                if (keys [KeyEvent.VK_NUMPAD4])
                    x4 -= 1;
                if (keys [KeyEvent.VK_NUMPAD6])
                    x4 += 1;
            }
            if (x < 2)
                x += 1;
            if (x > 490)
                x -= 1;
            if (y < 2)
                y += 1;
            if (y > 490)
                y -= 1;
            if (x2 < 2)
                x2 += 1;
            if (x2 > 490)
                x2 -= 1;
            if (y2 < 2)
                y2 += 1;
            if (y2 > 490)
                y2 -= 1;
            if (x3 < 2)
                x3 += 1;
            if (x3 > 490)
                x3 -= 1;
            if (y3 < 2)
                y3 += 1;
            if (y3 > 490)
                y3 -= 1;
            if (x4 < 2)
                x4 += 1;
            if (x4 > 490)
                x4 -= 1;
            if (y4 < 2)
                y4 += 1;
            if (y4 > 490)
                y4 -= 1;
            if (Point2D.distance (x, y, x2, y2) < 10)
            {
                if (tagged == 1 && tag == 0)
                {
                    tagged = 2;
                    tag = 2;
                }
                else if (tagged == 2 && tag == 0)
                {
                    tagged = 1;
                    tag = 1;
                }
            }
            if (Point2D.distance (x3, y3, x2, y2) < 10)
            {
                if (tagged == 2 && tag == 0)
                {
                    tagged = 3;
                    tag = 3;
                }
                else if (tagged == 3 && tag == 0)
                {
                    tagged = 2;
                    tag = 2;
                }
            }
            if (Point2D.distance (x, y, x3, y3) < 10)
            {
                if (tagged == 1 && tag == 0)
                {
                    tagged = 3;
                    tag = 3;
                }
                else if (tagged == 3 && tag == 0)
                {
                    tagged = 1;
                    tag = 1;
                }
            }
            if (Point2D.distance (x, y, x4, y4) < 10)
            {
                if (tagged == 1 && tag == 0)
                {
                    tagged = 4;
                    tag = 4;
                }
                else if (tagged == 4 && tag == 0)
                {
                    tagged = 1;
                    tag = 1;
                }
            }
            if (Point2D.distance (x2, y2, x4, y4) < 10)
            {
                if (tagged == 2 && tag == 0)
                {
                    tagged = 4;
                    tag = 4;
                }
                else if (tagged == 4 && tag == 0)
                {
                    tagged = 2;
                    tag = 2;
                }
            }
            if (Point2D.distance (x3, y3, x4, y4) < 10)
            {
                if (tagged == 3 && tag == 0)
                {
                    tagged = 4;
                    tag = 4;
                }
                else if (tagged == 4 && tag == 0)
                {
                    tagged = 3;
                    tag = 3;
                }
            }
            try
            {
                Thread.sleep (1);
            }
            catch (Exception e)
            {
            }
            if (tag > 0)
                tagCounter += 1;
            if (tagCounter == 300)
            {
                tagCounter = 0;
                tag = 0;
            }
            repaint ();
        }
    }


    public void processEvent (AWTEvent event)
    {
        boolean key = false;
        switch (event.getID ())
        {
            case KeyEvent.KEY_PRESSED:
                key = true;
            case KeyEvent.KEY_RELEASED:
                keys [((KeyEvent) event).getKeyCode ()] = key;
                break;
        }
    }
}
Sponsor
Sponsor
Sponsor
sponsor
Velocity




PostPosted: Thu Nov 24, 2011 1:49 pm   Post subject: RE:4 player tag

nice game, butt... array your mess!
make an array on the data types.
Display posts from previous:   
   Index -> Programming, Java -> Java Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: