Skip to content

Instantly share code, notes, and snippets.

@feehe21
Created December 1, 2018 04:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save feehe21/2d1352199226b53deb2bfdf2c5bbbced to your computer and use it in GitHub Desktop.
Save feehe21/2d1352199226b53deb2bfdf2c5bbbced to your computer and use it in GitHub Desktop.
import javax.swing.JFrame;
public class Starter extends JFrame {
public Starter()
{
add(new Board());
setTitle("Board");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(500,500);
setLocationRelativeTo(null);
setVisible(true);
setResizable(false);
}
public static void main(String[] args) {
new Starter();
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.Timer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.imageio.*;
import java.awt.image.*;
import java.io.*;
public class Board extends JPanel implements Runnable, MouseListener {
boolean ingame = false;
private Dimension d;
int BOARD_WIDTH = 500;
int BOARD_HEIGHT = 500;
String message = "Click Board to Start";
private Thread animator;
private boolean startGame = true;
private double collentValues;
AlienSwarm swarm;
Player ship;
Shots shot;
public Board() {
addKeyListener(new TAdapter());
addMouseListener(this);
setFocusable(true);
d = new Dimension(BOARD_WIDTH, BOARD_HEIGHT);
setBackground(Color.black);
/*
* try { img = ImageIO.read(this.getClass().getResource("mount.jpg")); } catch
* (IOException e) { System.out.println("Image could not be read"); //
* System.exit(1); }
*/
if (animator == null || !ingame) {
animator = new Thread(this);
animator.start();
}
setDoubleBuffered(true);
}
/*
* public Board(){
*
* }
*/
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.white);
g.fillRect(0, 0, d.width, d.height);
// g.fillOval(x,y,r,r);
Font small = new Font("Helvetica", Font.BOLD, 14);
FontMetrics metr = this.getFontMetrics(small);
g.setColor(Color.black);
g.setFont(small);
g.drawString(message, 10, d.height - 60);
synchronized (this) {
// Draw the score
if (this.ship != null)
g.drawString(this.ship.aliensHit + "", 10, 20);
if (ingame) {
this.swarm.paint(g);
this.ship.paint(g);
this.shot.paint(g);
}
}
Toolkit.getDefaultToolkit().sync();
g.dispose();
}
public void update(){
synchronized (this) {
if(!ingame){
return;
}
this.swarm.update();
this.shot.update();
Alien target = this.swarm.locate(this.shot.x, this.shot.y);
if (target != null && this.shot.isVisible) {
target.isVisible = false;
this.shot.isVisible = false;
this.ship.hit();
}
Alien near = this.swarm.locate(this.ship.x, this.ship.y);
if (near != null) {
ingame=false;
message = "Click to play again";
}
if(swarm.aliensDefeated()){
ingame=false;
}
}
}
private class TAdapter extends KeyAdapter {
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
}
public void keyPressed(KeyEvent e) {
// System.out.println( e.getKeyCode());
// message = "Key Pressed: " + e.getKeyCode();
int key = e.getKeyCode();
if (key == 38) {// up
shot.isVisible = true;
shot.x = ship.x + 15;
shot.y = ship.y;
}
if (key == 39) {// right
ship.move(5);
}
if (key == 37) {// left
ship.move(-5);
}
}
}
public void mousePressed(MouseEvent e) {
if(!ingame){
synchronized (this) {
this.swarm = new AlienSwarm(10,10,10,3);
this.ship = new Player(true, 100, 350, 0);
this.shot = new Shots(false, 0, 0);
ingame = true;
}
}
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
public void run() {
long beforeTime, timeDiff, sleep;
beforeTime = System.currentTimeMillis();
int animationDelay = 50;
long time = System.currentTimeMillis();
while (true) {// infinite loop
// spriteManager.update();
this.update();
repaint();
try {
time += animationDelay;
Thread.sleep(Math.max(0, time - System.currentTimeMillis()));
} catch (InterruptedException e) {
System.out.println(e);
} // end catch
} // end while loop
}// end of run
}// end of class
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
import java.awt.Graphics;
import java.awt.image.BufferedImage;
public class Shots{
protected static BufferedImage TEXTURE = Utils.loadTexture("pshot.png");
public int x;
public int y;
public boolean isVisible;
public Shots(boolean isVisible, int x, int y){
//super(isVisible,x,y);
this.x = x;
this.y = y;
this.isVisible = isVisible;
}
public boolean hitAlien(){
return true;
}
public void paint(Graphics g) {
if(this.isVisible){
g.drawImage(TEXTURE, this.x, this.y, 7, 10, null);
}
}
public void update(){
this.y -=4;
}
/*public int moveShot(){
shotY -= 4;
return shotY;
}*/
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Player {
protected static BufferedImage TEXTURE = Utils.loadTexture("player.png");
public int x;
public int y;
public boolean isAlive;
public int aliensHit;
public Player(boolean isAlive, int x, int y, int aliensHit) {
this.x = x;
this.y = y;
this.isAlive = isAlive;
this.aliensHit = aliensHit;
}
public void paint(Graphics g) {
g.drawImage(TEXTURE, this.x, this.y, 30, 20, null);
}
public void hit() {
aliensHit++;
}
public void move(int dx){
this.x += dx;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
import java.awt.Graphics;
import java.awt.image.BufferedImage;
public class Alien {
protected static BufferedImage TEXTURE = Utils.loadTexture("alien.png");
public int x;
public int y;
public boolean isVisible;
public Alien(int x, int y) {
this.x = x;
this.y = y;
this.isVisible = true;
}
public void paint(Graphics g, int offsetX, int offsetY) {
if(this.isVisible){
g.drawImage(TEXTURE, this.x + offsetX, this.y + offsetY, 20, 30, null);
}
}
public boolean isAlive() {
return true;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
import java.awt.Graphics;
import java.awt.image.BufferedImage;
/**
* Write a description of class alienSwarm here.
*
* @author Erica
*/
public class AlienSwarm {
public int x;
public int y;
public Alien[][] swarm;
public boolean left;
public int alienSpeed;
public int swarmWidth;
public int swarmHeight;
/**
* Constructor for objects of class alienSwarm
*/
public AlienSwarm(int x, int y, int swarmWidth, int swarmHeight) {
this.x = x;
this.y = y;
this.swarmWidth = swarmWidth;
this.swarmHeight = swarmHeight;
left = false;
alienSpeed = 2;
this.swarm = new Alien[swarmHeight][swarmWidth];
for (int r = 0; r < swarmHeight; r++)
for (int c = 0; c < swarmWidth; c++)
swarm[r][c] = new Alien(c * 30 + 10, r * 30 + 10);
}
public void paint(Graphics g) {
for (Alien[] row : swarm)
for (Alien alien : row)
alien.paint(g, this.x, this.y);
}
public void update() {
if (this.x <= 0 || this.x >= 200) {
this.left = (this.left == false);
this.y += 10;
if (this.y % 50 == 0) {
this.alienSpeed += 1;
}
}
if (!left) {
this.x += alienSpeed;
} else {
this.x -= alienSpeed;
}
}
public Alien locate(int x, int y) {
if (this.x < x && x < this.x + 30 * this.swarmWidth) {
if (this.y <= y && y < this.y + 30 * this.swarmHeight) {
int lookX = ((x - this.x) / 30);
int lookY = ((y - this.y) / 30);
if (swarm[lookY][lookX].isVisible)
return swarm[lookY][lookX];
}
}
return null;
}
public boolean aliensDefeated() {
for(int a = 0; a < this.swarmWidth; a++){
for(int s = 0; s < this.swarmHeight; s++){
if(swarm[s][a].isVisible){
return false;
}
}
}
return true;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Utils {
public static BufferedImage loadTexture(String name) {
try {
return ImageIO.read(Player.class.getResource(name));
} catch (IOException e) {
System.err.println("Error loading texture: " + name);
e.printStackTrace();
System.exit(-1);
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment