Skip to content

Commit

Permalink
Added examples: Wasp and Balls
Browse files Browse the repository at this point in the history
  • Loading branch information
divs1210 committed Apr 16, 2013
1 parent c836a57 commit 1a21df0
Show file tree
Hide file tree
Showing 43 changed files with 544 additions and 0 deletions.
Binary file added bin/example1_Wasp/Wasp.class
Binary file not shown.
Binary file added bin/example1_Wasp/package-info.class
Binary file not shown.
Binary file added bin/example2_Balls/Ball.class
Binary file not shown.
Binary file added bin/example2_Balls/Cloud.class
Binary file not shown.
Binary file added bin/example2_Balls/Game.class
Binary file not shown.
Binary file added bin/example2_Balls/Highscores$1.class
Binary file not shown.
Binary file added bin/example2_Balls/Highscores.class
Binary file not shown.
Binary file added bin/example2_Balls/StickMan.class
Binary file not shown.
Binary file added bin/example2_Balls/package-info.class
Binary file not shown.
59 changes: 59 additions & 0 deletions examples/example1_Wasp/Wasp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License.
* To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/ or send
* a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
*/

package example1_Wasp;

import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

import org.imgscalr.Scalr;

import com.threecoffee.anim.Sprite;
import com.threecoffee.control.GameWindow;

/**
* A simple animation using a spritesheet
*
* @author Divyansh Prakash
*/
class Wasp{

Wasp(){
//Create new window with given width and height
GameWindow g = new GameWindow(150, 150);

//Load spritesheet
BufferedImage spritesheet=null;
try {
spritesheet = ImageIO.read(new File("media/wasp/wasp.png"));
}catch (Exception e) {
e.printStackTrace();
}

//Create new sprite
Sprite wasp = new Sprite();
//Crop images from spritesheet, and add them to the sprite
for(int i=0; i<8; i++)
wasp.addImage(new ImageIcon( Scalr.crop(spritesheet, 100*i, 0, 100, 100) ));

//Add sprite to GameWindow, set its location on screen,
//and play it.
wasp.addTo(g);
wasp.setLocation(0, 0);
wasp.play();

//Make window visible on screen
g.setVisible(true);
}

public static void main(String[] args){
new Wasp();
}

}
11 changes: 11 additions & 0 deletions examples/example1_Wasp/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License.
* To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/ or send
* a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
*/

/**
* A simple demo animation using a spritesheet.
*/
package example1_Wasp;
//nothing here
90 changes: 90 additions & 0 deletions examples/example2_Balls/Ball.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License.
* To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/ or send
* a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
*/

package example2_Balls;

import java.util.Date;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

import com.threecoffee.anim.Actor;
import com.threecoffee.anim.Sprite;

/**
* Represents a flying, burning ball that bounces off the sides
* of the window. The player dies if it comes in contact with a
* Ball object.
*
* @author Divyansh Prakash
*/
public class Ball extends Sprite {

int xvel=1, yvel=1;

Ball(){
setRandomVel();

addImage(new ImageIcon("media/balls/sprites/ball/1.png"));
addImage(new ImageIcon("media/balls/sprites/ball/2.png"));
addImage(new ImageIcon("media/balls/sprites/ball/3.png"));
setDelay(5);
}

public void update(){
moveSprite(xvel, yvel);

if(getX()+getWidth()>=getGameWindow().getWidth() || getX()<=0)
xvel *= -1;
if(getY()+getHeight()>=getGameWindow().getHeight()-20 || getY()<=0)
yvel *= -1;
}

public void setVel(int x, int y){
xvel = x;
yvel = y;
}

public void setRandomVel(){
Random r = new Random();
xvel = r.nextInt(2)+1;
yvel = r.nextInt(2)+1;

if(r.nextBoolean())
xvel *= -1;
if(r.nextBoolean())
yvel *= -1;
}

public double getVel(){
return Math.sqrt(xvel*xvel + yvel*yvel);
}

public void collided(Sprite s){
if(Math.abs((s.getX()+s.getWidth()/2)-(getX()+getWidth()/2))<=20 && getY()+getHeight()>=s.getY()+10){

Game.curr = new Date();
Game.time = (int) (Math.abs(Game.init.getTime() - Game.curr.getTime())/1000);

getGameWindow().pause(true);

((Actor)s).setCurrentAnimation(3);
s.pause(false);

Game.ended = true;
s.setGravity(true);
s.getGravity().setDelay(50);

getGameWindow().setAlwaysOnTop(false);
JOptionPane.showMessageDialog(getGameWindow(), "You survived for "+(Game.time-Game.ptime)+" seconds.");

Highscores h = new Highscores();
h.addScore(Game.time-Game.ptime);
h.display();
}
}
}
32 changes: 32 additions & 0 deletions examples/example2_Balls/Cloud.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License.
* To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/ or send
* a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
*/

package example2_Balls;

import javax.swing.ImageIcon;

import com.threecoffee.anim.Sprite;

/**
* Represents a cloud that keeps drifting across the game-window
* at constant speed.
*
* @author Divyansh Prakash
*/
public class Cloud extends Sprite {

Cloud(int type){
addImage(new ImageIcon("media/balls/sprites/clouds/cloud"+type+".png"));
}

public void update(){
if(getX()+getWidth()<=0)
setLocation(getGameWindow().getWidth(), getY());

moveSprite(-1, 0);
}

}
121 changes: 121 additions & 0 deletions examples/example2_Balls/Game.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License.
* To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/ or send
* a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
*/

package example2_Balls;

import java.awt.event.KeyEvent;
import java.util.Date;
import java.util.Random;

import javax.swing.ImageIcon;

import com.threecoffee.anim.Sprite;
import com.threecoffee.control.GameWindow;
import com.threecoffee.media.ImageControl;
import com.threecoffee.util.Logger;

/**
* This is the main class for Balls.
* It is a GameWindow with different Actors and Sprites
* working together.
*
* @author Divyansh Prakash
*/
public class Game extends GameWindow{

public static GameWindow gw;
public static boolean ended;

public static Date init, curr, pstart, pend;
public static int time, ptime;

public Game() {
super(600, 400);
gw = this;
ended = false;
init=curr=null;
ptime=time=0;

Random r = new Random();
int bg = r.nextInt(3)+1;

Sprite background = new Sprite();
background.addImage(ImageControl.resize(new ImageIcon("media/balls/sprites/background/bg"+bg+".jpg"), 600, 400));
background.setLocation(0, 0);

Ball b = new Ball();
b.setLocation(r.nextInt(getWidth()-b.getWidth()-5)+5, r.nextInt(getHeight()/2)+5);
b.yvel = -1;
b.addTo(this);

Ball b2 = new Ball();
b2.setLocation(r.nextInt(getWidth()-b.getWidth()-5)+5, r.nextInt(getHeight()/2)+5);
while(b.getVel()>=2.2 && b2.getVel()>=2.2)
b2.setRandomVel();
b2.yvel = -1;
b2.addTo(this);

Ball b3 = new Ball();
b3.setLocation(r.nextInt(getWidth()-b.getWidth()-5)+5, r.nextInt(b.getHeight()/2)+5);
while((b.getVel()>=2.2 || b2.getVel()>=2.2) && b3.getVel()>=2.2)
b3.setRandomVel();
b3.yvel = -1;
b3.addTo(this);

Cloud c= new Cloud(bg);
c.setLocation(0, 0);
c.addTo(this);

Cloud c1= new Cloud(bg);
c1.setLocation(300, 70);
c1.addTo(this);

StickMan sm = new StickMan();
sm.setLocation(0, getHeight()-sm.getHeight()-30);
sm.addTo(this);

b.addCollider(sm);
b2.addCollider(sm);
b3.addCollider(sm);

background.addTo(this);

background.play();
b.play();
b2.play();
b3.play();
sm.play();
c.play();
c1.play();

init = new Date();

setVisible(true);
}

public void update(){
if(GameWindow.isKeyDown(KeyEvent.VK_SPACE) && !ended){
if(!this.isPaused())
pstart = new Date();
else{
pend = new Date();
ptime += (pend.getTime()-pstart.getTime())/1000;
}

pause(!this.isPaused());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
Logger.log(e);
}
}
}

public static void main(String[] args){
Game g = new Game();
}

}
Loading

0 comments on commit 1a21df0

Please sign in to comment.