import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class Drawing4 extends Applet implements KeyListener{
int PlayerX = 0;
int PlayerY = 0;
int GrassX = 5;
public void init()
{
this.addKeyListener(this);
resize(501, 502);
}
public void paint(Graphics g)
{
g.setColor(Color.green);
while(GrassX < 500)
{
g.drawLine(GrassX, 0, GrassX, 500);
GrassX = GrassX + 5;
}
g.setColor(Color.black);
g.drawLine(0,0,500,0);
g.drawLine(500,500,500,0);
g.drawLine(500,500,0,500);
g.drawLine(0,500,0,0);
g.setColor(Color.red);
g.fillRect(PlayerX, PlayerY, 10, 10);
}
public void keyPressed(KeyEvent arg0){}
public void keyReleased(KeyEvent e)
{
char key = e.getKeyChar();
if(key == 'w')
{
PlayerY = PlayerY - 10;
}
if(key == 'a')
{
PlayerX = PlayerX - 10;
}
if(key == 's')
{
PlayerY = PlayerY + 10;
}
if(key == 'd')
{
PlayerX = PlayerX + 10;
}
repaint();
}
public void keyTyped(KeyEvent arg0){}
}