Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.imageio.ImageIO;
- import javax.swing.*;
- import javax.swing.text.html.ImageView;
- import java.awt.*;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import java.net.URL;
- public class Player extends Sprite{
- private int x;
- private int y;
- private int r;
- private int dx;
- private int dy;
- private int speed;
- private boolean left;
- private boolean right;
- private boolean fire;
- private long fireTime;
- private long fireDelay;
- private boolean hit;
- private int lives;
- private Color color1;
- private int width;
- private static BufferedImage image;
- private BufferedImage pSprite;
- private Sound shootSound;
- public Player(){
- r = 5;
- x = GamePanel.WIDTH/2;
- y = GamePanel.HEIGHT -(r*5);
- try{
- URL url = this.getClass().getResource("sprites/player.png");
- pSprite = ImageIO.read(url);
- }catch(IOException e){};
- dy = 0;
- dx = 0;
- speed = 5;
- lives = 3;
- fire = false;
- fireTime = System.nanoTime();
- fireDelay = 1000;
- hit = false;
- score = 0;
- setX(x);
- setY(y);
- }
- private int score;
- public int getx(){return x;}
- public int gety(){return y;}
- public int getr(){return r;}
- public int getScore(){ return score;}
- public int getLives(){return lives;}
- public boolean isHit(){ return hit;}
- public void loseLife(){
- lives--;
- hit = true;
- }
- public void addScore(long i){score +=i;}
- public void setLeft(boolean b){left = b;}
- public void setRight(boolean b){right = b;}
- public void setFire(boolean b){fire = b;}
- public void update(){
- if(left){
- dx = -speed;
- }
- if(right){
- dx = speed;
- }
- x += dx;
- if( x < r ) x = r;
- if( x > GamePanel.WIDTH - r) x = GamePanel.WIDTH-r;
- dx = 0;
- if(fire){
- long elapsed = (System.nanoTime() - fireTime)/ 1000000;
- if(elapsed > fireDelay){
- GamePanel.bullets.add(new Bullet(270,x,y));
- fireTime = System.nanoTime();
- shootSound = new Sound("sounds/shoot/shoot.wav");
- }
- }
- }
- public void draw (Graphics2D g){
- g.drawImage(pSprite,x-r,y-r,50, 50, null);
- }
- }
- import javax.swing.*;
- import java.awt.*;
- import java.awt.desktop.SystemEventListener;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.awt.image.BufferedImage;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.Random;
- public class GamePanel extends JPanel implements Runnable, KeyListener {
- public static int WIDTH = 700;
- public static int HEIGHT = 700;
- private Thread thread;
- private boolean running;
- private BufferedImage image;
- private Graphics2D g;
- private int direction = -2;
- private int FPS = 30;
- private double averageFPS;
- private boolean playing = true;
- // private Sound shot;
- public static Player player;
- public static ArrayList<Bullet> bullets;
- public static ArrayList<Bullet> bullets1;
- public static ArrayList<Enemy> enemies;
- public static Bases base;
- public int totEnem = 50;
- public boolean end;
- public GamePanel() {
- super();
- setPreferredSize(new Dimension(WIDTH, HEIGHT));
- setFocusable(true);
- requestFocus();
- }
- public void addNotify() {
- super.addNotify();
- if (thread == null) {
- thread = new Thread(this);
- thread.start();
- }
- addKeyListener(this);
- }
- public void run() {
- running = true;
- image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
- g = (Graphics2D) image.getGraphics();
- player = new Player();
- bullets = new ArrayList<Bullet>();
- }
- end = false;
- long startTime;
- long URDTimeMillis;
- long waitTime;
- long totalTime = 0;
- int frameCount = 0;
- int maxFrameCount = 30;
- long targetTime = 1000 / FPS;
- while (running) {
- startTime = System.nanoTime();
- gameUpdate();
- gameRender();
- gameDraw();
- URDTimeMillis = (System.nanoTime() - startTime) / 1000000;
- waitTime = targetTime - URDTimeMillis;
- try {
- Thread.sleep(waitTime);
- } catch (Exception e) {
- }
- totalTime += (System.nanoTime() - startTime);
- frameCount++;
- if (frameCount == maxFrameCount) {
- averageFPS = 1000.0 / ((totalTime / frameCount) / 1000000);
- frameCount = 0;
- totalTime = 0;
- }
- }
- }
- private void gameUpdate() {
- //player update
- player.update();
- //bullets shot by player update
- for (int i = 0; i < bullets.size(); i++) {
- // if(!shot.isPlaying()){
- // shot.play();
- // }
- boolean remove = bullets.get(i).update();
- if (remove) {
- bullets.remove(i);
- i--;
- }
- }
- private void gameRender(){
- //end game displayed message
- //draw background
- g.setColor(Color.BLACK);
- g.fillRect(0, 0, WIDTH, HEIGHT);
- g.setColor(Color.WHITE);
- //draw player
- player.draw(g);
- }
- private void gameDraw(){
- Graphics g2 = this.getGraphics();
- g2.drawImage(image, 0 ,0,null);
- g2.dispose();
- }
- public void keyTyped(KeyEvent e){}
- public void keyPressed(KeyEvent e){
- int keyCode = e.getKeyCode();
- if(keyCode == KeyEvent.VK_LEFT){
- player.setLeft(true);
- }
- if(keyCode == KeyEvent.VK_RIGHT){
- player.setRight(true);
- }
- if(keyCode == KeyEvent.VK_SPACE){
- player.setFire(true);
- // System.out.print("Shot");
- }
- }
- public void keyReleased(KeyEvent e) {
- int keyCode = e.getKeyCode();
- if (keyCode == KeyEvent.VK_LEFT) {
- player.setLeft(false);
- }
- if (keyCode == KeyEvent.VK_RIGHT) {
- player.setRight(false);
- }
- if (keyCode == KeyEvent.VK_SPACE) {
- player.setFire(false);
- }
- }
- }
- import javax.sound.sampled.AudioFormat;
- import javax.sound.sampled.AudioInputStream;
- import javax.sound.sampled.AudioSystem;
- import javax.sound.sampled.Clip;
- import javax.sound.sampled.DataLine;
- import javax.sound.sampled.DataLine.Info;
- import javax.sound.sampled.LineEvent;
- import javax.sound.sampled.LineListener;
- private Clip soundClip;
- public Sound(String path) {
- try {
- URL url = getClass().getResource(path);
- AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(url);
- AudioFormat format = audioInputStream.getFormat();
- DataLine.Info info = new Info(Clip.class, format);
- soundClip = (Clip) AudioSystem.getLine(info);
- soundClip.open(audioInputStream);
- soundClip.addLineListener(this);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- @Override
- public void update(LineEvent event) {
- if (event.getType().equals(LineEvent.Type.STOP)) {
- soundClip.setFramePosition(1);
- }
- }
- public void play() {
- soundClip.start();
- }
- public void loop() {
- soundClip.loop(Clip.LOOP_CONTINUOUSLY);
- }
- public void stop() {
- soundClip.stop();
- soundClip.setFramePosition(1);
- }
- public boolean isPlaying() {
- return soundClip.isRunning();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement