Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package RedHazard;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.util.Random;
- public class Obstacle
- {
- public int x, y, width = 25, height = 25;
- public int xspeed, yspeed;
- public Random random;
- private Redhazard rh;
- private int left;
- public Obstacle(Redhazard rh, int a)
- {
- this.rh = rh;
- this.random = new Random();
- left = a;
- spawn(left);
- }
- public void update(Ship ship)
- {
- x += xspeed;
- y += yspeed;
- if(CheckCollision(ship) > 0)
- {
- if(CheckCollision(ship) == 1)
- {
- ship.score -= 1;
- }
- spawn(left);
- }
- }
- public void spawn(int a)
- {
- left = a;
- if(left == 1)
- {
- this.x = 0;
- xspeed = random.nextInt(6) + 2;
- }
- else
- {
- this.x = Redhazard.rh.width;
- xspeed = -1*(random.nextInt(6) + 2);
- }
- this.y = random.nextInt(Redhazard.rh.height-150);
- yspeed = random.nextInt(4) + 2;
- }
- public int CheckCollision(Ship ship)
- {
- if(this.x + width > ship.x && this.x < ship.x + ship.width && this.y + height > ship.y && this.y < ship.y + ship.height)
- {
- return 1;
- }
- if(this.x < 0 || this.y > Redhazard.rh.height || this.x > Redhazard.rh.width)
- {
- return 2;
- }
- return 0;
- }
- public void render(Graphics g)
- {
- g.setColor(Color.RED);
- g.fillRect(x, y, width, height);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement