Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Description: An applet that runs a space shooter game (Method: Enemy)
- *Version: 1.0
- *Date: December 13, 2018
- *Author: Sean Feigis
- */
- import java.awt.Graphics;
- import java.awt.Image;
- import java.awt.Rectangle;
- import java.awt.Color;
- import java.util.Random;
- import javax.swing.ImageIcon;
- public class Enemy {
- // Variables
- Image img = new ImageIcon(this.getClass().getResource("/enemy.png")).getImage();
- private Graphics graph;
- int y_pos; // y position of the enemy
- int x_pos; // x position of the enemy
- Rectangle recEnemy = new Rectangle(x_pos, y_pos, 50, 22); // Rectangle for collision of the enemy
- // Constructors
- Enemy(int x, int y) {
- x_pos = x;
- y_pos = y;
- }// end constructor
- // METHODS:
- // GetX - returns the value of X
- public int getx() {
- return x_pos; // returns x_pos
- }// end method getx
- // Input: None
- // Returns the value of x_pos
- // Output: returns x_pos
- // SetX - Changes the value of X to move to left or right
- public void xleft() {
- x_pos -= 2; // change the value of x_pos to x_pos -2
- recEnemy.setLocation(x_pos, y_pos); // adjusts the value of the rectangle to match the new x value
- }// end method xleft
- // Input: None
- // sets the value of x_pos to -=2 and adjusts the position of the rectangle
- // Output: None
- public void xright() {
- x_pos += 2; // change the value of x_pos to x_pos +2
- recEnemy.setLocation(x_pos, y_pos); // adjusts the value of the rectangle to match the new x value
- }// end method xright
- // Input: None
- // sets the value of x_pos to +=2 and adjusts the position of the rectangle
- // Output: None
- // Generate shots
- public Shots shoot() {
- return new Shots(x_pos, (y_pos + 10)); // create new shot object
- }// end method shoot
- // Input: None
- // Create a shot object that inherits the x and y pos of the spacecraft
- // Output: new shot
- // Draw the polygon shaped spacecraft
- public void drawEnemy(Graphics g) {
- g.drawImage(img, x_pos, y_pos, 50, 22, null); //draw the image of the spaceship in the correct position
- //Code used to draw the old model, currently defunct
- //g.setColor(Color.red); // sets the colour of the shot to red
- //int[] x_poly = { x_pos, x_pos - 10, x_pos, x_pos + 10 };
- //int[] y_poly = { y_pos, y_pos - 15, y_pos - 10, y_pos - 15 }; // creates the shape of the shot
- //g.fillPolygon(x_poly, y_poly, 4);
- //g.drawRect(recEnemy.x, recEnemy.y, 50, 22); //draws the rectangle for collison testing
- }// end method drawEnemy
- }// end method Enemy
- SHOTS
- /*Description: An applet that runs a space shooter game (Method: Enemy)
- *Version: 1.0
- *Date: December 13, 2018
- *Author: Sean Feigis
- */
- import java.awt.Graphics;
- import java.awt.Image;
- import java.awt.Rectangle;
- import java.awt.Color;
- import java.util.Random;
- import javax.swing.ImageIcon;
- public class Enemy {
- // Variables
- Image img = new ImageIcon(this.getClass().getResource("/enemy.png")).getImage();
- private Graphics graph;
- int y_pos; // y position of the enemy
- int x_pos; // x position of the enemy
- Rectangle recEnemy = new Rectangle(x_pos, y_pos, 50, 22); // Rectangle for collision of the enemy
- // Constructors
- Enemy(int x, int y) {
- x_pos = x;
- y_pos = y;
- }// end constructor
- // METHODS:
- // GetX - returns the value of X
- public int getx() {
- return x_pos; // returns x_pos
- }// end method getx
- // Input: None
- // Returns the value of x_pos
- // Output: returns x_pos
- // SetX - Changes the value of X to move to left or right
- public void xleft() {
- x_pos -= 2; // change the value of x_pos to x_pos -2
- recEnemy.setLocation(x_pos, y_pos); // adjusts the value of the rectangle to match the new x value
- }// end method xleft
- // Input: None
- // sets the value of x_pos to -=2 and adjusts the position of the rectangle
- // Output: None
- public void xright() {
- x_pos += 2; // change the value of x_pos to x_pos +2
- recEnemy.setLocation(x_pos, y_pos); // adjusts the value of the rectangle to match the new x value
- }// end method xright
- // Input: None
- // sets the value of x_pos to +=2 and adjusts the position of the rectangle
- // Output: None
- // Generate shots
- public Shots shoot() {
- return new Shots(x_pos, (y_pos + 10)); // create new shot object
- }// end method shoot
- // Input: None
- // Create a shot object that inherits the x and y pos of the spacecraft
- // Output: new shot
- // Draw the polygon shaped spacecraft
- public void drawEnemy(Graphics g) {
- g.drawImage(img, x_pos, y_pos, 50, 22, null); //draw the image of the spaceship in the correct position
- //Code used to draw the old model, currently defunct
- //g.setColor(Color.red); // sets the colour of the shot to red
- //int[] x_poly = { x_pos, x_pos - 10, x_pos, x_pos + 10 };
- //int[] y_poly = { y_pos, y_pos - 15, y_pos - 10, y_pos - 15 }; // creates the shape of the shot
- //g.fillPolygon(x_poly, y_poly, 4);
- //g.drawRect(recEnemy.x, recEnemy.y, 50, 22); //draws the rectangle for collison testing
- }// end method drawEnemy
- }// end method Enemy
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement