Advertisement
Guest User

Untitled

a guest
Oct 3rd, 2013
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.28 KB | None | 0 0
  1. // fuck java for life
  2. // <my name and class> 2010
  3.  
  4. import javax.swing.*;
  5. import java.awt.*;
  6. import java.awt.event.*;
  7. import sun.audio.*;
  8. import java.io.*;
  9.  
  10. public class VisaAsteroider extends JPanel implements ActionListener, KeyListener {
  11. Asteroid ast[] = new Asteroid[10];
  12. Timer t, t2;
  13. Star sta[] = new Star[10];
  14. Rymdskepp r;
  15. JLabel text = new JLabel();
  16. // JLabel hiscore = new JLabel();
  17. JButton save = new JButton();
  18. Font f = new Font("Arial", 0, 12);
  19. int x, y, score;
  20. Skott s;
  21. boolean left = false, right = false;
  22. boolean up = false, down = false;
  23. boolean visad = false;
  24. //JLabel poang = new JLabel("Antal asteroider: " + Asteroid.antalast);
  25.  
  26. InputStream in = new FileInputStream("dask.wav");
  27. AudioStream as = new AudioStream(in);
  28.  
  29. String dinScore;
  30.  
  31. public VisaAsteroider() throws IOException
  32. {
  33.  
  34.  
  35. x = 320;
  36. y = 380;
  37. t = new Timer( 30, this );
  38. t2 = new Timer( 2, this );
  39. t.start();
  40. setLayout( null );
  41. for( int i = 0 ; i < 10 ; i++ )
  42. {
  43. ast[i] = new Asteroid();
  44. add( ast[i] );
  45. }
  46.  
  47. text.setForeground(Color.WHITE);
  48. text.setBounds(10, 10, 170, 20);
  49. text.setFont(f);
  50. text.setText("asd");
  51.  
  52. for( int i = 0 ; i < sta.length ; i++ )
  53. {
  54. sta[i] = new Star( 40, 15 );
  55. sta[i].setLocation( (int) ( Math.random()*599 ),
  56. (int) ( Math.random()*440 ));
  57. add( sta[i] );
  58. }
  59.  
  60. r = new Rymdskepp( 40, 60, 0, 0 );
  61. add(r);
  62.  
  63. s = new Skott();
  64. add(s);
  65.  
  66. add(text);
  67.  
  68. Asteroid.antalast = 10; //Det antal asteroider som skapas
  69.  
  70. setBackground( Color.black );
  71.  
  72. this.addKeyListener( this );
  73.  
  74. }
  75.  
  76.  
  77.  
  78. public void actionPerformed( ActionEvent e )
  79. {
  80. for( int i=0; i<10; i++ )
  81. {
  82. ast[i].nyPosition();
  83. ast[i].hit( x, s.getSkott() );
  84. }
  85.  
  86. if (e.getSource() == t2)
  87. {
  88.  
  89. AudioPlayer.player.stop(as); // stoppa ljudet så att det kan spelas upp igen
  90.  
  91. }
  92.  
  93. for (int i = 0; i < ast.length; i++)
  94. {
  95. ast[i].nyPosition();
  96. if( ast[i].hit( x, s.getSkott() ) )
  97. {
  98. this.score += 50; // uppa poängräknaren
  99. AudioPlayer.player.start(as); // spela ljud
  100. }
  101. if (Asteroid.antalast == 0 && !visad)
  102. {
  103. JOptionPane.showMessageDialog( null, "Du vann!!" );
  104. visad = true;
  105. }
  106. }
  107. for (int i = 0; i < sta.length; i++)
  108. {
  109. sta[i].nyPosition();
  110. }
  111.  
  112. dinScore = String.valueOf(score);
  113. text.setText("Din poäng: " + dinScore);
  114.  
  115. if ( left )
  116. {
  117. x = x - 5;
  118. }
  119. if ( right )
  120. {
  121. x = x + 5;
  122. }
  123. if ( up )
  124. {
  125. y = y - 5;
  126. }
  127. if ( down )
  128. {
  129. y = y + 5;
  130. }
  131. // if (e.getSource() == t)
  132. // {
  133. // ast[0].nyPosition();
  134. // ast[1].nyPosition();
  135. // ast[2].nyPosition();
  136. // ast[3].nyPosition();
  137. // ast[4].nyPosition();
  138. // ast[5].nyPosition();
  139. // ast[6].nyPosition();
  140. // ast[7].nyPosition();
  141. // ast[8].nyPosition();
  142. // ast[9].nyPosition();
  143. //
  144. // if( ast[0].hit( x, s.getSkott() ) )
  145. // {
  146. // this.score += 50;
  147. // AudioPlayer.player.start(as);
  148. // }
  149. // if( ast[1].hit( x, s.getSkott() ) )
  150. // {
  151. // this.score += 50;
  152. // AudioPlayer.player.start(as);
  153. // }
  154. // if( ast[2].hit( x, s.getSkott() ) )
  155. // {
  156. // this.score += 50;
  157. // AudioPlayer.player.start(as);
  158. // }
  159. // if( ast[3].hit( x, s.getSkott() ) )
  160. // {
  161. // this.score += 50;
  162. // AudioPlayer.player.start(as);
  163. // }
  164. // if( ast[4].hit( x, s.getSkott() ) )
  165. // {
  166. // this.score += 50;
  167. // AudioPlayer.player.start(as);
  168. // }
  169. // if( ast[5].hit( x, s.getSkott() ) )
  170. // {
  171. // this.score += 50;
  172. // AudioPlayer.player.start(as);
  173. // }
  174. // if( ast[6].hit( x, s.getSkott() ) )
  175. // {
  176. // this.score += 50;
  177. // AudioPlayer.player.start(as);
  178. // }
  179. // if( ast[7].hit( x, s.getSkott() ) )
  180. // {
  181. // this.score += 50;
  182. // AudioPlayer.player.start(as);
  183. // }
  184. // if( ast[8].hit( x, s.getSkott() ) )
  185. // {
  186. // this.score += 50;
  187. // AudioPlayer.player.start(as);
  188. // }
  189. // if( ast[9].hit( x, s.getSkott() ) )
  190. // {
  191. // this.score += 50;
  192. // AudioPlayer.player.start(as);
  193. // }
  194. // }
  195.  
  196. r.nyPosition( x,y );
  197. repaint();
  198. }
  199.  
  200.  
  201.  
  202.  
  203. public void keyTyped( KeyEvent e )
  204. {
  205.  
  206. }
  207.  
  208. public void keyPressed( KeyEvent e )
  209. {
  210. if ( e.getKeyCode()== 37 )
  211. {
  212. left = true;
  213. s.skjutEj();
  214. }
  215. if ( e.getKeyCode() == 39 )
  216. {
  217. right = true;
  218. s.skjutEj();
  219. }
  220. if ( e.getKeyCode() == 38 )
  221. {
  222. up = true;
  223. s.skjutEj();
  224. }
  225. if ( e.getKeyCode() == 40 )
  226. {
  227. down = true;
  228. s.skjutEj();
  229. }
  230. if ( e.getKeyCode()== 32 )
  231. {
  232. s.skjut( x, y );
  233. }
  234. r.nyPosition( x, y );
  235. repaint();
  236. }
  237.  
  238. public void keyReleased( KeyEvent e )
  239. {
  240. if (e.getKeyCode() == 37){
  241. left = false;
  242. }
  243.  
  244. if (e.getKeyCode() == 38){
  245. up = false;
  246. }
  247.  
  248. if (e.getKeyCode() == 39){
  249. right = false;
  250. }
  251.  
  252. if (e.getKeyCode() == 40){
  253. down = false;
  254. }
  255. s.skjutEj();
  256. repaint();
  257. }
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement