Skip to content

Instantly share code, notes, and snippets.

@macroxela
Created April 24, 2014 01:46
Show Gist options
  • Save macroxela/11238740 to your computer and use it in GitHub Desktop.
Save macroxela/11238740 to your computer and use it in GitHub Desktop.
A Galaga game developed in Java
import java.util.*;
public class Alien extends GameObject
{
Random dice;
int cnt = 1;
int dx;
int rand = 0;
boolean shot;
Bullet bullet;
Alien()
{
dice = new Random();
x = dice.nextInt(400);
y = dice.nextInt(250);
dx = dice.nextInt(20) + 10;
attribute = "alien";
bullet = new Bullet();
bullet.y = -10;
shot = false;
}
public void update()
{
if(cnt%2 != 1)
{
x += dx;
if(x > 500)
cnt++;
}
else
{
x -= dx;
if(x < 0)
cnt++;
}
rand = dice.nextInt(2);
if(alive)
{
if(rand == 0 && !shot)
{
bullet.x = x + width/2;
bullet.y = y + 5;
shot = true;
}
if(shot)
{
bullet.y += 10;
if(bullet.y >= 500)
{
bullet.y = -5;
shot = false;
}
}
}
else
{
bullet.y = -15;
}
}
}
import java.awt.*;
import javax.swing.*;
public class Bullet extends GameObject
{
Color col;
Bullet()
{
width = 10;
height = 10;
x = 0;
y = 0;
attribute = "bullet";
col = Color.RED;
}
public void update()
{
y -= 10;
}
public void makeColor()
{
col = Color.GREEN;
}
public void draw(Graphics g, Component c)
{
g.setColor(col);
g.fillOval(x, y, width, height);
}
}
public class CircleAlien extends Alien
{
double t;
public void update()
{
x = 100+ (int) (100 * Math.sin(t));
y = 100+ (int) (100 *Math.cos(t));
t += .2;
if(alive)
{
if(rand == 0 && !shot)
{
bullet.x = x + width/2;
bullet.y = y + 5;
shot = true;
}
if(shot)
{
bullet.y += 10;
if(bullet.y >= 500)
{
bullet.y = -5;
shot = false;
}
}
}
else
{
bullet.y = -15;
}
}
}
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
public class GalagaPanel extends JPanel implements KeyListener
{
//images
ImageIcon space, shippic, dumbalien, pred, seekpic;
//list of all objects in game
LinkedList<Alien> masterList;
//ship
Ship ship;
Bullet bullet;
int dead, listlength;
GalagaPanel()
{
//load images, data, whatever
space = new ImageIcon("data/space.GIF");
shippic = new ImageIcon("data/e23.GIF");
dumbalien = new ImageIcon("data/Alien1.PNG");
pred = new ImageIcon("data/Alien2.PNG");
//add some aliens to game
masterList = new LinkedList<Alien>();
for(int i=0; i<3; i++)
{
Alien a = new Alien();
a.setPicture(dumbalien);
masterList.add(a);
}
CircleAlien ca = new CircleAlien();
ca.setPicture(dumbalien);
masterList.add(ca);
//add a ship to the game
ship = new Ship();
ship.setPicture(shippic);
ship.x = 200;
ship.y = 400;
Predator p1 = new Predator();
p1.setPicture(pred);
p1.setPrey(ship);
masterList.add(p1);
Seeker seek = new Seeker();
seekpic = new ImageIcon("data/Alien4.PNG");
seek.setPicture(seekpic);
seek.setPrey(ship);
masterList.add(seek);
Predator p2 = new Predator();
p2.setPicture(pred);
p2.setPrey(ship);
masterList.add(p2);
bullet = new Bullet();
UpdateThread ut = new UpdateThread(this);
ut.start();
//stupid key listener stuff
addKeyListener(this);
setFocusable(true);
}
public void paintComponent(Graphics g)
{
//clear screen
g.drawImage(space.getImage(),0,0,getWidth(),getHeight(),this);
//draw all objects in game
for( Alien go : masterList )
{
go.draw(g,this);
listlength++;
go.bullet.draw(g, this);
if(!go.alive)
dead++;
}
ship.draw(g, this);
bullet.draw(g, this);
if(ship.alive == false)
{
g.setFont(new Font("sansserif", Font.BOLD, 32));
g.drawString("GAME OVER!!!!", 150, 300);
}
if(dead == listlength)
{
g.setFont(new Font("sansserif", Font.BOLD, 32));
g.drawString("GAME WON!!!!", 150, 300);
}
else
{
listlength = 0;
dead = 0;
}
}
public void update()
{
//update all objects in game
for( Alien go : masterList )
{
go.update();
if(bullet.intersects(go))
{
go.kill();
}
if(go.intersects(ship) && !go.attribute.equalsIgnoreCase("ship"))
{
ship.kill();
System.out.println(go.attribute + " killed you");
}
if(go.bullet.intersects(ship))
{
ship.kill();
System.out.println("Shot by " + go.attribute);
}
}
//check for bullet shot aliens
bullet.update();
ship.update();
repaint();
}
public void keyPressed(KeyEvent k)
{
char c = k.getKeyChar();
if( k.getKeyCode() == KeyEvent.VK_RIGHT )
ship.dx = 5;
if( k.getKeyCode() == KeyEvent.VK_LEFT )
ship.dx = -5;
if(c == ' ')
{
bullet.x = ship.x;
bullet.y = ship.y - 30;
}
}
public void keyReleased(KeyEvent k)
{
if( k.getKeyCode() == KeyEvent.VK_LEFT || k.getKeyCode() == KeyEvent.VK_RIGHT )
ship.dx = 0;
}
public void keyTyped(KeyEvent k)
{
}
}
import javax.swing.*;
import java.awt.*;
public class GameObject extends Rectangle
{
ImageIcon picture;
boolean alive;
String attribute = "nothing";
GameObject()
{
x= 200;
y = 200;
width = 50;
height = 50;
alive = true;
}
public void setPicture(ImageIcon p)
{
picture = p;
}
public void draw(Graphics g, Component c)
{
if(alive)
{
if( picture != null )
g.drawImage(picture.getImage(),x,y,width,height,c);
else
{
g.setColor(Color.BLUE);
g.fillRect(x,y,width,height);
}
}
}
public void kill()
{
x = 0;
y = 0;
width = 0;
height = 0;
System.out.println(attribute + " killed");
alive = false;
}
public void update()
{
x += 1;
y += 1;
}
}
import javax.swing.*;
import java.awt.*;
public class Galaga extends JApplet
{
public static void main(String [] args)
{
//step 1, create a frame
JFrame frame = new JFrame("Galaga!");
//step 2, create a panel, put it in frame
GalagaPanel panel = new GalagaPanel();
frame.getContentPane().add(panel);
//step 3, set frame size, make it visible
frame.setSize(600,600);
frame.setVisible(true);
}
}
public class Predator extends Alien
{
GameObject prey;
//Predator, follows you
Predator()
{
x = dice.nextInt(400);
y = dice.nextInt(200);
attribute = "Predator";
}
public void setPrey(GameObject o)
{
prey = o;
}
public void update()
{
if(prey.x < x)
{
x -= 5;
}
if(prey.x > x)
{
x += 5;
}
if(prey.y > y)
{
y += 3;
}
if(prey.y < y)
{
y -= 3;
}
if(alive)
{
if(rand == 0 && !shot)
{
bullet.x = x + width/2;
bullet.y = y + 5;
shot = true;
}
if(shot)
{
bullet.y += 5;
if(bullet.y >= 500)
{
bullet.y = -5;
shot = false;
}
}
}
else
{
bullet.y = -15;
}
}
}
public class Seeker extends Alien
{
double t;
GameObject prey;
//Seeker, moves around in sine wave, shoots bullets that follow you
Seeker()
{
x = dice.nextInt(400);
y = dice.nextInt(200);
attribute = "Seeker";
bullet.makeColor();
}
public void setPrey(GameObject o)
{
prey = o;
}
public void update()
{
if(cnt%2 != 1)
{
x += dx;
y = 120+ (int) (150 *Math.cos(t));
t+= .2;
if(x > 500)
cnt++;
}
else
{
x -= dx;
y = 120+ (int) (150 *Math.cos(t));
t+= .2;
if(x < 0)
cnt++;
}
rand = dice.nextInt(2);
if(alive)
{
if(rand == 0 && !shot)
{
bullet.x = x + width/2;
bullet.y = y + 5;
shot = true;
}
if(shot)
{
bullet.y += 5;
if(prey.x < bullet.x)
{
bullet.x -= 4;
}
if(prey.x > bullet.x)
{
bullet.x += 4;
}
if(bullet.y >= 500)
{
bullet.y = -5;
shot = false;
}
}
}
else
{
bullet.y = -15;
}
}
}
public class Ship extends GameObject
{
//ship velocity
int dx;
Ship()
{
attribute = "ship";
}
public void update()
{
x+=dx;
}
}
import javax.swing.*;
public class UpdateThread extends Thread
{
GalagaPanel panel;
UpdateThread(GalagaPanel p)
{
panel = p;
}
public void run()
{
while(true)
{
panel.update();
//wait awhile
try{Thread.sleep(50);}
catch(Exception e){}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment