Code:
package kallekanin;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Event;
import java.awt.Font;
import java.awt.Image;
public class Rabbit extends java.applet.Applet implements Runnable {
Font f = new Font("Comic Sans", Font.BOLD, 36);
public static int kaninx;
public static int knapp1;
public static int kaniny;
public static int Lives;
public static int Level;
public static int Points;
public static int cSpeed;
boolean menu;
Image bildbuffer;
Graphics bild;
Image img;
public static int screenX;
public static int screenY;
Thread Kanintrad;
private int speed;
private int speed2=2;
public void init() {
setSize(1360, 768);
kaniny = 0;
setBackground(Color.black);
}
public void start() {
if (Kanintrad == null);
{
Kanintrad = new Thread(this);
Kanintrad.start();
}
}
public void stop() {
if (Kanintrad != null);
{
Kanintrad.stop();
Kanintrad = null;
}
}
public boolean keyDown(Event evt, int knapptryck) {
knapp1 = knapptryck;
speed = 15;
repaint();
return true;
}
public boolean keyUp(Event evt, int knapptryck) {
knapp1 = knapptryck;
speed = 0;
repaint();
return false;
}
public void run() {
Thread thisThread = Thread.currentThread();
int NumOfCarrots = 10;
int[] CarrotsX = new int[NumOfCarrots];
int[] CarrotsY = new int[NumOfCarrots];
while (1 != 2) {
//Randomize each carrot's x before the game starts
for(int z = 0;z < NumOfCarrots;z++)
{
int randx = ((int) (Math.random() * 1280) + 1);
int randy = ((int)(Math.random() *100)+1);
CarrotsX[z] = randx;
CarrotsY[z] = randy;
}
Lives = 20;
Level = 1;
while (Lives > 0) {
screenX = (this.getSize().width);
screenY = (this.getSize().height) - 70;
bildbuffer = createImage(screenX, screenY + 70);
bild = bildbuffer.getGraphics();
for (int i = 0; i < Level; i++) {
int reqP = 1000*Level;
if (Points >= reqP) {
Level++;
}
//Add 1 to each carrots Y
for(int z = 0; z < NumOfCarrots; z++)
CarrotsY[z] = CarrotsY[z] + speed2;
switch (knapp1) {
case Event.LEFT:
kaninx = kaninx - speed;
repaint:
break;
case Event.RIGHT:
kaninx = kaninx + speed;
repaint:
break;
}
if (kaninx > 1300) {
kaninx = 1300;
}
if (kaninx < 0) {
kaninx = 0;
}
//Do it for every carrot
for(int z = 0;z < NumOfCarrots;z++)
{
if (CarrotsX[z] >= kaninx+50-(this.getSize().width/20)&& CarrotsX[z]+20 <= kaninx+ 50 + (this.getSize().width/20) && CarrotsY[z] == kaniny + 625){
Points = +1000;
int randx = ((int) (Math.random() * 1280) + 1);
CarrotsX[z] = randx;
int randy = ((int) (Math.random() * 100) + 1);
CarrotsY[z] = randy;
}
if (CarrotsY[z] > kaniny+625) {
Lives --;
int randx = ((int) (Math.random() * 1280) + 1);
CarrotsX[z] = randx;
int randy = ((int) (Math.random() * 100) + 1);
CarrotsY[z] = randy;
}
//End previous for loop
}
bild.drawImage(img, 0, 0, this);
bild.setColor(Color.ORANGE);
for (int f = 1; f < NumOfCarrots; f++) {
// rand = ((int) (Math.random() * 1280) + 1);
// morotX = rand;
bild.fillRect(CarrotsX[f], CarrotsY[f], 10, 30);
}
bild.setColor(Color.RED);
bild.fillRect(0, 0, screenX, 60);
bild.setColor(Color.BLACK);
bild.drawString("LEVEL:" + Level, 10, 40);
bild.drawString("SCORE:" + Points, 150, 40);
bild.drawString("Required score for next level:" + reqP, 300, 40);
bild.drawString("Lives left:"+ Lives, 550, 40);
bild.setColor(Color.blue);
bild.fillOval(kaninx, kaniny + 595, (this.getSize().width / 20), 80);
bild.setColor(Color.PINK);
bild.fillOval(kaninx + 10, kaniny + 662, (this.getSize().width / 20), (this.getSize().height / 40));
bild.setColor(Color.red);
bild.fillRect(kaninx + 50, kaniny + 625, (this.getSize().width / 20), (this.getSize().height / 30));
bild.setColor(Color.lightGray);
bild.fillOval(kaninx + 42, kaniny + 622, (this.getSize().width / 60), (this.getSize().height / 30));
bild.setColor(Color.PINK);
bild.fillOval(kaninx + 15, kaniny + 560, (this.getSize().width / 30), 50);
bild.setColor(Color.lightGray);
bild.fillOval(kaninx + 30, kaniny + 540, (this.getSize().width / 110), 30);
bild.setColor(Color.BLACK);
bild.fillOval(kaninx + 40, kaniny + 580, (this.getSize().width / 150), 5);
bild.drawLine(kaninx + 30, kaniny + 595, kaninx + 50, kaniny + 600);
try {
repaint();
thisThread.sleep(15);
setBackground(Color.black);
} catch (InterruptedException e) {
}
}
}
}
}
public void update(Graphics G) {
G.drawImage(bildbuffer, 0, 0, this);
}
public void paint(Graphics G) {
update(G);
{
}
}
}