Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.88 KB | None | 0 0
  1. import javax.imageio.ImageIO;
  2. import javax.swing.*;
  3. import javax.swing.text.html.ImageView;
  4. import java.awt.*;
  5. import java.awt.image.BufferedImage;
  6. import java.io.File;
  7. import java.io.IOException;
  8. import java.net.URL;
  9.  
  10. public class Player extends Sprite{
  11. private int x;
  12. private int y;
  13. private int r;
  14.  
  15. private int dx;
  16. private int dy;
  17. private int speed;
  18.  
  19. private boolean left;
  20. private boolean right;
  21.  
  22. private boolean fire;
  23. private long fireTime;
  24. private long fireDelay;
  25.  
  26. private boolean hit;
  27.  
  28. private int lives;
  29. private Color color1;
  30. private int width;
  31. private static BufferedImage image;
  32.  
  33. private BufferedImage pSprite;
  34.  
  35. private Sound shootSound;
  36.  
  37.  
  38.  
  39. public Player(){
  40. r = 5;
  41. x = GamePanel.WIDTH/2;
  42. y = GamePanel.HEIGHT -(r*5);
  43.  
  44.  
  45. try{
  46. URL url = this.getClass().getResource("sprites/player.png");
  47. pSprite = ImageIO.read(url);
  48. }catch(IOException e){};
  49.  
  50. dy = 0;
  51. dx = 0;
  52. speed = 5;
  53.  
  54. lives = 3;
  55.  
  56. fire = false;
  57. fireTime = System.nanoTime();
  58. fireDelay = 1000;
  59. hit = false;
  60.  
  61. score = 0;
  62. setX(x);
  63. setY(y);
  64.  
  65. }
  66.  
  67. private int score;
  68.  
  69.  
  70. public int getx(){return x;}
  71. public int gety(){return y;}
  72. public int getr(){return r;}
  73.  
  74. public int getScore(){ return score;}
  75.  
  76. public int getLives(){return lives;}
  77.  
  78. public boolean isHit(){ return hit;}
  79.  
  80.  
  81. public void loseLife(){
  82. lives--;
  83. hit = true;
  84.  
  85. }
  86.  
  87. public void addScore(long i){score +=i;}
  88.  
  89. public void setLeft(boolean b){left = b;}
  90. public void setRight(boolean b){right = b;}
  91.  
  92. public void setFire(boolean b){fire = b;}
  93.  
  94. public void update(){
  95. if(left){
  96. dx = -speed;
  97. }
  98. if(right){
  99. dx = speed;
  100. }
  101.  
  102. x += dx;
  103.  
  104. if( x < r ) x = r;
  105. if( x > GamePanel.WIDTH - r) x = GamePanel.WIDTH-r;
  106.  
  107. dx = 0;
  108.  
  109. if(fire){
  110. long elapsed = (System.nanoTime() - fireTime)/ 1000000;
  111. if(elapsed > fireDelay){
  112. GamePanel.bullets.add(new Bullet(270,x,y));
  113. fireTime = System.nanoTime();
  114. shootSound = new Sound("sounds/shoot/shoot.wav");
  115.  
  116. }
  117. }
  118.  
  119. }
  120.  
  121. public void draw (Graphics2D g){
  122. g.drawImage(pSprite,x-r,y-r,50, 50, null);
  123.  
  124.  
  125.  
  126. }
  127. }
  128.  
  129. import javax.swing.*;
  130. import java.awt.*;
  131. import java.awt.desktop.SystemEventListener;
  132. import java.awt.event.KeyEvent;
  133. import java.awt.event.KeyListener;
  134. import java.awt.image.BufferedImage;
  135. import java.util.ArrayList;
  136. import java.util.Iterator;
  137. import java.util.Random;
  138.  
  139.  
  140. public class GamePanel extends JPanel implements Runnable, KeyListener {
  141.  
  142. public static int WIDTH = 700;
  143. public static int HEIGHT = 700;
  144. private Thread thread;
  145. private boolean running;
  146. private BufferedImage image;
  147. private Graphics2D g;
  148. private int direction = -2;
  149. private int FPS = 30;
  150. private double averageFPS;
  151. private boolean playing = true;
  152.  
  153. // private Sound shot;
  154.  
  155. public static Player player;
  156. public static ArrayList<Bullet> bullets;
  157. public static ArrayList<Bullet> bullets1;
  158. public static ArrayList<Enemy> enemies;
  159.  
  160. public static Bases base;
  161.  
  162.  
  163. public int totEnem = 50;
  164.  
  165. public boolean end;
  166.  
  167. public GamePanel() {
  168. super();
  169. setPreferredSize(new Dimension(WIDTH, HEIGHT));
  170. setFocusable(true);
  171. requestFocus();
  172. }
  173.  
  174. public void addNotify() {
  175. super.addNotify();
  176. if (thread == null) {
  177. thread = new Thread(this);
  178. thread.start();
  179. }
  180. addKeyListener(this);
  181. }
  182.  
  183. public void run() {
  184. running = true;
  185. image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
  186. g = (Graphics2D) image.getGraphics();
  187. player = new Player();
  188. bullets = new ArrayList<Bullet>();
  189. }
  190.  
  191. end = false;
  192.  
  193. long startTime;
  194. long URDTimeMillis;
  195. long waitTime;
  196. long totalTime = 0;
  197.  
  198. int frameCount = 0;
  199. int maxFrameCount = 30;
  200.  
  201. long targetTime = 1000 / FPS;
  202. while (running) {
  203.  
  204.  
  205. startTime = System.nanoTime();
  206.  
  207. gameUpdate();
  208. gameRender();
  209. gameDraw();
  210. URDTimeMillis = (System.nanoTime() - startTime) / 1000000;
  211.  
  212. waitTime = targetTime - URDTimeMillis;
  213. try {
  214. Thread.sleep(waitTime);
  215. } catch (Exception e) {
  216. }
  217.  
  218. totalTime += (System.nanoTime() - startTime);
  219. frameCount++;
  220.  
  221. if (frameCount == maxFrameCount) {
  222. averageFPS = 1000.0 / ((totalTime / frameCount) / 1000000);
  223. frameCount = 0;
  224. totalTime = 0;
  225. }
  226. }
  227. }
  228.  
  229.  
  230. private void gameUpdate() {
  231.  
  232. //player update
  233. player.update();
  234.  
  235.  
  236. //bullets shot by player update
  237. for (int i = 0; i < bullets.size(); i++) {
  238. // if(!shot.isPlaying()){
  239. // shot.play();
  240. // }
  241. boolean remove = bullets.get(i).update();
  242. if (remove) {
  243. bullets.remove(i);
  244. i--;
  245. }
  246. }
  247.  
  248.  
  249.  
  250.  
  251. private void gameRender(){
  252. //end game displayed message
  253.  
  254. //draw background
  255. g.setColor(Color.BLACK);
  256. g.fillRect(0, 0, WIDTH, HEIGHT);
  257. g.setColor(Color.WHITE);
  258.  
  259.  
  260. //draw player
  261. player.draw(g);
  262.  
  263.  
  264. }
  265.  
  266.  
  267. private void gameDraw(){
  268. Graphics g2 = this.getGraphics();
  269. g2.drawImage(image, 0 ,0,null);
  270. g2.dispose();
  271. }
  272.  
  273.  
  274.  
  275. public void keyTyped(KeyEvent e){}
  276.  
  277. public void keyPressed(KeyEvent e){
  278. int keyCode = e.getKeyCode();
  279. if(keyCode == KeyEvent.VK_LEFT){
  280. player.setLeft(true);
  281. }
  282. if(keyCode == KeyEvent.VK_RIGHT){
  283. player.setRight(true);
  284. }
  285. if(keyCode == KeyEvent.VK_SPACE){
  286. player.setFire(true);
  287.  
  288. // System.out.print("Shot");
  289.  
  290. }
  291. }
  292. public void keyReleased(KeyEvent e) {
  293. int keyCode = e.getKeyCode();
  294. if (keyCode == KeyEvent.VK_LEFT) {
  295. player.setLeft(false);
  296. }
  297. if (keyCode == KeyEvent.VK_RIGHT) {
  298. player.setRight(false);
  299. }
  300. if (keyCode == KeyEvent.VK_SPACE) {
  301. player.setFire(false);
  302. }
  303. }
  304. }
  305.  
  306. import javax.sound.sampled.AudioFormat;
  307. import javax.sound.sampled.AudioInputStream;
  308. import javax.sound.sampled.AudioSystem;
  309. import javax.sound.sampled.Clip;
  310. import javax.sound.sampled.DataLine;
  311. import javax.sound.sampled.DataLine.Info;
  312. import javax.sound.sampled.LineEvent;
  313. import javax.sound.sampled.LineListener;
  314.  
  315. private Clip soundClip;
  316.  
  317. public Sound(String path) {
  318. try {
  319. URL url = getClass().getResource(path);
  320. AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(url);
  321. AudioFormat format = audioInputStream.getFormat();
  322. DataLine.Info info = new Info(Clip.class, format);
  323. soundClip = (Clip) AudioSystem.getLine(info);
  324. soundClip.open(audioInputStream);
  325. soundClip.addLineListener(this);
  326. } catch (Exception e) {
  327. e.printStackTrace();
  328. }
  329. }
  330.  
  331. @Override
  332. public void update(LineEvent event) {
  333. if (event.getType().equals(LineEvent.Type.STOP)) {
  334. soundClip.setFramePosition(1);
  335. }
  336. }
  337.  
  338. public void play() {
  339. soundClip.start();
  340. }
  341.  
  342. public void loop() {
  343. soundClip.loop(Clip.LOOP_CONTINUOUSLY);
  344. }
  345.  
  346. public void stop() {
  347. soundClip.stop();
  348. soundClip.setFramePosition(1);
  349. }
  350.  
  351. public boolean isPlaying() {
  352. return soundClip.isRunning();
  353. }
  354. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement