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,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