Add second year
This commit is contained in:
31
second/semester2/CT255/Assignments/Assigment-04/Alien.java
Normal file
31
second/semester2/CT255/Assignments/Assigment-04/Alien.java
Normal 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;
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
13
second/semester2/CT255/Assignments/Assigment-04/Player.java
Normal file
13
second/semester2/CT255/Assignments/Assigment-04/Player.java
Normal 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
|
||||
}
|
||||
}
|
@ -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; }
|
||||
}
|
BIN
second/semester2/CT255/Assignments/Assigment-04/alien_ship_1.png
Executable file
BIN
second/semester2/CT255/Assignments/Assigment-04/alien_ship_1.png
Executable file
Binary file not shown.
After Width: | Height: | Size: 3.3 KiB |
BIN
second/semester2/CT255/Assignments/Assigment-04/player_ship.png
Executable file
BIN
second/semester2/CT255/Assignments/Assigment-04/player_ship.png
Executable file
Binary file not shown.
After Width: | Height: | Size: 3.7 KiB |
Reference in New Issue
Block a user