diff --git a/src/GamePanel.java b/src/GamePanel.java index f8ce02f..db87605 100644 --- a/src/GamePanel.java +++ b/src/GamePanel.java @@ -40,6 +40,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{ public static ArrayListmap = new ArrayList(); public static ArrayListenemy = new ArrayList(); + public static StickyBomb b; public static Camera camera; // image imports begin here @@ -85,6 +86,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{ addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { player.mousePressed(e); + b.mousePressed(e); } }); this.setPreferredSize(new Dimension(GAME_WIDTH, GAME_HEIGHT)); @@ -114,6 +116,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{ enemy.get(i).draw(g, enemyFrame); } playerFrameCounter += player.draw(g, playerFrame); + b.draw(g); g.drawString(camera.x+" "+player.y,100,100); } @@ -124,6 +127,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{ for (NonPlayer n: enemy) { n.move(); } + b.move(); } //handles all collision detection and responds accordingly @@ -165,7 +169,9 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{ //run() method is what makes the game continue running without end. It calls other methods to move objects, check for collision, and update the screen public void run(){ + b = new StickyBomb(600, 100, 20, 1,-5); LevelManager.setLevel(1); + //the CPU runs our game code too quickly - we need to slow it down! The following lines of code "force" the computer to get stuck in a loop for short intervals between calling other methods to update the screen. long lastTime = System.nanoTime(); double amountOfTicks = 60; diff --git a/src/GenericSprite.java b/src/GenericSprite.java index f5c13a2..c11d8a9 100644 --- a/src/GenericSprite.java +++ b/src/GenericSprite.java @@ -28,6 +28,7 @@ public class GenericSprite extends Rectangle{ HEIGHT = height; } + //called from GamePanel when any keyboard input is detected //updates the direction of the ball based on user input //if the keyboard input isn't any of the options (d, a, w, s), then nothing happens @@ -60,7 +61,25 @@ public class GenericSprite extends Rectangle{ } } - + private boolean collide(Tile tile, double x, double y){ + if(!tile.collision){ + return false; + } + if(x+WIDTH>tile.x&&xtile.x&&xp.x&&realXtile.realX&&xtile.realX&&x0){ + while(canUpdate(updateAmount, 0)){ + updateAmount++; + } + x+=updateAmount-1; + } else if(xVelocity<0){ + while(canUpdate(updateAmount, 0)){ + updateAmount--; + } + x+=updateAmount+1; + } + xVelocity = 0; + } + if(!canUpdate(0, yVelocity)){ + isMove = false; + if(yVelocity>0){ + while(canUpdate(0,1)){ + y+=1; + } + isGrounded = true; + } else if(yVelocity<0){ + while(canUpdate(0,-1)){ + y-=1; + } + } + yVelocity = 0; + } + if(canUpdate(xVelocity, yVelocity)) { + y = y + (int) yVelocity; + x = x + (int) xVelocity; + } else { + isMove = false; + } + yVelocity+=3; + } + } + + public void mousePressed(MouseEvent e){ + int xx = e.getX(); + int yy = e.getY(); + GamePanel.b = new StickyBomb(GamePanel.player.x+GamePanel.camera.x,GamePanel.player.y,35, (xx-GamePanel.player.x-GamePanel.camera.x)/20, (yy-GamePanel.player.y)/10); + } + + public void draw(Graphics g){ + g.drawRect(x-GamePanel.camera.x,y,length,length); + + } +}