Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // fuck java for life
- // <my name and class> 2010
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- import sun.audio.*;
- import java.io.*;
- public class VisaAsteroider extends JPanel implements ActionListener, KeyListener {
- Asteroid ast[] = new Asteroid[10];
- Timer t, t2;
- Star sta[] = new Star[10];
- Rymdskepp r;
- JLabel text = new JLabel();
- // JLabel hiscore = new JLabel();
- JButton save = new JButton();
- Font f = new Font("Arial", 0, 12);
- int x, y, score;
- Skott s;
- boolean left = false, right = false;
- boolean up = false, down = false;
- boolean visad = false;
- //JLabel poang = new JLabel("Antal asteroider: " + Asteroid.antalast);
- InputStream in = new FileInputStream("dask.wav");
- AudioStream as = new AudioStream(in);
- String dinScore;
- public VisaAsteroider() throws IOException
- {
- x = 320;
- y = 380;
- t = new Timer( 30, this );
- t2 = new Timer( 2, this );
- t.start();
- setLayout( null );
- for( int i = 0 ; i < 10 ; i++ )
- {
- ast[i] = new Asteroid();
- add( ast[i] );
- }
- text.setForeground(Color.WHITE);
- text.setBounds(10, 10, 170, 20);
- text.setFont(f);
- text.setText("asd");
- for( int i = 0 ; i < sta.length ; i++ )
- {
- sta[i] = new Star( 40, 15 );
- sta[i].setLocation( (int) ( Math.random()*599 ),
- (int) ( Math.random()*440 ));
- add( sta[i] );
- }
- r = new Rymdskepp( 40, 60, 0, 0 );
- add(r);
- s = new Skott();
- add(s);
- add(text);
- Asteroid.antalast = 10; //Det antal asteroider som skapas
- setBackground( Color.black );
- this.addKeyListener( this );
- }
- public void actionPerformed( ActionEvent e )
- {
- for( int i=0; i<10; i++ )
- {
- ast[i].nyPosition();
- ast[i].hit( x, s.getSkott() );
- }
- if (e.getSource() == t2)
- {
- AudioPlayer.player.stop(as); // stoppa ljudet så att det kan spelas upp igen
- }
- for (int i = 0; i < ast.length; i++)
- {
- ast[i].nyPosition();
- if( ast[i].hit( x, s.getSkott() ) )
- {
- this.score += 50; // uppa poängräknaren
- AudioPlayer.player.start(as); // spela ljud
- }
- if (Asteroid.antalast == 0 && !visad)
- {
- JOptionPane.showMessageDialog( null, "Du vann!!" );
- visad = true;
- }
- }
- for (int i = 0; i < sta.length; i++)
- {
- sta[i].nyPosition();
- }
- dinScore = String.valueOf(score);
- text.setText("Din poäng: " + dinScore);
- if ( left )
- {
- x = x - 5;
- }
- if ( right )
- {
- x = x + 5;
- }
- if ( up )
- {
- y = y - 5;
- }
- if ( down )
- {
- y = y + 5;
- }
- // if (e.getSource() == t)
- // {
- // ast[0].nyPosition();
- // ast[1].nyPosition();
- // ast[2].nyPosition();
- // ast[3].nyPosition();
- // ast[4].nyPosition();
- // ast[5].nyPosition();
- // ast[6].nyPosition();
- // ast[7].nyPosition();
- // ast[8].nyPosition();
- // ast[9].nyPosition();
- //
- // if( ast[0].hit( x, s.getSkott() ) )
- // {
- // this.score += 50;
- // AudioPlayer.player.start(as);
- // }
- // if( ast[1].hit( x, s.getSkott() ) )
- // {
- // this.score += 50;
- // AudioPlayer.player.start(as);
- // }
- // if( ast[2].hit( x, s.getSkott() ) )
- // {
- // this.score += 50;
- // AudioPlayer.player.start(as);
- // }
- // if( ast[3].hit( x, s.getSkott() ) )
- // {
- // this.score += 50;
- // AudioPlayer.player.start(as);
- // }
- // if( ast[4].hit( x, s.getSkott() ) )
- // {
- // this.score += 50;
- // AudioPlayer.player.start(as);
- // }
- // if( ast[5].hit( x, s.getSkott() ) )
- // {
- // this.score += 50;
- // AudioPlayer.player.start(as);
- // }
- // if( ast[6].hit( x, s.getSkott() ) )
- // {
- // this.score += 50;
- // AudioPlayer.player.start(as);
- // }
- // if( ast[7].hit( x, s.getSkott() ) )
- // {
- // this.score += 50;
- // AudioPlayer.player.start(as);
- // }
- // if( ast[8].hit( x, s.getSkott() ) )
- // {
- // this.score += 50;
- // AudioPlayer.player.start(as);
- // }
- // if( ast[9].hit( x, s.getSkott() ) )
- // {
- // this.score += 50;
- // AudioPlayer.player.start(as);
- // }
- // }
- r.nyPosition( x,y );
- repaint();
- }
- public void keyTyped( KeyEvent e )
- {
- }
- public void keyPressed( KeyEvent e )
- {
- if ( e.getKeyCode()== 37 )
- {
- left = true;
- s.skjutEj();
- }
- if ( e.getKeyCode() == 39 )
- {
- right = true;
- s.skjutEj();
- }
- if ( e.getKeyCode() == 38 )
- {
- up = true;
- s.skjutEj();
- }
- if ( e.getKeyCode() == 40 )
- {
- down = true;
- s.skjutEj();
- }
- if ( e.getKeyCode()== 32 )
- {
- s.skjut( x, y );
- }
- r.nyPosition( x, y );
- repaint();
- }
- public void keyReleased( KeyEvent e )
- {
- if (e.getKeyCode() == 37){
- left = false;
- }
- if (e.getKeyCode() == 38){
- up = false;
- }
- if (e.getKeyCode() == 39){
- right = false;
- }
- if (e.getKeyCode() == 40){
- down = false;
- }
- s.skjutEj();
- repaint();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement