commit e03f206e92732747765c05da8f411d5de8e86070
parent be102abf63d0616bd23a64df5f974d89800d303e
Author: mpizzzle <michael.770211@gmail.com>
Date: Sat, 2 Mar 2019 12:30:01 +0000
updating Link game to use swing components
Diffstat:
2 files changed, 338 insertions(+), 347 deletions(-)
diff --git a/Link/src/LinkArea.java b/Link/src/LinkArea.java
@@ -1,7 +1,15 @@
-import java.awt.*;
+import java.awt.Color;
+import java.awt.Graphics;
+import java.awt.Image;
+import java.awt.MediaTracker;
+import java.io.File;
+import java.io.IOException;
-public class LinkArea extends Panel {
-
+import javax.imageio.ImageIO;
+import javax.swing.JPanel;
+
+public class LinkArea extends JPanel {
+ private static final long serialVersionUID = 635033554190094921L;
static final int FLOORLEVEL = 587;
static final int FLOORWIDTH = 150;
LinkGame myApplet = null;
@@ -28,115 +36,107 @@ public class LinkArea extends Panel {
Image gemstandgems;
Image gemstandempty;
Image blob;
- MediaTracker mt=null;
-
+ MediaTracker mt = null;
+
public LinkArea(LinkGame parent) {
- mt=new MediaTracker(parent);
+ mt = new MediaTracker(parent);
myApplet = parent;
- yellowgem = load(myApplet, "yellowgem.gif");
- spider = load(myApplet, "spider.gif");
- obstacle = load(myApplet, "rock.gif");
- redgem = load(myApplet, "redgem.gif");
- octopellets = load(myApplet, "octopellets.gif");
- octo = load(myApplet, "octo.gif");
- madblob = load(myApplet, "madblob.gif");
- heroswordright = load(myApplet, "linkswordright.gif");
- heroswordleft = load(myApplet, "linkswordleft.gif");
- herostand = load(myApplet, "linkstandright.gif");
- hero1 = load(myApplet, "linkstandright.gif");
- hero2 = load(myApplet, "linkstandright2.gif");
- herostandleft = load(myApplet, "linkstandleft.gif");
- hero1left = load(myApplet, "linkstandleft.gif");
- hero2left = load(myApplet, "linkstandleft2.gif");
- heroshieldright = load(myApplet, "linkshieldright.gif");
- heroshieldleft = load(myApplet, "linkshieldleft.gif");
- greengem = load(myApplet, "greengem.gif");
- bluegem = load(myApplet, "bluegem.gif");
- goldskullata = load(myApplet, "goldskullata.gif");
- gemstandgems = load(myApplet, "gemstand(with gems).gif");
- gemstandempty = load(myApplet, "gemstand(without gems).gif");
- blob = load(myApplet, "blob.gif");
+ yellowgem = load(myApplet, "yellowgem.gif");
+ spider = load(myApplet, "spider.gif");
+ obstacle = load(myApplet, "rock.gif");
+ redgem = load(myApplet, "redgem.gif");
+ octopellets = load(myApplet, "octopellets.gif");
+ octo = load(myApplet, "octo.gif");
+ madblob = load(myApplet, "madblob.gif");
+ heroswordright = load(myApplet, "linkswordright.gif");
+ heroswordleft = load(myApplet, "linkswordleft.gif");
+ herostand = load(myApplet, "linkstandright.gif");
+ hero1 = load(myApplet, "linkstandright.gif");
+ hero2 = load(myApplet, "linkstandright2.gif");
+ herostandleft = load(myApplet, "linkstandleft.gif");
+ hero1left = load(myApplet, "linkstandleft.gif");
+ hero2left = load(myApplet, "linkstandleft2.gif");
+ heroshieldright = load(myApplet, "linkshieldright.gif");
+ heroshieldleft = load(myApplet, "linkshieldleft.gif");
+ greengem = load(myApplet, "greengem.gif");
+ bluegem = load(myApplet, "bluegem.gif");
+ goldskullata = load(myApplet, "goldskullata.gif");
+ gemstandgems = load(myApplet, "gemstand(with gems).gif");
+ gemstandempty = load(myApplet, "gemstand(without gems).gif");
+ blob = load(myApplet, "blob.gif");
}
+
void checkImage(Image image, String name) {
if (mt != null) {
- mt.addImage(image,0);
+ mt.addImage(image, 0);
try {
- mt.waitForID(0,5000);
- }
- catch (InterruptedException ie) {
+ mt.waitForID(0, 5000);
+ } catch (InterruptedException ie) {
// nothing to do
}
- if (mt.isErrorID(0)) System.out.println("Image Not found: "+name.toString());
+ if (mt.isErrorID(0))
+ System.out.println("Image Not found: " + name.toString());
}
}
-
+
Image load(LinkGame parent, String picture) {
- Image im = parent.getImage(myApplet.getCodeBase(), "Link\\" + picture);
- checkImage(im, picture);
- return(im);
+ try {
+ Image im = ImageIO.read(new File("../Assets/" + picture));
+ checkImage(im, picture);
+ return (im);
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ return null;
}
-
- public synchronized void paint (Graphics g) {
+
+ @Override
+ public synchronized void paintComponent(Graphics g) {
+ super.paintComponent(g);
Image obstacleimage;
- if (myApplet == null) return;
+ if (myApplet == null)
+ return;
final int w = getBounds().width;
final int h = getBounds().height;
g.setColor(Color.white);
g.fillRect(0, 0, w, h);
- FontMetrics fm = getFontMetrics(getFont());
-
g.setColor(Color.black);
- int asc = fm.getAscent() + 4;
/*
- // paint the floor
- for (int pos=0; pos < w; pos+=FLOORWIDTH) {
- g.drawImage(floor,pos, FLOORLEVEL,Color.black, null);
- }
- */
+ * // paint the floor for (int pos=0; pos < w; pos+=FLOORWIDTH) {
+ * g.drawImage(floor,pos, FLOORLEVEL,Color.black, null); }
+ */
- if (myApplet.obstaclePositions != null) {
+ if (LinkGame.obstaclePositions != null) {
// show the obstacles
- for (int i=0; i < myApplet.obstaclePositions.length; i++) {
+ for (int i = 0; i < LinkGame.obstaclePositions.length; i++) {
obstacleimage = myApplet.getObstacleImage(i);
- g.drawImage(obstacleimage, myApplet.obstaclePositions[myApplet.level-1][i].x, myApplet.obstaclePositions[myApplet.level-1][i].y-4,Color.white, null);
+ g.drawImage(obstacleimage, LinkGame.obstaclePositions[LinkGame.level - 1][i].x,
+ LinkGame.obstaclePositions[LinkGame.level - 1][i].y - 4, Color.white, null);
}
}
/*
- // show the enemies
- for (int i=0; i < myApplet.enemyPositions.length; i++) {
- if (myApplet.enemyPositions[i].x > -1)
- g.drawImage(enemyImage, myApplet.enemyPositions[i].x, myApplet.enemyPositions[i].y,null);
- }
- }
- */
- // draw Hero (on top of preceding images)
- if (myApplet.playerPosition != null) {
+ * // show the enemies for (int i=0; i < myApplet.enemyPositions.length; i++) {
+ * if (myApplet.enemyPositions[i].x > -1) g.drawImage(enemyImage,
+ * myApplet.enemyPositions[i].x, myApplet.enemyPositions[i].y,null); } }
+ */
+ // draw Hero (on top of preceding images)
+ if (LinkGame.playerPosition != null) {
// show the player
Image herosimage = myApplet.getHeroImage();
/*
- if (myApplet.herocrouching) {
- if (myApplet.heroleft)
- herosimage = herocrouchleft;
- else
- herosimage = herocrouch;
- }
- if (myApplet.jumping) {
- if (myApplet.heroleft)
- herosimage = herojumpleft;
- else
- herosimage = herojump;
- }
- */
- g.drawImage(herosimage, myApplet.playerPosition.x, myApplet.playerPosition.y,/*Color.white,*/ null);
+ * if (myApplet.herocrouching) { if (myApplet.heroleft) herosimage =
+ * herocrouchleft; else herosimage = herocrouch; } if (myApplet.jumping) { if
+ * (myApplet.heroleft) herosimage = herojumpleft; else herosimage = herojump; }
+ */
+ g.drawImage(herosimage, LinkGame.playerPosition.x, LinkGame.playerPosition.y, /* Color.white, */ null);
}
/*
- // draw warp image (on top of Hero)
- if (myApplet.warpzone != null) {
- // show the warp zone at the end of the level
- g.drawImage(herowarp, myApplet.warpzone.x, myApplet.warpzone.y,Color.white, null);
- }
- */
-
+ * // draw warp image (on top of Hero) if (myApplet.warpzone != null) { // show
+ * the warp zone at the end of the level g.drawImage(herowarp,
+ * myApplet.warpzone.x, myApplet.warpzone.y,Color.white, null); }
+ */
+
}
}
\ No newline at end of file
diff --git a/Link/src/LinkGame.java b/Link/src/LinkGame.java
@@ -16,114 +16,111 @@ public class LinkGame extends JFrame implements KeyListener, Runnable {
static boolean finished = false;
static boolean ingame = false;
static boolean juststarted = false;
- static int level = 1;
+ static int level = 0;
static Point[] enemyPositions;
static Point playerPosition;
static Point[][] obstaclePositions;
static int direction[];
- Thread timer;
- static int enemyNumber=1, enemiesRemaining=1, obstacleNumber=1;
+ static Thread timer;
+ static int enemyNumber = 1, enemiesRemaining = 1, obstacleNumber = 1;
static boolean herocrouching = false;
static final int MAXLEVELS = 1;
static final int levellengths[] = new int[MAXLEVELS];
- boolean hasWeaponOut=false;
-
- static int LEFT=-1;
- static int RIGHT=1;
-
+ boolean hasWeaponOut = false;
+
+ static int LEFT = -1;
+ static int RIGHT = 1;
+
// the next 2 constants are overridden in init once we know the screen size
static int RIGHTEDGE = 789;
- static int BOTTOMEDGE = 900;
+ static int BOTTOMEDGE = 800;
//
- static Point IMAGESIZE=new Point(152,107);
- static Point HEROPOS = new Point(63,28);
+ static Point IMAGESIZE = new Point(152, 107);
+ static Point HEROPOS = new Point(63, 28);
static final int LEFTEDGE = -15;
static final int TOPEDGE = -10;
- static final int FALLAMOUNT=16; // amount he falls each time interval
- static final int RISEAMOUNT=16; // amount he rises each time interval
+ static final int FALLAMOUNT = 16; // amount he falls each time interval
+ static final int RISEAMOUNT = 16; // amount he rises each time interval
static int GROUNDLEVEL = 500;
- static int XAMOUNT=5;
- static int YAMOUNT=5;
- static int JUMPAMOUNT=70;
+ static int XAMOUNT = 5;
+ static int YAMOUNT = 5;
+ static int JUMPAMOUNT = 70;
static final int X = 1;
static final int Y = 2;
- static final int NOT=3;
+ static final int NOT = 3;
static final int DELAY = 60;
-
+
static int heroImageNo = 0;
static final int OBSTACLEIMAGES = 1;
static final int HEROIMAGES = 4;
static Image[] heroImages = new Image[HEROIMAGES];
- static Image[] obstacleImages = new Image[OBSTACLEIMAGES]; // obstacles don't move
+ static Image[] obstacleImages = new Image[OBSTACLEIMAGES]; // obstacles don't move
static int obstacleImageNo[];
static boolean heroleft = false;
static boolean jumping = false;
static Point warpzone;
- //static boolean superjump = false;
- static final int JUMPDURATION=5;
- static final int SUPERJUMPDURATION=8;
+ // static boolean superjump = false;
+ static final int JUMPDURATION = 5;
+ static final int SUPERJUMPDURATION = 8;
static final int LEFTOFOBSTACLE = 27;
static final int RIGHTOFOBSTACLE = 33;
static int[] keysdown = new int[4];
- static final int LEFTDOWN=0;
- static final int RIGHTDOWN=1;
- static final int DOWNDOWN=2;
- static final int SPACEDOWN=3;
-
- static final int OBSTACLEHEIGHT=42;
- boolean onTheWayUp=false;
+ static final int LEFTDOWN = 0;
+ static final int RIGHTDOWN = 1;
+ static final int DOWNDOWN = 2;
+ static final int SPACEDOWN = 3;
+
+ static final int OBSTACLEHEIGHT = 42;
+ boolean onTheWayUp = false;
static int startPosY = GROUNDLEVEL;
-
- public void init() {
- setLayout(null);
- setBackground(Color.white);
+ public static void main(String[] args) {
+ LinkGame linkGame = new LinkGame();
+
+ linkGame.setLayout(null);
+ linkGame.setBackground(Color.white);
+ linkGame.setSize(RIGHTEDGE, BOTTOMEDGE);
- area = new LinkArea(this);
- add(area);
+ area = new LinkArea(linkGame);
+ linkGame.add(area);
XAMOUNT = 12;
YAMOUNT = 12;
- RIGHTEDGE = (getBounds().width/XAMOUNT)*XAMOUNT +LEFTEDGE;
- BOTTOMEDGE = getBounds().height - 1;
- setVisible(true);
- area.setBounds(0,0,getBounds().width,BOTTOMEDGE);
+ //RIGHTEDGE = (linkGame.getBounds().width / XAMOUNT) * XAMOUNT + LEFTEDGE;
+ //BOTTOMEDGE = linkGame.getBounds().height - 1;
+ linkGame.setVisible(true);
+ //area.setBounds(0, 0, linkGame.getBounds().width, BOTTOMEDGE);
+ area.setBounds(0, 0, RIGHTEDGE, BOTTOMEDGE);
area.setVisible(true);
- addKeyListener(this);
- area.addKeyListener(this);
+ linkGame.addKeyListener(linkGame);
+ area.addKeyListener(linkGame);
area.requestFocus();
-
+
levellengths[0] = RIGHTEDGE;
obstacleImages[0] = area.obstacle;
- timer = new Thread(this);
+ timer = new Thread(linkGame);
timer.start();
-
- }
-
- public void start() {
- // called by Applet Viewer or Browser
- doLevel(1);
- }
-
+ }
+
synchronized void faceRight() {
heroImages[0] = area.herostand;
heroImages[1] = area.hero1;
heroImages[2] = area.herostand;
heroImages[3] = area.hero2;
heroImageNo = 0;
- heroleft=false;
+ heroleft = false;
}
-
+
synchronized void faceLeft() {
heroImages[0] = area.herostandleft;
heroImages[1] = area.hero1left;
heroImages[2] = area.herostandleft;
heroImages[3] = area.hero2left;
heroImageNo = 0;
- heroleft=true;
+ heroleft = true;
}
-
+
synchronized void getSwordOut() {
hasWeaponOut = true;
if (heroleft) {
@@ -131,8 +128,7 @@ public class LinkGame extends JFrame implements KeyListener, Runnable {
heroImages[1] = area.heroswordleft;
heroImages[2] = area.heroswordleft;
heroImages[3] = area.heroswordleft;
- }
- else {
+ } else {
heroImages[0] = area.heroswordright;
heroImages[1] = area.heroswordright;
heroImages[2] = area.heroswordright;
@@ -140,7 +136,7 @@ public class LinkGame extends JFrame implements KeyListener, Runnable {
}
heroImageNo = 0;
}
-
+
synchronized void getShieldOut() {
hasWeaponOut = true;
if (heroleft) {
@@ -148,8 +144,7 @@ public class LinkGame extends JFrame implements KeyListener, Runnable {
heroImages[1] = area.heroshieldleft;
heroImages[2] = area.heroshieldleft;
heroImages[3] = area.heroshieldleft;
- }
- else {
+ } else {
heroImages[0] = area.heroshieldright;
heroImages[1] = area.heroshieldright;
heroImages[2] = area.heroshieldright;
@@ -157,201 +152,201 @@ public class LinkGame extends JFrame implements KeyListener, Runnable {
}
heroImageNo = 0;
}
-
+
synchronized void putWeaponAway() {
hasWeaponOut = false;
- if (heroleft) faceLeft();
- else faceRight();
+ if (heroleft)
+ faceLeft();
+ else
+ faceRight();
}
-
+
synchronized void standStill() {
heroImageNo = 0;
}
-
+
public void doLevel(int level) {
// this runs one 'level'
juststarted = true;
-
+
System.out.println("Started level " + level);
-
+
// position Hero at the bottom left
- playerPosition = new Point(LEFTEDGE,GROUNDLEVEL);
+ playerPosition = new Point(LEFTEDGE, GROUNDLEVEL);
faceRight();
-
+
// initialise obstacle states
obstacleImageNo = new int[obstacleNumber];
obstacleImageNo[0] = 0;
-
+
// initialise the arrays of enemies, etc
enemyPositions = new Point[enemyNumber];
obstaclePositions = new Point[MAXLEVELS][obstacleNumber];
direction = new int[enemyNumber];
- for (int i=0; i < enemyNumber; i++) {
+ for (int i = 0; i < enemyNumber; i++) {
enemyPositions[i] = new Point(RIGHTEDGE, GROUNDLEVEL);
direction[i] = LEFT;
}
- for (int i=0; i < obstacleNumber; i++) {
- switch(level) {
- case 1:
- obstaclePositions[level-1][i] = new Point((RIGHTEDGE+15)/2, GROUNDLEVEL);
- System.out.println("Obstacle positioned at (" + (RIGHTEDGE-LEFTEDGE)/2+", " + GROUNDLEVEL + ")");
- break;
- default:
+ for (int i = 0; i < obstacleNumber; i++) {
+ switch (level) {
+ case 1:
+ obstaclePositions[level - 1][i] = new Point((RIGHTEDGE + 15) / 2, GROUNDLEVEL);
+ System.out.println("Obstacle positioned at (" + (RIGHTEDGE - LEFTEDGE) / 2 + ", " + GROUNDLEVEL + ")");
+ break;
+ default:
}
}
-
+
// position the warp zone at the end of the level
- warpzone = new Point(levellengths[level-1], GROUNDLEVEL);
-
+ warpzone = new Point(levellengths[level - 1], GROUNDLEVEL);
+
// paint the panel here
- //area.repaint();
+ // area.repaint();
ingame = true;
}
-
+
Point randomPosition() {
- return new Point( (int) (Math.random() * RIGHTEDGE) / XAMOUNT * XAMOUNT,
- (int) (Math.random() * BOTTOMEDGE) / YAMOUNT * YAMOUNT);
+ return new Point((int) (Math.random() * RIGHTEDGE) / XAMOUNT * XAMOUNT,
+ (int) (Math.random() * BOTTOMEDGE) / YAMOUNT * YAMOUNT);
}
-
+
boolean isPlayerPosition(Point p) {
- return(p.x == playerPosition.x && p.y == playerPosition.y);
+ return (p.x == playerPosition.x && p.y == playerPosition.y);
}
-
+
boolean isEnemyPosition(Point p) {
- for (int i = 0 ; i < enemyNumber; i++) {
- if(p.x == enemyPositions[i].x && p.y == enemyPositions[i].y) return(true);
+ for (int i = 0; i < enemyNumber; i++) {
+ if (p.x == enemyPositions[i].x && p.y == enemyPositions[i].y)
+ return (true);
}
- return(false);
+ return (false);
}
-
+
boolean isObstaclePosition(Point p) {
- for (int i = 0 ; i < obstacleNumber; i++) {
- if(isObstacleX(p) != -1 && isObstacleY(p) != -1) {
- return(true);
+ for (int i = 0; i < obstacleNumber; i++) {
+ if (isObstacleX(p) != -1 && isObstacleY(p) != -1) {
+ return (true);
}
}
- return(false);
+ return (false);
}
-
+
boolean checkIfBlockingObstacle(Point p) {
boolean result = false;
- for (int i = 0 ; i < obstacleNumber; i++) {
- if(isObstacleX(p) != -1 && isBlockingObstacleY(p) != -1) {
- //piranhaPopping[i] = false;
+ for (int i = 0; i < obstacleNumber; i++) {
+ if (isObstacleX(p) != -1 && isBlockingObstacleY(p) != -1) {
+ // piranhaPopping[i] = false;
result = true;
}
- //else piranhaPopping[i] = true;
+ // else piranhaPopping[i] = true;
}
- if (!result) startPosY = GROUNDLEVEL;
- return(result);
+ if (!result)
+ startPosY = GROUNDLEVEL;
+ return (result);
}
-
+
int isObstacleX(Point p) {
- int obstaclex;
- for (int i = 0 ; i < obstacleNumber; i++) {
- // if x is >= obstacleposition - (LEFTOFOBSTACLE) and x <= obstacleposition + (RIGHTOFOBSTACLE)
- obstaclex = obstaclePositions[level-1][i].x;
- if(p.x >= (obstaclex - LEFTOFOBSTACLE) && p.x <= (obstaclex + RIGHTOFOBSTACLE))
- return(i); // return the number of the matching obstacle
+ int obstaclex;
+ for (int i = 0; i < obstacleNumber; i++) {
+ // if x is >= obstacleposition - (LEFTOFOBSTACLE) and x <= obstacleposition +
+ // (RIGHTOFOBSTACLE)
+ obstaclex = obstaclePositions[level - 1][i].x;
+ if (p.x >= (obstaclex - LEFTOFOBSTACLE) && p.x <= (obstaclex + RIGHTOFOBSTACLE))
+ return (i); // return the number of the matching obstacle
}
- return(-1);
+ return (-1);
}
-
+
int isBlockingObstacleY(Point p) {
- for (int i = 0 ; i < obstacleNumber; i++) {
- if(p.y <= obstaclePositions[level-1][i].y-OBSTACLEHEIGHT) return(i);
+ for (int i = 0; i < obstacleNumber; i++) {
+ if (p.y <= obstaclePositions[level - 1][i].y - OBSTACLEHEIGHT)
+ return (i);
}
- return(-1);
+ return (-1);
}
-
+
int isObstacleY(Point p) {
- for (int i = 0 ; i < obstacleNumber; i++) {
- if(p.y > obstaclePositions[level-1][i].y-OBSTACLEHEIGHT) return(i);
+ for (int i = 0; i < obstacleNumber; i++) {
+ if (p.y > obstaclePositions[level - 1][i].y - OBSTACLEHEIGHT)
+ return (i);
}
- return(-1);
+ return (-1);
}
public void finished() {
finished = true;
System.exit(0);
}
-
- public String getAppletInfo() {
- return ("Link Game");
- }
-
+
int randomMove() {
// return + or - value (randomly)
- if (Math.random() >= 0.5) return(24);
- else return(-24);
+ if (Math.random() >= 0.5)
+ return (24);
+ else
+ return (-24);
}
-
+
private synchronized void incKeysDown(int whichone) {
keysdown[whichone] = 1;
}
-
+
private synchronized void decKeysDown(int whichone) {
keysdown[whichone] = 0;
}
-
+
// this class will use just the key pressed event
+ @Override
public void keyPressed(KeyEvent e) {
if (ingame) {
- if (!
- ((e.getKeyCode() == KeyEvent.VK_LEFT && playerPosition.x <= (LEFTEDGE + XAMOUNT)) ||
- (e.getKeyCode() == KeyEvent.VK_RIGHT && playerPosition.x >= (RIGHTEDGE - XAMOUNT)))) {
+ if (!((e.getKeyCode() == KeyEvent.VK_LEFT && playerPosition.x <= (LEFTEDGE + XAMOUNT))
+ || (e.getKeyCode() == KeyEvent.VK_RIGHT && playerPosition.x >= (RIGHTEDGE - XAMOUNT)))) {
// valid move: move the player and then move the enemies
- if (e.getKeyCode() == KeyEvent.VK_DOWN) {
+ if (e.getKeyCode() == KeyEvent.VK_DOWN) {
herocrouching = true;
incKeysDown(DOWNDOWN);
- }
- else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
+ } else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
incKeysDown(LEFTDOWN);
leftPressed();
- }
- else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
+ } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
incKeysDown(RIGHTDOWN);
rightPressed();
- }
- else if (e.getKeyCode() == KeyEvent.VK_SPACE) {
- //System.out.println("Jump when jumping is " + jumping);
+ } else if (e.getKeyCode() == KeyEvent.VK_SPACE) {
+ // System.out.println("Jump when jumping is " + jumping);
if (!jumping) {
- //incKeysDown(SPACEDOWN);
+ // incKeysDown(SPACEDOWN);
startPosY = playerPosition.y;
jumping = true;
- onTheWayUp=true;
+ onTheWayUp = true;
}
- }
- else if (e.getKeyCode() == KeyEvent.VK_Z) {
+ } else if (e.getKeyCode() == KeyEvent.VK_Z) {
getSwordOut();
- }
- else if (e.getKeyCode() == KeyEvent.VK_X) {
+ } else if (e.getKeyCode() == KeyEvent.VK_X) {
getShieldOut();
}
if (isEnemyPosition(playerPosition)) {
System.out.println("The Player ran into an enemy!! Press the Enter key to restart the level");
ingame = false;
}
- //area.repaint();
- //System.out.println("Hero's position = (" + playerPosition.x + ", " + playerPosition.y + ")");
+ // area.repaint();
+ // System.out.println("Hero's position = (" + playerPosition.x + ", " +
+ // playerPosition.y + ")");
System.out.println(" ");
} // if valid key press
- }
- else if (e.getKeyCode() == KeyEvent.VK_ENTER) {
- doLevel(level++);
+ } else if (e.getKeyCode() == KeyEvent.VK_ENTER) {
+ doLevel(++level);
}
}
-
+
void leftPressed() {
if (!herocrouching) {
- //superjump = true; // if jumping
+ // superjump = true; // if jumping
// if Hero was facing right, turn him left
- if (!heroleft && !hasWeaponOut)
+ if (!heroleft && !hasWeaponOut)
faceLeft();
else {
// if the target position is not occupied by a obstacle...
- for (int i=0; i < obstacleNumber; i++) {
- if (isObstaclePosition(new Point(playerPosition.x-XAMOUNT, playerPosition.y)))
+ for (int i = 0; i < obstacleNumber; i++) {
+ if (isObstaclePosition(new Point(playerPosition.x - XAMOUNT, playerPosition.y)))
return; // can't move
}
playerPosition.x -= XAMOUNT;
@@ -359,16 +354,16 @@ public class LinkGame extends JFrame implements KeyListener, Runnable {
}
}
}
-
+
void rightPressed() {
if (!herocrouching) {
- //superjump = true; // if jumping
+ // superjump = true; // if jumping
// if Hero was facing left, turn him right
- if (heroleft && !hasWeaponOut)
+ if (heroleft && !hasWeaponOut)
faceRight();
else {
- for (int i=0; i < obstacleNumber; i++) {
- if (isObstaclePosition(new Point(playerPosition.x+XAMOUNT, playerPosition.y)))
+ for (int i = 0; i < obstacleNumber; i++) {
+ if (isObstaclePosition(new Point(playerPosition.x + XAMOUNT, playerPosition.y)))
return; // can't move
}
playerPosition.x += XAMOUNT;
@@ -376,133 +371,135 @@ public class LinkGame extends JFrame implements KeyListener, Runnable {
}
}
}
-
+
+ @Override
public void run() {
while (true) {
if (juststarted) {
- try {Thread.sleep(750);}
- catch (Exception e) {}
+ try {
+ Thread.sleep(750);
+ } catch (Exception e) {
+ }
area.repaint();
juststarted = false;
}
- try {Thread.sleep(DELAY);}
- catch (InterruptedException e){}
+ try {
+ Thread.sleep(DELAY);
+ } catch (InterruptedException e) {
+ }
if (ingame) {
- //System.out.println("Run: BEFORE: Hero's position = (" + playerPosition.x + ", " + playerPosition.y + ")");
- if (keysdown[LEFTDOWN] == 0 &&
- keysdown[RIGHTDOWN] == 0 &&
- keysdown[DOWNDOWN] == 0 /*&&
- keysdown[SPACEDOWN] == 0*/) standStill();
-
- if (keysdown[LEFTDOWN] > 0) leftPressed();
- else if (keysdown[RIGHTDOWN] > 0) rightPressed();
-
+ // System.out.println("Run: BEFORE: Hero's position = (" + playerPosition.x + ",
+ // " + playerPosition.y + ")");
+ if (keysdown[LEFTDOWN] == 0 && keysdown[RIGHTDOWN] == 0
+ && keysdown[DOWNDOWN] == 0 /*
+ * && keysdown[SPACEDOWN] == 0
+ */)
+ standStill();
+
+ if (keysdown[LEFTDOWN] > 0)
+ leftPressed();
+ else if (keysdown[RIGHTDOWN] > 0)
+ rightPressed();
+
// for each obstacle, cycle through the piranha pictures
- for (int i=0; i < obstacleNumber; i++) {
+ for (int i = 0; i < obstacleNumber; i++) {
incrementObstacleImage(i);
}
-
+
/*
- // for each enemy, move the enemy in the direction it was going
- for (int i=0; i < enemyPositions.length; i++) {
- if (!deadHero(enemyPositions[i])) {
- enemyPositions[i] = move(enemyPositions[i], direction[i]);
- }
- // if there is a obstacle at that position, fall into it and decrement count of enemies
- // and set that enemy's position to -1, -1
- if (isObstaclePosition(enemyPositions[i])) {
- System.out.println("A enemy fell into a obstacle");
-
- enemyPositions[i] = new Point(-1, -1);
- enemiesRemaining--;
- System.out.println(enemyNumber-enemiesRemaining + " down, " + enemiesRemaining + " to go...");
- }
- // if player is caught by a enemy or all enemies are gone, end the game
- if (isPlayerPosition(enemyPositions[i])) {
- System.out.println("\"Mmm! Brains...\". The Player is dead. Press the Enter key to restart the level");
- level--; // because it will be incremented in a minute and we want to stay on the same 'level'
- ingame = false;
- }
- }
- if (enemiesRemaining == 0) {
- System.out.println("All the enemies are gone -- you won!! Press the Enter key to start the next level");
- enemyNumber++;
- if (obstacleNumber > 2) obstacleNumber--;
- ingame = false;
- }
- */
+ * // for each enemy, move the enemy in the direction it was going for (int i=0;
+ * i < enemyPositions.length; i++) { if (!deadHero(enemyPositions[i])) {
+ * enemyPositions[i] = move(enemyPositions[i], direction[i]); } // if there is a
+ * obstacle at that position, fall into it and decrement count of enemies // and
+ * set that enemy's position to -1, -1 if
+ * (isObstaclePosition(enemyPositions[i])) {
+ * System.out.println("A enemy fell into a obstacle");
+ *
+ * enemyPositions[i] = new Point(-1, -1); enemiesRemaining--;
+ * System.out.println(enemyNumber-enemiesRemaining + " down, " +
+ * enemiesRemaining + " to go..."); } // if player is caught by a enemy or all
+ * enemies are gone, end the game if (isPlayerPosition(enemyPositions[i])) {
+ * System.out.
+ * println("\"Mmm! Brains...\". The Player is dead. Press the Enter key to restart the level"
+ * ); level--; // because it will be incremented in a minute and we want to stay
+ * on the same 'level' ingame = false; } } if (enemiesRemaining == 0) {
+ * System.out.
+ * println("All the enemies are gone -- you won!! Press the Enter key to start the next level"
+ * ); enemyNumber++; if (obstacleNumber > 2) obstacleNumber--; ingame = false; }
+ */
if (onTheWayUp) {
- //System.out.println("OnTheWayUp. Start Y Position =" + startPosY);
- if (playerPosition.y > startPosY-JUMPAMOUNT)
+ // System.out.println("OnTheWayUp. Start Y Position =" + startPosY);
+ if (playerPosition.y > startPosY - JUMPAMOUNT)
playerPosition = new Point(playerPosition.x, playerPosition.y - RISEAMOUNT);
else {
- onTheWayUp=false;
+ onTheWayUp = false;
}
- }
- else {
+ } else {
int obstacleno = isObstacleX(playerPosition);
if (obstacleno == -1) { // no obstacle at this position
if (onTheWayUp == false) {
- if (playerPosition.y < startPosY)
- playerPosition = new Point(playerPosition.x, playerPosition.y+FALLAMOUNT);
- else jumping = false;
+ if (playerPosition.y < startPosY)
+ playerPosition = new Point(playerPosition.x, playerPosition.y + FALLAMOUNT);
+ else
+ jumping = false;
}
- }
- else {
- Point obstaclepos = obstaclePositions[level-1][obstacleno];
+ } else {
+ Point obstaclepos = obstaclePositions[level - 1][obstacleno];
// if hero's y position <= obstacle's height
- if (playerPosition.y <= (obstaclepos.y-OBSTACLEHEIGHT)) { // can't land on the obstacle if just < (??!)
+ if (playerPosition.y <= (obstaclepos.y - OBSTACLEHEIGHT)) { // can't land on the obstacle if
+ // just < (??!)
// allow hero to land on the obstacle
- //System.out.println("Allow Hero to land (or stay) on obstacle");
- jumping=false;
- playerPosition = new Point(playerPosition.x, obstaclepos.y-OBSTACLEHEIGHT);
+ // System.out.println("Allow Hero to land (or stay) on obstacle");
+ jumping = false;
+ playerPosition = new Point(playerPosition.x, obstaclepos.y - OBSTACLEHEIGHT);
// if the piranha was up, Hero dies
if (obstacleImages[obstacleImageNo[obstacleno]] != area.obstacle) {
System.out.println("Hero dies!!");
- ingame=false;
-
- }
- /*
- else {
- // otherwise stop this Piranha
- piranhaPopping[obstacleno] = false;
+ ingame = false;
+
}
- */
- }
- else {
+ /*
+ * else { // otherwise stop this Piranha piranhaPopping[obstacleno] = false; }
+ */
+ } else {
if (onTheWayUp == false) {
- if (playerPosition.y < startPosY)
- playerPosition = new Point(playerPosition.x, playerPosition.y+FALLAMOUNT);
- else jumping = false;
+ if (playerPosition.y < startPosY)
+ playerPosition = new Point(playerPosition.x, playerPosition.y + FALLAMOUNT);
+ else
+ jumping = false;
}
}
- }
+ }
}
- checkIfBlockingObstacle(playerPosition);
- //System.out.println("Run: AFTER: Hero's position = (" + playerPosition.x + ", " + playerPosition.y + ")");
- area.repaint();
+ checkIfBlockingObstacle(playerPosition);
+ // System.out.println("Run: AFTER: Hero's position = (" + playerPosition.x + ",
+ // " + playerPosition.y + ")");
+ area.repaint();
}
}
}
-
+
synchronized void incrementObstacleImage(int i) {
obstacleImageNo[i]++;
- if (obstacleImageNo[i] == OBSTACLEIMAGES) obstacleImageNo[i] = 0;
+ if (obstacleImageNo[i] == OBSTACLEIMAGES)
+ obstacleImageNo[i] = 0;
}
-
+
synchronized void incrementHeroImage() {
heroImageNo++;
- if (heroImageNo == HEROIMAGES) heroImageNo = 0;
+ if (heroImageNo == HEROIMAGES)
+ heroImageNo = 0;
}
-
+
Point move(Point start, int direction) {
- return(new Point(start.x + direction, start.y));
+ return (new Point(start.x + direction, start.y));
}
boolean deadHero(Point p) {
- return(p.x == -1 && p.y ==-1);
+ return (p.x == -1 && p.y == -1);
}
-
+
+ @Override
public void keyReleased(KeyEvent e) {
boolean needrepaint = false;
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
@@ -511,35 +508,29 @@ public class LinkGame extends JFrame implements KeyListener, Runnable {
needrepaint = true;
}
/*
- else if (e.getKeyCode() == KeyEvent.VK_SPACE) {
- decKeysDown(SPACEDOWN);
- }
- */
+ * else if (e.getKeyCode() == KeyEvent.VK_SPACE) { decKeysDown(SPACEDOWN); }
+ */
else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
decKeysDown(LEFTDOWN);
- //superjump = false;
- }
- else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
+ // superjump = false;
+ } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
decKeysDown(RIGHTDOWN);
- //superjump = false;
- }
- else putWeaponAway();
- //if (needrepaint) area.repaint();
+ // superjump = false;
+ } else
+ putWeaponAway();
+ // if (needrepaint) area.repaint();
}
-
+
+ @Override
public void keyTyped(KeyEvent e) {
}
-
+
synchronized Image getHeroImage() {
- return(heroImages[heroImageNo]);
+ return (heroImages[heroImageNo]);
}
synchronized Image getObstacleImage(int obstaclenum) {
- return(obstacleImages[obstacleImageNo[obstaclenum]]);
+ return (obstacleImages[obstacleImageNo[obstaclenum]]);
}
-
-
-}
-
-
+}