I showed Tara one of the physics based games I created and she wasn't impressed and said I should make something more like a shooter. In the proceeding 2.5 hours while she was at softball practice and I was waiting in her car for her to be finished, I programmed this (incredibly difficult to win) game.
read moresystem.java
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.AffineTransform;
import java.util.ArrayList;
import java.util.Random;
@SuppressWarnings("serial")
public class system extends java.applet.Applet implements Runnable,
ActionListener, MouseListener, MouseMotionListener, KeyListener {
int frame;
int delay;
Image face;
Image secondFace;
Image star;
Image background;
Image ship;
Image saber;
Image explode;
MediaTracker tr;
boolean invalidClick = false;
int increment = 1;
int numberOfPoints = 0;
int startClickx;
int startClicky;
int currentx;
int currenty;
int xsize = 800;
int ysize = 800;
int score = 0;
int bulletxsize = 5;
int bulletysize = 5;
boolean playerDestroyed = false;
Rectangle fieldOfView = new Rectangle(xsize,xsize);
Thread animator;
Font font = new Font("SansSerif", Font.BOLD, 16);
// int points[][]=new int [2][100];
Random ran = new Random();
ArrayList bullets = new ArrayList();
ArrayList points = new ArrayList();
ArrayList toRemove = new ArrayList();
ArrayList bulletsToRemove = new ArrayList();
ArrayList stars = new ArrayList();
// Collection allThePoints = new ArrayList();
// attempt blah = new attempt();
/**
* Initialize the applet and compute the delay between frames.
*/
public void init() {
String str = getParameter("fps");
int fps = (str != null) ? Integer.parseInt(str) : 50;
delay = (fps > 0) ? (1000 / fps) : 100;
// Button nameField =new Button("Type here Something");
addMouseListener(this);
addMouseMotionListener(this);
addKeyListener(this);
// nameField.addActionListener(blah);
// add(nameField);
tr = new MediaTracker(this);
explode = getImage(getCodeBase(), "explode.gif");
tr.addImage(explode, 0);
star = getImage(getCodeBase(), "star.gif");
tr.addImage(star, 0);
face = getImage(getCodeBase(), "Ryan copy copy.png");
secondFace = getImage(getCodeBase(), "daniel.jpg");
ship = getImage(getCodeBase(), "ship1.png");
background = getImage(getCodeBase(), "stars.jpg");
saber = getImage(getCodeBase(), "light.png");
tr.addImage(face, 0);
tr.addImage(secondFace, 0);
tr.addImage(ship, 0);
tr.addImage(background, 0);
tr.addImage(saber, 0);
int blah2;
int blah;
for (int i = 0; i < 10; i++) {
int blah3 = ran.nextInt(2);
int blah4 = ran.nextInt(2);
if (blah3 == 1) {
blah = ran.nextInt(xsize) + xsize;
} else {
blah = ran.nextInt(xsize) - xsize;
}
if (blah4 == 1) {
blah2 = ran.nextInt(ysize) + ysize;
} else {
blah2 = ran.nextInt(ysize) - ysize;
}
star aoeu = new star(user, blah, blah2, 30, 30);
stars.add(aoeu);
// secondPoints.add(secondFace);
// Rectangle second = new Rectangle(blah3, blah4, 20, 20);
// fakePoints.add(second);
}
}
/**
* This method is called when the applet becomes visible on the screen.
* Create a thread and start it.
*/
public void start() {
animator = new Thread(this);
animator.start();
}
/**
* This method is called by the thread that was created in the start method.
* It does the main animation.
*/
public void run() {
while (Thread.currentThread() == animator) {
// Display the next frame of animation.
increment++;
repaint();
// Delay for a while
try {
Thread.sleep(delay);
} catch (InterruptedException e) {
break;
}
// Advance the frame
frame++;
}
}
/**
* This method is called when the applet is no longer visible. Set the
* animator variable to null so that the thread will exit before displaying
* the next frame.
*/
public void stop() {
animator = null;
}
/**
* Paint a frame of animation.
*/
player user = new player();
boolean collision = false;
boolean readyToStart = false;
int constantincrementor = 0;
public void paint(Graphics g) {
g.drawImage(background, 0, 0, this);
Graphics2D g2d = (Graphics2D) g;
AffineTransform origXform = g2d.getTransform();
AffineTransform newXform = (AffineTransform) (origXform.clone());
// center of rotation is center of the panel
int xRot = (int) user.xPosition;// this.getWidth() / 2 +
int yRot = (int) user.yPosition;
newXform.rotate(user.directionPointing, xRot, yRot);
g2d.setTransform(newXform);
// draw image centered in panel
int x = (int) user.xPosition - (ship.getWidth(this)) / 2;
int y = (int) user.yPosition - (ship.getHeight(this)) / 2;
if (!playerDestroyed) {
g2d.drawImage(ship, x, y, this);
}
g2d.setTransform(origXform);
this.setSize(xsize, ysize);
user.update();
for (star s : stars) {
g.drawImage(star, (int) s.xPosition, (int) s.yPosition, this);
s.update(user);
if (s.killedPlayer(user)) {
playerDestroyed = true;
}
}
for (bullet a : bullets) {
a.update();
if(!fieldOfView.contains(a.xPosition, a.yPosition)){
bulletsToRemove.add(a);
}
g2d.drawImage(saber, (int) a.xPosition, (int) a.yPosition, this);
g.setColor(Color.black);
for (star s : stars) {
if (a.intersects(s)) {
// objects.massOne.collision = true;
//g.drawImage(explode,(int) a.xPosition,(int) a.yPosition, this);
bulletsToRemove.add(a);
toRemove.add(s);
score++;
}
}
}
// g.fillOval((int) user.xPosition, (int) user.yPosition, 10,20);
for (star a : toRemove) {
stars.remove(a);
}
toRemove.clear();
for (bullet a : bulletsToRemove) {
bullets.remove(a);
}
g.setColor(Color.YELLOW);
g.setFont(font);
g.drawString(Integer.toString(score), xsize-50, ysize-50);
g.setColor(Color.BLACK);
}
@Override
public void mouseClicked(MouseEvent e) {
if(!playerDestroyed){
// TODO Auto-generated method stub
int thex = e.getX();
int they = e.getY();
if (thex > user.xPosition) {
user.directionPointing = Math
.atan(((user.yPosition - they) / (user.xPosition - thex)));
}
if (thex < user.xPosition) {
user.directionPointing = Math.PI
+ Math.atan(((user.yPosition - they) / (user.xPosition - thex)));
}
//System.out.println(Math.toDegrees(user.directionPointing));
bullet aNewBullet = new bullet(user.xPosition, user.yPosition,
user.directionPointing, bulletxsize, bulletysize);
bullets.add(aNewBullet);
}
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 39||e.getKeyCode() == 69) // Right arrow key code
{
user.xVelocity += .5;
}
else if (e.getKeyCode() == 37||e.getKeyCode() == 65)// Left arrow key code
{
user.xVelocity -= .5;
}
else if (e.getKeyCode() == 38||e.getKeyCode() == 87)// Left arrow key code
{
user.yVelocity -= .5;
}
else if (e.getKeyCode() == 40||e.getKeyCode() == 79)// Left arrow key code
{
user.yVelocity += .5;
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseDragged(MouseEvent e) {
if(!playerDestroyed){
// TODO Auto-generated method stub
int thex = e.getX();
int they = e.getY();
if (thex > user.xPosition) {
user.directionPointing = Math
.atan(((user.yPosition - they) / (user.xPosition - thex)));
}
if (thex < user.xPosition) {
user.directionPointing = Math.PI
+ Math.atan(((user.yPosition - they) / (user.xPosition - thex)));
}
//System.out.println(Math.toDegrees(user.directionPointing));
bullet aNewBullet = new bullet(user.xPosition, user.yPosition,
user.directionPointing, bulletxsize, bulletysize);
bullets.add(aNewBullet);
}
}
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
if(!playerDestroyed){
// TODO Auto-generated method stub
int thex = e.getX();
int they = e.getY();
if (thex > user.xPosition) {
user.directionPointing = Math
.atan(((user.yPosition - they) / (user.xPosition - thex)));
}
if (thex < user.xPosition) {
user.directionPointing = Math.PI
+ Math.atan(((user.yPosition - they) / (user.xPosition - thex)));
}
//System.out.println(Math.toDegrees(user.directionPointing));
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}
star.java
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Rectangle;
import java.awt.MediaTracker;
public class star {
public double xAcceleration;
public double yAcceleration;
public double xVelocity;
public double yVelocity;
public double xPosition;
public double yPosition;
public double totalVelocity=3;
public int xsize;
public int ysize;
public double targetXPosition;
public double targetYPosition;
public double directionPointing;
Image explode;
MediaTracker tr;
public star(player user, double xPosition, double yPosition,int xsize,int ysize){
this.xPosition=xPosition;
this.yPosition=yPosition;
this.xsize=xsize;
this.ysize=ysize;
this.targetXPosition=user.xPosition;
this.targetYPosition=user.yPosition;
if(targetXPosition>this.xPosition){
directionPointing=Math.atan(((this.yPosition-targetYPosition)/(this.xPosition-targetXPosition)));
}
if(targetXPosition<this.xPosition){
directionPointing=Math.PI+Math.atan(((this.yPosition-targetYPosition)/(this.xPosition-targetXPosition)));
}
this.yVelocity=totalVelocity*Math.sin(directionPointing);
this.xVelocity=totalVelocity*Math.cos(directionPointing);
}
public void update(player user){
this.targetXPosition=user.xPosition;
this.targetYPosition=user.yPosition;
if(targetXPosition>this.xPosition){
directionPointing=Math.atan(((this.yPosition-targetYPosition)/(this.xPosition-targetXPosition)));
}
if(targetXPosition<this.xPosition){
directionPointing=Math.PI+Math.atan(((this.yPosition-targetYPosition)/(this.xPosition-targetXPosition)));
}
this.yVelocity=totalVelocity*Math.sin(directionPointing);
this.xVelocity=totalVelocity*Math.cos(directionPointing);
this.yPosition += this.yVelocity;
this.xPosition += this.xVelocity;
}
public boolean isContained(Rectangle r){
Rectangle bullet = new Rectangle((int)xPosition,(int)yPosition,xsize,ysize);
return r.intersects(bullet);
}
public boolean killedPlayer(player p){
Rectangle star = new Rectangle((int)xPosition,(int)yPosition,xsize,ysize);
Rectangle player = new Rectangle((int)p.xPosition,(int)p.yPosition,xsize,ysize);
return star.intersects(player);
}
}
player.java
public class player {
public double xAcceleration;
public double yAcceleration;
public double xVelocity;
public double yVelocity;
public double xPosition;
public double yPosition;
public int xsize=800;
public int ysize=800;
public double directionPointing =0;
public player(){
this.xPosition= xsize/2;
this.yPosition= ysize/2;
this.yVelocity = 0;
this.xVelocity = 0;
}
public void update(){
this.yVelocity+=yAcceleration;
this.xVelocity+=xAcceleration;
this.yPosition += this.yVelocity;
this.xPosition += this.xVelocity;
}
}
bullet.java
import java.awt.Rectangle;
public class bullet {
public double xAcceleration;
public double yAcceleration;
public double xVelocity;
public double yVelocity;
public double xPosition;
public double yPosition;
public double totalVelocity=5;
public int xsize;
public int ysize;
public bullet(double xPosition, double yPosition,double directionPointing,int xsize,int ysize){
this.xPosition=xPosition;
this.yPosition=yPosition;
this.yVelocity=totalVelocity*Math.sin(directionPointing);
this.xVelocity=totalVelocity*Math.cos(directionPointing);
this.xsize=xsize;
this.ysize=ysize;
}
public void update(){
this.yPosition += this.yVelocity;
this.xPosition += this.xVelocity;
}
public boolean intersects(star s){
Rectangle bullet = new Rectangle((int)xPosition,(int)yPosition,xsize,ysize);
Rectangle star = new Rectangle((int)s.xPosition,(int)s.yPosition,xsize,ysize);
return star.intersects(bullet);
}
}
ship1.png
star.gif
stars.jpg