Add second year

This commit is contained in:
2023-12-07 01:19:12 +00:00
parent 3291e5c79e
commit 3d12031ab8
1168 changed files with 431409 additions and 0 deletions

View File

@ -0,0 +1,56 @@
// package MyApplication;
import java.awt.*;
import javax.swing.*;
public class MyApplication extends JFrame {
private static final Dimension WindowSize = new Dimension(600,600);
public MyApplication() {
// create & set up the window
this.setTitle("Pacman, or something...");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// display the window. centred on the screen
Dimension screensize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
int x = screensize.width/2 - WindowSize.width/2;
int y = screensize.height/2 - WindowSize.height/2;
setBounds(x, y, WindowSize.width, WindowSize.height);
setVisible(true);
}
public static void main(String[] args) {
MyApplication w = new MyApplication();
}
public void paint(Graphics g) {
int height = 50, width = 50; // defining the height & width of the rectangle
int x = 10, y = 10; // defining the vertical & horizontal position between rectangles
// looping for each of the 10 rows in the window
for (int row = 0; row < 10; row++) {
// looping for each of the 10 columns in the window
for (int column = 0; column < 10; column++) {
// randomly generating an RGB colour & setting the graphic to use it
Color c = new Color((int)(Math.random()*255), (int)(Math.random()*255), (int)(Math.random()*255));
g.setColor(c);
// make a filled rectangle with the specified dimensions at the specified x, y co-ordinates
g.fillRect(x, y, height, width);
// increasing the x by adding the size of a square plus the desired padding
x += width + 10;
}
// resetting the x value to 10 for the next row
x = 10;
// increasing the y by adding the height of a square plus the desired padding
y += height + 10;
}
}
}

View File

@ -0,0 +1,41 @@
import java.awt.*;
import javax.swing.*;
import java.util.*;
public class GameObject {
// member data
private int x, y;
private int height = 50, width = 50;
private Color c;
private int stepSize = 10; // how many pixels the object will move at a time
// constructor
public GameObject() {
// randomly generating colour
c = new Color((int)(Math.random()*255), (int)(Math.random()*255), (int)(Math.random()*255));
// randomly generating initial x y co-ordinates
x = (int)(Math.random()*600);
y = (int)(Math.random()*600);
}
// public interface
public void move() {
// generating either a 1 or a 0 to determine if the square should move left or right and up or down - 1 = left/up, 0 = right/down
int xDirection = (int)(Math.random()*2);
int yDirection = (int)(Math.random()*2);
// changing the x & y co-ordinates by either plus or minus the stepSize, depending on the direction
x += (xDirection == 1) ? -stepSize : + stepSize;
y += (yDirection == 1) ? -stepSize : + stepSize;
}
public void paint(Graphics g) {
// setting colour
g.setColor(c);
// make a filled rectangle with the specified dimensions at the specified x, y co-ordinates
g.fillRect(x, y, height, width);
}
}

View File

@ -0,0 +1,76 @@
import java.awt.*;
import javax.swing.*;
import java.util.*;
public class MovingSquaresApplication extends JFrame implements Runnable {
// window dimensions
private static final Dimension WindowSize = new Dimension(600,600);
// array of gameobjects (squares)
private GameObject gameobjects[] = new GameObject[100];
// constructor
public MovingSquaresApplication() {
// create & set up the window
this.setTitle("Moving Squares Apllication");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// display the window. centred on the screen
Dimension screensize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
int x = screensize.width/2 - WindowSize.width/2;
int y = screensize.height/2 - WindowSize.height/2;
setBounds(x, y, WindowSize.width, WindowSize.height);
setVisible(true);
// create array of game objects.
//Arrays.fill(gameobjects, 100);
for (int i = 0; i < 100; i++) {
gameobjects[i] = new GameObject();
}
// creating a new thread & starting it
Thread t = new Thread(this);
t.start();
}
// run method called by thread
public void run() {
while (true) {
// iterating over array of gameobjects & calling move() on each object
for (GameObject go : gameobjects) {
go.move();
}
try {
// sleeping
Thread.sleep(100);
// catching exception
} catch (InterruptedException e) {
e.printStackTrace();
}
// repainting now that every object has been moved
this.repaint();
}
}
// paint method
public void paint(Graphics g) {
// filling the screen with a white square between "frames" of animation to get rid of trails caused by moving objects
Color c = new Color(255, 255, 255);
g.setColor(c);
g.fillRect(0, 0, 1000, 1000);
// iterating over each GameObject and calling paint on it
for (GameObject go : gameobjects) {
go.paint(g);
}
}
public static void main(String[] args) {
// instantiating class in main method to begin application
MovingSquaresApplication w = new MovingSquaresApplication();
}
}

View File

@ -0,0 +1,22 @@
import java.awt.*;
import javax.swing.*;
public class Alien extends Sprite2D {
private int stepSize = 5;
public Alien(Image image) {
super(image);
}
// method to randomly move the alien
public void move() {
// generating either a 1 or a 0 to determine if the square should move left or right and up or down - 1 = left/up, 0 = right/down
int xDirection = (int)(Math.random()*2);
int yDirection = (int)(Math.random()*2);
// changing the x & y co-ordinates (inherited from superclass) by either plus or minus the stepSize, depending on the direction
x += (xDirection == 1) ? -stepSize : + stepSize;
y += (yDirection == 1) ? -stepSize : + stepSize;
}
}

View File

@ -0,0 +1,125 @@
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class InvadersApplication extends JFrame implements Runnable, KeyListener {
// member data
private static String workingDirectory = System.getProperty("user.dir");
private Image alienImage;
private Image playerImage;
private static final Dimension WindowSize = new Dimension(600, 600);
private static final int NUMALIENS = 30;
private Alien[] AliensArray = new Alien[NUMALIENS];
private Player player;
// variable to hold how far the player should be moving in the x axis per frame - default 0.
private int dx = 0;
// constructor
public InvadersApplication() {
// set up window
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// display the window. centred on the screen
Dimension screensize = java.awt.Toolkit.getDefaultToolkit().getScreenSize(); int x = screensize.width/2 - WindowSize.width/2; int y = screensize.height/2 - WindowSize.height/2;
setBounds(x, y, WindowSize.width, WindowSize.height);
// adding a key listener
addKeyListener(this);
// load image from disk
ImageIcon alienIcon = new ImageIcon(workingDirectory + "/alien_ship_1.png");
ImageIcon playerIcon = new ImageIcon(workingDirectory + "/player_ship.png");
alienImage = alienIcon.getImage();
playerImage = playerIcon.getImage();
// instantiating an alien for each index in the aliens array
for (int i = 0; i < NUMALIENS; i++) {
AliensArray[i] = new Alien(alienImage);
// generating a random starting position for the alien
AliensArray[i].setPosition((int) (Math.random()*600), (int) (Math.random()*600));
}
// creating a player icon & setting it's position
player = new Player(playerImage);
player.setPosition(270, 550);
setVisible(true);
// create a new thread & start it
Thread t = new Thread(this);
t.start();
}
// thread's entry point
public void run() {
while (true) {
// repainting
this.repaint();
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void keyPressed(KeyEvent e) {
// getting the keycode of the event
int key = e.getKeyCode();
// if right key pressed, making the distance to be moved 10px to the right
if (key == KeyEvent.VK_RIGHT) {
dx = 5;
}
// if left key pressed, making the distance to be moved 10px to the left
if (key == KeyEvent.VK_LEFT) {
dx = -5;
}
}
@Override
public void keyReleased(KeyEvent e) {
// getting the keycode of the event
int key = e.getKeyCode();
// if the key released was the left or right arrow, setting dx back to 0
if (key == KeyEvent.VK_RIGHT || key == KeyEvent.VK_LEFT) {
dx = 0;
}
}
@Override
public void keyTyped(KeyEvent e) {
}
// application's paint method
public void paint(Graphics g) {
// draw a black rectangle on the whole canvas
g.setColor(Color.BLACK);
g.fillRect(0, 0, 600, 600);
// iterating through each sprite, moving it, and calling it's paint method
for (Alien a : AliensArray) {
a.move();
a.paint(g);
}
// moving the player dx pixels - should be 0 if no key is being pressed
player.move(dx);
// calling the Player's paint method
player.paint(g);
}
// application entry point
public static void main(String[] args) {
InvadersApplication ia = new InvadersApplication();
}
}

View File

@ -0,0 +1,13 @@
import java.awt.*;
import javax.swing.*;
public class Player extends Sprite2D {
public Player(Image image) {
super(image);
}
// method to move the player by the supplied distance
public void move(int distance) {
x += distance; // x is inherited from the Sprite2D superclass
}
}

View File

@ -0,0 +1,24 @@
import java.awt.*;
import javax.swing.*;
public class Sprite2D {
// member data
public int x,y; // public so that it can be inherited
private Image image;
public Sprite2D(Image image) {
this.image = image;
}
// paint method
public void paint(Graphics g) {
// draw the image
g.drawImage(image, x, y, null);
}
// set the position of the object
public void setPosition(int x, int y) {
this.x = x;
this.y = y;
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.7 KiB

View File

@ -0,0 +1,31 @@
import java.awt.*;
import javax.swing.*;
public class Alien extends Sprite2D {
private int stepSize = 2;
public Alien(Image image) {
super(image);
}
// method to randomly move the alien
public void move(boolean right) {
// changing the x & y co-ordinates (inherited from superclass) by either plus or minus the stepSize, depending on the direction
if (right) {
x += stepSize;
}
else {
x -= stepSize;
}
}
// method to move aliend down
public void moveDown() {
y += 10;
}
// getter method for the alien's x coordinate
public int getx() {
return x;
}
}

View File

@ -0,0 +1,173 @@
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.image.*;
public class InvadersApplication extends JFrame implements Runnable, KeyListener {
// member data
private static String workingDirectory = System.getProperty("user.dir");
private Image alienImage;
private Image playerImage;
private static final Dimension WindowSize = new Dimension(800, 600);
private static final int NUMALIENS = 30;
private Alien[] AliensArray = new Alien[NUMALIENS];
boolean right = true; // boolean to tell which direction the aliens are moving
private Player player;
private BufferStrategy strategy;
// variable to hold how far the player should be moving in the x axis per frame - default 0.
private int dx = 0;
// constructor
public InvadersApplication() {
// set up window
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// adding a key listener
addKeyListener(this);
// display the window. centred on the screen
Dimension screensize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
int x = screensize.width/2 - WindowSize.width/2; int y = screensize.height/2 - WindowSize.height/2;
setBounds(x, y, WindowSize.width, WindowSize.height);
// load image from disk
ImageIcon alienIcon = new ImageIcon(workingDirectory + "/alien_ship_1.png");
ImageIcon playerIcon = new ImageIcon(workingDirectory + "/player_ship.png");
alienImage = alienIcon.getImage();
playerImage = playerIcon.getImage();
// initial x and y coordinates for the aliens
int alienx = 200;
int alieny = 25;
int column = 0; // keeps track of which column the alien is in
for (int i = 0; i < NUMALIENS; i++) {
AliensArray[i] = new Alien(alienImage);
AliensArray[i].setPosition(alienx, alieny);
alienx += 60;
column++;
// go onto a new line every 5 aliens
if (column >= 6) {
column = 0;
alienx = 200;
alieny += 60;
}
}
// creating a player icon & setting it's position
player = new Player(playerImage);
player.setPosition(270, 550);
setVisible(true);
createBufferStrategy(2);
strategy = getBufferStrategy();
// create a new thread & start it
Thread t = new Thread(this);
t.start();
}
// thread's entry point
public void run() {
while (true) {
// repainting
this.repaint();
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void keyPressed(KeyEvent e) {
// getting the keycode of the event
int key = e.getKeyCode();
// if right key pressed, making the distance to be moved 10px to the right
if (key == KeyEvent.VK_RIGHT) {
dx = 5;
}
// if left key pressed, making the distance to be moved 10px to the left
if (key == KeyEvent.VK_LEFT) {
dx = -5;
}
}
@Override
public void keyReleased(KeyEvent e) {
// getting the keycode of the event
int key = e.getKeyCode();
// if the key released was the left or right arrow, setting dx back to 0
if (key == KeyEvent.VK_RIGHT || key == KeyEvent.VK_LEFT) {
dx = 0;
}
}
@Override
public void keyTyped(KeyEvent e) {
}
// application's paint method
public void paint(Graphics g) {
g = strategy.getDrawGraphics();
// draw a black rectangle on the whole canvas
g.setColor(Color.BLACK);
g.fillRect(0, 0, 800, 600);
// iterating through each sprite, moving it, and calling it's paint method
for (Alien a : AliensArray) {
a.move(right);
a.paint(g);
}
// checking if any of the aliens hit the edge when they were moved
for (Alien a : AliensArray) {
// changing direction & moving down if edge is hit
if (a.getx() > 750) {
right = false;
// looping through all the aliens and moving them down
for (Alien alien : AliensArray) {
alien.moveDown();
}
break;
}
else if (a.getx() < 0) {
right = true;
// looping through all the aliens and moving them down
for (Alien alien : AliensArray) {
alien.moveDown();
}
break;
}
}
// moving the player dx pixels - should be 0 if no key is being pressed
player.move(dx);
// calling the Player's paint method
player.paint(g);
strategy.show();
}
// application entry point
public static void main(String[] args) {
InvadersApplication ia = new InvadersApplication();
}
}

View File

@ -0,0 +1,13 @@
import java.awt.*;
import javax.swing.*;
public class Player extends Sprite2D {
public Player(Image image) {
super(image);
}
// method to move the player by the supplied distance
public void move(int distance) {
x += distance; // x is inherited from the Sprite2D superclass
}
}

View File

@ -0,0 +1,23 @@
import java.awt.*;
import javax.swing.*;
public class Sprite2D {
// member data
protected int x,y;
private Image image;
public Sprite2D(Image image) {
this.image = image;
}
// paint method
public void paint(Graphics g) {
// draw the image
g.drawImage(image, x, y, null);
}
// set the position of the object
public void setPosition(int x, int y) {
this.x = x;
this.y = y; }
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.7 KiB

View File

@ -0,0 +1,55 @@
import java.awt.*;
import javax.swing.*;
public class Alien extends Sprite2D {
public static long framesDrawn = 0; // variable to hold the number of frames drawn
public static int aliensKilled = 0; // static variable to hold the count of the aliens killed - in alien class as it's intended to be modified by aliens
public static int stepSize = 0; // how far the aliens move each frame - 0 by default but will get increased to 5 immediately
private Image altImage;
public Alien(Image image, Image altImage) {
super(image);
this.altImage = altImage;
}
// method to randomly move the alien
public void move(boolean right) {
// changing the x & y co-ordinates (inherited from superclass) by either plus or minus the stepSize, depending on the direction
if (right) {
x += stepSize;
}
else {
x -= stepSize;
}
}
// method to move aliend down
public void moveDown() {
y += 25;
}
// overridden paint method to allow animation
@Override
public void paint(Graphics g) {
framesDrawn++;
if (framesDrawn % 100 < 50) {
g.drawImage(image, x, y, null);
}
else {
g.drawImage(altImage, x, y, null);
}
}
@Override
// overridden kill method to set isAlive to false and increment the aliensKilled counter
public void kill() {
isAlive = false;
aliensKilled++;
}
// method to increase the stepsize of the aliens
public static void speedUp() {
stepSize = stepSize + 3;
}
}

View File

@ -0,0 +1,315 @@
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.image.*;
import java.util.*;
public class InvadersApplication extends JFrame implements Runnable, KeyListener {
// member data
private static String workingDirectory = System.getProperty("user.dir");
private static boolean isGraphicsInitialised = false;
private Image alienImage;
private Image altAlienImage;
private Image playerImage;
private Image bulletImage;
private static final Dimension WindowSize = new Dimension(800, 600);
private static final int NUMALIENS = 30;
private Alien[] aliensArray = new Alien[NUMALIENS]; // arraylist of all the currently extant bullets
private ArrayList<PlayerBullet> bullets = new ArrayList<PlayerBullet>();
boolean right = true; // boolean to tell which direction the aliens are moving
private Player player;
private BufferStrategy strategy;
// variable to hold how far the player should be moving in the x axis per frame - default 0.
private int dx = 0;
public boolean gameOver = false; // boolean to tell what state the game is in
private long score = 0; // player's score
private long bestScore = 0; // the best score the player has achieved
// constructor
public InvadersApplication() {
// set up window
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// adding a key listener
addKeyListener(this);
// display the window. centred on the screen
Dimension screensize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
int x = screensize.width/2 - WindowSize.width/2; int y = screensize.height/2 - WindowSize.height/2;
setBounds(x, y, WindowSize.width, WindowSize.height);
// load image from disk
ImageIcon alienIcon = new ImageIcon(workingDirectory + "/alien_ship_1.png");
ImageIcon altAlienIcon = new ImageIcon(workingDirectory + "/alien_ship_2.png");
ImageIcon playerIcon = new ImageIcon(workingDirectory + "/player_ship.png");
ImageIcon bulletIcon = new ImageIcon(workingDirectory + "/bullet.png");
alienImage = alienIcon.getImage();
altAlienImage = altAlienIcon.getImage();
playerImage = playerIcon.getImage();
bulletImage = bulletIcon.getImage();
// creating a player icon & setting it's position
player = new Player(playerImage);
player.setPosition(270, 550);
setVisible(true);
createBufferStrategy(2);
strategy = getBufferStrategy();
// create a new thread & start it
Thread t = new Thread(this);
t.start();
isGraphicsInitialised = true;
}
// thread's entry point
public void run() {
while (true) {
// repainting
try {
this.repaint();
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void keyPressed(KeyEvent e) {
// what to do on keypress if the player is alive (game is being played)
if (player.isAlive) {
// getting the keycode of the event
int key = e.getKeyCode();
// if right key pressed, making the distance to be moved 10px to the right
if (key == KeyEvent.VK_RIGHT) {
dx = 5;
}
// if left key pressed, making the distance to be moved 10px to the left
if (key == KeyEvent.VK_LEFT) {
dx = -5;
}
}
// what to do on keypress if the player is not alive (game is over)
else {
// resetting everything so that the game can be restarted
resetAll();
}
}
@Override
public void keyReleased(KeyEvent e) {
// getting the keycode of the event
int key = e.getKeyCode();
// if the key released was the left or right arrow, setting dx back to 0
if (key == KeyEvent.VK_RIGHT || key == KeyEvent.VK_LEFT) {
dx = 0;
}
// if space key released "firing" a bullet (creating one)
if (key == KeyEvent.VK_SPACE) { // firing bullets on release so that the player can't just hold down space to fire
bullets.add(new PlayerBullet(player, bulletImage));
}
}
@Override
public void keyTyped(KeyEvent e) {
}
// method to reset everything so that a new game may be started
public void resetAll() {
Alien.framesDrawn = 0;
Alien.aliensKilled = 0;
Alien.stepSize = 0;
newWave();
//aliensArray = null;
player.setPosition(270, 550);
player.isAlive = true;
}
// gameplay method
public void gameplay(Graphics g) {
// checking if any of the aliens hit the edge when they were moved
for (Alien a : aliensArray) {
// changing direction & moving down if edge is hit
if (a.getX() > 750) {
right = false;
// looping through all the aliens and moving them down
for (Alien alien : aliensArray) {
alien.moveDown();
}
break;
}
else if (a.getX() < 0) {
right = true;
// looping through all the aliens and moving them down
for (Alien alien : aliensArray) {
alien.moveDown();
}
break;
}
}
// looping through each bullet, moving it, and calling its paint method if it has not collided, removing it and the alien it collided with if it has
// using an iterator so that bullets can be removed from the list as we loop through
Iterator bulletsIterator = bullets.iterator();
while (bulletsIterator.hasNext()) {
PlayerBullet b = (PlayerBullet) bulletsIterator.next();
b.move();
// bullets will be removed to save memory, but aliens will not be removed until a wave is over to preserve correct movement
for (Alien a : aliensArray) {
// only checking for collisions if the alien is alive
if (a.isAlive) {
// if the bullet collides with an alien, removing the bullet & hiding the alien
// the aliens dimensions are 50x32
// the bullets dimensions are 6x16
if (
( (a.getX() < b.getX() && a.getX()+50 > b.getX()) || (b.getX() < a.getX() && b.getX()+6 > a.getX()) ) &&
( (a.getY() < b.getY() && a.getY()+32 > b.getY()) || (b.getY() < a.getY() && b.getY()+16 > a.getY()))
) {
bulletsIterator.remove();
a.kill();
score = score + 10; // increasing the score once the alien is killed
b.kill(); // "killing" the bullet so it's not painted before it's been removed
}
}
}
// painting the bullet if it is "alive"
if (b.isAlive) {
b.paint(g);
}
}
// looping through each alien to see if it has collided with the player
for (Alien a : aliensArray) {
// checking to see if any of the (alive) aliens have collided with the player
if (a.isAlive) {
if (
// the player's dimensions are 54 x 32
( (a.getX() < player.getX() && a.getX()+50 > player.getX()) || (player.getX() < a.getX() && player.getX()+54 > a.getX()) )
&&
( (a.getY() < player.getY() && a.getY()+32 > player.getY()) || (player.getY() < a.getY() && player.getY()+32 > a.getY()))
) {
player.kill(); // "killing" the player
return;
}
}
}
// moving the player dx pixels - should be 0 if no key is being pressed
player.move(dx);
// iterating through each sprite, moving it, and calling its paint method
for (Alien a : aliensArray) {
a.move(right);
// only painting the alien if it is alive
if (a.isAlive) {
a.paint(g);
}
}
// calling the Player's paint method
player.paint(g);
}
// application's paint method
public void paint(Graphics g) {
// making sure that the graphics are initialised before painting
if (isGraphicsInitialised) {
g = strategy.getDrawGraphics();
// draw a black rectangle on the whole canvas
g.setColor(Color.BLACK);
g.fillRect(0, 0, 800, 600);
Font f = new Font("Times", Font.PLAIN, 50);
g.setColor(Color.WHITE);
// calculating score
score = Alien.aliensKilled * 10;
// displaying score and best score
String scorebar = "Score: " + score + " Best: " + bestScore;
g.drawString(scorebar, 50, 50);
// if the game is not over, playing the game. else, displaying a gameover screen.
if (player.isAlive) {
// checking if all the aliens are dead, and if so, spawning a new wave
boolean noAliens = true;
for (Alien a : aliensArray) {
if (a != null && a.isAlive) {
noAliens = false;
break;
}
}
if (noAliens) {
newWave();
}
gameplay(g);
}
else {
if (score > bestScore) {
bestScore = score;
}
String scores = "Score: " + score + " Best Score: " + bestScore;
g.drawString("GAME OVER", 400, 300);
g.drawString(scores, 400, 350);
g.drawString("Press any key to continue", 400, 400);
}
strategy.show();
}
}
// method to spawn a new wave of aliens
public void newWave() {
// deleting any existing bullets so the aliens don't spawn on top of them
bullets.clear();
// speeding up the aliens
Alien.speedUp();
// initial x and y coordinates for the aliens
int alienx = 200;
int alieny = 25;
int column = 0; // keeps track of which column the alien is in
for (int i = 0; i < NUMALIENS; i++) {
aliensArray[i] = new Alien(alienImage, altAlienImage);
aliensArray[i].setPosition(alienx, alieny);
alienx += 60;
column++;
// go onto a new line every 5 aliens
if (column >= 6) {
column = 0;
alienx = 200;
alieny += 60;
}
}
}
// application entry point
public static void main(String[] args) {
InvadersApplication ia = new InvadersApplication();
}
}

View File

@ -0,0 +1,13 @@
import java.awt.*;
import javax.swing.*;
public class Player extends Sprite2D {
public Player(Image image) {
super(image);
}
// method to move the player by the supplied distance
public void move(int distance) {
x += distance; // x is inherited from the Sprite2D superclass
}
}

View File

@ -0,0 +1,15 @@
import java.awt.*;
import javax.swing.*;
public class PlayerBullet extends Sprite2D {
public PlayerBullet(Player player, Image image) {
super(image);
x = player.getX() + 54/2;
y = player.getY();
}
// method to move the bullet up each frame
public void move() {
y = y - 10;
}
}

View File

@ -0,0 +1,41 @@
import java.awt.*;
import javax.swing.*;
public class Sprite2D {
// member data
protected int x,y;
protected Image image;
public boolean isAlive; // boolean to tell if the sprite is alive or not
public Sprite2D(Image image) {
this.image = image;
this.isAlive = true;
}
// paint method
public void paint(Graphics g) {
// draw the image
g.drawImage(image, x, y, null);
}
// set the position of the object
public void setPosition(int x, int y) {
this.x = x;
this.y = y;
}
// getter method for the sprite's x position
public int getX() {
return x;
}
// getter method for the sprite's y position
public int getY() {
return y;
}
// method to set isAlive to false
public void kill() {
isAlive = false;
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.7 KiB

View File

@ -0,0 +1 @@
- Multiple games

View File

@ -0,0 +1,88 @@
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.image.*;
import java.util.*;
public class GameOfLife extends JFrame implements Runnable, MouseListener {
private static final Dimension WindowSize = new Dimension(800, 800);
private BufferStrategy strategy;
private static boolean isGraphicsInitialised = false;
private boolean gameState[][] = new boolean[40][40];
public GameOfLife() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// display the window. centred on the screen
Dimension screensize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
int x = screensize.width/2 - WindowSize.width/2; int y = screensize.height/2 - WindowSize.height/2;
setBounds(x, y, WindowSize.width, WindowSize.height);
setVisible(true);
createBufferStrategy(2);
strategy = getBufferStrategy();
// create a new thread & start it
Thread t = new Thread(this);
t.start();
// adding mouse listener
addMouseListener(this);
isGraphicsInitialised = true;
}
// entry point of the application
public static void main(String[] args) {
GameOfLife game = new GameOfLife();
}
public void run() {
while (true) {
// repainting
try {
this.repaint();
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
// application's paint method
public void paint(Graphics g) {
// making sure that the graphics are initialised before painting
if (isGraphicsInitialised) {
g = strategy.getDrawGraphics();
// draw a white rectangle on the whole canvas
g.setColor(Color.WHITE);
g.fillRect(0, 0, 800, 800);
// looping through the gameState array
for (int i = 0; i < 40; i++) {
for (int j = 0; j < 40; j++) {
// drawing a black square if true
if (gameState[i][j]) {
g.setColor(Color.BLACK);
g.fillRect(i * 20, j * 20, 20, 20);
}
}
}
strategy.show();
}
}
public void mousePressed(MouseEvent e) { }
public void mouseReleased(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
// mouse click event
public void mouseClicked(MouseEvent e) {
// setting boolean corresponding to the square that was clicked to true
gameState[(int) Math.floor(e.getX() / 20)][(int) Math.floor(e.getY() / 20)] = true;
}
}

View File

@ -0,0 +1,171 @@
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import java.util.*;
public class GameOfLife extends JFrame implements Runnable, MouseListener {
private static final Dimension WindowSize = new Dimension(800, 800);
private BufferStrategy strategy;
private static boolean isGraphicsInitialised = false;
private boolean gameState[][][] = new boolean[40][40][2];
private static boolean isPlaying = false;
public GameOfLife() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// display the window. centred on the screen
Dimension screensize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
int x = screensize.width/2 - WindowSize.width/2; int y = screensize.height/2 - WindowSize.height/2;
setBounds(x, y, WindowSize.width, WindowSize.height);
setVisible(true);
createBufferStrategy(2);
strategy = getBufferStrategy();
// create a new thread & start it
Thread t = new Thread(this);
t.start();
// adding mouse listener
addMouseListener(this);
isGraphicsInitialised = true;
}
// entry point of the application
public static void main(String[] args) {
GameOfLife game = new GameOfLife();
}
public void run() {
while (true) {
// repainting
try {
this.repaint();
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
// application's paint method
public void paint(Graphics g) {
// making sure that the graphics are initialised before painting
if (isGraphicsInitialised) {
g = strategy.getDrawGraphics();
// draw a white rectangle on the whole canvas
g.setColor(Color.BLACK);
g.fillRect(0, 0, WindowSize.width, WindowSize.height);
// looping through the gameState array
for (int i = 0; i < 40; i++) {
for (int j = 0; j < 40; j++) {
// drawing a white square if true
if (gameState[i][j][0]) {
g.setColor(Color.WHITE);
g.fillRect(i * 20, j * 20, 20, 20);
}
}
}
if (isPlaying) {
for (int x = 0; x < 40; x++) {
for (int y = 0; y < 40; y++) {
// count the live neighbours of cell [x][y][0]
int numLiveNeighbours = 0;
for (int xx = -1 ; xx <= 1; xx++) {
for (int yy = -1; yy <= 1; yy++) {
if (xx != 0 || yy != 0) {
// check cell [x+xx][y+yy][0]
// if x+xx or y+yy is out of bounds, getting the inverse of it modulo 40
int newX = (x+xx >= 0) ? (x+xx) % 40 : 40 + x+xx;
int newY = (y+yy >= 0) ? (y+yy) % 40 : 40 + y+yy;
if (gameState[newX][newY][0]) {
numLiveNeighbours++;
}
}
}
}
// killing the cell if it is alive and has fewer than two live neighbours or greater than 3 live neighbours
if (gameState[x][y][0] && (numLiveNeighbours < 2 || numLiveNeighbours > 3)) {
gameState[x][y][1] = false;
}
// bringing the cell back to life it is dead and has exactly 3 live neighbours
else if (!gameState[x][y][0] && numLiveNeighbours == 3) {
gameState[x][y][1] = true;
}
}
}
// making the "front buffer" equal to the "back buffer"
for (int x = 0; x < 40; x++) {
for (int y = 0; y < 40; y++) {
gameState[x][y][0] = gameState[x][y][1];
}
}
}
// if the game is not being played, drawing the buttons
else {
Font f = new Font("Times", Font.PLAIN, 12);
g.setFont(f);
FontMetrics fm = getFontMetrics(f);
// drawing the two buttons
g.setColor(Color.GREEN);
g.fillRect(20, 20, 60, 20);
g.fillRect(100, 20, 60, 20);
g.setColor(Color.BLACK);
g.drawString("Start", 35, 35);
g.drawString("Random", 105, 35);
}
strategy.show();
}
}
public void mousePressed(MouseEvent e) { }
public void mouseReleased(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
// mouse click event
public void mouseClicked(MouseEvent e) {
// if (!isPlaying) {
// if start button was pressed, switching to the playing game state
if ((e.getX() >= 20 && e.getX() <= 80) && (e.getY() >= 20 && e.getY() <= 40)) {
isPlaying = true;
}
// else if the random button was pressed, calling the random method
else if ((e.getX() >= 100 && e.getX() <= 160) && (e.getY() >= 20 && e.getY() <= 40)) {
random();
}
// else, setting the clicked square to true (white)
else {
// setting boolean corresponding to the square that was clicked to true
gameState[(int) Math.floor(e.getX() / 20)][(int) Math.floor(e.getY() / 20)][0] = true;
}
// }
}
// method to generate a random starting state
public void random() {
// looping through the gameState array
for (int i = 0; i < 40; i++) {
for (int j = 0; j < 40; j++) {
// setting the square to alive with a probability of 1 in 10
if (((int) (Math.random() * 10)) == 1) {
gameState[i][j][0] = true;
}
}
}
}
}

View File

@ -0,0 +1,256 @@
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
public class GameOfLife extends JFrame implements Runnable, MouseListener, MouseMotionListener {
private static String workingDirectory = System.getProperty("user.dir");
private static final Dimension WindowSize = new Dimension(800, 800);
private BufferStrategy strategy;
private static boolean isGraphicsInitialised = false;
private boolean gameState[][][] = new boolean[40][40][2];
private static boolean isPlaying = false;
public GameOfLife() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// display the window. centred on the screen
Dimension screensize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
int x = screensize.width/2 - WindowSize.width/2; int y = screensize.height/2 - WindowSize.height/2;
setBounds(x, y, WindowSize.width, WindowSize.height);
setVisible(true);
createBufferStrategy(2);
strategy = getBufferStrategy();
// create a new thread & start it
Thread t = new Thread(this);
t.start();
// adding mouse listener
addMouseListener(this);
addMouseMotionListener(this);
isGraphicsInitialised = true;
}
// entry point of the application
public static void main(String[] args) {
GameOfLife game = new GameOfLife();
}
public void run() {
while (true) {
// repainting
try {
this.repaint();
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
// application's paint method
public void paint(Graphics g) {
// making sure that the graphics are initialised before painting
if (isGraphicsInitialised) {
g = strategy.getDrawGraphics();
// draw a white rectangle on the whole canvas
g.setColor(Color.BLACK);
g.fillRect(0, 0, WindowSize.width, WindowSize.height);
// looping through the gameState array
for (int i = 0; i < 40; i++) {
for (int j = 0; j < 40; j++) {
// drawing a white square if true
if (gameState[i][j][0]) {
g.setColor(Color.WHITE);
g.fillRect(i * 20, j * 20, 20, 20);
}
}
}
if (isPlaying) {
for (int x = 0; x < 40; x++) {
for (int y = 0; y < 40; y++) {
// count the live neighbours of cell [x][y][0]
int numLiveNeighbours = 0;
for (int xx = -1 ; xx <= 1; xx++) {
for (int yy = -1; yy <= 1; yy++) {
if (xx != 0 || yy != 0) {
// check cell [x+xx][y+yy][0]
// if x+xx or y+yy is out of bounds, getting the inverse of it modulo 40
int newX = (x+xx >= 0) ? (x+xx) % 40 : 40 + x+xx;
int newY = (y+yy >= 0) ? (y+yy) % 40 : 40 + y+yy;
if (gameState[newX][newY][0]) {
numLiveNeighbours++;
}
}
}
}
// killing the cell if it is alive and has fewer than two live neighbours or greater than 3 live neighbours
if (gameState[x][y][0] && (numLiveNeighbours < 2 || numLiveNeighbours > 3)) {
gameState[x][y][1] = false;
}
// bringing the cell back to life it is dead and has exactly 3 live neighbours
else if (!gameState[x][y][0] && numLiveNeighbours == 3) {
gameState[x][y][1] = true;
}
}
}
// making the "front buffer" equal to the "back buffer"
for (int x = 0; x < 40; x++) {
for (int y = 0; y < 40; y++) {
gameState[x][y][0] = gameState[x][y][1];
}
}
}
// if the game is not being played, drawing the buttons
else {
Font f = new Font("Times", Font.PLAIN, 12);
g.setFont(f);
FontMetrics fm = getFontMetrics(f);
// drawing the two buttons
g.setColor(Color.GREEN);
g.fillRect( 20, 20, 60, 20);
g.fillRect(100, 20, 60, 20);
g.fillRect(180, 20, 60, 20);
g.fillRect(260, 20, 60, 20);
g.setColor(Color.BLACK);
g.drawString("Start", 35, 35);
g.drawString("Random", 105, 35);
g.drawString("Save", 185, 35);
g.drawString("Load", 265, 35);
}
strategy.show();
}
}
public void mousePressed(MouseEvent e) { }
public void mouseReleased(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
public void mouseMoved(MouseEvent e) { }
public void mouseDragged(MouseEvent e) {
// checking that index is in bounds before drawing
if (e.getX() < 800 && e.getX() >= 0 && e.getY() < 800 && e.getY() >= 0) {
gameState[(int) Math.floor(e.getX() / 20)][(int) Math.floor(e.getY() / 20)][0] = true;
}
}
// mouse click event
public void mouseClicked(MouseEvent e) {
// if (!isPlaying) {
// if start button was pressed, switching to the playing game state
if ((e.getX() >= 20 && e.getX() <= 80) && (e.getY() >= 20 && e.getY() <= 40)) {
isPlaying = true;
}
// else if the random button was pressed, calling the random method
else if ((e.getX() >= 100 && e.getX() <= 160) && (e.getY() >= 20 && e.getY() <= 40)) {
random();
}
// else if the save button was clicked
else if ((e.getX() >= 180 && e.getX() <= 240) && (e.getY() >= 20 && e.getY() <= 40)) {
save();
}
// else if the load button was clicked
else if ((e.getX() >= 260 && e.getX() <= 320) && (e.getY() >= 20 && e.getY() <= 40)) {
load();
}
// else, setting the clicked square to true (white)
else {
// setting boolean corresponding to the square that was clicked to true
gameState[(int) Math.floor(e.getX() / 20)][(int) Math.floor(e.getY() / 20)][0] = true;
}
// }
}
// method to generate a random starting state
public void random() {
// looping through the gameState array
for (int i = 0; i < 40; i++) {
for (int j = 0; j < 40; j++) {
// setting the square to alive with a probability of 1 in 10
if (((int) (Math.random() * 10)) == 1) {
gameState[i][j][0] = true;
}
}
}
}
// method to save the gamestate to a txt file
public void save() {
// string that will be written to the file
StringBuilder output = new StringBuilder();
// looping through each index in the array
for (int i = 0; i < 40; i++) {
for (int j = 0; j < 40; j++) {
// appending a 1 if true, 0 if false
if (gameState[i][j][0]) {
output.append("1");
}
else {
output.append("0");
}
}
}
// writing the string to the file
String filename = workingDirectory + "/gamestate.txt";
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(filename));
writer.write(output.toString());
writer.close();
}
catch (IOException e) {}
}
// method to load the gamestate from a txt file
public void load() {
String filename = workingDirectory + "/gamestate.txt";
String loadedtext = "";
String line = null;
try {
BufferedReader reader = new BufferedReader(new FileReader(filename));
do {
try {
line = reader.readLine();
// appending to loadedtext if not null
if (line != null) {
loadedtext += line;
}
} catch (IOException e) {}
} while (line != null);
} catch (IOException e) {}
// looping through each element of the front buffer and assigning true or false if the next char is 1 or 0
for (int i = 0; i < 40; i++) {
for (int j = 0; j < 40; j++) {
// getting the character that corresponds to the index of the array
if (loadedtext.charAt(40*i + j) == '1') {
gameState[i][j][0] = true;
}
else {
gameState[i][j][0] = false;
}
}
}
}
}

View File

@ -0,0 +1 @@
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000001100000000000000000000000000000000000011100000000000000000000000000000000001110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011111000000000000000000000000000000000001000100000000000000000000000000000000001100010000000000000000000000000000011111110001000000000000000000000000000011000000101100000000000000000000000000011000000011000000000000000000000000000001000000001110000000000000000000000000000100000001101000000000000000000000000000001110001100100000000000000000000000000000001111100110000000000000000000000000000000000011110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

View File

@ -0,0 +1,273 @@
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.image.*;
import java.io.*;
public class AStarMaze extends JFrame implements Runnable, MouseListener, MouseMotionListener, KeyListener {
// member data
private static final Dimension WindowSize = new Dimension(800, 800);
private boolean isInitialised = false;
private BufferStrategy strategy;
private Graphics offscreenBuffer;
private static boolean map[][] = new boolean[40][40];
private boolean isGameRunning = false;
private BadGuy badguy;
private Player player;
private String FilePath;
// constructor
public AStarMaze() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Display the window, centred on the screen
Dimension screensize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
int x = screensize.width/2 - WindowSize.width/2; int y = screensize.height/2 - WindowSize.height/2;
setBounds(x, y, WindowSize.width, WindowSize.height);
setVisible(true);
this.setTitle("A* Pathfinding Demo");
FilePath = System.getProperty("user.dir") + "/";
// load raster graphics and instantiate game objects
ImageIcon icon = new ImageIcon(FilePath+"badguy.png");
Image img = icon.getImage();
badguy = new BadGuy(img);
icon = new ImageIcon(FilePath+"player.png");
img = icon.getImage();
player = new Player(img);
// initialise double-buffering
createBufferStrategy(2);
strategy = getBufferStrategy();
// create and start our animation thread
Thread t = new Thread(this);
t.start();
// register the Jframe itself to receive mouse and keyboard events
addMouseListener(this);
addMouseMotionListener(this);
addKeyListener(this);
// initialise the map state
for (x=0;x<40;x++) {
for (y=0;y<40;y++) {
map[x][y]=false;
}
}
isInitialised = true;
}
// thread's entry point
public void run() {
long loops = 0;
while (true) {
// 1: sleep for 1/5 sec
try {
Thread.sleep(200);
} catch (InterruptedException e) { }
// 2: animate game objects
if (isGameRunning) {
loops++;
player.move(map); // player moves every frame
// recalculating the badguys path every move, highly inefficient but easier to program lol
badguy.reCalcPath(map, player.x, player.y);
if (loops%10==0) // badguy moves once every 3 frames
badguy.move(map,player.x,player.y);
}
// 3: force an application repaint
this.repaint();
}
}
private void loadMaze() {
String filename = FilePath+"maze.txt";
String textinput = null;
try {
BufferedReader reader = new BufferedReader(new FileReader(filename));
textinput = reader.readLine();
reader.close();
}
catch (IOException e) { }
if (textinput!=null) {
for (int x=0;x<40;x++) {
for (int y=0;y<40;y++) {
map[x][y] = (textinput.charAt(x*40+y)=='1');
}
}
}
}
private void saveMaze() {
// pack maze into a string
String outputtext="";
for (int x=0;x<40;x++) {
for (int y=0;y<40;y++) {
if (map[x][y])
outputtext+="1";
else
outputtext+="0";
}
}
try {
String filename = FilePath+"maze.txt";
BufferedWriter writer = new BufferedWriter(new FileWriter(filename));
writer.write(outputtext);
writer.close();
}
catch (IOException e) { }
}
// mouse events which must be implemented for MouseListener
public void mousePressed(MouseEvent e) {
if (!isGameRunning) {
// was the click on the 'start button'?
int x = e.getX();
int y = e.getY();
if (x>=15 && x<=85 && y>=40 && y<=70) {
isGameRunning=true;
return;
}
// or the 'load' button?
if (x>=315 && x<=385 && y>=40 && y<=70) {
loadMaze();
return;
}
// or the 'save' button?
if (x>=415 && x<=485 && y>=40 && y<=70) {
saveMaze();
return;
}
}
// determine which cell of the gameState array was clicked on
int x = e.getX()/20;
int y = e.getY()/20;
// toggle the state of the cell
map[x][y] = !map[x][y];
// throw an extra repaint, to get immediate visual feedback
this.repaint();
// store mouse position so that each tiny drag doesn't toggle the cell
// (see mouseDragged method below)
prevx=x;
prevy=y;
}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
//
// mouse events which must be implemented for MouseMotionListener
public void mouseMoved(MouseEvent e) {}
// mouse position on previous mouseDragged event
// must be member variables for lifetime reasons
int prevx=-1, prevy=-1;
public void mouseDragged(MouseEvent e) {
// determine which cell of the gameState array was clicked on
// and make sure it has changed since the last mouseDragged event
if (e.getX() < 800 && e.getX() >= 0 && e.getY() < 800 && e.getY() >= 0) {
int x = e.getX()/20;
int y = e.getY()/20;
if (x!=prevx || y!=prevy) {
// toggle the state of the cell
map[x][y] = !map[x][y];
// throw an extra repaint, to get immediate visual feedback
this.repaint();
// store mouse position so that each tiny drag doesn't toggle the cell
prevx=x;
prevy=y;
}
}
badguy.reCalcPath(map, player.x, player.y);
}
// Keyboard events
public void keyPressed(KeyEvent e) {
if (e.getKeyCode()==KeyEvent.VK_LEFT)
player.setXSpeed(-1);
else if (e.getKeyCode()==KeyEvent.VK_RIGHT)
player.setXSpeed(1);
else if (e.getKeyCode()==KeyEvent.VK_UP)
player.setYSpeed(-1);
else if (e.getKeyCode()==KeyEvent.VK_DOWN)
player.setYSpeed(1);
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode()==KeyEvent.VK_LEFT || e.getKeyCode()==KeyEvent.VK_RIGHT)
player.setXSpeed(0);
else if (e.getKeyCode()==KeyEvent.VK_UP || e.getKeyCode()==KeyEvent.VK_DOWN)
player.setYSpeed(0);
}
public void keyTyped(KeyEvent e) { }
// application's paint method
public void paint(Graphics g) {
if (!isInitialised)
return;
// g = offscreenBuffer; // draw to offscreen buffer
g = strategy.getDrawGraphics();
// clear the canvas with a big black rectangle
g.setColor(Color.BLACK);
g.fillRect(0, 0, 800, 800);
// redraw the map
g.setColor(Color.WHITE);
for (int x=0;x<40;x++) {
for (int y=0;y<40;y++) {
if (map[x][y]) {
g.fillRect(x*20, y*20, 20, 20);
}
}
}
// redraw the player and badguy
// paint the game objects
player.paint(g);
badguy.paint(g);
if (!isGameRunning) {
// game is not running..
// draw a 'start button' as a rectangle with text on top
// also draw 'load' and 'save' buttons
g.setColor(Color.GREEN);
g.fillRect(15, 40, 70, 30);
g.fillRect(315, 40, 70, 30);
g.fillRect(415, 40, 70, 30);
g.setFont(new Font("Times", Font.PLAIN, 24));
g.setColor(Color.BLACK);
g.drawString("Start", 22, 62);
g.drawString("Load", 322, 62);
g.drawString("Save", 422, 62);
}
// flip the buffers
strategy.show();
}
// application entry point
public static void main(String[] args) {
AStarMaze w = new AStarMaze();
}
}

View File

@ -0,0 +1,298 @@
import java.awt.*;
import java.util.*;
public class BadGuy {
/* list of nodes to which the algorithm has already found a route (i.e., one of its conencted neighbours has been expanded)
* but have not themselves been expanded */
LinkedList<Node> openlist = new LinkedList<Node>();
// list of nodes that have been expanded and which therefore should not be revisited
LinkedList<Node> closedlist = new LinkedList<Node>();
Node[][] allnodes = new Node[40][40] ; // array of all the nodes
Stack<Node> finalpath = new Stack<Node>();
Image myImage;
int x=0,y=0;
boolean hasPath=false;
public BadGuy( Image i ) {
myImage=i;
x = 30;
y = 10;
}
public void reCalcPath(boolean map[][],int targx, int targy) {
System.out.println();
System.out.println("recalculating path");
hasPath = false;
openlist.clear();
closedlist.clear();
finalpath.clear();
// looping through map[][], generating each node, and marking each wall node as closed
for (int i = 0; i < 40; i++) {
for (int j = 0; j < 40; j++) {
allnodes[i][j] = new Node(i, j); // generating node
if (map[i][j]) {
allnodes[i][j].closed = true;
allnodes[i][j].open = false;
closedlist.add(allnodes[i][j]);
}
}
}
// calculate f,g,h for the starting node and set to open
Node starting = allnodes[x][y];
starting.g = 0;
starting.h = targx - x + targy - y; // manhattan distance
starting.f = starting.g + starting.h;
starting.open = true;
starting.closed = false;
openlist.add(starting);
// looping while a path has not been found
// end condition: if a neighbour is the target, or if there are no open nodes
while (!hasPath) {
// breaking if there are no open nodes
if (openlist.size() == 0) {
break;
}
/* progress is made by identifying the most promising node in the open list (i.e., the one with lowest f value) and
* expanding it by adding each of its connected neighbours to the open list, unless they are already closed. */
// looping through open list to find most promising node
Node mostpromising = openlist.get(0);
for (int i = 1; i < openlist.size(); i++) {
if (openlist.get(i).f < mostpromising.f) {
mostpromising = openlist.get(i);
}
}
int mx = mostpromising.x;
int my = mostpromising.y;
// expanding the most promising node by adding each of its connected neighbours to the open list, unless they are already closed
// as nodes are added to the open list, their f, g, h, & parent values are recorded
// the g value of a node is equal to the g value of its parent + the cost of moving from the parent to the node itself. t
// as nodes are expanded, they are moved to the closed list
mostpromising.open = false;
mostpromising.closed = true;
closedlist.add(mostpromising);
// openlist.remove(mostpromising);
// northwest neighbour
if (mx-1 >= 0 && my-1 >= 0 && !allnodes[mx-1][my-1].closed) {
// checking if this node is the target node, and breaking if so
if (mx-1 == targx && my-1 == targy) {
allnodes[mx-1][my-1].open = true;
allnodes[mx-1][my-1].closed = false;
closedlist.remove(allnodes[mx-1][my-1]);
openlist.add(allnodes[mx-1][my-1]);
break;
}
if (!allnodes[mx-1][my-1].open) {
allnodes[mx-1][my-1].g = mostpromising.g + 1;
allnodes[mx-1][my-1].h = targx - mx + targy - my;
allnodes[mx-1][my-1].f = allnodes[mx-1][my-1].g + allnodes[mx-1][my-1].h;
allnodes[mx-1][my-1].open = true;
allnodes[mx-1][my-1].closed = false;
openlist.add(allnodes[mx-1][my-1]);
}
}
// north neighbour
if (my-1 >= 0 && !allnodes[mx][my-1].closed) {
// checking if this node is the target node, and breaking if so
if (mx == targx && my-1 == targy) {
allnodes[mx][my-1].open = true;
allnodes[mx][my-1].closed = false;
closedlist.remove(allnodes[mx][my-1]);
openlist.add(allnodes[mx][my-1]);
break;
}
if (!allnodes[mx][my-1].open) {
allnodes[mx][my-1].g = mostpromising.g + 1;
allnodes[mx][my-1].h = targx - mx + targy - my;
allnodes[mx][my-1].f = allnodes[mx][my-1].g + allnodes[mx][my-1].h;
allnodes[mx][my-1].open = true;
allnodes[mx][my-1].closed = false;
openlist.add(allnodes[mx][my-1]);
}
}
// northeast neighbour
if (mx+1 < 40 && my-1 >= 0 && !allnodes[mx+1][my-1].closed) {
// checking if this node is the target node, and breaking if so
if (mx+1 == targx && my-1 == targy) {
allnodes[mx+1][my-1].open = true;
allnodes[mx+1][my-1].closed = false;
closedlist.remove(allnodes[mx+1][my-1]);
openlist.add(allnodes[mx+1][my-1]);
break;
}
if (!allnodes[mx+1][my-1].open && !allnodes[mx+1][my-1].closed) {
allnodes[mx+1][my-1].g = mostpromising.g + 1;
allnodes[mx+1][my-1].h = targx - mx+1 + targy - my;
allnodes[mx+1][my-1].f = allnodes[mx+1][my-1].g + allnodes[mx+1][my-1].h;
allnodes[mx+1][my-1].open = true;
allnodes[mx+1][my-1].closed = false;
openlist.add(allnodes[mx+1][my-1]);
}
}
// west neighbour
if (mx-1 >= 0 && !allnodes[mx-1][my].closed) {
// checking if this node is the target node, and breaking if so
if (mx-1 == targx && my == targy) {
allnodes[mx-1][my].open = true;
allnodes[mx-1][my].closed = false;
closedlist.remove(allnodes[mx-1][my]);
openlist.add(allnodes[mx-1][my]);
break;
}
if (!allnodes[mx-1][my].open && !allnodes[mx-1][my].closed) {
allnodes[mx-1][my].g = mostpromising.g + 1;
allnodes[mx-1][my].h = targx - mx + targy - my;
allnodes[mx-1][my].f = allnodes[mx-1][my].g + allnodes[mx-1][my].h;
allnodes[mx-1][my].open = true;
allnodes[mx-1][my].closed = false;
openlist.add(allnodes[mx-1][my]);
}
}
// east neighbour
if (mx+1 < 40 && !allnodes[mx+1][my].closed) {
// checking if this node is the target node, and breaking if so
if (mx+1 == targx && my == targy) {
allnodes[mx+1][my].open = true;
allnodes[mx+1][my].closed = false;
closedlist.remove(allnodes[mx+1][my]);
openlist.add(allnodes[mx+1][my]);
break;
}
if (!allnodes[mx+1][my].open) {
allnodes[mx+1][my].g = mostpromising.g + 1;
allnodes[mx+1][my].h = targx - mx+1 + targy - my;
allnodes[mx+1][my].f = allnodes[mx+1][my].g + allnodes[mx+1][my].h;
allnodes[mx+1][my].open = true;
allnodes[mx+1][my].closed = false;
openlist.add(allnodes[mx+1][my]);
}
}
// southwest neighbour
if (mx-1 >= 0 && my+1 < 40 && !allnodes[mx-1][my+1].closed) {
// checking if this node is the target node, and breaking if so
if (mx-1 == targx && my+1 == targy) {
allnodes[mx-1][my+1].open = true;
allnodes[mx-1][my+1].closed = false;
closedlist.remove(allnodes[mx-1][my+1]);
openlist.add(allnodes[mx-1][my+1]);
break;
}
if (!allnodes[mx-1][my+1].open) {
allnodes[mx-1][my+1].g = mostpromising.g + 1;
allnodes[mx-1][my+1].h = targx - mx + targy - my;
allnodes[mx-1][my+1].f = allnodes[mx-1][my+1].g + allnodes[mx-1][my+1].h;
allnodes[mx-1][my+1].open = true;
allnodes[mx-1][my+1].closed = false;
openlist.add(allnodes[mx-1][my+1]);
}
}
// south neighbour
if (my+1 < 40 && !allnodes[mx][my+1].closed) {
// checking if this node is the target node, and breaking if so
if (mx == targx && my+1 == targy) {
allnodes[mx][my+1].open = true;
allnodes[mx][my+1].closed = false;
closedlist.remove(allnodes[mx][my+1]);
openlist.add(allnodes[mx][my+1]);
break;
}
if (!allnodes[mx][my+1].open) {
allnodes[mx][my+1].g = mostpromising.g + 1;
allnodes[mx][my+1].h = targx - mx + targy - my;
allnodes[mx][my+1].f = allnodes[mx][my+1].g + allnodes[mx][my+1].h;
allnodes[mx][my+1].open = true;
allnodes[mx][my+1].closed = false;
openlist.add(allnodes[mx][my+1]);
}
}
// southeast neighbour
if (mx+1 < 40 && my+1 < 40 && !allnodes[mx+1][my+1].closed) {
// checking if this node is the target node, and breaking if so
if (mx+1 == targx && my+1 == targy) {
allnodes[mx+1][my+1].open = true;
allnodes[mx+1][my+1].closed = false;
closedlist.remove(allnodes[mx+1][my+1]);
openlist.add(allnodes[mx+1][my+1]);
break;
}
if (!allnodes[mx+1][my+1].open) {
allnodes[mx+1][my+1].g = mostpromising.g + 1;
allnodes[mx+1][my+1].h = targx - mx+1 + targy - my;
allnodes[mx+1][my+1].f = allnodes[mx+1][my+1].g + allnodes[mx+1][my+1].h;
allnodes[mx+1][my+1].open = true;
allnodes[mx+1][my+1].closed = false;
openlist.add(allnodes[mx+1][my+1]);
}
}
}
// generate final path by pushing target onto stack, followed by its parent in closedlist, ..., followed by start node
for (int i = openlist.size()-1; i >= 0; i--) {
System.out.println("pushing x=" + openlist.get(i).x + " y =" + openlist.get(i).y);
finalpath.push(openlist.get(i));
}
hasPath = true;
return;
}
public void move(boolean map[][],int targx, int targy) {
if (hasPath) {
Node nextnode = finalpath.pop();
System.out.println("next node x=" + nextnode.x + " y=" + nextnode.y);
x = nextnode.x;
y = nextnode.y;
}
else {
// no path known, so just do a dumb 'run towards' behaviour
int newx=x, newy=y;
if (targx<x)
newx--;
else if (targx>x)
newx++;
if (targy<y)
newy--;
else if (targy>y)
newy++;
if ((newx < 40 && newx >= 0 && newy < 40 && newy >=0) && !map[newx][newy]) {
x=newx;
y=newy;
}
}
}
public void paint(Graphics g) {
g.drawImage(myImage, x*20, y*20, null);
}
}

View File

@ -0,0 +1,15 @@
public class Node {
public int x;
public int y;
public int g; // cost of getting from the starting node to this node
public int h; // estimated heuristic cost of getting from this node to the target node
public int f; // sum of g & h, the algorithm's best current estimate as to the total cost of travelling from the starting location to the target location via this node
public boolean closed = false;
public boolean open = false;
public Node(int x, int y) {
this.x = x;
this.y = y;
}
}

View File

@ -0,0 +1,42 @@
import java.awt.Graphics;
import java.awt.Image;
public class Player {
Image myImage;
int x=0,y=0;
int xSpeed=0, ySpeed=0;
public Player( Image i ) {
myImage=i;
x=10;
y=35;
}
public void setXSpeed( int x ) {
xSpeed=x;
}
public void setYSpeed( int y ) {
ySpeed=y;
}
public void move(boolean map[][]) {
int newx=x+xSpeed;
int newy=y+ySpeed;
// making sure that the newx & newy are not off the map or blocked
// if (e.getX() < 800 && e.getX() >= 0 && e.getY() < 800 && e.getY() >= 0) {
if ((newx < 40 && newx >= 0 && newy < 40 && newy >=0) && !map[newx][newy]) {
x=newx;
y=newy;
}
}
public void paint(Graphics g) {
g.drawImage(myImage, x*20, y*20, null);
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

@ -0,0 +1 @@
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011000000000000000000000000000000000000001100000000000000000000000000000000000000110000000000000000000000000000000000000001000000000000000000000000000000000000000100000000000000000000000000000000000000010000000000000000000000000000000000000011000000000000000000000011111111111111111100000000000000000000000000000000000000110000000000000000000000000000000000000001000000000000000000000000000000000000000100000000000000000000000000000000000000010000000000000000000000000000000000000011000000000000000000000000000000000000001100000000000000000000000000000000000000110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

View File

@ -0,0 +1,110 @@
import java.awt.*;
import javax.swing.*;
public class Cave extends JFrame implements Runnable {
private static final Dimension WindowSize = new Dimension(800, 800);
private static boolean isGraphicsInitialised = false;
private boolean map[][][] = new boolean[200][200][2];
public Cave() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// randomly deciding whether each position in the map is a wall (true) or floor (false)
for (int i = 0; i < 200; i++) {
for (int j = 0; j < 200; j++) {
// setting the square to wall with a probability of 60%
map[i][j][0] = Math.random() < 0.6;
}
}
// display the window. centred on the screen
Dimension screensize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
int x = screensize.width/2 - WindowSize.width/2; int y = screensize.height/2 - WindowSize.height/2;
setBounds(x, y, WindowSize.width, WindowSize.height);
setVisible(true);
// create a new thread & start it
Thread t = new Thread(this);
t.start();
isGraphicsInitialised = true;
}
public void run() {
if (isGraphicsInitialised) {
// looping 5 times to show the initial state also, but re-running the cave procedure 4 times
for (int i = 0; i <= 4; i++){
// repainting
try {
this.repaint();
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public void paint (Graphics g) {
// draw a white rectangle on the whole canvas
g.setColor(Color.BLACK);
g.fillRect(0, 0, WindowSize.width, WindowSize.height);
// looping through map and drawing a white square if floor (true)
for (int i = 0; i < 200; i++) {
for (int j = 0; j < 200; j++) {
// drawing a white square if true
if (map[i][j][0]) {
g.setColor(Color.WHITE);
g.fillRect(i * 4, j * 4, 4, 4);
}
}
}
// looping through array to make clumping happen
for (int x = 0; x < 200; x++) {
for (int y = 0; y < 200; y++) {
// count the wall neighbours of cell [x][y]
int numWallNeighbours = 0;
// looping through neighbour cells
for (int xx = -1 ; xx <= 1; xx++) {
for (int yy = -1; yy <= 1; yy++) {
if (xx != 0 || yy != 0) {
// check cell [x+xx][y+yy]()
// if x+xx or y+yy is out of bounds, getting the inverse of it modulo 200
int newX = (x+xx >= 0) ? (x+xx) % 200 : 200 + x+xx;
int newY = (y+yy >= 0) ? (y+yy) % 200 : 200 + y+yy;
// if the neighbour is a wall, increasing the count
if (map[newX][newY][0]) {
numWallNeighbours++;
}
}
}
}
// defining each cell that has at least 5 neighbouring wall cells as a wall itself
if (numWallNeighbours >= 5) {
map[x][y][1] = true;
}
// otherwise defining it as a floor
else {
map[x][y][1] = false;
}
}
}
// moving back buffer to front
for (int x = 0; x < 200; x++) {
for (int y = 0; y < 200; y++) {
map[x][y][0] = map[x][y][1];
}
}
}
public static void main(String[] args) {
Cave cave = new Cave();
}
}