import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.Timer;
public class character extends JApplet implements ActionListener, KeyListener{
//variables
Random r = new Random();
Scanner s;
private Timer t = new Timer(5, this);
Graphics g2;
Image offscreen;
boolean gameOver = false, wait = false, help = false, h = false, c = false;
int cx = 40, cy = 40, upspeed, downspeed, leftspeed, rightspeed, score, hscore,
ex = 350, ey = 350, w = 16, bx = getRandomX(30, 30), by = getRandomY(30, 30);
public void init(){
setSize(400, 400);
t.start();
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
offscreen = createImage(400, 400);
g2 = offscreen.getGraphics();
}
public void update(Graphics g){
paint(g);
}
public void paint(Graphics g){
super.paint(g2);
g2.clearRect(0, 0, 400, 400);
g2.setColor(Color.BLACK);
g2.fillRect(0, 0, 400, 400);
if(help){
g2.setColor(Color.WHITE);
g2.drawString("You are the red box. Use the arrow keys to move it.", 0, 10);
g2.drawString("The green box chases you. Avoid it with the arrow keys.", 0, 22);
g2.drawString("Every time the green and blue boxes touch, you get a point.", 0, 34);
g2.drawString("You also get bigger the higher your score is.", 0, 46);
g2.drawString("If you touch the green or blue box, it's game over.", 0, 58);
g2.drawString("Once you reach 30 points you won't get any bigger.", 0, 70);
g2.drawString("PRESS [H] TO RETURN", 0, 82);
}
if(gameOver && help == false){
g2.setColor(Color.WHITE);
g2.drawString("GAME OVER", 170, 363);
g2.drawString("SCORE: " + score, 0, 10);
g2.drawString("HIGH SCORE: " + hscore, 0, 22);
g2.drawString("PRESS [SPACE] TO RESTART", 120, 375);
g2.drawString("PRESS H FOR HOW TO PLAY", 122, 387);
g2.setColor(Color.RED);
g2.fillRect(30, 30, 16, 16);
g2.setColor(Color.GREEN);
g2.fillRect(350, 350, 16, 16);
g2.setColor(Color.BLUE);
g2.fillRect(bx, by, 16, 16);
}
if(gameOver == false && help == false){
g2.setColor(Color.WHITE);
g2.drawString("SCORE: " + score, 0, 10);
g2.drawString("HIGH SCORE: " + hscore, 0, 22);
g2.setColor(Color.RED);
g2.fillRect(cx, cy, w, w);
g2.setColor(Color.BLUE);
g2.fillRect(bx, by, 16, 16);
g2.setColor(Color.GREEN);
g2.fillRect(ex, ey, 16, 16);
if(wait == false){
g2.setColor(Color.WHITE);
g2.drawString("PRESS [SPACE] TO PLAY", 130, 375);
g2.drawString("PRESS H FOR HOW TO PLAY", 120, 387);
}
g.drawImage(offscreen, 0, 0, this);
}
}
//action listener
public void actionPerformed(ActionEvent e){
repaint();
if(cx > 0){
cx -= leftspeed;
}
if(cx < 400-w){
cx += rightspeed;
}
if(cy > 0){
cy -= upspeed;
}
if(cy < 400-w){
cy += downspeed;
}
if(wait && help == false){
if(cy+w/2 <= ey+8 && ey > 0){
ey -= 1;
}
if(cy+w/2 >= ey+8 && ey < 384){
ey++;
}
if(cx+w/2 >= ex+8 && ex < 384){
ex++;
}
if(cx+w/2 <= ex+8 && ex > 0){
ex -= 1;
}
}
if(cx >= ex-w && cx <= ex+16 && cy >= ey-w && cy <= ey+16 && wait){
gameOver = true;
}
if(cx >= bx-w && cx <= bx+16 && cy >= by-w && cy <= by+16 && wait){
gameOver = true;
}
if(ex < bx+18 && ex > bx-18 && ey < by+18 && ey > by-18){
if(gameOver == false){
score++;
setRandom(cx, cy);
}
}
if(score > hscore){
hscore = score;
}
if(gameOver && wait){
setRandom(30, 30);
wait = false;
}
if(score < 30){
w = 16+score;
}
else{
w = 46;
}
}
//key listener
public void keyPressed(KeyEvent e){
int code = e.getKeyCode();
if(wait && help == false){
if(code == KeyEvent.VK_LEFT){
leftspeed = 2;
}
if(code == KeyEvent.VK_RIGHT){
rightspeed = 2;
}
if(code == KeyEvent.VK_UP){
upspeed = 2;
}
if(code == KeyEvent.VK_DOWN){
downspeed = 2;
}
}
if((wait == false || gameOver) && code == KeyEvent.VK_H){
h = true;
}
if(code == KeyEvent.VK_SPACE && help == false){
if(gameOver && help == false){
cx = 30;
cy = 30;
ex = 350;
ey = 350;
score = 0;
gameOver = false;
}
if(wait == false){
wait = true;
}
}
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){
int code = e.getKeyCode();
if(code == KeyEvent.VK_LEFT){
leftspeed = 0;
}
if(code == KeyEvent.VK_RIGHT){
rightspeed = 0;
}
if(code == KeyEvent.VK_UP){
upspeed = 0;
}
if(code == KeyEvent.VK_DOWN){
downspeed = 0;
}
if(code == KeyEvent.VK_H){
if(h && help == false){
help = true;
}
else if(h && help){
help = false;
}
h = false;
}
}
//random methods
public void setRandom(int px, int py){
int x = r.nextInt(2);
if(x == 0){
if(px > 180){
bx = r.nextInt(176-(3*w))+w+2;
}
else{
bx = r.nextInt(200-(4*w))+(2*w)+182;
}
by = r.nextInt(380-(3*w))+w+2;
}
else{
if(py > 180){
by = r.nextInt(176-(3*w))+w+2;
}
else{
by = r.nextInt(200-(4*w))+(2*w)+182;
}
bx = r.nextInt(380-(3*w))+w+2;
}
}
public int getRandomX(int a, int b){
setRandom(a, b);
return bx;
}
public int getRandomY(int a, int b){
setRandom(a, b);
return by;
}
}