Asteroids.java
上传用户:haoxsx
上传日期:2022-03-22
资源大小:20k
文件大小:8k
- package asteroids;
- import java.applet.Applet;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.HeadlessException;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.awt.geom.AffineTransform;
- import java.awt.image.BufferedImage;
- import java.util.Random;
- import javax.sound.sampled.*;
- public class Asteroids extends Applet implements Runnable, KeyListener {
- Thread gameloop;
- BufferedImage backbuffer;
- Graphics2D g2d;
- boolean showBounds =false;
-
- int ASTEROIDS=20;
- Asteroid[] asteroid=new Asteroid[ASTEROIDS];
-
- int currentBullet = 0;
-
- int BULLETS=10;
- Bullet[] bullet=new Bullet[BULLETS];
-
- Ship ship =new Ship();
-
- AffineTransform identity=new AffineTransform();
-
- Random rand=new Random();
-
- SoundClip shoot;
- SoundClip explode;
-
- public void init(){
- backbuffer=new BufferedImage(640,480,BufferedImage.TYPE_INT_RGB);
- g2d = backbuffer.createGraphics();
-
- ship.setX(320);
- ship.setY(240);
-
- for(int i=0;i<BULLETS;i++){
- bullet[i]=new Bullet();
- }
-
- for(int i=0;i<ASTEROIDS;i++){
- asteroid[i]=new Asteroid();
- asteroid[i].setRotationVelocity(rand.nextInt(3)+1);
- asteroid[i].setX((double)rand.nextInt(600)+20);
- asteroid[i].setY((double)rand.nextInt(440)+20);
- asteroid[i].setMoveAngle(rand.nextInt(360));
- double ang =asteroid[i].getMoveAngle()-90;
- asteroid[i].setVelX(calcAngleMoveX(ang));
- asteroid[i].setVelY(calcAngleMoveY(ang));
-
- }
-
- shoot = new SoundClip("shoot.WAV");
- explode = new SoundClip("explode.WAV");
-
- addKeyListener(this);
-
-
- }
- public double calcAngleMoveY(double angle) {
- // TODO Auto-generated method stub
- return (double)(Math.sin(angle*Math.PI/180));
- }
- public double calcAngleMoveX(double angle) {
- // TODO Auto-generated method stub
- return (double)(Math.cos(angle*Math.PI/180));
- }
-
- public void update(Graphics g){
- g2d.setTransform(identity);
-
- g2d.setPaint(Color.BLACK);
- g2d.fillRect(0, 0, getSize().width, getSize().height);
-
- g2d.setColor(Color.WHITE);
- g2d.drawString("ship:"+Math.round(ship.getX())+","+Math.round(ship.getY()),5,10);
- g2d.drawString("Move angle:"+Math.round(ship.getMoveAngle())+90, 5, 25);
- g2d.drawString("Face angle:"+Math.round(ship.getFaceAngle()),5,40);
-
- drawShip();
- drawBullets();
- drawAsteroids();
-
- paint(g);
-
-
-
-
- }
- public void drawAsteroids() {
- // TODO Auto-generated method stub
- for(int n=0;n<ASTEROIDS;n++){
- if(asteroid[n].isAlive()){
- g2d.setTransform(identity);
- g2d.translate(asteroid[n].getX(), asteroid[n].getY());
- g2d.rotate(Math.toRadians(asteroid[n].getMoveAngle()));
- g2d.setColor(Color.DARK_GRAY);
- g2d.fill(asteroid[n].getShape());
-
- if(showBounds){
- g2d.setTransform(identity);
- g2d.setColor(Color.BLUE);
- g2d.draw(asteroid[n].getBounds());
- }
- }
- }
-
- }
- public void drawBullets() {
- // TODO Auto-generated method stub
- for(int n=0;n<BULLETS;n++){
- if(bullet[n].isAlive()){
- g2d.setTransform(identity);
- g2d.translate(bullet[n].getX(), bullet[n].getY());
- g2d.setColor(Color.MAGENTA);
- g2d.draw(bullet[n].getShape());
- }
- }
- }
- public void drawShip() {
- // TODO Auto-generated method stub
- g2d.setTransform(identity);
- g2d.translate(ship.getX(), ship.getY());
- g2d.rotate(Math.toRadians(ship.getFaceAngle()));
- g2d.setColor(Color.ORANGE);
- g2d.fill(ship.getShape());
-
- if(showBounds){
- g2d.setTransform(identity);
- g2d.setColor(Color.BLUE);
- g2d.draw(ship.getBounds());
- }
-
- }
-
- public void paint(Graphics g){
- g.drawImage(backbuffer,0,0,this);
- }
- public void start(){
- gameloop=new Thread(this);
- gameloop.start();
- }
- @Override
- public void run() {
- // TODO Auto-generated method stub
- Thread t=Thread.currentThread();
- while(t==gameloop){
- try {
- gameUpdate();
- Thread.sleep(20);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- repaint();
- }
-
- }
- public void stop(){
- gameloop=null;
- }
- public void gameUpdate() {
- // TODO Auto-generated method stub
- updateShip();
- updateBullets();
- updateAsteroids();
- checkCollisions();
-
-
- }
- public void checkCollisions() {
- // TODO Auto-generated method stub
- for(int m=0;m<ASTEROIDS;m++){
- if(asteroid[m].isAlive()){
- for(int n=0;n<BULLETS;n++){
- if(bullet[n].isAlive()){
- if(asteroid[m].getBounds().contains(bullet[n].getX(),bullet[n].getY())){
- bullet[n].setAlive(false);
- asteroid[m].setAlive(false);
- explode.play();
- continue;
- }
- }
- }
- if(asteroid[m].getBounds().intersects(ship.getBounds())){
- asteroid[m].setAlive(false);
- explode.play();
- ship.setX(320);
- ship.setY(240);
- ship.setFaceAngle(0);
- ship.setVelX(0);
- ship.setVelY(0);
- continue;
- }
- }
- }
-
- }
- public void updateAsteroids() {
- // TODO Auto-generated method stub
- for(int n=0;n<ASTEROIDS;n++){
- if(asteroid[n].isAlive()){
- asteroid[n].incX(asteroid[n].getVelX());
- if(asteroid[n].getX()<-20)
- asteroid[n].setX(getSize().width+20);
- else if(asteroid[n].getX()>getSize().width+20)
- asteroid[n].setX(-20);
- asteroid[n].incY(asteroid[n].getVelY());
- if(asteroid[n].getY()<-20)
- asteroid[n].setY(getSize().height+20);
- else if(asteroid[n].getY()>getSize().height+20)
- asteroid[n].setY(-20);
- asteroid[n].incMoveAngle(asteroid[n].getRotationVelocity());
- if(asteroid[n].getMoveAngle()<0)
- asteroid[n].setMoveAngle(360-asteroid[n].getRotationVelocity());
- else if (asteroid[n].getMoveAngle()>360)
- asteroid[n].setMoveAngle(asteroid[n].getRotationVelocity());
-
- }
- }
-
- }
- public void updateBullets() {
- // TODO Auto-generated method stub
- for(int n=0;n<BULLETS;n++){
- if(bullet[n].isAlive()){
- bullet[n].incX(bullet[n].getVelX());
- if(bullet[n].getX()<0||bullet[n].getX()>getSize().width){
- bullet[n].setAlive(false);
- }
- bullet[n].incY(bullet[n].getVelY());
- if(bullet[n].getY()<0||bullet[n].getY()>getSize().height){
- bullet[n].setAlive(false);
- }
- }
- }
- }
- public void updateShip() {
- // TODO Auto-generated method stub
- ship.incX(ship.getVelX());
- if(ship.getX()<-10)
- ship.setX(getSize().width+10);
- else if(ship.getX()>getSize().width+10)
- ship.setX(-10);
- ship.incY(ship.getVelY());
- if(ship.getY()<-10)
- ship.setY(getSize().height+10);
- else if(ship.getY()>getSize().height+10)
- ship.setY(-10);
-
- }
- @Override
- public void keyPressed(KeyEvent k) {
- // TODO Auto-generated method stub
- int keyCode = k.getKeyCode();
- switch(keyCode){
- case KeyEvent.VK_LEFT:
- ship.incFaceAngle(-5);
- if(ship.getFaceAngle()<0)ship.setFaceAngle(360-5);
- break;
- case KeyEvent.VK_RIGHT:
- ship.incFaceAngle(5);
- if(ship.getFaceAngle()>360)ship.setFaceAngle(5);
- break;
- case KeyEvent.VK_UP:
- ship.setMoveAngle(ship.getFaceAngle()-90);
- ship.incVelX(calcAngleMoveX(ship.getMoveAngle())*0.1);
- ship.incVelY(calcAngleMoveY(ship.getMoveAngle())*0.1);
- break;
- case KeyEvent.VK_CONTROL:
- case KeyEvent.VK_ENTER:
- case KeyEvent.VK_SPACE:
- currentBullet++;
- if(currentBullet>BULLETS-1)currentBullet=0;
- bullet[currentBullet].setAlive(true);
- bullet[currentBullet].setX(ship.getX());
- bullet[currentBullet].setY(ship.getY());
- bullet[currentBullet].setMoveAngle(ship.getFaceAngle()-90);
-
- double angle = bullet[currentBullet].getMoveAngle();
- double svx=ship.getVelX();
- double svy=ship.getVelY();
- bullet[currentBullet].setVelX(svx+calcAngleMoveX(angle)*2);
- bullet[currentBullet].setVelY(svy+calcAngleMoveY(angle)*2);
- shoot.play();
- break;
- case KeyEvent.VK_B:
- showBounds =!showBounds;
- break;
-
-
-
-
- }
-
- }
- @Override
- public void keyReleased(KeyEvent k) {
- // TODO Auto-generated method stub
- }
- @Override
- public void keyTyped(KeyEvent k) {
- // TODO Auto-generated method stub
- }
- }