Skip to content

Instantly share code, notes, and snippets.

@macroxela
Created April 24, 2014 23:20
Show Gist options
  • Save macroxela/11272865 to your computer and use it in GitHub Desktop.
Save macroxela/11272865 to your computer and use it in GitHub Desktop.
A Frogger game where the user tries to get the frog across the street to the log without getting run over or drowning.
import java.awt.*;
import javax.swing.*;
public class Frog extends Rectangle
{
ImageIcon pic;
Frog()
{
x = 300;
y = 160;
width = 30;
height = 30;
}
public void setPic(ImageIcon p)
{
pic = p;
}
public void update()
{
x = 100;
y = 300;
}
public void draw(Graphics g, Component c)
{
g.drawImage(pic.getImage(),x,y,width,height,c);
g.drawRect(x, y, width, height);
}
}
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class FrogPanel extends JPanel implements KeyListener
{
ImageIcon frogpic, background;
ImageIcon busR, carR, car2R, logpic;
Frog frog;
boolean win = false;
MovingObject bus, car, car2, log;
LinkedList<MovingObject> moveList;
FrogPanel()
{
frogpic = new ImageIcon("data/frogU.gif");
background = new ImageIcon("data/background.jpg");
frog = new Frog();
frog.setPic(frogpic);
log = new MovingObject();
logpic = new ImageIcon("data/medlog.png");
log.setPic(logpic);
log.y = 100;
log.dx = 3;
log.att = "log";
createCars();
addKeyListener(this);
setFocusable(true);
UpdateThread ut = new UpdateThread(this);
ut.start();
}
public void createCars()
{
moveList = new LinkedList<MovingObject>();
busR = new ImageIcon("data/busR.gif");
carR = new ImageIcon("data/carR.gif");
car2R = new ImageIcon("data/car2Right.gif");
bus = new MovingObject();
bus.setPic(busR);
moveList.add(bus);
car = new MovingObject();
car.setPic(carR);
car.setSpeed(8, 0);
car.setXY(200, 200);
moveList.add(car);
car2 = new MovingObject();
car2.setPic(car2R);
car2.setSpeed(6, 0);
car2.setXY(200, 250);
moveList.add(car2);
}
public boolean checkCollision(MovingObject o)
{
if(o.intersects(frog) == true )
{
if(o.att.equalsIgnoreCase("Car"))
{
frog.update();
System.out.println("Run Over");
}
else if(o.att.equalsIgnoreCase("log"))
{
if(frog.x > o.scw)
win = true;
frog.x += o.dx;
}
return true;
}
return false;
}
public void update()
{
for(MovingObject o : moveList)
{
checkCollision(o);
o.update();
}
if(frog.y + frog.height <= log.y + log.height)
{
if(checkCollision(log))
{
System.out.println("Hit log");
}
else
{
frog.update();
System.out.println("Drown");
}
}
log.update();
repaint();
}
public void paintComponent(Graphics g)
{
g.drawImage(background.getImage(), 0, 0, getWidth(), getHeight(), this);
log.draw(g, this);
frog.draw(g,this);
for(MovingObject o : moveList)
{
o.draw(g, this);
}
if(win)
{
g.setColor(Color.red);
g.setFont( new Font( "stupid font", Font.BOLD, 50 ) );
g.drawString("WIN!!!!", 100,100);
}
}
public void keyPressed(KeyEvent k)
{
char c = k.getKeyChar();
if( k.getKeyCode() == KeyEvent.VK_LEFT )
frog.x -= 10;
if( k.getKeyCode() == KeyEvent.VK_RIGHT )
frog.x += 10;
if( k.getKeyCode() == KeyEvent.VK_DOWN )
frog.y += 10;
if( k.getKeyCode() == KeyEvent.VK_UP )
frog.y -= 10;
repaint();
}
public void keyReleased(KeyEvent k)
{
// TODO Auto-generated method stub
}
public void keyTyped(KeyEvent k)
{
// TODO Auto-generated method stub
}
}
import javax.swing.*;
public class Game {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame = new JFrame("Frogger");
FrogPanel panel = new FrogPanel();
frame.getContentPane().add(panel);
frame.setSize(400,400);
frame.setVisible(true);
}
}
import java.awt.*;
import javax.swing.*;
public class MovingObject extends Rectangle
{
int dx, dy, sch, scw;
String att;
ImageIcon pic;
MovingObject()
{
x = 100;
y = 150;
dx = 5;
dy = 0;
width = 100;
height = 50;
att = "car";
}
public void setSpeed(int xspeed, int yspeed)
{
dx = xspeed;
dy = yspeed;
}
public void setXY(int xpos, int ypos)
{
x = xpos;
y = ypos;
}
public void update()
{
if(x < scw )
x += dx;
else
x = 0;
}
public void setPic(ImageIcon p)
{
pic = p;
}
public void draw(Graphics g, Component c)
{
sch = c.getHeight();
scw = c.getWidth();
g.drawImage(pic.getImage(), x, y%c.getHeight(), width, height, c);
g.drawRect(x, y%c.getHeight(), width, height);
}
}
import javax.swing.*;
public class UpdateThread extends Thread
{
FrogPanel panel;
UpdateThread(FrogPanel p)
{
panel = p;
}
public void run()
{
while(!panel.win)
{
panel.update();
try
{
Thread.sleep(50);
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment