Why wont my shot show
Author |
Message |
goroyoshi
|
Posted: Thu Sep 22, 2011 8:28 pm Post subject: Why wont my shot show |
|
|
For some reason it won't fire when i press space, also how do i prevent flickering
Java: |
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class game extends Applet implements Runnable
{
int x = 2, y = 2, width, height, fireX, fireY, pastX, pastY, bulletX, bulletY;
Color c;
String direction;
boolean[] keys = new boolean [32767];
public void fire ()
{
Graphics g = getGraphics ();
if (direction == "up")
{
pastX = x;
for (int s = pastY ; s < bulletY ; y++ )
{
g. setColor (Color. white);
g. drawOval (pastX, s - 2, 3, 3);
g. setColor (Color. red);
g. drawOval (pastX, s, 3, 3);
try
{
Thread. sleep (3);
}
catch (Exception e )
{
}
}
}
else if (direction == "down")
{
pastX = x;
for (int s = pastY ; s > bulletY ; y-- )
{
g. setColor (Color. white);
g. drawOval (pastX, s + 2, 3, 3);
g. setColor (Color. red);
g. drawOval (pastX, s, 3, 3);
try
{
Thread. sleep (3);
}
catch (Exception e )
{
}
}
}
else if (direction == "left")
{
pastY = y;
for (int s = pastX ; s > bulletX ; x-- )
{
g. setColor (Color. white);
g. drawOval (s + 2, pastY, 3, 3);
g. setColor (Color. red);
g. drawOval (s, pastY, 3, 3);
try
{
Thread. sleep (3);
}
catch (Exception e )
{
}
}
}
else
{
pastY = y;
for (int s = pastX ; s < bulletX ; x++ )
{
g. setColor (Color. white);
g. drawOval (s - 2, pastY, 3, 3);
g. setColor (Color. red);
g. drawOval (s, pastY, 3, 3);
try
{
Thread. sleep (3);
}
catch (Exception e )
{
}
}
}
}
public void start ()
{
enableEvents (AWTEvent. KEY_EVENT_MASK);
new Thread (this). start ();
setSize (502, 502);
}
public void run ()
{
String vars;
Graphics g = getGraphics ();
boolean key = false;
while (true)
{
if (keys [KeyEvent. VK_W])
{
y -= 1;
direction = "up";
}
else if (keys [KeyEvent. VK_S])
{
y += 1;
direction = "down";
}
else if (keys [KeyEvent. VK_D])
{
x += 1;
direction = "right";
}
else if (keys [KeyEvent. VK_A])
{
x -= 1;
direction = "left";
}
if (keys [KeyEvent. VK_SPACE])
{
if (direction == "up")
{
pastY = y;
for (int s = y ; s < 501 ; s++ )
{
try
{
Robot r = new Robot ();
c = r. getPixelColor (pastY, s );
}
catch (AWTException e )
{
e. printStackTrace ();
}
if (c == Color. blue)
{
bulletY = s;
fire ();
break;
}
}
}
if (direction == "down")
{
for (int s = y ; s > - 1 ; s-- )
{
try
{
Robot r = new Robot ();
c = r. getPixelColor (pastY, s );
}
catch (AWTException e )
{
e. printStackTrace ();
}
if (c == Color. blue)
{
bulletY = s;
fire ();
break;
}
}
}
if (direction == "left")
{
for (int s = x ; s > - 1 ; s-- )
{
try
{
Robot r = new Robot ();
c = r. getPixelColor (s, pastX );
}
catch (AWTException e )
{
bulletX = s;
e. printStackTrace ();
}
if (c == Color. blue)
{
bulletX = x;
fire ();
break;
}
}
}
if (direction == "right")
{
for (int s = x ; s < 501 ; s++ )
{
try
{
Robot r = new Robot ();
c = r. getPixelColor (s, pastX );
}
catch (AWTException e )
{
e. printStackTrace ();
}
if (c == Color. blue)
{
bulletX = s;
fire ();
break;
}
}
}
}
if (x < 2)
x += 1;
if (x > 490)
x -= 1;
if (y < 2)
y += 1;
if (y > 490)
y -= 1;
try
{
Thread. sleep (5);
}
catch (Exception e )
{
}
g. setColor (Color. white);
g. fillRect (2, 2, 498, 498);
g. setColor (Color. blue);
g. drawRect (1, 1, 499, 499);
g. setColor (Color. green);
g. fillOval (x, y, 10, 10);
}
}
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
|
|
|
|
|